func sortAC(r io.Reader) []byte { bs := bufio.NewScanner(r) var header []string var lines []string for bs.Scan() { t := bs.Text() lines = append(lines, t) if t == "# Please keep the list sorted." { header = lines lines = nil continue } } if err := bs.Err(); err != nil { log.Fatal(err) } var out bytes.Buffer c := collate.New(language.Und, collate.Loose) c.SortStrings(lines) for _, l := range header { fmt.Fprintln(&out, l) } for _, l := range lines { fmt.Fprintln(&out, l) } return out.Bytes() }
func BenchmarkNumericWeighter(b *testing.B) { c := collate.New(language.English, collate.Numeric) input := bytes.Repeat([]byte("Testing, testing 123..."), 100) b.SetBytes(int64(2 * len(input))) for i := 0; i < b.N; i++ { c.Compare(input, input) } }
func (env *CollationEnvironment) getCacheEntry(locale string) collationEnvironmentCacheEntry { entry, ok := env.cache[locale] if !ok { if env.cache == nil { env.cache = make(map[string]collationEnvironmentCacheEntry) } entry = collationEnvironmentCacheEntry{locale, collate.New(language.MustParse(locale))} env.cache[locale] = entry } return entry }
func TestNumericCompare(t *testing.T) { c := collate.New(language.English, collate.Loose, collate.Numeric) // Iterate over all digits. for _, r16 := range unicode.Nd.R16 { testDigitCompare(t, c, rune(r16.Lo), rune(r16.Hi)) } for _, r32 := range unicode.Nd.R32 { testDigitCompare(t, c, rune(r32.Lo), rune(r32.Hi)) } }
func ExampleCollator_Strings() { c := collate.New(language.Und) strings := []string{ "ad", "ab", "äb", "ac", } c.SortStrings(strings) fmt.Println(strings) // Output: [ab äb ac ad] }
func TestNonDigits(t *testing.T) { c := collate.New(language.English, collate.Loose, collate.Numeric) // Verify that all non-digit numbers sort outside of the number range. for r, hi := rune(unicode.N.R16[0].Lo), rune(unicode.N.R32[0].Hi); r <= hi; r++ { if unicode.In(r, unicode.Nd) || !unicode.In(r, assigned) { continue } if a := string(r); c.CompareString(a, "0") != -1 && c.CompareString(a, "999999") != 1 { t.Errorf("%+q non-digit number is collated as digit", a) } } }
// getCollator returns a collate package Collator pointer. This can result in a // panic, so this function must recover from that if it happens. func getCollator(locale string) *collate.Collator { defer func() { recover() }() tag := language.Make(locale) if tag == language.Und { return nil } return collate.New(tag) }
func TestSort(t *testing.T) { c := collate.New(language.English) strings := []string{ "bcd", "abc", "ddd", } c.Sort(sorter(strings)) res := fmt.Sprint(strings) want := "[abc bcd ddd]" if res != want { t.Errorf("found %s; want %s", res, want) } }
func newPooledCollator() interface{} { // Ref: http://www.unicode.org/reports/tr10/#Introduction. // Unicode seems to define a universal (or default) order. // But various locales have conflicting order, // which they have the right to override. // Unfortunately, the Go library requires you to specify a locale. // So, I chose English assuming that it won't override // the Unicode universal order. But I couldn't find an easy // way to verify this. // Also, the locale differences are not an issue for level 1, // because the conservative comparison makes them all equal. return pooledCollator{ col: collate.New(language.English, collate.Loose), buf: new(collate.Buffer), } }
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 newGoCollator(loc string) (Collator, error) { c := &goCollator{c: collate.New(language.Make(loc))} return c, nil }
var ( lastViewedTimeCmpLess = _lessCmper(AttrLastViewedByMeTime) md5CmpLess = _lessCmper(AttrMd5Checksum) mimeTypeCmpLess = _lessCmper(AttrMimeType) modTimeCmpLess = _lessCmper(AttrModTime) nameCmpLess = _lessCmper(AttrName) sizeCmpLess = _lessCmper(AttrSize) versionCmpLess = _lessCmper(AttrVersion) typeCmpLess = _lessCmper(AttrIsDir) ) var ( // TODO get collation order from system's locale // language.Und seems to work well for common western locales collator *collate.Collator = collate.New(language.Und) ) func (fl modTimeFlist) Less(i, j int) bool { return modTimeCmpLess(fl[i], fl[j]) } func (fl modTimeFlist) Len() int { return len(fl) } func (fl modTimeFlist) Swap(i, j int) { fl[i], fl[j] = fl[j], fl[i] } func (fl typeFlist) Less(i, j int) bool {
func init() { simplifyNameRegex = regexp.MustCompile("[^a-zA-Z0-9]") collation = collate.New(language.English, collate.Loose, collate.IgnoreCase, collate.Numeric) }