Beispiel #1
0
func doTest(t *Test, f norm.Form, gold, test string) {
	testb := []byte(test)
	result := f.Bytes(testb)
	cmpResult(t, "Bytes", f, gold, test, string(result))

	sresult := f.String(test)
	cmpResult(t, "String", f, gold, test, sresult)

	acc := []byte{}
	i := norm.Iter{}
	i.InitString(f, test)
	for !i.Done() {
		acc = append(acc, i.Next()...)
	}
	cmpResult(t, "Iter.Next", f, gold, test, string(acc))

	buf := make([]byte, 128)
	acc = nil
	for p := 0; p < len(testb); {
		nDst, nSrc, _ := f.Transform(buf, testb[p:], true)
		acc = append(acc, buf[:nDst]...)
		p += nSrc
	}
	cmpResult(t, "Transform", f, gold, test, string(acc))

	for i := range test {
		out := f.Append(f.Bytes([]byte(test[:i])), []byte(test[i:])...)
		cmpResult(t, fmt.Sprintf(":Append:%d", i), f, gold, test, string(out))
	}
	cmpIsNormal(t, "IsNormal", f, test, f.IsNormal([]byte(test)), test == gold)
	cmpIsNormal(t, "IsNormalString", f, test, f.IsNormalString(test), test == gold)
}
// 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()
}
// 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()
}