/
gbk.go
147 lines (127 loc) · 2.32 KB
/
gbk.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package charset
import (
"github.com/3th1nk/easygo/util/logs"
"golang.org/x/text/encoding/simplifiedchinese"
)
func IsGB2312(s string) bool {
for i := 0; i < len(s); {
c0 := s[i]
switch {
default:
return false
case c0 < 0x80:
i++
case (0xA1 <= c0 && c0 <= 0xA9) || (0xB0 <= c0 && c0 <= 0xF7):
// 双字节
// 0xA1-0xA9 0xA1-0xFE
// 0xB0-0xF7 0xA1-0xFE
if i+1 >= len(s) {
// 数据不完整
return false
}
c1 := s[i+1]
if c1 < 0xA1 || 0xFE < c1 {
return false
}
i += 2
}
}
return true
}
func IsGBK(s string) bool {
for i := 0; i < len(s); {
c0 := s[i]
switch {
default:
return false
case c0 <= 0x80:
// Microsoft's Code Page 936 extends GBK 1.0 to encode the euro sign U+20AC as 0x80
i++
continue
case c0 < 0xFF:
// 双字节 0x81–0xFE 0x40–0xFE(0x7F除外)
if i+1 >= len(s) {
// 数据不完整
return false
}
c1 := s[i+1]
if c1 < 0x40 || c1 == 0x7F || 0xFE < c1 {
return false
}
i += 2
}
}
return true
}
func IsGB18030(s string) bool {
i := 0
loop:
for i < len(s) {
c0 := s[i]
switch {
default:
return false
case c0 <= 0x80:
// Microsoft's Code Page 936 extends GBK 1.0 to encode the euro sign U+20AC as 0x80
i++
continue
case c0 < 0xFF:
if i+1 >= len(s) {
// 数据不完整
return false
}
c1 := s[i+1]
switch {
default:
return false
case 0x40 <= c1 && c1 != 0x7F && c1 < 0xFF:
// 双字节 0x81–0xFE 0x40–0xFE(0x7F除外)
i += 2
goto loop
case 0x30 <= c1 && c1 < 0x40:
// 四字节 0x81-0xFE 0x30-0x39 0x81-0xFE 0x30-0x39
if i+3 >= len(s) {
// 数据不完整
return false
}
c2 := s[i+2]
if c2 < 0x81 || 0xFF <= c2 {
return false
}
c3 := s[i+3]
if c3 < 0x30 || 0x3A <= c3 {
return false
}
i += 4
goto loop
}
}
}
return true
}
func ToGBK(s string) string {
if IsGBK(s) {
return s
}
if IsUTF8(s) {
s1, err := simplifiedchinese.GBK.NewEncoder().String(s)
if err == nil {
return s1
}
logs.Default.Error(err.Error())
}
return s
}
func ToGB18030(s string) string {
if IsGB18030(s) {
return s
}
if IsUTF8(s) {
s1, err := simplifiedchinese.GB18030.NewEncoder().String(s)
if err == nil {
return s1
}
logs.Default.Error(err.Error())
}
return s
}