-
Notifications
You must be signed in to change notification settings - Fork 12
/
charsets.go
88 lines (80 loc) · 2.07 KB
/
charsets.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package tds
import (
"fmt"
"sync"
"golang.org/x/net/html/charset"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/ianaindex"
)
// utility functions to convert sybase charset names to html charset names.
// maps sybase charset to their htlm/iana names
var nameMap = map[string]string{
"utf8": "utf-8",
"big5": "big5",
"big5hk": "big5",
"iso_1": "iso-8859-1",
"cp874": "windows-874",
"cp437": "cp437",
"cp850": "cp850",
"cp852": "cp852",
"cp855": "cp855",
"cp857": "cp857",
"cp858": "cp858",
"cp860": "cp860",
"cp864": "cp864",
"cp866": "cp866",
"cp869": "cp869",
"cp932": "cp932",
"cp936": "cp936",
"cp950": "cp950",
"cp1250": "cp1250",
"cp1251": "cp1251",
"cp1252": "cp1252",
"cp1253": "cp1253",
"cp1254": "cp1254",
"cp1255": "cp1255",
"cp1256": "cp1256",
"cp1257": "cp1257",
"cp1258": "cp1258",
"gb18030": "gb18030",
"greek8": "greek8",
"iso88592": "iso88592",
"iso88595": "iso88595",
"iso88596": "iso88596",
"iso88597": "iso88597",
"iso88598": "iso88598",
"iso88599": "iso88599",
"iso15": "iso-8859-15",
"koi8": "koi8-r",
"sjis": "sjis",
"tis-620": "tis-620",
"roman8": "HPRoman8",
}
var nameToCharset = map[string]encoding.Encoding{}
var nameToCharsetMutex sync.RWMutex
// fetch the encodings from html aliases and iana index
func init() {
for sybName, goName := range nameMap {
if e, _ := charset.Lookup(goName); e != nil {
nameToCharset[sybName] = e
continue
}
if e, _ := ianaindex.IANA.Encoding(goName); e != nil {
nameToCharset[sybName] = e
}
}
}
func getEncoding(sybName string) (encoding.Encoding, error) {
nameToCharsetMutex.RLock()
defer nameToCharsetMutex.RUnlock()
if e, found := nameToCharset[sybName]; found {
return e, nil
}
return nil, fmt.Errorf("netlib: unsupported charset: %s", sybName)
}
// RegisterEncoding register encoding for the charset
func RegisterEncoding(sybaseCharsetName string, e encoding.Encoding) {
nameToCharsetMutex.Lock()
defer nameToCharsetMutex.Unlock()
nameToCharset[sybaseCharsetName] = e
}