// Copyright 2015 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 colltab contains functionality related to collation tables.// It is only to be used by the collate and search packages.
package colltab // import "golang.org/x/text/internal/colltab"import ()// MatchLang finds the index of t in tags, using a matching algorithm used for// collation and search. tags[0] must be language.Und, the remaining tags should// be sorted alphabetically.//// Language matching for collation and search is different from the matching// defined by language.Matcher: the (inferred) base language must be an exact// match for the relevant fields. For example, "gsw" should not match "de".// Also the parent relation is different, as a parent may have a different// script. So usually the parent of zh-Hant is und, whereas for MatchLang it is// zh.func ( language.Tag, []language.Tag) int {// Canonicalize the values, including collapsing macro languages. , _ = language.All.Canonicalize() , := .Base()// Estimate the base language, but only use high-confidence values.if < language.High {// The root locale supports "search" and "standard". We assume that any // implementation will only use one of both.return0 }// Maximize base and script and normalize the tag.if , , := .Raw(); ( != language.Region{}) { , := language.Raw.Compose(, , )// Taking the parent forces the script to be maximized. = .Parent()// Add back region and extensions. , _ = language.Raw.Compose(, , .Extensions()) } else {// Set the maximized base language. , _ = language.Raw.Compose(, , .Extensions()) }// Find start index of the language tag. := 1 + sort.Search(len()-1, func( int) bool { , , := [+1].Raw()return .String() <= .String() })if < len() {if , , := [].Raw(); != {return0 } }// Besides the base language, script and region, only the collation type and // the custom variant defined in the 'u' extension are used to distinguish a // locale. // Strip all variants and extensions and add back the custom variant. , := language.Raw.Compose(.Raw()) , _ = .SetTypeForKey("va", .TypeForKey("va"))// First search for a specialized collation type, if present. := []language.Tag{}if := .TypeForKey("co"); != "" { , := .SetTypeForKey("co", ) = []language.Tag{, } }for , := range {for ; != language.Und; = parent() {for , := range [:] {if , , := .Raw(); != {break }if == {return + } } } }return0}// parent computes the structural parent. This means inheritance may change// script. So, unlike the CLDR parent, parent(zh-Hant) == zh.func parent( language.Tag) language.Tag {if .TypeForKey("va") != "" { , _ = .SetTypeForKey("va", "")return } := language.Undif , , := .Raw(); ( != language.Region{}) { , _ = language.Raw.Compose(, , .Extensions()) } elseif ( != language.Script{}) { , _ = language.Raw.Compose(, .Extensions()) } elseif ( != language.Base{}) { , _ = language.Raw.Compose(.Extensions()) }return}
The pages are generated with Goldsv0.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.