1 // Copyright 2012 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.
10 "golang.org/x/text/unicode/norm"
13 // Table holds all collation data for a given collation ordering.
15 Index Trie // main trie
21 ContractTries ContractTrieSet
27 func (t *Table) AppendNext(w []Elem, b []byte) (res []Elem, n int) {
28 return t.appendNext(w, source{bytes: b})
31 func (t *Table) AppendNextString(w []Elem, s string) (res []Elem, n int) {
32 return t.appendNext(w, source{str: s})
35 func (t *Table) Start(p int, b []byte) int {
37 panic("not implemented")
40 func (t *Table) StartString(p int, s string) int {
42 panic("not implemented")
45 func (t *Table) Domain() []string {
47 panic("not implemented")
50 func (t *Table) Top() uint32 {
59 func (src *source) lookup(t *Table) (ce Elem, sz int) {
61 return t.Index.lookupString(src.str)
63 return t.Index.lookup(src.bytes)
66 func (src *source) tail(sz int) {
68 src.str = src.str[sz:]
70 src.bytes = src.bytes[sz:]
74 func (src *source) nfd(buf []byte, end int) []byte {
76 return norm.NFD.AppendString(buf[:0], src.str[:end])
78 return norm.NFD.Append(buf[:0], src.bytes[:end]...)
81 func (src *source) rune() (r rune, sz int) {
83 return utf8.DecodeRuneInString(src.str)
85 return utf8.DecodeRune(src.bytes)
88 func (src *source) properties(f norm.Form) norm.Properties {
90 return f.PropertiesString(src.str)
92 return f.Properties(src.bytes)
95 // appendNext appends the weights corresponding to the next rune or
96 // contraction in s. If a contraction is matched to a discontinuous
97 // sequence of runes, the weights for the interstitial runes are
98 // appended as well. It returns a new slice that includes the appended
99 // weights and the number of bytes consumed from s.
100 func (t *Table) appendNext(w []Elem, src source) (res []Elem, n int) {
101 ce, sz := src.lookup(t)
111 if r >= firstHangul && r <= lastHangul {
112 // TODO: performance can be considerably improved here.
114 var buf [16]byte // Used for decomposing Hangul.
115 for b := src.nfd(buf[:0], hangulSize); len(b) > 0; b = b[sz:] {
116 ce, sz = t.Index.lookup(b)
121 ce = makeImplicitCE(implicitPrimary(r))
124 } else if tp == ceExpansionIndex {
125 w = t.appendExpansion(w, ce)
126 } else if tp == ceContractionIndex {
129 if src.bytes == nil {
130 w, n = t.matchContractionString(w, ce, src.str)
132 w, n = t.matchContraction(w, ce, src.bytes)
135 } else if tp == ceDecompose {
136 // Decompose using NFKD and replace tertiary weights.
137 t1, t2 := splitDecompose(ce)
139 nfkd := src.properties(norm.NFKD).Decomposition()
140 for p := 0; len(nfkd) > 0; nfkd = nfkd[p:] {
141 w, p = t.appendNext(w, source{bytes: nfkd})
143 w[i] = w[i].updateTertiary(t1)
145 w[i] = w[i].updateTertiary(t2)
146 for i++; i < len(w); i++ {
147 w[i] = w[i].updateTertiary(maxTertiary)
154 func (t *Table) appendExpansion(w []Elem, ce Elem) []Elem {
155 i := splitExpandIndex(ce)
156 n := int(t.ExpandElem[i])
158 for _, ce := range t.ExpandElem[i : i+n] {
159 w = append(w, Elem(ce))
164 func (t *Table) matchContraction(w []Elem, ce Elem, suffix []byte) ([]Elem, int) {
165 index, n, offset := splitContractIndex(ce)
167 scan := t.ContractTries.scanner(index, n, suffix)
168 buf := [norm.MaxSegmentSize]byte{}
172 if !scan.done && p < len(suffix) && suffix[p] >= utf8.RuneSelf {
173 // By now we should have filtered most cases.
176 rune := norm.NFD.Properties(suffix[p:])
178 if rune.LeadCCC() != 0 {
179 prevCC := rune.TrailCCC()
180 // A gap may only occur in the last normalization segment.
181 // This also ensures that len(scan.s) < norm.MaxSegmentSize.
182 if end := norm.NFD.FirstBoundary(suffix[p:]); end != -1 {
183 scan.s = suffix[:p+end]
185 for p < len(suffix) && !scan.done && suffix[p] >= utf8.RuneSelf {
186 rune = norm.NFD.Properties(suffix[p:])
187 if ccc := rune.LeadCCC(); ccc == 0 || prevCC >= ccc {
190 prevCC = rune.TrailCCC()
191 if pp := scan.scan(p); pp != p {
192 // Copy the interstitial runes for later processing.
193 bufn += copy(buf[bufn:], suffix[p0:p])
194 if scan.pindex == pp {
204 // Append weights for the matched contraction, which may be an expansion.
205 i, n := scan.result()
206 ce = Elem(t.ContractElem[i+offset])
207 if ce.ctype() == ceNormal {
210 w = t.appendExpansion(w, ce)
212 // Append weights for the runes in the segment not part of the contraction.
213 for b, p := buf[:bufp], 0; len(b) > 0; b = b[p:] {
214 w, p = t.appendNext(w, source{bytes: b})
219 // TODO: unify the two implementations. This is best done after first simplifying
220 // the algorithm taking into account the inclusion of both NFC and NFD forms
222 func (t *Table) matchContractionString(w []Elem, ce Elem, suffix string) ([]Elem, int) {
223 index, n, offset := splitContractIndex(ce)
225 scan := t.ContractTries.scannerString(index, n, suffix)
226 buf := [norm.MaxSegmentSize]byte{}
230 if !scan.done && p < len(suffix) && suffix[p] >= utf8.RuneSelf {
231 // By now we should have filtered most cases.
234 rune := norm.NFD.PropertiesString(suffix[p:])
236 if rune.LeadCCC() != 0 {
237 prevCC := rune.TrailCCC()
238 // A gap may only occur in the last normalization segment.
239 // This also ensures that len(scan.s) < norm.MaxSegmentSize.
240 if end := norm.NFD.FirstBoundaryInString(suffix[p:]); end != -1 {
241 scan.s = suffix[:p+end]
243 for p < len(suffix) && !scan.done && suffix[p] >= utf8.RuneSelf {
244 rune = norm.NFD.PropertiesString(suffix[p:])
245 if ccc := rune.LeadCCC(); ccc == 0 || prevCC >= ccc {
248 prevCC = rune.TrailCCC()
249 if pp := scan.scan(p); pp != p {
250 // Copy the interstitial runes for later processing.
251 bufn += copy(buf[bufn:], suffix[p0:p])
252 if scan.pindex == pp {
262 // Append weights for the matched contraction, which may be an expansion.
263 i, n := scan.result()
264 ce = Elem(t.ContractElem[i+offset])
265 if ce.ctype() == ceNormal {
268 w = t.appendExpansion(w, ce)
270 // Append weights for the runes in the segment not part of the contraction.
271 for b, p := buf[:bufp], 0; len(b) > 0; b = b[p:] {
272 w, p = t.appendNext(w, source{bytes: b})