1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 //go:generate go run gen.go
7 // Package htmlindex maps character set encoding names to Encodings as
8 // recommended by the W3C for use in HTML 5. See http://www.w3.org/TR/encoding.
11 // TODO: perhaps have a "bare" version of the index (used by this package) that
12 // is not pre-loaded with all encodings. Global variables in encodings prevent
13 // the linker from being able to purge unneeded tables. This means that
14 // referencing all encodings, as this package does for the default index, links
15 // in all encodings unconditionally.
17 // This issue can be solved by either solving the linking issue (see
18 // https://github.com/golang/go/issues/6330) or refactoring the encoding tables
19 // (e.g. moving the tables to internal packages that do not use global
22 // TODO: allow canonicalizing names
29 "golang.org/x/text/encoding"
30 "golang.org/x/text/encoding/internal/identifier"
31 "golang.org/x/text/language"
35 errInvalidName = errors.New("htmlindex: invalid encoding name")
36 errUnknown = errors.New("htmlindex: unknown Encoding")
37 errUnsupported = errors.New("htmlindex: this encoding is not supported")
42 matcher language.Matcher
45 // LanguageDefault returns the canonical name of the default encoding for a
47 func LanguageDefault(tag language.Tag) string {
48 matcherOnce.Do(func() {
49 tags := []language.Tag{}
50 for _, t := range strings.Split(locales, " ") {
51 tags = append(tags, language.MustParse(t))
53 matcher = language.NewMatcher(tags, language.PreferSameScript(true))
55 _, i, _ := matcher.Match(tag)
56 return canonical[localeMap[i]] // Default is Windows-1252.
59 // Get returns an Encoding for one of the names listed in
60 // http://www.w3.org/TR/encoding using the Default Index. Matching is case-
62 func Get(name string) (encoding.Encoding, error) {
63 x, ok := nameMap[strings.ToLower(strings.TrimSpace(name))]
65 return nil, errInvalidName
67 return encodings[x], nil
70 // Name reports the canonical name of the given Encoding. It will return
71 // an error if e is not associated with a supported encoding scheme.
72 func Name(e encoding.Encoding) (string, error) {
73 id, ok := e.(identifier.Interface)
83 return "", errUnsupported
85 return canonical[v], nil