func normalize(col *collate.Collator, buf *collate.Buffer, in []byte) ([]byte, error) { // We cannot pass invalid UTF-8 to the collator. if !utf8.Valid(in) { return nil, fmt.Errorf("cannot normalize string containing invalid UTF-8: %q", string(in)) } // Ref: http://dev.mysql.com/doc/refman/5.6/en/char.html. // Trailing spaces are ignored by MySQL. in = bytes.TrimRight(in, " ") // We use the collation key which can be used to // perform lexical comparisons. return col.Key(buf, in), nil }
func testCollator(c *collate.Collator) { c0 := collate.New(language.Und) // iterator over all characters for all locales and check // whether Key is equal. buf := collate.Buffer{} // Add all common and not too uncommon runes to the test set. for i := rune(0); i < 0x30000; i++ { testInput.add(string(i)) } for i := rune(0xE0000); i < 0xF0000; i++ { testInput.add(string(i)) } for _, str := range testInput.values() { k0 := c0.KeyFromString(&buf, str) k := c.KeyFromString(&buf, str) if !bytes.Equal(k0, k) { failOnError(fmt.Errorf("test:%U: keys differ (%x vs %x)", []rune(str), k0, k)) } buf.Reset() } fmt.Println("PASS") }
func testDigitCompare(t *testing.T, c *collate.Collator, zero, nine rune) { if !unicode.In(zero, assigned) { return } n := int(nine - zero + 1) if n%10 != 0 { t.Fatalf("len([%+q, %+q]) = %d; want a multiple of 10", zero, nine, n) } for _, tt := range []struct { prefix string b [11]string }{ { prefix: "", b: [11]string{ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", }, }, { prefix: "1", b: [11]string{ "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", }, }, { prefix: "0", b: [11]string{ "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", }, }, { prefix: "00", b: [11]string{ "000", "001", "002", "003", "004", "005", "006", "007", "008", "009", "010", }, }, { prefix: "9", b: [11]string{ "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100", }, }, } { for k := 0; k <= n; k++ { i := k % 10 a := tt.prefix + string(zero+rune(i)) for j, b := range tt.b { want := 0 switch { case i < j: want = -1 case i > j: want = 1 } got := c.CompareString(a, b) if got != want { t.Errorf("Compare(%+q, %+q) = %d; want %d", a, b, got, want) return } } } } }