Skip to content

Commit 378916e

Browse files
committed
Add East Asian charset mappings
1 parent f7a42f6 commit 378916e

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

‎go/sql/encoding.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ package sql
88
import (
99
"golang.org/x/text/encoding"
1010
"golang.org/x/text/encoding/charmap"
11+
"golang.org/x/text/encoding/japanese"
12+
"golang.org/x/text/encoding/korean"
1113
"golang.org/x/text/encoding/simplifiedchinese"
14+
"golang.org/x/text/encoding/traditionalchinese"
1215
)
1316

1417
type charsetEncoding map[string]encoding.Encoding
@@ -20,4 +23,8 @@ func init() {
2023
// Begin mappings
2124
charsetEncodingMap["latin1"] = charmap.Windows1252
2225
charsetEncodingMap["gbk"] = simplifiedchinese.GBK
26+
charsetEncodingMap["big5"] = traditionalchinese.Big5
27+
charsetEncodingMap["sjis"] = japanese.ShiftJIS
28+
charsetEncodingMap["ujis"] = japanese.EUCJP
29+
charsetEncodingMap["euckr"] = korean.EUCKR
2330
}

‎go/sql/encoding_test.go‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Copyright 2016 GitHub Inc.
3+
See https://github.com/github/gh-ost/blob/master/LICENSE
4+
*/
5+
6+
package sql
7+
8+
import (
9+
"testing"
10+
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
func TestCharsetEncodingMap(t *testing.T) {
15+
testCases := []struct {
16+
name string
17+
charset string
18+
encoded []byte
19+
decoded string
20+
}{
21+
{name: "Big5", charset: "big5", encoded: []byte{0xa4, 0xa4, 0xa4, 0xe5}, decoded: "中文"},
22+
{name: "Shift-JIS", charset: "sjis", encoded: []byte{0x93, 0xfa, 0x96, 0x7b}, decoded: "日本"},
23+
{name: "EUC-JP", charset: "ujis", encoded: []byte{0xc6, 0xfc, 0xcb, 0xdc}, decoded: "日本"},
24+
{name: "EUC-KR", charset: "euckr", encoded: []byte{0xc7, 0xd1, 0xb1, 0xb9}, decoded: "한국"},
25+
}
26+
27+
for _, testCase := range testCases {
28+
t.Run(testCase.name, func(t *testing.T) {
29+
charset, ok := charsetEncodingMap[testCase.charset]
30+
require.True(t, ok)
31+
32+
decoded, err := charset.NewDecoder().Bytes(testCase.encoded)
33+
require.NoError(t, err)
34+
require.Equal(t, testCase.decoded, string(decoded))
35+
})
36+
}
37+
}

0 commit comments

Comments
 (0)