-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrans-utf32.go
211 lines (190 loc) · 3.87 KB
/
trans-utf32.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package chardet
import (
. "unicode/utf8"
"golang.org/x/text/encoding"
"golang.org/x/text/transform"
)
// 提供给用户使用的并发安全的编解码器接口
var (
UTF32BE = &utf32be{}
UTF32LE = &utf32le{}
)
type u32bt8 struct {
hold []byte
}
// UTF-16BE => UTF-8
func (u *u32bt8) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
ld, ls := len(dst), len(src)
us := []rune{}
for nSrc < ls {
if nSrc+3 >= ls {
if !atEOF {
err = transform.ErrShortSrc
break
}
us = append(us, 0xFFFD)
nSrc++
} else {
a := src[nSrc]
nSrc++
b := src[nSrc]
nSrc++
c := src[nSrc]
nSrc++
d := src[nSrc]
nSrc++
us = append(us, (rune(a)<<24)|(rune(b)<<16)|(rune(c)<<8)|rune(d))
}
}
if l := len(us); l > 0 {
u.hold = append(u.hold, string(us)...)
}
lu := len(u.hold)
copy(dst, u.hold)
if lu > ld {
u.hold = u.hold[ld:]
return ld, nSrc, transform.ErrShortDst
}
nDst = lu
return
}
func (u *u32bt8) Reset() {
u.hold = nil
}
type u32lt8 struct {
hold []byte
}
// UTF-16BE => UTF-8
func (u *u32lt8) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
ld, ls := len(dst), len(src)
us := []rune{}
for nSrc < ls {
if nSrc+3 >= ls {
if !atEOF {
err = transform.ErrShortSrc
break
}
us = append(us, 0xFFFD)
nSrc++
} else {
a := src[nSrc]
nSrc++
b := src[nSrc]
nSrc++
c := src[nSrc]
nSrc++
d := src[nSrc]
nSrc++
us = append(us, (rune(d)<<24)|(rune(c)<<16)|(rune(b)<<8)|rune(a))
}
}
if l := len(us); l > 0 {
u.hold = append(u.hold, string(us)...)
}
lu := len(u.hold)
copy(dst, u.hold)
if lu > ld {
u.hold = u.hold[ld:]
return ld, nSrc, transform.ErrShortDst
}
nDst = lu
return
}
func (u *u32lt8) Reset() {
u.hold = nil
}
type u8t32b struct {
hold []byte
}
// UTF-8 => UTF-16BE
func (u *u8t32b) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
ld, ls := len(dst), len(src)
rs := []rune{}
for nSrc < ls {
r, n := DecodeRune(src[nSrc:])
if n == 1 && r == 0xFFFD {
if nSrc+1 >= ls {
if !atEOF {
err = transform.ErrShortSrc
break
}
}
}
rs = append(rs, r)
nSrc += n
}
for _, r := range rs {
a := byte((r >> 0x18) & 255)
b := byte((r >> 0x10) & 255)
c := byte((r >> 0x08) & 255)
d := byte((r >> 0x00) & 255)
u.hold = append(u.hold, a, b, c, d)
}
lu := len(u.hold)
copy(dst, u.hold)
if lu > ld {
u.hold = u.hold[ld:]
return ld, nSrc, transform.ErrShortDst
}
nDst = lu
return
}
func (u *u8t32b) Reset() {
u.hold = nil
}
type u8t32l struct {
hold []byte
}
// UTF-8 => UTF-16BE
func (u *u8t32l) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
ld, ls := len(dst), len(src)
rs := []rune{}
for nSrc < ls {
r, n := DecodeRune(src[nSrc:])
if n == 1 && r == 0xFFFD {
if nSrc+1 >= ls {
if !atEOF {
err = transform.ErrShortSrc
break
}
}
}
rs = append(rs, r)
nSrc += n
}
for _, r := range rs {
a := byte((r >> 0x18) & 255)
b := byte((r >> 0x10) & 255)
c := byte((r >> 0x08) & 255)
d := byte((r >> 0x00) & 255)
u.hold = append(u.hold, d, c, b, a)
}
lu := len(u.hold)
copy(dst, u.hold)
if lu > ld {
u.hold = u.hold[ld:]
return ld, nSrc, transform.ErrShortDst
}
nDst = lu
return
}
func (u *u8t32l) Reset() {
u.hold = nil
}
type utf32le struct{}
// 返回解码小端在前的UTF-16到UTF-8的解码器
func (u utf32le) NewDecoder() *encoding.Decoder {
return &encoding.Decoder{Transformer: &u32lt8{}}
}
func (u utf32le) NewEncoder() *encoding.Encoder {
return &encoding.Encoder{Transformer: &u8t32l{}}
}
type utf32be struct{}
// 返回编码UTF-8到大端在前的UTF-16的解码器
func (u utf32be) NewDecoder() *encoding.Decoder {
return &encoding.Decoder{Transformer: &u32bt8{}}
}
// 返回解码大端在前的UTF-16到UTF-8的解码器
func (u utf32be) NewEncoder() *encoding.Encoder {
return &encoding.Encoder{Transformer: &u32bt8{}}
}