Source File
option.go
Belonging Package
golang.org/x/text/collate
// Copyright 2014 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.package collateimport ()// newCollator creates a new collator with default options configured.func newCollator( colltab.Weighter) *Collator {// Initialize a collator with default options.:= &Collator{options: options{ignore: [colltab.NumLevels]bool{colltab.Quaternary: true,colltab.Identity: true,},f: norm.NFD,t: ,},}// TODO: store vt in tags or remove..variableTop = .Top()return}// An Option is used to change the behavior of a Collator. Options override the// settings passed through the locale identifier.type Option struct {priority intf func(o *options)}type prioritizedOptions []Optionfunc ( prioritizedOptions) () int {return len()}func ( prioritizedOptions) (, int) {[], [] = [], []}func ( prioritizedOptions) (, int) bool {return [].priority < [].priority}type options struct {// ignore specifies which levels to ignore.ignore [colltab.NumLevels]bool// caseLevel is true if there is an additional level of case matching// between the secondary and tertiary levels.caseLevel bool// backwards specifies the order of sorting at the secondary level.// This option exists predominantly to support reverse sorting of accents in French.backwards bool// numeric specifies whether any sequence of decimal digits (category is Nd)// is sorted at a primary level with its numeric value.// For example, "A-21" < "A-123".// This option is set by wrapping the main Weighter with NewNumericWeighter.numeric bool// alternate specifies an alternative handling of variables.alternate alternateHandling// variableTop is the largest primary value that is considered to be// variable.variableTop uint32t colltab.Weighterf norm.Form}func ( *options) ( []Option) {sort.Sort(prioritizedOptions())for , := range {.f()}}// OptionsFromTag extracts the BCP47 collation options from the tag and// configures a collator accordingly. These options are set before any other// option.func ( language.Tag) Option {return Option{0, func( *options) {.setFromTag()}}}func ( *options) ( language.Tag) {.caseLevel = ldmlBool(, .caseLevel, "kc").backwards = ldmlBool(, .backwards, "kb").numeric = ldmlBool(, .numeric, "kn")// Extract settings from the BCP47 u extension.switch .TypeForKey("ks") { // strengthcase "level1":.ignore[colltab.Secondary] = true.ignore[colltab.Tertiary] = truecase "level2":.ignore[colltab.Tertiary] = truecase "level3", "":// The default.case "level4":.ignore[colltab.Quaternary] = falsecase "identic":.ignore[colltab.Quaternary] = false.ignore[colltab.Identity] = false}switch .TypeForKey("ka") {case "shifted":.alternate = altShifted// The following two types are not official BCP47, but we support them to// give access to this otherwise hidden functionality. The name blanked is// derived from the LDML name blanked and posix reflects the main use of// the shift-trimmed option.case "blanked":.alternate = altBlankedcase "posix":.alternate = altShiftTrimmed}// TODO: caseFirst ("kf"), reorder ("kr"), and maybe variableTop ("vt").// Not used:// - normalization ("kk", not necessary for this implementation)// - hiraganaQuatenary ("kh", obsolete)}func ldmlBool( language.Tag, bool, string) bool {switch .TypeForKey() {case "true":return truecase "false":return falsedefault:return}}var (// IgnoreCase sets case-insensitive comparison.IgnoreCase Option = ignoreCaseignoreCase = Option{3, ignoreCaseF}// IgnoreDiacritics causes diacritical marks to be ignored. ("o" == "รถ").IgnoreDiacritics Option = ignoreDiacriticsignoreDiacritics = Option{3, ignoreDiacriticsF}// IgnoreWidth causes full-width characters to match their half-width// equivalents.IgnoreWidth Option = ignoreWidthignoreWidth = Option{2, ignoreWidthF}// Loose sets the collator to ignore diacritics, case and width.Loose Option = looseloose = Option{4, looseF}// Force ordering if strings are equivalent but not equal.Force Option = forceforce = Option{5, forceF}// Numeric specifies that numbers should sort numerically ("2" < "12").Numeric Option = numericnumeric = Option{5, numericF})func ignoreWidthF( *options) {.ignore[colltab.Tertiary] = true.caseLevel = true}func ignoreDiacriticsF( *options) {.ignore[colltab.Secondary] = true}func ignoreCaseF( *options) {.ignore[colltab.Tertiary] = true.caseLevel = false}func looseF( *options) {ignoreWidthF()ignoreDiacriticsF()ignoreCaseF()}func forceF( *options) {.ignore[colltab.Identity] = false}func numericF( *options) { .numeric = true }// Reorder overrides the pre-defined ordering of scripts and character sets.func ( ...string) Option {// TODO: need fractional weights to implement this.panic("TODO: implement")}// TODO: consider making these public again. These options cannot be fully// specified in BCP47, so an API interface seems warranted. Still a higher-level// interface would be nice (e.g. a POSIX option for enabling altShiftTrimmed)// alternateHandling identifies the various ways in which variables are handled.// A rune with a primary weight lower than the variable top is considered a// variable.// See https://www.unicode.org/reports/tr10/#Variable_Weighting for details.type alternateHandling intconst (// altNonIgnorable turns off special handling of variables.altNonIgnorable alternateHandling = iota// altBlanked sets variables and all subsequent primary ignorables to be// ignorable at all levels. This is identical to removing all variables// and subsequent primary ignorables from the input.altBlanked// altShifted sets variables to be ignorable for levels one through three and// adds a fourth level based on the values of the ignored levels.altShifted// altShiftTrimmed is a slight variant of altShifted that is used to// emulate POSIX.altShiftTrimmed)
![]() |
The pages are generated with Golds v0.8.2. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |