本文整理汇总了Golang中golang.org/x/text/collate.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了New函数的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: sortAC
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()
}
示例2: BenchmarkNumericWeighter
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)
}
}
示例3: getCacheEntry
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
}
示例4: TestNumericCompare
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))
}
}
示例5: ExampleCollator_Strings
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]
}
示例6: TestNonDigits
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)
}
}
}
示例7: getCollator
// 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)
}
示例8: TestSort
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)
}
}
示例9: newPooledCollator
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),
}
}
示例10: testCollator
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")
}
示例11: newGoCollator
func newGoCollator(loc string) (Collator, error) {
c := &goCollator{c: collate.New(language.Make(loc))}
return c, nil
}
示例12: Less
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 {
示例13: init
func init() {
simplifyNameRegex = regexp.MustCompile("[^a-zA-Z0-9]")
collation = collate.New(language.English, collate.Loose, collate.IgnoreCase, collate.Numeric)
}