func addTags()

in internal/language/match.go [71:187]


func addTags(t Tag) (Tag, error) {
	// We leave private use identifiers alone.
	if t.IsPrivateUse() {
		return t, nil
	}
	if t.ScriptID != 0 && t.RegionID != 0 {
		if t.LangID != 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.RegionID : t.RegionID+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 Script(x.script) == t.ScriptID {
				t.setUndefinedLang(Language(x.lang))
				return t, nil
			}
		}
	}
	if t.LangID != 0 {
		// Search matches for lang-script and lang-region, where lang != und.
		if t.LangID < langNoIndexOffset {
			x := likelyLang[t.LangID]
			if x.flags&isList != 0 {
				list := likelyLangList[x.region : x.region+uint16(x.script)]
				if t.ScriptID != 0 {
					for _, x := range list {
						if Script(x.script) == t.ScriptID && x.flags&scriptInFrom != 0 {
							t.setUndefinedRegion(Region(x.region))
							return t, nil
						}
					}
				} else if t.RegionID != 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.RegionID.Contains(Region(x.region)) {
							tt.RegionID = Region(x.region)
							tt.setUndefinedScript(Script(x.script))
							goodScript = goodScript && tt.ScriptID == Script(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.ScriptID = tt.ScriptID
					}
				}
			}
		}
	} else {
		// Search matches for und-script.
		if t.ScriptID != 0 {
			x := likelyScript[t.ScriptID]
			if x.region != 0 {
				t.setUndefinedRegion(Region(x.region))
				t.setUndefinedLang(Language(x.lang))
				return t, nil
			}
		}
		// Search matches for und-region. If und-script-region exists, it would
		// have been found earlier.
		if t.RegionID != 0 {
			if i := regionInclusion[t.RegionID]; i < nRegionGroups {
				x := likelyRegionGroup[i]
				if x.region != 0 {
					t.setUndefinedLang(Language(x.lang))
					t.setUndefinedScript(Script(x.script))
					t.RegionID = Region(x.region)
				}
			} else {
				x := likelyRegion[t.RegionID]
				if x.flags&isList != 0 {
					x = likelyRegionList[x.lang]
				}
				if x.script != 0 && x.flags != scriptInFrom {
					t.setUndefinedLang(Language(x.lang))
					t.setUndefinedScript(Script(x.script))
					return t, nil
				}
			}
		}
	}

	// Search matches for lang.
	if t.LangID < langNoIndexOffset {
		x := likelyLang[t.LangID]
		if x.flags&isList != 0 {
			x = likelyLangList[x.region]
		}
		if x.region != 0 {
			t.setUndefinedScript(Script(x.script))
			t.setUndefinedRegion(Region(x.region))
		}
		specializeRegion(&t)
		if t.LangID == 0 {
			t.LangID = _en // default language
		}
		return t, nil
	}
	return t, ErrMissingLikelyTagsData
}