-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgonvert.go
More file actions
132 lines (121 loc) · 2.72 KB
/
Copy pathgonvert.go
File metadata and controls
132 lines (121 loc) · 2.72 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package gonvert
import (
"github.com/k0kubun/pp"
"github.com/saintfish/chardet"
c "github.com/timakin/gonvert/converter"
)
type CharCode int
const (
UTF8 CharCode = iota
SJIS
EUCJP
GBK
UTF16BE
UTF16LE
)
var charcodes = map[string]CharCode{
"UTF-8": UTF8,
"Shift_JIS": SJIS,
"EUC-JP": EUCJP,
"GBK": GBK,
"UTF-16BE": UTF16BE,
"UTF-16LE": UTF16LE,
}
type CodePair struct {
From, To CharCode
}
func New(text string, codes ...CharCode) c.Converter {
converter := createConverter(text, codes)
return converter
}
func createConverter(text string, codes []CharCode) c.Converter {
textByte := []byte(text)
var fromCode, toCode CharCode
if len(codes) == 1 {
charDetector := chardet.NewTextDetector()
detectResult, err := charDetector.DetectBest(textByte)
if err != nil {
pp.Fatal(err)
}
fromCode = charcodes[detectResult.Charset]
toCode = codes[0]
} else if len(codes) >= 2 {
toCode = codes[0]
fromCode = codes[1]
} else {
panic("Please specify 1 or 2 character-code arguements.")
}
var converter c.Converter
codepair := CodePair{fromCode, toCode}
switch codepair {
case (CodePair{UTF8, SJIS}):
{
converter = &c.UTF8ToSJISConverter{TextByte: textByte}
}
case (CodePair{SJIS, UTF8}):
{
converter = &c.SJISToUTF8Converter{TextByte: textByte}
}
case (CodePair{UTF8, EUCJP}):
{
converter = &c.UTF8ToEUCJPConverter{TextByte: textByte}
}
case (CodePair{EUCJP, UTF8}):
{
converter = &c.EUCJPToUTF8Converter{TextByte: textByte}
}
case (CodePair{SJIS, EUCJP}):
{
converter = &c.SJISToEUCJPConverter{TextByte: textByte}
}
case (CodePair{EUCJP, SJIS}):
{
converter = &c.EUCJPToSJISConverter{TextByte: textByte}
}
case (CodePair{UTF8, GBK}):
{
converter = &c.UTF8ToGBKConverter{TextByte: textByte}
}
case (CodePair{GBK, UTF8}):
{
converter = &c.GBKToUTF8Converter{TextByte: textByte}
}
case (CodePair{GBK, SJIS}):
{
converter = &c.GBKToSJISConverter{TextByte: textByte}
}
case (CodePair{SJIS, GBK}):
{
converter = &c.SJISToGBKConverter{TextByte: textByte}
}
case (CodePair{GBK, EUCJP}):
{
converter = &c.GBKToEUCJPConverter{TextByte: textByte}
}
case (CodePair{EUCJP, GBK}):
{
converter = &c.EUCJPToGBKConverter{TextByte: textByte}
}
case (CodePair{UTF8, UTF16BE}):
{
converter = &c.UTF8ToUTF16BEConverter{TextByte: textByte}
}
case (CodePair{UTF16BE, UTF8}):
{
converter = &c.UTF16BEToUTF8Converter{TextByte: textByte}
}
case (CodePair{UTF8, UTF16LE}):
{
converter = &c.UTF8ToUTF16LEConverter{TextByte: textByte}
}
case (CodePair{UTF16LE, UTF8}):
{
converter = &c.UTF16LEToUTF8Converter{TextByte: textByte}
}
default:
{
converter = &c.DefaultConverter{TextByte: textByte}
}
}
return converter
}