Debian Codegraph @

unicode/norm/forminfo.go

raw
1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package norm
6
7 import "encoding/binary"
8
9 // This file contains Form-specific logic and wrappers for data in tables.go.
10
11 // Rune info is stored in a separate trie per composing form. A composing form
12 // and its corresponding decomposing form share the same trie. Each trie maps
13 // a rune to a uint16. The values take two forms. For v >= 0x8000:
14 // bits
15 // 15: 1 (inverse of NFD_QC bit of qcInfo)
16 // 12..7: qcInfo (see below). isYesD is always true (no decomposition).
17 // 6..0: ccc (compressed CCC value).
18 // For v < 0x8000, the respective rune has a decomposition and v is an index
19 // into a byte array of UTF-8 decomposition sequences and additional info and
20 // has the form:
21 // <header> <decomp_byte>* [<tccc> [<lccc>]]
22 // The header contains the number of bytes in the decomposition (excluding this
23 // length byte), with 33 mapped to 31 to fit in 5 bits.
24 // (If any 31- or 32-byte decompositions come along, we could switch to using
25 // use a general lookup table as long as there are at most 32 distinct lengths.)
26 // The three most significant bits of this length byte correspond
27 // to bit 5, 4, and 3 of qcInfo (see below). The byte sequence itself starts at v+1.
28 // The byte sequence is followed by a trailing and leading CCC if the values
29 // for these are not zero. The value of v determines which ccc are appended
30 // to the sequences. For v < firstCCC, there are none, for v >= firstCCC,
31 // the sequence is followed by a trailing ccc, and for v >= firstLeadingCC
32 // there is an additional leading ccc. The value of tccc itself is the
33 // trailing CCC shifted left 2 bits. The two least-significant bits of tccc
34 // are the number of trailing non-starters.
35
36 const (
37 qcInfoMask = 0x3F // to clear all but the relevant bits in a qcInfo
38 headerLenMask = 0x1F // extract the length value from the header byte (31 => 33)
39 headerFlagsMask = 0xE0 // extract the qcInfo bits from the header byte
40 )
41
42 // Properties provides access to normalization properties of a rune.
43 type Properties struct {
44 pos uint8 // start position in reorderBuffer; used in composition.go
45 size uint8 // length of UTF-8 encoding of this rune
46 ccc uint8 // leading canonical combining class (ccc if not decomposition)
47 tccc uint8 // trailing canonical combining class (ccc if not decomposition)
48 nLead uint8 // number of leading non-starters.
49 flags qcInfo // quick check flags
50 index uint16
51 }
52
53 // functions dispatchable per form
54 type lookupFunc func(b input, i int) Properties
55
56 // formInfo holds Form-specific functions and tables.
57 type formInfo struct {
58 form Form
59 composing, compatibility bool // form type
60 info lookupFunc
61 nextMain iterFunc
62 }
63
64 var formTable = []*formInfo{{
65 form: NFC,
66 composing: true,
67 compatibility: false,
68 info: lookupInfoNFC,
69 nextMain: nextComposed,
70 }, {
71 form: NFD,
72 composing: false,
73 compatibility: false,
74 info: lookupInfoNFC,
75 nextMain: nextDecomposed,
76 }, {
77 form: NFKC,
78 composing: true,
79 compatibility: true,
80 info: lookupInfoNFKC,
81 nextMain: nextComposed,
82 }, {
83 form: NFKD,
84 composing: false,
85 compatibility: true,
86 info: lookupInfoNFKC,
87 nextMain: nextDecomposed,
88 }}
89
90 // We do not distinguish between boundaries for NFC, NFD, etc. to avoid
91 // unexpected behavior for the user. For example, in NFD, there is a boundary
92 // after 'a'. However, 'a' might combine with modifiers, so from the application's
93 // perspective it is not a good boundary. We will therefore always use the
94 // boundaries for the combining variants.
95
96 // BoundaryBefore returns true if this rune starts a new segment and
97 // cannot combine with any rune on the left.
98 func (p Properties) BoundaryBefore() bool {
99 if p.ccc == 0 && !p.combinesBackward() {
100 return true
101 }
102 // We assume that the CCC of the first character in a decomposition
103 // is always non-zero if different from info.ccc and that we can return
104 // false at this point. This is verified by maketables.
105 return false
106 }
107
108 // BoundaryAfter returns true if runes cannot combine with or otherwise
109 // interact with this or previous runes.
110 func (p Properties) BoundaryAfter() bool {
111 // TODO: loosen these conditions.
112 return p.isInert()
113 }
114
115 // We pack quick check data in 6 bits:
116 //
117 // 5: Combines forward (0 == false, 1 == true)
118 // 4..3: NFC_QC Yes(00), No (10), or Maybe (11)
119 // 2: NFD_QC Yes (0) or No (1). No also means there is a decomposition.
120 // 1..0: Number of trailing non-starters.
121 //
122 // When all 6 bits are zero, the character is inert, meaning it is never
123 // influenced by normalization.
124 //
125 // We set flags to 0x80 (high bit 7 unused in quick check data) to indicate an invalid rune.
126 type qcInfo uint8
127
128 func (p Properties) isInvalid() bool { return p.flags == 0x80 }
129
130 func (p Properties) isYesC() bool { return p.flags&0x10 == 0 }
131 func (p Properties) isYesD() bool { return p.flags&0x4 == 0 }
132
133 func (p Properties) combinesForward() bool { return p.flags&0x20 != 0 }
134 func (p Properties) combinesBackward() bool { return p.flags&0x8 != 0 } // == isMaybe
135 func (p Properties) hasDecomposition() bool { return p.flags&0x4 != 0 } // == isNoD
136
137 func (p Properties) isInert() bool {
138 return p.flags&qcInfoMask == 0 && p.ccc == 0
139 }
140
141 func (p Properties) multiSegment() bool {
142 return p.index >= firstMulti && p.index < endMulti
143 }
144
145 func (p Properties) nLeadingNonStarters() uint8 {
146 return p.nLead
147 }
148
149 func (p Properties) nTrailingNonStarters() uint8 {
150 return uint8(p.flags & 0x03)
151 }
152
153 // Decomposition returns the decomposition for the underlying rune
154 // or nil if there is none.
155 func (p Properties) Decomposition() []byte {
156 // TODO: create the decomposition for Hangul?
157 if p.index == 0 {
158 return nil
159 }
160 i := p.index
161 n := decomps[i] & headerLenMask
162 if n == 31 {
163 n = 33
164 }
165 i++
166 return decomps[i : i+uint16(n)]
167 }
168
169 // Size returns the length of UTF-8 encoding of the rune.
170 func (p Properties) Size() int {
171 return int(p.size)
172 }
173
174 // CCC returns the canonical combining class of the underlying rune.
175 func (p Properties) CCC() uint8 {
176 if p.index >= firstCCCZeroExcept {
177 return 0
178 }
179 return ccc[p.ccc]
180 }
181
182 // LeadCCC returns the CCC of the first rune in the decomposition.
183 // If there is no decomposition, LeadCCC equals CCC.
184 func (p Properties) LeadCCC() uint8 {
185 return ccc[p.ccc]
186 }
187
188 // TrailCCC returns the CCC of the last rune in the decomposition.
189 // If there is no decomposition, TrailCCC equals CCC.
190 func (p Properties) TrailCCC() uint8 {
191 return ccc[p.tccc]
192 }
193
194 func buildRecompMap() {
195 recompMap = make(map[uint32]rune, len(recompMapPacked)/8)
196 var buf [8]byte
197 for i := 0; i < len(recompMapPacked); i += 8 {
198 copy(buf[:], recompMapPacked[i:i+8])
199 key := binary.BigEndian.Uint32(buf[:4])
200 val := binary.BigEndian.Uint32(buf[4:])
201 recompMap[key] = rune(val)
202 }
203 }
204
205 // Recomposition
206 // We use 32-bit keys instead of 64-bit for the two codepoint keys.
207 // This clips off the bits of three entries, but we know this will not
208 // result in a collision. In the unlikely event that changes to
209 // UnicodeData.txt introduce collisions, the compiler will catch it.
210 // Note that the recomposition map for NFC and NFKC are identical.
211
212 // combine returns the combined rune or 0 if it doesn't exist.
213 //
214 // The caller is responsible for calling
215 // recompMapOnce.Do(buildRecompMap) sometime before this is called.
216 func combine(a, b rune) rune {
217 key := uint32(uint16(a))<<16 + uint32(uint16(b))
218 if recompMap == nil {
219 panic("caller error") // see func comment
220 }
221 return recompMap[key]
222 }
223
224 func lookupInfoNFC(b input, i int) Properties {
225 v, sz := b.charinfoNFC(i)
226 return compInfo(v, sz)
227 }
228
229 func lookupInfoNFKC(b input, i int) Properties {
230 v, sz := b.charinfoNFKC(i)
231 return compInfo(v, sz)
232 }
233
234 // Properties returns properties for the first rune in s.
235 func (f Form) Properties(s []byte) Properties {
236 if f == NFC || f == NFD {
237 return compInfo(nfcData.lookup(s))
238 }
239 return compInfo(nfkcData.lookup(s))
240 }
241
242 // PropertiesString returns properties for the first rune in s.
243 func (f Form) PropertiesString(s string) Properties {
244 if f == NFC || f == NFD {
245 return compInfo(nfcData.lookupString(s))
246 }
247 return compInfo(nfkcData.lookupString(s))
248 }
249
250 // compInfo converts the information contained in v and sz
251 // to a Properties. See the comment at the top of the file
252 // for more information on the format.
253 func compInfo(v uint16, sz int) Properties {
254 if sz == 0 {
255 return Properties{flags: 0x80, size: 1}
256 }
257 if v == 0 {
258 return Properties{size: uint8(sz)}
259 } else if v >= 0x8000 {
260 p := Properties{
261 size: uint8(sz),
262 ccc: uint8(v),
263 tccc: uint8(v),
264 flags: qcInfo(v>>8) & 0x3f,
265 }
266 if p.ccc > 0 || p.combinesBackward() {
267 p.nLead = uint8(p.flags & 0x3)
268 }
269 return p
270 }
271 // has decomposition
272 h := decomps[v]
273 f := (qcInfo(h&headerFlagsMask) >> 2) | 0x4
274 p := Properties{size: uint8(sz), flags: f, index: v}
275 if v >= firstCCC {
276 n := uint16(h & headerLenMask)
277 if n == 31 {
278 n = 33
279 }
280 v += n + 1
281 c := decomps[v]
282 p.tccc = c >> 2
283 p.flags |= qcInfo(c & 0x3)
284 if v >= firstLeadingCCC {
285 p.nLead = c & 0x3
286 if v >= firstStarterWithNLead {
287 // We were tricked. Remove the decomposition.
288 p.flags &= 0x03
289 p.index = 0
290 return p
291 }
292 p.ccc = decomps[v+1]
293 }
294 }
295 return p
296 }