1 |
747 |
jeremybenn |
// 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 |
|
|
// This file contains Form-specific logic and wrappers for data in tables.go.
|
8 |
|
|
|
9 |
|
|
type runeInfo struct {
|
10 |
|
|
pos uint8 // start position in reorderBuffer; used in composition.go
|
11 |
|
|
size uint8 // length of UTF-8 encoding of this rune
|
12 |
|
|
ccc uint8 // canonical combining class
|
13 |
|
|
flags qcInfo // quick check flags
|
14 |
|
|
}
|
15 |
|
|
|
16 |
|
|
// functions dispatchable per form
|
17 |
|
|
type lookupFunc func(b input, i int) runeInfo
|
18 |
|
|
type decompFunc func(b input, i int) []byte
|
19 |
|
|
|
20 |
|
|
// formInfo holds Form-specific functions and tables.
|
21 |
|
|
type formInfo struct {
|
22 |
|
|
form Form
|
23 |
|
|
|
24 |
|
|
composing, compatibility bool // form type
|
25 |
|
|
|
26 |
|
|
decompose decompFunc
|
27 |
|
|
info lookupFunc
|
28 |
|
|
}
|
29 |
|
|
|
30 |
|
|
var formTable []*formInfo
|
31 |
|
|
|
32 |
|
|
func init() {
|
33 |
|
|
formTable = make([]*formInfo, 4)
|
34 |
|
|
|
35 |
|
|
for i := range formTable {
|
36 |
|
|
f := &formInfo{}
|
37 |
|
|
formTable[i] = f
|
38 |
|
|
f.form = Form(i)
|
39 |
|
|
if Form(i) == NFKD || Form(i) == NFKC {
|
40 |
|
|
f.compatibility = true
|
41 |
|
|
f.decompose = decomposeNFKC
|
42 |
|
|
f.info = lookupInfoNFKC
|
43 |
|
|
} else {
|
44 |
|
|
f.decompose = decomposeNFC
|
45 |
|
|
f.info = lookupInfoNFC
|
46 |
|
|
}
|
47 |
|
|
if Form(i) == NFC || Form(i) == NFKC {
|
48 |
|
|
f.composing = true
|
49 |
|
|
}
|
50 |
|
|
}
|
51 |
|
|
}
|
52 |
|
|
|
53 |
|
|
// We do not distinguish between boundaries for NFC, NFD, etc. to avoid
|
54 |
|
|
// unexpected behavior for the user. For example, in NFD, there is a boundary
|
55 |
|
|
// after 'a'. However, a might combine with modifiers, so from the application's
|
56 |
|
|
// perspective it is not a good boundary. We will therefore always use the
|
57 |
|
|
// boundaries for the combining variants.
|
58 |
|
|
func (i runeInfo) boundaryBefore() bool {
|
59 |
|
|
if i.ccc == 0 && !i.combinesBackward() {
|
60 |
|
|
return true
|
61 |
|
|
}
|
62 |
|
|
// We assume that the CCC of the first character in a decomposition
|
63 |
|
|
// is always non-zero if different from info.ccc and that we can return
|
64 |
|
|
// false at this point. This is verified by maketables.
|
65 |
|
|
return false
|
66 |
|
|
}
|
67 |
|
|
|
68 |
|
|
func (i runeInfo) boundaryAfter() bool {
|
69 |
|
|
return i.isInert()
|
70 |
|
|
}
|
71 |
|
|
|
72 |
|
|
// We pack quick check data in 4 bits:
|
73 |
|
|
// 0: NFD_QC Yes (0) or No (1). No also means there is a decomposition.
|
74 |
|
|
// 1..2: NFC_QC Yes(00), No (10), or Maybe (11)
|
75 |
|
|
// 3: Combines forward (0 == false, 1 == true)
|
76 |
|
|
//
|
77 |
|
|
// When all 4 bits are zero, the character is inert, meaning it is never
|
78 |
|
|
// influenced by normalization.
|
79 |
|
|
//
|
80 |
|
|
// We pack the bits for both NFC/D and NFKC/D in one byte.
|
81 |
|
|
type qcInfo uint8
|
82 |
|
|
|
83 |
|
|
func (i runeInfo) isYesC() bool { return i.flags&0x4 == 0 }
|
84 |
|
|
func (i runeInfo) isYesD() bool { return i.flags&0x1 == 0 }
|
85 |
|
|
|
86 |
|
|
func (i runeInfo) combinesForward() bool { return i.flags&0x8 != 0 }
|
87 |
|
|
func (i runeInfo) combinesBackward() bool { return i.flags&0x2 != 0 } // == isMaybe
|
88 |
|
|
func (i runeInfo) hasDecomposition() bool { return i.flags&0x1 != 0 } // == isNoD
|
89 |
|
|
|
90 |
|
|
func (r runeInfo) isInert() bool {
|
91 |
|
|
return r.flags&0xf == 0 && r.ccc == 0
|
92 |
|
|
}
|
93 |
|
|
|
94 |
|
|
// Wrappers for tables.go
|
95 |
|
|
|
96 |
|
|
// The 16-bit value of the decomposition tries is an index into a byte
|
97 |
|
|
// array of UTF-8 decomposition sequences. The first byte is the number
|
98 |
|
|
// of bytes in the decomposition (excluding this length byte). The actual
|
99 |
|
|
// sequence starts at the offset+1.
|
100 |
|
|
func decomposeNFC(s input, i int) []byte {
|
101 |
|
|
p := s.decomposeNFC(i)
|
102 |
|
|
n := decomps[p]
|
103 |
|
|
p++
|
104 |
|
|
return decomps[p : p+uint16(n)]
|
105 |
|
|
}
|
106 |
|
|
|
107 |
|
|
func decomposeNFKC(s input, i int) []byte {
|
108 |
|
|
p := s.decomposeNFKC(i)
|
109 |
|
|
n := decomps[p]
|
110 |
|
|
p++
|
111 |
|
|
return decomps[p : p+uint16(n)]
|
112 |
|
|
}
|
113 |
|
|
|
114 |
|
|
// Recomposition
|
115 |
|
|
// We use 32-bit keys instead of 64-bit for the two codepoint keys.
|
116 |
|
|
// This clips off the bits of three entries, but we know this will not
|
117 |
|
|
// result in a collision. In the unlikely event that changes to
|
118 |
|
|
// UnicodeData.txt introduce collisions, the compiler will catch it.
|
119 |
|
|
// Note that the recomposition map for NFC and NFKC are identical.
|
120 |
|
|
|
121 |
|
|
// combine returns the combined rune or 0 if it doesn't exist.
|
122 |
|
|
func combine(a, b rune) rune {
|
123 |
|
|
key := uint32(uint16(a))<<16 + uint32(uint16(b))
|
124 |
|
|
return recompMap[key]
|
125 |
|
|
}
|
126 |
|
|
|
127 |
|
|
// The 16-bit character info has the following bit layout:
|
128 |
|
|
// 0..7 CCC value.
|
129 |
|
|
// 8..11 qcInfo for NFC/NFD
|
130 |
|
|
// 12..15 qcInfo for NFKC/NFKD
|
131 |
|
|
func lookupInfoNFC(b input, i int) runeInfo {
|
132 |
|
|
v, sz := b.charinfo(i)
|
133 |
|
|
return runeInfo{size: uint8(sz), ccc: uint8(v), flags: qcInfo(v >> 8)}
|
134 |
|
|
}
|
135 |
|
|
|
136 |
|
|
func lookupInfoNFKC(b input, i int) runeInfo {
|
137 |
|
|
v, sz := b.charinfo(i)
|
138 |
|
|
return runeInfo{size: uint8(sz), ccc: uint8(v), flags: qcInfo(v >> 12)}
|
139 |
|
|
}
|