github.com/icodeface/tls@v0.0.0-20230910023335-34df9250cd12/internal/x/text/unicode/norm/example_iter_test.go (about) 1 // Code generated by running "go run gen.go -core" in golang.org/x/text. DO NOT EDIT. 2 3 // Copyright 2012 The Go Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file. 6 7 package norm_test 8 9 import ( 10 "bytes" 11 "fmt" 12 "unicode/utf8" 13 14 "github.com/icodeface/tls/internal/x/text/unicode/norm" 15 ) 16 17 // EqualSimple uses a norm.Iter to compare two non-normalized 18 // strings for equivalence. 19 func EqualSimple(a, b string) bool { 20 var ia, ib norm.Iter 21 ia.InitString(norm.NFKD, a) 22 ib.InitString(norm.NFKD, b) 23 for !ia.Done() && !ib.Done() { 24 if !bytes.Equal(ia.Next(), ib.Next()) { 25 return false 26 } 27 } 28 return ia.Done() && ib.Done() 29 } 30 31 // FindPrefix finds the longest common prefix of ASCII characters 32 // of a and b. 33 func FindPrefix(a, b string) int { 34 i := 0 35 for ; i < len(a) && i < len(b) && a[i] < utf8.RuneSelf && a[i] == b[i]; i++ { 36 } 37 return i 38 } 39 40 // EqualOpt is like EqualSimple, but optimizes the special 41 // case for ASCII characters. 42 func EqualOpt(a, b string) bool { 43 n := FindPrefix(a, b) 44 a, b = a[n:], b[n:] 45 var ia, ib norm.Iter 46 ia.InitString(norm.NFKD, a) 47 ib.InitString(norm.NFKD, b) 48 for !ia.Done() && !ib.Done() { 49 if !bytes.Equal(ia.Next(), ib.Next()) { 50 return false 51 } 52 if n := int64(FindPrefix(a[ia.Pos():], b[ib.Pos():])); n != 0 { 53 ia.Seek(n, 1) 54 ib.Seek(n, 1) 55 } 56 } 57 return ia.Done() && ib.Done() 58 } 59 60 var compareTests = []struct{ a, b string }{ 61 {"aaa", "aaa"}, 62 {"aaa", "aab"}, 63 {"a\u0300a", "\u00E0a"}, 64 {"a\u0300\u0320b", "a\u0320\u0300b"}, 65 {"\u1E0A\u0323", "\x44\u0323\u0307"}, 66 // A character that decomposes into multiple segments 67 // spans several iterations. 68 {"\u3304", "\u30A4\u30CB\u30F3\u30AF\u3099"}, 69 } 70 71 func ExampleIter() { 72 for i, t := range compareTests { 73 r0 := EqualSimple(t.a, t.b) 74 r1 := EqualOpt(t.a, t.b) 75 fmt.Printf("%d: %v %v\n", i, r0, r1) 76 } 77 // Output: 78 // 0: true true 79 // 1: false false 80 // 2: true true 81 // 3: true true 82 // 4: true true 83 // 5: true true 84 }