example_iter_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. }