...
1
2
3
4
5 package norm
6
7 import "encoding/binary"
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 const (
34 qcInfoMask = 0x3F
35 headerLenMask = 0x3F
36 headerFlagsMask = 0xC0
37 )
38
39
40 type Properties struct {
41 pos uint8
42 size uint8
43 ccc uint8
44 tccc uint8
45 nLead uint8
46 flags qcInfo
47 index uint16
48 }
49
50
51 type lookupFunc func(b input, i int) Properties
52
53
54 type formInfo struct {
55 form Form
56 composing, compatibility bool
57 info lookupFunc
58 nextMain iterFunc
59 }
60
61 var formTable = []*formInfo{{
62 form: NFC,
63 composing: true,
64 compatibility: false,
65 info: lookupInfoNFC,
66 nextMain: nextComposed,
67 }, {
68 form: NFD,
69 composing: false,
70 compatibility: false,
71 info: lookupInfoNFC,
72 nextMain: nextDecomposed,
73 }, {
74 form: NFKC,
75 composing: true,
76 compatibility: true,
77 info: lookupInfoNFKC,
78 nextMain: nextComposed,
79 }, {
80 form: NFKD,
81 composing: false,
82 compatibility: true,
83 info: lookupInfoNFKC,
84 nextMain: nextDecomposed,
85 }}
86
87
88
89
90
91
92
93
94
95 func (p Properties) BoundaryBefore() bool {
96 if p.ccc == 0 && !p.combinesBackward() {
97 return true
98 }
99
100
101
102 return false
103 }
104
105
106
107 func (p Properties) BoundaryAfter() bool {
108
109 return p.isInert()
110 }
111
112
113
114
115
116
117
118
119
120
121 type qcInfo uint8
122
123 func (p Properties) isYesC() bool { return p.flags&0x10 == 0 }
124 func (p Properties) isYesD() bool { return p.flags&0x4 == 0 }
125
126 func (p Properties) combinesForward() bool { return p.flags&0x20 != 0 }
127 func (p Properties) combinesBackward() bool { return p.flags&0x8 != 0 }
128 func (p Properties) hasDecomposition() bool { return p.flags&0x4 != 0 }
129
130 func (p Properties) isInert() bool {
131 return p.flags&qcInfoMask == 0 && p.ccc == 0
132 }
133
134 func (p Properties) multiSegment() bool {
135 return p.index >= firstMulti && p.index < endMulti
136 }
137
138 func (p Properties) nLeadingNonStarters() uint8 {
139 return p.nLead
140 }
141
142 func (p Properties) nTrailingNonStarters() uint8 {
143 return uint8(p.flags & 0x03)
144 }
145
146
147
148 func (p Properties) Decomposition() []byte {
149
150 if p.index == 0 {
151 return nil
152 }
153 i := p.index
154 n := decomps[i] & headerLenMask
155 i++
156 return decomps[i : i+uint16(n)]
157 }
158
159
160 func (p Properties) Size() int {
161 return int(p.size)
162 }
163
164
165 func (p Properties) CCC() uint8 {
166 if p.index >= firstCCCZeroExcept {
167 return 0
168 }
169 return ccc[p.ccc]
170 }
171
172
173
174 func (p Properties) LeadCCC() uint8 {
175 return ccc[p.ccc]
176 }
177
178
179
180 func (p Properties) TrailCCC() uint8 {
181 return ccc[p.tccc]
182 }
183
184 func buildRecompMap() {
185 recompMap = make(map[uint32]rune, len(recompMapPacked)/8)
186 var buf [8]byte
187 for i := 0; i < len(recompMapPacked); i += 8 {
188 copy(buf[:], recompMapPacked[i:i+8])
189 key := binary.BigEndian.Uint32(buf[:4])
190 val := binary.BigEndian.Uint32(buf[4:])
191 recompMap[key] = rune(val)
192 }
193 }
194
195
196
197
198
199
200
201
202
203
204
205
206 func combine(a, b rune) rune {
207 key := uint32(uint16(a))<<16 + uint32(uint16(b))
208 if recompMap == nil {
209 panic("caller error")
210 }
211 return recompMap[key]
212 }
213
214 func lookupInfoNFC(b input, i int) Properties {
215 v, sz := b.charinfoNFC(i)
216 return compInfo(v, sz)
217 }
218
219 func lookupInfoNFKC(b input, i int) Properties {
220 v, sz := b.charinfoNFKC(i)
221 return compInfo(v, sz)
222 }
223
224
225 func (f Form) Properties(s []byte) Properties {
226 if f == NFC || f == NFD {
227 return compInfo(nfcData.lookup(s))
228 }
229 return compInfo(nfkcData.lookup(s))
230 }
231
232
233 func (f Form) PropertiesString(s string) Properties {
234 if f == NFC || f == NFD {
235 return compInfo(nfcData.lookupString(s))
236 }
237 return compInfo(nfkcData.lookupString(s))
238 }
239
240
241
242
243 func compInfo(v uint16, sz int) Properties {
244 if v == 0 {
245 return Properties{size: uint8(sz)}
246 } else if v >= 0x8000 {
247 p := Properties{
248 size: uint8(sz),
249 ccc: uint8(v),
250 tccc: uint8(v),
251 flags: qcInfo(v >> 8),
252 }
253 if p.ccc > 0 || p.combinesBackward() {
254 p.nLead = uint8(p.flags & 0x3)
255 }
256 return p
257 }
258
259 h := decomps[v]
260 f := (qcInfo(h&headerFlagsMask) >> 2) | 0x4
261 p := Properties{size: uint8(sz), flags: f, index: v}
262 if v >= firstCCC {
263 v += uint16(h&headerLenMask) + 1
264 c := decomps[v]
265 p.tccc = c >> 2
266 p.flags |= qcInfo(c & 0x3)
267 if v >= firstLeadingCCC {
268 p.nLead = c & 0x3
269 if v >= firstStarterWithNLead {
270
271 p.flags &= 0x03
272 p.index = 0
273 return p
274 }
275 p.ccc = decomps[v+1]
276 }
277 }
278 return p
279 }
280
View as plain text