65aaa607ab
Change made by: - running "gvt fetch" on each of the packages mentioned in Godeps/Godeps.json - `rm -rf Godeps` - tweaking the build scripts to not mention Godeps - tweaking the build scripts to test `./lib/...`, `./cmd/...` explicitly (to avoid testing vendor) - tweaking the build scripts to not juggle GOPATH for Godeps and instead set GO15VENDOREXPERIMENT. This also results in some updated packages at the same time I bet. Building with Go 1.3 and 1.4 still *works* but won't use our vendored dependencies - the user needs to have the actual packages in their GOPATH then, which they'll get with a normal "go get". Building with Go 1.6+ will get our vendored dependencies by default even when not using our build script, which is nice. By doing this we gain some freedom in that we can pick and choose manually what to include in vendor, as it's not based on just dependency analysis of our own code. This is also a risk as we might pick up dependencies we are unaware of, as the build may work locally with those packages present in GOPATH. On the other hand the build server will detect this as it has no packages in it's GOPATH beyond what is included in the repo. Recommended tool to manage dependencies is github.com/FiloSottile/gvt.
83 lines
1.8 KiB
Go
83 lines
1.8 KiB
Go
// Copyright 2012 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package norm_test
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"unicode/utf8"
|
|
|
|
"golang.org/x/text/unicode/norm"
|
|
)
|
|
|
|
// EqualSimple uses a norm.Iter to compare two non-normalized
|
|
// strings for equivalence.
|
|
func EqualSimple(a, b string) bool {
|
|
var ia, ib norm.Iter
|
|
ia.InitString(norm.NFKD, a)
|
|
ib.InitString(norm.NFKD, b)
|
|
for !ia.Done() && !ib.Done() {
|
|
if !bytes.Equal(ia.Next(), ib.Next()) {
|
|
return false
|
|
}
|
|
}
|
|
return ia.Done() && ib.Done()
|
|
}
|
|
|
|
// FindPrefix finds the longest common prefix of ASCII characters
|
|
// of a and b.
|
|
func FindPrefix(a, b string) int {
|
|
i := 0
|
|
for ; i < len(a) && i < len(b) && a[i] < utf8.RuneSelf && a[i] == b[i]; i++ {
|
|
}
|
|
return i
|
|
}
|
|
|
|
// EqualOpt is like EqualSimple, but optimizes the special
|
|
// case for ASCII characters.
|
|
func EqualOpt(a, b string) bool {
|
|
n := FindPrefix(a, b)
|
|
a, b = a[n:], b[n:]
|
|
var ia, ib norm.Iter
|
|
ia.InitString(norm.NFKD, a)
|
|
ib.InitString(norm.NFKD, b)
|
|
for !ia.Done() && !ib.Done() {
|
|
if !bytes.Equal(ia.Next(), ib.Next()) {
|
|
return false
|
|
}
|
|
if n := int64(FindPrefix(a[ia.Pos():], b[ib.Pos():])); n != 0 {
|
|
ia.Seek(n, 1)
|
|
ib.Seek(n, 1)
|
|
}
|
|
}
|
|
return ia.Done() && ib.Done()
|
|
}
|
|
|
|
var compareTests = []struct{ a, b string }{
|
|
{"aaa", "aaa"},
|
|
{"aaa", "aab"},
|
|
{"a\u0300a", "\u00E0a"},
|
|
{"a\u0300\u0320b", "a\u0320\u0300b"},
|
|
{"\u1E0A\u0323", "\x44\u0323\u0307"},
|
|
// A character that decomposes into multiple segments
|
|
// spans several iterations.
|
|
{"\u3304", "\u30A4\u30CB\u30F3\u30AF\u3099"},
|
|
}
|
|
|
|
func ExampleIter() {
|
|
for i, t := range compareTests {
|
|
r0 := EqualSimple(t.a, t.b)
|
|
r1 := EqualOpt(t.a, t.b)
|
|
fmt.Printf("%d: %v %v\n", i, r0, r1)
|
|
}
|
|
// Output:
|
|
// 0: true true
|
|
// 1: false false
|
|
// 2: true true
|
|
// 3: true true
|
|
// 4: true true
|
|
// 5: true true
|
|
}
|