tag.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2015 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package tag contains functionality handling tags and related data.
  5. package tag // import "golang.org/x/text/internal/tag"
  6. import "sort"
  7. // An Index converts tags to a compact numeric value.
  8. //
  9. // All elements are of size 4. Tags may be up to 4 bytes long. Excess bytes can
  10. // be used to store additional information about the tag.
  11. type Index string
  12. // Elem returns the element data at the given index.
  13. func (s Index) Elem(x int) string {
  14. return string(s[x*4 : x*4+4])
  15. }
  16. // Index reports the index of the given key or -1 if it could not be found.
  17. // Only the first len(key) bytes from the start of the 4-byte entries will be
  18. // considered for the search and the first match in Index will be returned.
  19. func (s Index) Index(key []byte) int {
  20. n := len(key)
  21. // search the index of the first entry with an equal or higher value than
  22. // key in s.
  23. index := sort.Search(len(s)/4, func(i int) bool {
  24. return cmp(s[i*4:i*4+n], key) != -1
  25. })
  26. i := index * 4
  27. if cmp(s[i:i+len(key)], key) != 0 {
  28. return -1
  29. }
  30. return index
  31. }
  32. // Next finds the next occurrence of key after index x, which must have been
  33. // obtained from a call to Index using the same key. It returns x+1 or -1.
  34. func (s Index) Next(key []byte, x int) int {
  35. if x++; x*4 < len(s) && cmp(s[x*4:x*4+len(key)], key) == 0 {
  36. return x
  37. }
  38. return -1
  39. }
  40. // cmp returns an integer comparing a and b lexicographically.
  41. func cmp(a Index, b []byte) int {
  42. n := len(a)
  43. if len(b) < n {
  44. n = len(b)
  45. }
  46. for i, c := range b[:n] {
  47. switch {
  48. case a[i] > c:
  49. return 1
  50. case a[i] < c:
  51. return -1
  52. }
  53. }
  54. switch {
  55. case len(a) < len(b):
  56. return -1
  57. case len(a) > len(b):
  58. return 1
  59. }
  60. return 0
  61. }
  62. // Compare returns an integer comparing a and b lexicographically.
  63. func Compare(a string, b []byte) int {
  64. return cmp(Index(a), b)
  65. }
  66. // FixCase reformats b to the same pattern of cases as form.
  67. // If returns false if string b is malformed.
  68. func FixCase(form string, b []byte) bool {
  69. if len(form) != len(b) {
  70. return false
  71. }
  72. for i, c := range b {
  73. if form[i] <= 'Z' {
  74. if c >= 'a' {
  75. c -= 'z' - 'Z'
  76. }
  77. if c < 'A' || 'Z' < c {
  78. return false
  79. }
  80. } else {
  81. if c <= 'Z' {
  82. c += 'z' - 'Z'
  83. }
  84. if c < 'a' || 'z' < c {
  85. return false
  86. }
  87. }
  88. b[i] = c
  89. }
  90. return true
  91. }