Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

157 linhas
4.0 KiB

  1. // Copyright 2016 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package fields
  15. // This file was copied from https://go.googlesource.com/go/+/go1.7.3/src/encoding/json/fold.go.
  16. // Only the license and package were changed.
  17. import (
  18. "bytes"
  19. "unicode/utf8"
  20. )
  21. const (
  22. caseMask = ^byte(0x20) // Mask to ignore case in ASCII.
  23. kelvin = '\u212a'
  24. smallLongEss = '\u017f'
  25. )
  26. // foldFunc returns one of four different case folding equivalence
  27. // functions, from most general (and slow) to fastest:
  28. //
  29. // 1) bytes.EqualFold, if the key s contains any non-ASCII UTF-8
  30. // 2) equalFoldRight, if s contains special folding ASCII ('k', 'K', 's', 'S')
  31. // 3) asciiEqualFold, no special, but includes non-letters (including _)
  32. // 4) simpleLetterEqualFold, no specials, no non-letters.
  33. //
  34. // The letters S and K are special because they map to 3 runes, not just 2:
  35. // * S maps to s and to U+017F 'ſ' Latin small letter long s
  36. // * k maps to K and to U+212A 'K' Kelvin sign
  37. // See https://play.golang.org/p/tTxjOc0OGo
  38. //
  39. // The returned function is specialized for matching against s and
  40. // should only be given s. It's not curried for performance reasons.
  41. func foldFunc(s []byte) func(s, t []byte) bool {
  42. nonLetter := false
  43. special := false // special letter
  44. for _, b := range s {
  45. if b >= utf8.RuneSelf {
  46. return bytes.EqualFold
  47. }
  48. upper := b & caseMask
  49. if upper < 'A' || upper > 'Z' {
  50. nonLetter = true
  51. } else if upper == 'K' || upper == 'S' {
  52. // See above for why these letters are special.
  53. special = true
  54. }
  55. }
  56. if special {
  57. return equalFoldRight
  58. }
  59. if nonLetter {
  60. return asciiEqualFold
  61. }
  62. return simpleLetterEqualFold
  63. }
  64. // equalFoldRight is a specialization of bytes.EqualFold when s is
  65. // known to be all ASCII (including punctuation), but contains an 's',
  66. // 'S', 'k', or 'K', requiring a Unicode fold on the bytes in t.
  67. // See comments on foldFunc.
  68. func equalFoldRight(s, t []byte) bool {
  69. for _, sb := range s {
  70. if len(t) == 0 {
  71. return false
  72. }
  73. tb := t[0]
  74. if tb < utf8.RuneSelf {
  75. if sb != tb {
  76. sbUpper := sb & caseMask
  77. if 'A' <= sbUpper && sbUpper <= 'Z' {
  78. if sbUpper != tb&caseMask {
  79. return false
  80. }
  81. } else {
  82. return false
  83. }
  84. }
  85. t = t[1:]
  86. continue
  87. }
  88. // sb is ASCII and t is not. t must be either kelvin
  89. // sign or long s; sb must be s, S, k, or K.
  90. tr, size := utf8.DecodeRune(t)
  91. switch sb {
  92. case 's', 'S':
  93. if tr != smallLongEss {
  94. return false
  95. }
  96. case 'k', 'K':
  97. if tr != kelvin {
  98. return false
  99. }
  100. default:
  101. return false
  102. }
  103. t = t[size:]
  104. }
  105. if len(t) > 0 {
  106. return false
  107. }
  108. return true
  109. }
  110. // asciiEqualFold is a specialization of bytes.EqualFold for use when
  111. // s is all ASCII (but may contain non-letters) and contains no
  112. // special-folding letters.
  113. // See comments on foldFunc.
  114. func asciiEqualFold(s, t []byte) bool {
  115. if len(s) != len(t) {
  116. return false
  117. }
  118. for i, sb := range s {
  119. tb := t[i]
  120. if sb == tb {
  121. continue
  122. }
  123. if ('a' <= sb && sb <= 'z') || ('A' <= sb && sb <= 'Z') {
  124. if sb&caseMask != tb&caseMask {
  125. return false
  126. }
  127. } else {
  128. return false
  129. }
  130. }
  131. return true
  132. }
  133. // simpleLetterEqualFold is a specialization of bytes.EqualFold for
  134. // use when s is all ASCII letters (no underscores, etc) and also
  135. // doesn't contain 'k', 'K', 's', or 'S'.
  136. // See comments on foldFunc.
  137. func simpleLetterEqualFold(s, t []byte) bool {
  138. if len(s) != len(t) {
  139. return false
  140. }
  141. for i, b := range s {
  142. if b&caseMask != t[i]&caseMask {
  143. return false
  144. }
  145. }
  146. return true
  147. }