You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

84 lines
1.9 KiB

  1. // Copyright 2012 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 norm_test
  5. import (
  6. "bytes"
  7. "fmt"
  8. "io"
  9. "unicode/utf8"
  10. "golang.org/x/text/unicode/norm"
  11. )
  12. // EqualSimple uses a norm.Iter to compare two non-normalized
  13. // strings for equivalence.
  14. func EqualSimple(a, b string) bool {
  15. var ia, ib norm.Iter
  16. ia.InitString(norm.NFKD, a)
  17. ib.InitString(norm.NFKD, b)
  18. for !ia.Done() && !ib.Done() {
  19. if !bytes.Equal(ia.Next(), ib.Next()) {
  20. return false
  21. }
  22. }
  23. return ia.Done() && ib.Done()
  24. }
  25. // FindPrefix finds the longest common prefix of ASCII characters
  26. // of a and b.
  27. func FindPrefix(a, b string) int {
  28. i := 0
  29. for ; i < len(a) && i < len(b) && a[i] < utf8.RuneSelf && a[i] == b[i]; i++ {
  30. }
  31. return i
  32. }
  33. // EqualOpt is like EqualSimple, but optimizes the special
  34. // case for ASCII characters.
  35. func EqualOpt(a, b string) bool {
  36. n := FindPrefix(a, b)
  37. a, b = a[n:], b[n:]
  38. var ia, ib norm.Iter
  39. ia.InitString(norm.NFKD, a)
  40. ib.InitString(norm.NFKD, b)
  41. for !ia.Done() && !ib.Done() {
  42. if !bytes.Equal(ia.Next(), ib.Next()) {
  43. return false
  44. }
  45. if n := int64(FindPrefix(a[ia.Pos():], b[ib.Pos():])); n != 0 {
  46. ia.Seek(n, io.SeekCurrent)
  47. ib.Seek(n, io.SeekCurrent)
  48. }
  49. }
  50. return ia.Done() && ib.Done()
  51. }
  52. var compareTests = []struct{ a, b string }{
  53. {"aaa", "aaa"},
  54. {"aaa", "aab"},
  55. {"a\u0300a", "\u00E0a"},
  56. {"a\u0300\u0320b", "a\u0320\u0300b"},
  57. {"\u1E0A\u0323", "\x44\u0323\u0307"},
  58. // A character that decomposes into multiple segments
  59. // spans several iterations.
  60. {"\u3304", "\u30A4\u30CB\u30F3\u30AF\u3099"},
  61. }
  62. func ExampleIter() {
  63. for i, t := range compareTests {
  64. r0 := EqualSimple(t.a, t.b)
  65. r1 := EqualOpt(t.a, t.b)
  66. fmt.Printf("%d: %v %v\n", i, r0, r1)
  67. }
  68. // Output:
  69. // 0: true true
  70. // 1: false false
  71. // 2: true true
  72. // 3: true true
  73. // 4: true true
  74. // 5: true true
  75. }