// Copyright 2013 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.package jsonimport ()const (caseMask = ^byte(0x20) // Mask to ignore case in ASCII.kelvin = '\u212a'smallLongEss = '\u017f')// foldFunc returns one of four different case folding equivalence// functions, from most general (and slow) to fastest://// 1) bytes.EqualFold, if the key s contains any non-ASCII UTF-8// 2) equalFoldRight, if s contains special folding ASCII ('k', 'K', 's', 'S')// 3) asciiEqualFold, no special, but includes non-letters (including _)// 4) simpleLetterEqualFold, no specials, no non-letters.//// The letters S and K are special because they map to 3 runes, not just 2:// * S maps to s and to U+017F 'ſ' Latin small letter long s// * k maps to K and to U+212A 'K' Kelvin sign// See https://play.golang.org/p/tTxjOc0OGo//// The returned function is specialized for matching against s and// should only be given s. It's not curried for performance reasons.func ( []byte) func(, []byte) bool { := false := false// special letterfor , := range {if >= utf8.RuneSelf {returnbytes.EqualFold } := & caseMaskif < 'A' || > 'Z' { = true } elseif == 'K' || == 'S' {// See above for why these letters are special. = true } }if {returnequalFoldRight }if {returnasciiEqualFold }returnsimpleLetterEqualFold}// equalFoldRight is a specialization of bytes.EqualFold when s is// known to be all ASCII (including punctuation), but contains an 's',// 'S', 'k', or 'K', requiring a Unicode fold on the bytes in t.// See comments on foldFunc.func (, []byte) bool {for , := range {iflen() == 0 {returnfalse } := [0]if < utf8.RuneSelf {if != { := & caseMaskif'A' <= && <= 'Z' {if != &caseMask {returnfalse } } else {returnfalse } } = [1:]continue }// sb is ASCII and t is not. t must be either kelvin // sign or long s; sb must be s, S, k, or K. , := utf8.DecodeRune()switch {case's', 'S':if != smallLongEss {returnfalse }case'k', 'K':if != kelvin {returnfalse }default:returnfalse } = [:] }iflen() > 0 {returnfalse }returntrue}// asciiEqualFold is a specialization of bytes.EqualFold for use when// s is all ASCII (but may contain non-letters) and contains no// special-folding letters.// See comments on foldFunc.func (, []byte) bool {iflen() != len() {returnfalse }for , := range { := []if == {continue }if ('a' <= && <= 'z') || ('A' <= && <= 'Z') {if &caseMask != &caseMask {returnfalse } } else {returnfalse } }returntrue}// simpleLetterEqualFold is a specialization of bytes.EqualFold for// use when s is all ASCII letters (no underscores, etc) and also// doesn't contain 'k', 'K', 's', or 'S'.// See comments on foldFunc.func (, []byte) bool {iflen() != len() {returnfalse }for , := range {if &caseMask != []&caseMask {returnfalse } }returntrue}
The pages are generated with Goldsv0.3.6. (GOOS=darwin GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds.