Documentation ¶
Overview ¶
Package collate contains types for comparing and sorting Unicode strings according to a given collation order.
Index ¶
Examples ¶
Constants ¶
const CLDRVersion = "23"
CLDRVersion is the CLDR version from which the tables in this package are derived.
const UnicodeVersion = "6.2.0"
UnicodeVersion is the Unicode version from which the tables in this package are derived.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Buffer ¶
type Buffer struct {
// contains filtered or unexported fields
}
Buffer holds keys generated by Key and KeyString.
type Collator ¶
type Collator struct {
// contains filtered or unexported fields
}
Collator provides functionality for comparing strings for a given collation order.
Example (Strings) ¶
package main import ( "fmt" "golang.org/x/text/collate" "golang.org/x/text/language" ) func main() { c := collate.New(language.Und) strings := []string{ "ad", "ab", "äb", "ac", } c.SortStrings(strings) fmt.Println(strings) }
Output: [ab äb ac ad]
func New ¶
New returns a new Collator initialized for the given locale.
Example ¶
package main import ( "fmt" "golang.org/x/text/collate" "golang.org/x/text/language" ) func main() { letters := []string{"ä", "å", "ö", "o", "a"} ec := collate.New(language.English) ec.SortStrings(letters) fmt.Printf("English Sorting: %v\n", letters) sc := collate.New(language.Swedish) sc.SortStrings(letters) fmt.Printf("Swedish Sorting: %v\n", letters) numbers := []string{"0", "11", "01", "2", "3", "23"} ec.SortStrings(numbers) fmt.Printf("Alphabetic Sorting: %v\n", numbers) nc := collate.New(language.English, collate.Numeric) nc.SortStrings(numbers) fmt.Printf("Numeric Sorting: %v\n", numbers) }
Output: English Sorting: [a å ä o ö] Swedish Sorting: [a o å ä ö] Alphabetic Sorting: [0 01 11 2 23 3] Numeric Sorting: [0 01 2 3 11 23]
func NewFromTable ¶
NewFromTable returns a new Collator for the given Weighter.
func (*Collator) Compare ¶
Compare returns an integer comparing the two byte slices. The result will be 0 if a==b, -1 if a < b, and +1 if a > b.
Example ¶
package main import ( "fmt" "golang.org/x/text/collate" "golang.org/x/text/language" ) func main() { c := collate.New(language.English) r := c.Compare([]byte("meow"), []byte("woof")) fmt.Println(r) r = c.Compare([]byte("woof"), []byte("meow")) fmt.Println(r) r = c.Compare([]byte("meow"), []byte("meow")) fmt.Println(r) }
Output: -1 1 0
func (*Collator) CompareString ¶
CompareString returns an integer comparing the two strings. The result will be 0 if a==b, -1 if a < b, and +1 if a > b.
Example ¶
package main import ( "fmt" "golang.org/x/text/collate" "golang.org/x/text/language" ) func main() { c := collate.New(language.English) r := c.CompareString("meow", "woof") fmt.Println(r) r = c.CompareString("woof", "meow") fmt.Println(r) r = c.CompareString("meow", "meow") fmt.Println(r) }
Output: -1 1 0
func (*Collator) Key ¶
Key returns the collation key for str. Passing the buffer buf may avoid memory allocations. The returned slice will point to an allocation in Buffer and will remain valid until the next call to buf.Reset().
func (*Collator) KeyFromString ¶
KeyFromString returns the collation key for str. Passing the buffer buf may avoid memory allocations. The returned slice will point to an allocation in Buffer and will retain valid until the next call to buf.ResetKeys().
func (*Collator) Sort ¶
Sort uses sort.Sort to sort the strings represented by x using the rules of c.
Example ¶
package main import ( "fmt" "golang.org/x/text/collate" "golang.org/x/text/language" ) type book struct { title string } type bookcase struct { books []book } func (bc bookcase) Len() int { return len(bc.books) } func (bc bookcase) Swap(i, j int) { temp := bc.books[i] bc.books[i] = bc.books[j] bc.books[j] = temp } func (bc bookcase) Bytes(i int) []byte { // returns the bytes of text at index i return []byte(bc.books[i].title) } func main() { bc := bookcase{ books: []book{ {title: "If Cats Disappeared from the World"}, {title: "The Guest Cat"}, {title: "Catwings"}, }, } cc := collate.New(language.English) cc.Sort(bc) for _, b := range bc.books { fmt.Println(b.title) } }
Output: Catwings If Cats Disappeared from the World The Guest Cat
func (*Collator) SortStrings ¶
SortStrings uses sort.Sort to sort the strings in x using the rules of c.
Example ¶
package main import ( "fmt" "golang.org/x/text/collate" "golang.org/x/text/language" ) func main() { c := collate.New(language.English) words := []string{"meow", "woof", "bark", "moo"} c.SortStrings(words) fmt.Println(words) }
Output: [bark meow moo woof]
type Lister ¶
type Lister interface { Len() int Swap(i, j int) // Bytes returns the bytes of the text at index i. Bytes(i int) []byte }
A Lister can be sorted by Collator's Sort method.
type Option ¶
type Option struct {
// contains filtered or unexported fields
}
An Option is used to change the behavior of a Collator. Options override the settings passed through the locale identifier.
var ( // IgnoreCase sets case-insensitive comparison. IgnoreCase Option = ignoreCase // IgnoreDiacritics causes diacritical marks to be ignored. ("o" == "ö"). IgnoreDiacritics Option = ignoreDiacritics // IgnoreWidth causes full-width characters to match their half-width // equivalents. IgnoreWidth Option = ignoreWidth // Loose sets the collator to ignore diacritics, case and width. Loose Option = loose // Force ordering if strings are equivalent but not equal. Force Option = force // Numeric specifies that numbers should sort numerically ("2" < "12"). Numeric Option = numeric )
func OptionsFromTag ¶
OptionsFromTag extracts the BCP47 collation options from the tag and configures a collator accordingly. These options are set before any other option.