VOL-1691 Fix openolt adapter getting stuck while registartion with core

Change-Id: Ide8131f325bc15f1b909e14d7af6ee9bcd6b3b5b
diff --git a/vendor/golang.org/x/text/language/match.go b/vendor/golang.org/x/text/language/match.go
index 15b74d1..f734921 100644
--- a/vendor/golang.org/x/text/language/match.go
+++ b/vendor/golang.org/x/text/language/match.go
@@ -4,7 +4,12 @@
 
 package language
 
-import "errors"
+import (
+	"errors"
+	"strings"
+
+	"golang.org/x/text/internal/language"
+)
 
 // A MatchOption configures a Matcher.
 type MatchOption func(*matcher)
@@ -74,12 +79,13 @@
 }
 
 func (m *matcher) Match(want ...Tag) (t Tag, index int, c Confidence) {
+	var tt language.Tag
 	match, w, c := m.getBest(want...)
 	if match != nil {
-		t, index = match.tag, match.index
+		tt, index = match.tag, match.index
 	} else {
 		// TODO: this should be an option
-		t = m.default_.tag
+		tt = m.default_.tag
 		if m.preferSameScript {
 		outer:
 			for _, w := range want {
@@ -91,7 +97,7 @@
 				}
 				for i, h := range m.supported {
 					if script.scriptID == h.maxScript {
-						t, index = h.tag, i
+						tt, index = h.tag, i
 						break outer
 					}
 				}
@@ -99,238 +105,45 @@
 		}
 		// TODO: select first language tag based on script.
 	}
-	if w.region != 0 && t.region != 0 && t.region.contains(w.region) {
-		t, _ = Raw.Compose(t, Region{w.region})
+	if w.RegionID != tt.RegionID && w.RegionID != 0 {
+		if w.RegionID != 0 && tt.RegionID != 0 && tt.RegionID.Contains(w.RegionID) {
+			tt.RegionID = w.RegionID
+			tt.RemakeString()
+		} else if r := w.RegionID.String(); len(r) == 2 {
+			// TODO: also filter macro and deprecated.
+			tt, _ = tt.SetTypeForKey("rg", strings.ToLower(r)+"zzzz")
+		}
 	}
 	// Copy options from the user-provided tag into the result tag. This is hard
 	// to do after the fact, so we do it here.
 	// TODO: add in alternative variants to -u-va-.
 	// TODO: add preferred region to -u-rg-.
 	if e := w.Extensions(); len(e) > 0 {
-		t, _ = Raw.Compose(t, e)
+		b := language.Builder{}
+		b.SetTag(tt)
+		for _, e := range e {
+			b.AddExt(e)
+		}
+		tt = b.Make()
 	}
-	return t, index, c
-}
-
-type scriptRegionFlags uint8
-
-const (
-	isList = 1 << iota
-	scriptInFrom
-	regionInFrom
-)
-
-func (t *Tag) setUndefinedLang(id langID) {
-	if t.lang == 0 {
-		t.lang = id
-	}
-}
-
-func (t *Tag) setUndefinedScript(id scriptID) {
-	if t.script == 0 {
-		t.script = id
-	}
-}
-
-func (t *Tag) setUndefinedRegion(id regionID) {
-	if t.region == 0 || t.region.contains(id) {
-		t.region = id
-	}
+	return makeTag(tt), index, c
 }
 
 // ErrMissingLikelyTagsData indicates no information was available
 // to compute likely values of missing tags.
 var ErrMissingLikelyTagsData = errors.New("missing likely tags data")
 
-// addLikelySubtags sets subtags to their most likely value, given the locale.
-// In most cases this means setting fields for unknown values, but in some
-// cases it may alter a value.  It returns an ErrMissingLikelyTagsData error
-// if the given locale cannot be expanded.
-func (t Tag) addLikelySubtags() (Tag, error) {
-	id, err := addTags(t)
-	if err != nil {
-		return t, err
-	} else if id.equalTags(t) {
-		return t, nil
-	}
-	id.remakeString()
-	return id, nil
-}
-
-// specializeRegion attempts to specialize a group region.
-func specializeRegion(t *Tag) bool {
-	if i := regionInclusion[t.region]; i < nRegionGroups {
-		x := likelyRegionGroup[i]
-		if langID(x.lang) == t.lang && scriptID(x.script) == t.script {
-			t.region = regionID(x.region)
-		}
-		return true
-	}
-	return false
-}
-
-func addTags(t Tag) (Tag, error) {
-	// We leave private use identifiers alone.
-	if t.private() {
-		return t, nil
-	}
-	if t.script != 0 && t.region != 0 {
-		if t.lang != 0 {
-			// already fully specified
-			specializeRegion(&t)
-			return t, nil
-		}
-		// Search matches for und-script-region. Note that for these cases
-		// region will never be a group so there is no need to check for this.
-		list := likelyRegion[t.region : t.region+1]
-		if x := list[0]; x.flags&isList != 0 {
-			list = likelyRegionList[x.lang : x.lang+uint16(x.script)]
-		}
-		for _, x := range list {
-			// Deviating from the spec. See match_test.go for details.
-			if scriptID(x.script) == t.script {
-				t.setUndefinedLang(langID(x.lang))
-				return t, nil
-			}
-		}
-	}
-	if t.lang != 0 {
-		// Search matches for lang-script and lang-region, where lang != und.
-		if t.lang < langNoIndexOffset {
-			x := likelyLang[t.lang]
-			if x.flags&isList != 0 {
-				list := likelyLangList[x.region : x.region+uint16(x.script)]
-				if t.script != 0 {
-					for _, x := range list {
-						if scriptID(x.script) == t.script && x.flags&scriptInFrom != 0 {
-							t.setUndefinedRegion(regionID(x.region))
-							return t, nil
-						}
-					}
-				} else if t.region != 0 {
-					count := 0
-					goodScript := true
-					tt := t
-					for _, x := range list {
-						// We visit all entries for which the script was not
-						// defined, including the ones where the region was not
-						// defined. This allows for proper disambiguation within
-						// regions.
-						if x.flags&scriptInFrom == 0 && t.region.contains(regionID(x.region)) {
-							tt.region = regionID(x.region)
-							tt.setUndefinedScript(scriptID(x.script))
-							goodScript = goodScript && tt.script == scriptID(x.script)
-							count++
-						}
-					}
-					if count == 1 {
-						return tt, nil
-					}
-					// Even if we fail to find a unique Region, we might have
-					// an unambiguous script.
-					if goodScript {
-						t.script = tt.script
-					}
-				}
-			}
-		}
-	} else {
-		// Search matches for und-script.
-		if t.script != 0 {
-			x := likelyScript[t.script]
-			if x.region != 0 {
-				t.setUndefinedRegion(regionID(x.region))
-				t.setUndefinedLang(langID(x.lang))
-				return t, nil
-			}
-		}
-		// Search matches for und-region. If und-script-region exists, it would
-		// have been found earlier.
-		if t.region != 0 {
-			if i := regionInclusion[t.region]; i < nRegionGroups {
-				x := likelyRegionGroup[i]
-				if x.region != 0 {
-					t.setUndefinedLang(langID(x.lang))
-					t.setUndefinedScript(scriptID(x.script))
-					t.region = regionID(x.region)
-				}
-			} else {
-				x := likelyRegion[t.region]
-				if x.flags&isList != 0 {
-					x = likelyRegionList[x.lang]
-				}
-				if x.script != 0 && x.flags != scriptInFrom {
-					t.setUndefinedLang(langID(x.lang))
-					t.setUndefinedScript(scriptID(x.script))
-					return t, nil
-				}
-			}
-		}
-	}
-
-	// Search matches for lang.
-	if t.lang < langNoIndexOffset {
-		x := likelyLang[t.lang]
-		if x.flags&isList != 0 {
-			x = likelyLangList[x.region]
-		}
-		if x.region != 0 {
-			t.setUndefinedScript(scriptID(x.script))
-			t.setUndefinedRegion(regionID(x.region))
-		}
-		specializeRegion(&t)
-		if t.lang == 0 {
-			t.lang = _en // default language
-		}
-		return t, nil
-	}
-	return t, ErrMissingLikelyTagsData
-}
-
-func (t *Tag) setTagsFrom(id Tag) {
-	t.lang = id.lang
-	t.script = id.script
-	t.region = id.region
-}
-
-// minimize removes the region or script subtags from t such that
-// t.addLikelySubtags() == t.minimize().addLikelySubtags().
-func (t Tag) minimize() (Tag, error) {
-	t, err := minimizeTags(t)
-	if err != nil {
-		return t, err
-	}
-	t.remakeString()
-	return t, nil
-}
-
-// minimizeTags mimics the behavior of the ICU 51 C implementation.
-func minimizeTags(t Tag) (Tag, error) {
-	if t.equalTags(und) {
-		return t, nil
-	}
-	max, err := addTags(t)
-	if err != nil {
-		return t, err
-	}
-	for _, id := range [...]Tag{
-		{lang: t.lang},
-		{lang: t.lang, region: t.region},
-		{lang: t.lang, script: t.script},
-	} {
-		if x, err := addTags(id); err == nil && max.equalTags(x) {
-			t.setTagsFrom(id)
-			break
-		}
-	}
-	return t, nil
-}
+// func (t *Tag) setTagsFrom(id Tag) {
+// 	t.LangID = id.LangID
+// 	t.ScriptID = id.ScriptID
+// 	t.RegionID = id.RegionID
+// }
 
 // Tag Matching
 // CLDR defines an algorithm for finding the best match between two sets of language
 // tags. The basic algorithm defines how to score a possible match and then find
 // the match with the best score
-// (see http://www.unicode.org/reports/tr35/#LanguageMatching).
+// (see https://www.unicode.org/reports/tr35/#LanguageMatching).
 // Using scoring has several disadvantages. The scoring obfuscates the importance of
 // the various factors considered, making the algorithm harder to understand. Using
 // scoring also requires the full score to be computed for each pair of tags.
@@ -441,7 +254,7 @@
 type matcher struct {
 	default_         *haveTag
 	supported        []*haveTag
-	index            map[langID]*matchHeader
+	index            map[language.Language]*matchHeader
 	passSettings     bool
 	preferSameScript bool
 }
@@ -456,7 +269,7 @@
 // haveTag holds a supported Tag and its maximized script and region. The maximized
 // or canonicalized language is not stored as it is not needed during matching.
 type haveTag struct {
-	tag Tag
+	tag language.Tag
 
 	// index of this tag in the original list of supported tags.
 	index int
@@ -466,37 +279,37 @@
 	conf Confidence
 
 	// Maximized region and script.
-	maxRegion regionID
-	maxScript scriptID
+	maxRegion language.Region
+	maxScript language.Script
 
 	// altScript may be checked as an alternative match to maxScript. If altScript
 	// matches, the confidence level for this match is Low. Theoretically there
 	// could be multiple alternative scripts. This does not occur in practice.
-	altScript scriptID
+	altScript language.Script
 
 	// nextMax is the index of the next haveTag with the same maximized tags.
 	nextMax uint16
 }
 
-func makeHaveTag(tag Tag, index int) (haveTag, langID) {
+func makeHaveTag(tag language.Tag, index int) (haveTag, language.Language) {
 	max := tag
-	if tag.lang != 0 || tag.region != 0 || tag.script != 0 {
-		max, _ = max.canonicalize(All)
-		max, _ = addTags(max)
-		max.remakeString()
+	if tag.LangID != 0 || tag.RegionID != 0 || tag.ScriptID != 0 {
+		max, _ = canonicalize(All, max)
+		max, _ = max.Maximize()
+		max.RemakeString()
 	}
-	return haveTag{tag, index, Exact, max.region, max.script, altScript(max.lang, max.script), 0}, max.lang
+	return haveTag{tag, index, Exact, max.RegionID, max.ScriptID, altScript(max.LangID, max.ScriptID), 0}, max.LangID
 }
 
 // altScript returns an alternative script that may match the given script with
 // a low confidence.  At the moment, the langMatch data allows for at most one
 // script to map to another and we rely on this to keep the code simple.
-func altScript(l langID, s scriptID) scriptID {
+func altScript(l language.Language, s language.Script) language.Script {
 	for _, alt := range matchScript {
 		// TODO: also match cases where language is not the same.
-		if (langID(alt.wantLang) == l || langID(alt.haveLang) == l) &&
-			scriptID(alt.haveScript) == s {
-			return scriptID(alt.wantScript)
+		if (language.Language(alt.wantLang) == l || language.Language(alt.haveLang) == l) &&
+			language.Script(alt.haveScript) == s {
+			return language.Script(alt.wantScript)
 		}
 	}
 	return 0
@@ -508,7 +321,7 @@
 	h.original = h.original || exact
 	// Don't add new exact matches.
 	for _, v := range h.haveTags {
-		if v.tag.equalsRest(n.tag) {
+		if equalsRest(v.tag, n.tag) {
 			return
 		}
 	}
@@ -517,7 +330,7 @@
 	for i, v := range h.haveTags {
 		if v.maxScript == n.maxScript &&
 			v.maxRegion == n.maxRegion &&
-			v.tag.variantOrPrivateTagStr() == n.tag.variantOrPrivateTagStr() {
+			v.tag.VariantOrPrivateUseTags() == n.tag.VariantOrPrivateUseTags() {
 			for h.haveTags[i].nextMax != 0 {
 				i = int(h.haveTags[i].nextMax)
 			}
@@ -530,7 +343,7 @@
 
 // header returns the matchHeader for the given language. It creates one if
 // it doesn't already exist.
-func (m *matcher) header(l langID) *matchHeader {
+func (m *matcher) header(l language.Language) *matchHeader {
 	if h := m.index[l]; h != nil {
 		return h
 	}
@@ -554,7 +367,7 @@
 // for a given tag.
 func newMatcher(supported []Tag, options []MatchOption) *matcher {
 	m := &matcher{
-		index:            make(map[langID]*matchHeader),
+		index:            make(map[language.Language]*matchHeader),
 		preferSameScript: true,
 	}
 	for _, o := range options {
@@ -567,16 +380,18 @@
 	// Add supported languages to the index. Add exact matches first to give
 	// them precedence.
 	for i, tag := range supported {
-		pair, _ := makeHaveTag(tag, i)
-		m.header(tag.lang).addIfNew(pair, true)
+		tt := tag.tag()
+		pair, _ := makeHaveTag(tt, i)
+		m.header(tt.LangID).addIfNew(pair, true)
 		m.supported = append(m.supported, &pair)
 	}
-	m.default_ = m.header(supported[0].lang).haveTags[0]
+	m.default_ = m.header(supported[0].lang()).haveTags[0]
 	// Keep these in two different loops to support the case that two equivalent
 	// languages are distinguished, such as iw and he.
 	for i, tag := range supported {
-		pair, max := makeHaveTag(tag, i)
-		if max != tag.lang {
+		tt := tag.tag()
+		pair, max := makeHaveTag(tt, i)
+		if max != tt.LangID {
 			m.header(max).addIfNew(pair, true)
 		}
 	}
@@ -585,11 +400,11 @@
 	// update will only add entries to original indexes, thus not computing any
 	// transitive relations.
 	update := func(want, have uint16, conf Confidence) {
-		if hh := m.index[langID(have)]; hh != nil {
+		if hh := m.index[language.Language(have)]; hh != nil {
 			if !hh.original {
 				return
 			}
-			hw := m.header(langID(want))
+			hw := m.header(language.Language(want))
 			for _, ht := range hh.haveTags {
 				v := *ht
 				if conf < v.conf {
@@ -597,7 +412,7 @@
 				}
 				v.nextMax = 0 // this value needs to be recomputed
 				if v.altScript != 0 {
-					v.altScript = altScript(langID(want), v.maxScript)
+					v.altScript = altScript(language.Language(want), v.maxScript)
 				}
 				hw.addIfNew(v, conf == Exact && hh.original)
 			}
@@ -618,66 +433,67 @@
 	// First we match deprecated equivalents. If they are perfect equivalents
 	// (their canonicalization simply substitutes a different language code, but
 	// nothing else), the match confidence is Exact, otherwise it is High.
-	for i, lm := range langAliasMap {
+	for i, lm := range language.AliasMap {
 		// If deprecated codes match and there is no fiddling with the script or
 		// or region, we consider it an exact match.
 		conf := Exact
-		if langAliasTypes[i] != langMacro {
-			if !isExactEquivalent(langID(lm.from)) {
+		if language.AliasTypes[i] != language.Macro {
+			if !isExactEquivalent(language.Language(lm.From)) {
 				conf = High
 			}
-			update(lm.to, lm.from, conf)
+			update(lm.To, lm.From, conf)
 		}
-		update(lm.from, lm.to, conf)
+		update(lm.From, lm.To, conf)
 	}
 	return m
 }
 
 // getBest gets the best matching tag in m for any of the given tags, taking into
 // account the order of preference of the given tags.
-func (m *matcher) getBest(want ...Tag) (got *haveTag, orig Tag, c Confidence) {
+func (m *matcher) getBest(want ...Tag) (got *haveTag, orig language.Tag, c Confidence) {
 	best := bestMatch{}
-	for i, w := range want {
-		var max Tag
+	for i, ww := range want {
+		w := ww.tag()
+		var max language.Tag
 		// Check for exact match first.
-		h := m.index[w.lang]
-		if w.lang != 0 {
+		h := m.index[w.LangID]
+		if w.LangID != 0 {
 			if h == nil {
 				continue
 			}
 			// Base language is defined.
-			max, _ = w.canonicalize(Legacy | Deprecated | Macro)
+			max, _ = canonicalize(Legacy|Deprecated|Macro, w)
 			// A region that is added through canonicalization is stronger than
 			// a maximized region: set it in the original (e.g. mo -> ro-MD).
-			if w.region != max.region {
-				w.region = max.region
+			if w.RegionID != max.RegionID {
+				w.RegionID = max.RegionID
 			}
 			// TODO: should we do the same for scripts?
 			// See test case: en, sr, nl ; sh ; sr
-			max, _ = addTags(max)
+			max, _ = max.Maximize()
 		} else {
 			// Base language is not defined.
 			if h != nil {
 				for i := range h.haveTags {
 					have := h.haveTags[i]
-					if have.tag.equalsRest(w) {
+					if equalsRest(have.tag, w) {
 						return have, w, Exact
 					}
 				}
 			}
-			if w.script == 0 && w.region == 0 {
+			if w.ScriptID == 0 && w.RegionID == 0 {
 				// We skip all tags matching und for approximate matching, including
 				// private tags.
 				continue
 			}
-			max, _ = addTags(w)
-			if h = m.index[max.lang]; h == nil {
+			max, _ = w.Maximize()
+			if h = m.index[max.LangID]; h == nil {
 				continue
 			}
 		}
 		pin := true
 		for _, t := range want[i+1:] {
-			if w.lang == t.lang {
+			if w.LangID == t.lang() {
 				pin = false
 				break
 			}
@@ -685,11 +501,11 @@
 		// Check for match based on maximized tag.
 		for i := range h.haveTags {
 			have := h.haveTags[i]
-			best.update(have, w, max.script, max.region, pin)
+			best.update(have, w, max.ScriptID, max.RegionID, pin)
 			if best.conf == Exact {
 				for have.nextMax != 0 {
 					have = h.haveTags[have.nextMax]
-					best.update(have, w, max.script, max.region, pin)
+					best.update(have, w, max.ScriptID, max.RegionID, pin)
 				}
 				return best.have, best.want, best.conf
 			}
@@ -697,9 +513,9 @@
 	}
 	if best.conf <= No {
 		if len(want) != 0 {
-			return nil, want[0], No
+			return nil, want[0].tag(), No
 		}
-		return nil, Tag{}, No
+		return nil, language.Tag{}, No
 	}
 	return best.have, best.want, best.conf
 }
@@ -707,9 +523,9 @@
 // bestMatch accumulates the best match so far.
 type bestMatch struct {
 	have            *haveTag
-	want            Tag
+	want            language.Tag
 	conf            Confidence
-	pinnedRegion    regionID
+	pinnedRegion    language.Region
 	pinLanguage     bool
 	sameRegionGroup bool
 	// Cached results from applying tie-breaking rules.
@@ -734,19 +550,19 @@
 // still prefer a second language over a dialect of the preferred language by
 // explicitly specifying dialects, e.g. "en, nl, en-GB". In this case pin should
 // be false.
-func (m *bestMatch) update(have *haveTag, tag Tag, maxScript scriptID, maxRegion regionID, pin bool) {
+func (m *bestMatch) update(have *haveTag, tag language.Tag, maxScript language.Script, maxRegion language.Region, pin bool) {
 	// Bail if the maximum attainable confidence is below that of the current best match.
 	c := have.conf
 	if c < m.conf {
 		return
 	}
 	// Don't change the language once we already have found an exact match.
-	if m.pinLanguage && tag.lang != m.want.lang {
+	if m.pinLanguage && tag.LangID != m.want.LangID {
 		return
 	}
 	// Pin the region group if we are comparing tags for the same language.
-	if tag.lang == m.want.lang && m.sameRegionGroup {
-		_, sameGroup := regionGroupDist(m.pinnedRegion, have.maxRegion, have.maxScript, m.want.lang)
+	if tag.LangID == m.want.LangID && m.sameRegionGroup {
+		_, sameGroup := regionGroupDist(m.pinnedRegion, have.maxRegion, have.maxScript, m.want.LangID)
 		if !sameGroup {
 			return
 		}
@@ -756,7 +572,7 @@
 		// don't pin anything, otherwise pin the language.
 		m.pinLanguage = pin
 	}
-	if have.tag.equalsRest(tag) {
+	if equalsRest(have.tag, tag) {
 	} else if have.maxScript != maxScript {
 		// There is usually very little comprehension between different scripts.
 		// In a few cases there may still be Low comprehension. This possibility
@@ -786,7 +602,7 @@
 
 	// Tie-breaker rules:
 	// We prefer if the pre-maximized language was specified and identical.
-	origLang := have.tag.lang == tag.lang && tag.lang != 0
+	origLang := have.tag.LangID == tag.LangID && tag.LangID != 0
 	if !beaten && m.origLang != origLang {
 		if m.origLang {
 			return
@@ -795,7 +611,7 @@
 	}
 
 	// We prefer if the pre-maximized region was specified and identical.
-	origReg := have.tag.region == tag.region && tag.region != 0
+	origReg := have.tag.RegionID == tag.RegionID && tag.RegionID != 0
 	if !beaten && m.origReg != origReg {
 		if m.origReg {
 			return
@@ -803,7 +619,7 @@
 		beaten = true
 	}
 
-	regGroupDist, sameGroup := regionGroupDist(have.maxRegion, maxRegion, maxScript, tag.lang)
+	regGroupDist, sameGroup := regionGroupDist(have.maxRegion, maxRegion, maxScript, tag.LangID)
 	if !beaten && m.regGroupDist != regGroupDist {
 		if regGroupDist > m.regGroupDist {
 			return
@@ -811,7 +627,7 @@
 		beaten = true
 	}
 
-	paradigmReg := isParadigmLocale(tag.lang, have.maxRegion)
+	paradigmReg := isParadigmLocale(tag.LangID, have.maxRegion)
 	if !beaten && m.paradigmReg != paradigmReg {
 		if !paradigmReg {
 			return
@@ -820,7 +636,7 @@
 	}
 
 	// Next we prefer if the pre-maximized script was specified and identical.
-	origScript := have.tag.script == tag.script && tag.script != 0
+	origScript := have.tag.ScriptID == tag.ScriptID && tag.ScriptID != 0
 	if !beaten && m.origScript != origScript {
 		if m.origScript {
 			return
@@ -843,9 +659,9 @@
 	}
 }
 
-func isParadigmLocale(lang langID, r regionID) bool {
+func isParadigmLocale(lang language.Language, r language.Region) bool {
 	for _, e := range paradigmLocales {
-		if langID(e[0]) == lang && (r == regionID(e[1]) || r == regionID(e[2])) {
+		if language.Language(e[0]) == lang && (r == language.Region(e[1]) || r == language.Region(e[2])) {
 			return true
 		}
 	}
@@ -854,13 +670,13 @@
 
 // regionGroupDist computes the distance between two regions based on their
 // CLDR grouping.
-func regionGroupDist(a, b regionID, script scriptID, lang langID) (dist uint8, same bool) {
+func regionGroupDist(a, b language.Region, script language.Script, lang language.Language) (dist uint8, same bool) {
 	const defaultDistance = 4
 
 	aGroup := uint(regionToGroups[a]) << 1
 	bGroup := uint(regionToGroups[b]) << 1
 	for _, ri := range matchRegion {
-		if langID(ri.lang) == lang && (ri.script == 0 || scriptID(ri.script) == script) {
+		if language.Language(ri.lang) == lang && (ri.script == 0 || language.Script(ri.script) == script) {
 			group := uint(1 << (ri.group &^ 0x80))
 			if 0x80&ri.group == 0 {
 				if aGroup&bGroup&group != 0 { // Both regions are in the group.
@@ -876,31 +692,16 @@
 	return defaultDistance, true
 }
 
-func (t Tag) variants() string {
-	if t.pVariant == 0 {
-		return ""
-	}
-	return t.str[t.pVariant:t.pExt]
-}
-
-// variantOrPrivateTagStr returns variants or private use tags.
-func (t Tag) variantOrPrivateTagStr() string {
-	if t.pExt > 0 {
-		return t.str[t.pVariant:t.pExt]
-	}
-	return t.str[t.pVariant:]
-}
-
 // equalsRest compares everything except the language.
-func (a Tag) equalsRest(b Tag) bool {
+func equalsRest(a, b language.Tag) bool {
 	// TODO: don't include extensions in this comparison. To do this efficiently,
 	// though, we should handle private tags separately.
-	return a.script == b.script && a.region == b.region && a.variantOrPrivateTagStr() == b.variantOrPrivateTagStr()
+	return a.ScriptID == b.ScriptID && a.RegionID == b.RegionID && a.VariantOrPrivateUseTags() == b.VariantOrPrivateUseTags()
 }
 
 // isExactEquivalent returns true if canonicalizing the language will not alter
 // the script or region of a tag.
-func isExactEquivalent(l langID) bool {
+func isExactEquivalent(l language.Language) bool {
 	for _, o := range notEquivalent {
 		if o == l {
 			return false
@@ -909,25 +710,26 @@
 	return true
 }
 
-var notEquivalent []langID
+var notEquivalent []language.Language
 
 func init() {
 	// Create a list of all languages for which canonicalization may alter the
 	// script or region.
-	for _, lm := range langAliasMap {
-		tag := Tag{lang: langID(lm.from)}
-		if tag, _ = tag.canonicalize(All); tag.script != 0 || tag.region != 0 {
-			notEquivalent = append(notEquivalent, langID(lm.from))
+	for _, lm := range language.AliasMap {
+		tag := language.Tag{LangID: language.Language(lm.From)}
+		if tag, _ = canonicalize(All, tag); tag.ScriptID != 0 || tag.RegionID != 0 {
+			notEquivalent = append(notEquivalent, language.Language(lm.From))
 		}
 	}
 	// Maximize undefined regions of paradigm locales.
 	for i, v := range paradigmLocales {
-		max, _ := addTags(Tag{lang: langID(v[0])})
+		t := language.Tag{LangID: language.Language(v[0])}
+		max, _ := t.Maximize()
 		if v[1] == 0 {
-			paradigmLocales[i][1] = uint16(max.region)
+			paradigmLocales[i][1] = uint16(max.RegionID)
 		}
 		if v[2] == 0 {
-			paradigmLocales[i][2] = uint16(max.region)
+			paradigmLocales[i][2] = uint16(max.RegionID)
 		}
 	}
 }