Path: blob/main/vendor/golang.org/x/text/internal/match.go
2880 views
// Copyright 2015 The Go Authors. All rights reserved.1// Use of this source code is governed by a BSD-style2// license that can be found in the LICENSE file.34package internal56// This file contains matchers that implement CLDR inheritance.7//8// See https://unicode.org/reports/tr35/#Locale_Inheritance.9//10// Some of the inheritance described in this document is already handled by11// the cldr package.1213import (14"golang.org/x/text/language"15)1617// TODO: consider if (some of the) matching algorithm needs to be public after18// getting some feel about what is generic and what is specific.1920// NewInheritanceMatcher returns a matcher that matches based on the inheritance21// chain.22//23// The matcher uses canonicalization and the parent relationship to find a24// match. The resulting match will always be either Und or a language with the25// same language and script as the requested language. It will not match26// languages for which there is understood to be mutual or one-directional27// intelligibility.28//29// A Match will indicate an Exact match if the language matches after30// canonicalization and High if the matched tag is a parent.31func NewInheritanceMatcher(t []language.Tag) *InheritanceMatcher {32tags := &InheritanceMatcher{make(map[language.Tag]int)}33for i, tag := range t {34ct, err := language.All.Canonicalize(tag)35if err != nil {36ct = tag37}38tags.index[ct] = i39}40return tags41}4243type InheritanceMatcher struct {44index map[language.Tag]int45}4647func (m InheritanceMatcher) Match(want ...language.Tag) (language.Tag, int, language.Confidence) {48for _, t := range want {49ct, err := language.All.Canonicalize(t)50if err != nil {51ct = t52}53conf := language.Exact54for {55if index, ok := m.index[ct]; ok {56return ct, index, conf57}58if ct == language.Und {59break60}61ct = ct.Parent()62conf = language.High63}64}65return language.Und, 0, language.No66}676869