// getLangISO3 returns the langID for the given 3-letter ISO language code // or unknownLang if this does not exist. func getLangISO3(s []byte) (langID, error) { if tag.FixCase("und", s) { // first try to match canonical 3-letter entries for i := lang.Index(s[:2]); i != -1; i = lang.Next(s[:2], i) { if e := lang.Elem(i); e[3] == 0 && e[2] == s[2] { // We treat "und" as special and always translate it to "unspecified". // Note that ZZ and Zzzz are private use and are not treated as // unspecified by default. id := langID(i) if id == nonCanonicalUnd { return 0, nil } return id, nil } } if i := altLangISO3.Index(s); i != -1 { return langID(altLangIndex[altLangISO3.Elem(i)[3]]), nil } n := strToInt(s) if langNoIndex[n/8]&(1<<(n%8)) != 0 { return langID(n) + langNoIndexOffset, nil } // Check for non-canonical uses of ISO3. for i := lang.Index(s[:1]); i != -1; i = lang.Next(s[:1], i) { if e := lang.Elem(i); e[2] == s[1] && e[3] == s[2] { return langID(i), nil } } return 0, mkErrInvalid(s) } return 0, errSyntax }
// getLangISO2 returns the langID for the given 2-letter ISO language code // or unknownLang if this does not exist. func getLangISO2(s []byte) (langID, error) { if !tag.FixCase("zz", s) { return 0, errSyntax } if i := lang.Index(s); i != -1 && lang.Elem(i)[3] != 0 { return langID(i), nil } return 0, mkErrInvalid(s) }
// findIndex tries to find the given tag in idx and returns a standardized error // if it could not be found. func findIndex(idx tag.Index, key []byte, form string) (index int, err error) { if !tag.FixCase(form, key) { return 0, errSyntax } i := idx.Index(key) if i == -1 { return 0, mkErrInvalid(key) } return i, nil }
// ParseISO parses a 3-letter ISO 4217 code. It returns an error if s not // well-formed or not a recognized currency code. func ParseISO(s string) (Currency, error) { var buf [4]byte // Take one byte more to detect oversize keys. key := buf[:copy(buf[:], s)] if !tag.FixCase("XXX", key) { return Currency{}, errSyntax } if i := currency.Index(key); i >= 0 { return Currency{uint16(i)}, nil } return Currency{}, errValue }
// FromTag reports the most likely currency for the given tag. It considers the // currency defined in the -u extension and infers the region if necessary. func FromTag(t language.Tag) (Currency, language.Confidence) { if cur := t.TypeForKey("cu"); len(cur) == 3 { var buf [3]byte copy(buf[:], cur) tag.FixCase("XXX", buf[:]) if x := currency.Index(buf[:]); x > 0 { return Currency{uint16(x)}, language.Exact } } r, conf := t.Region() if cur, ok := FromRegion(r); ok { return cur, conf } return Currency{}, language.No }
// getRegionISO3 returns the regionID for the given 3-letter ISO country code // or unknownRegion if this does not exist. func getRegionISO3(s []byte) (regionID, error) { if tag.FixCase("ZZZ", s) { for i := regionISO.Index(s[:1]); i != -1; i = regionISO.Next(s[:1], i) { if e := regionISO.Elem(i); e[2] == s[1] && e[3] == s[2] { return regionID(i) + isoRegionOffset, nil } } for i := 0; i < len(altRegionISO3); i += 3 { if tag.Compare(altRegionISO3[i:i+3], s) == 0 { return regionID(altRegionIDs[i/3]), nil } } return 0, mkErrInvalid(s) } return 0, errSyntax }