1 // Copyright 2014 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.
12 // NewNumericWeighter wraps w to replace individual digits to sort based on their
15 // Weighter w must have a free primary weight after the primary weight for 9.
16 // If this is not the case, numeric value will sort at the same primary level
17 // as the first primary sorting after 9.
18 func NewNumericWeighter(w Weighter) Weighter {
19 getElem := func(s string) Elem {
20 elems, _ := w.AppendNextString(nil, s)
25 // Numbers should order before zero, but the DUCET has no room for this.
26 // TODO: move before zero once we use fractional collation elements.
27 ns, _ := MakeElem(nine.Primary()+1, nine.Secondary(), int(nine.Tertiary()), 0)
29 return &numericWeighter{
32 // We assume that w sorts digits of different kinds in order of numeric
33 // value and that the tertiary weight order is preserved.
35 // TODO: evaluate whether it is worth basing the ranges on the Elem
36 // encoding itself once the move to fractional weights is complete.
38 zeroSpecialLo: getElem("0"), // U+FF10 FULLWIDTH DIGIT ZERO
39 zeroSpecialHi: getElem("₀"), // U+2080 SUBSCRIPT ZERO
41 nineSpecialHi: getElem("₉"), // U+2089 SUBSCRIPT NINE
46 // A numericWeighter translates a stream of digits into a stream of weights
47 // representing the numeric value.
48 type numericWeighter struct {
51 // The Elems below all demarcate boundaries of specific ranges. With the
52 // current element encoding digits are in two ranges: normal (default
53 // tertiary value) and special. For most languages, digits have collation
54 // elements in the normal range.
56 // Note: the range tests are very specific for the element encoding used by
57 // this implementation. The tests in collate_test.go are designed to fail
58 // if this code is not updated when an encoding has changed.
60 zero Elem // normal digit zero
61 zeroSpecialLo Elem // special digit zero, low tertiary value
62 zeroSpecialHi Elem // special digit zero, high tertiary value
63 nine Elem // normal digit nine
64 nineSpecialHi Elem // special digit nine
68 // AppendNext calls the namesake of the underlying weigher, but replaces single
69 // digits with weights representing their value.
70 func (nw *numericWeighter) AppendNext(buf []Elem, s []byte) (ce []Elem, n int) {
71 ce, n = nw.Weighter.AppendNext(buf, s)
72 nc := numberConverter{
77 isZero, ok := nc.checkNextDigit(ce)
81 // ce might have been grown already, so take it instead of buf.
82 nc.init(ce, len(buf), isZero)
84 ce, sz := nw.Weighter.AppendNext(nc.elems, s[n:])
94 // AppendNextString calls the namesake of the underlying weigher, but replaces
95 // single digits with weights representing their value.
96 func (nw *numericWeighter) AppendNextString(buf []Elem, s string) (ce []Elem, n int) {
97 ce, n = nw.Weighter.AppendNextString(buf, s)
98 nc := numberConverter{
103 isZero, ok := nc.checkNextDigit(ce)
107 nc.init(ce, len(buf), isZero)
109 ce, sz := nw.Weighter.AppendNextString(nc.elems, s[n:])
116 return nc.result(), n
119 type numberConverter struct {
126 s string // set if the input was of type string
127 b []byte // set if the input was of type []byte
130 // init completes initialization of a numberConverter and prepares it for adding
131 // more digits. elems is assumed to have a digit starting at oldLen.
132 func (nc *numberConverter) init(elems []Elem, oldLen int, isZero bool) {
133 // Insert a marker indicating the start of a number and and a placeholder
134 // for the number of digits.
136 elems = append(elems[:oldLen], nc.w.numberStart, 0)
138 elems = append(elems, 0, 0)
139 copy(elems[oldLen+2:], elems[oldLen:])
140 elems[oldLen] = nc.w.numberStart
146 nc.lenIndex = oldLen + 1
149 // checkNextDigit reports whether bufNew adds a single digit relative to the old
150 // buffer. If it does, it also reports whether this digit is zero.
151 func (nc *numberConverter) checkNextDigit(bufNew []Elem) (isZero, ok bool) {
152 if len(nc.elems) >= len(bufNew) {
155 e := bufNew[len(nc.elems)]
156 if e < nc.w.zeroSpecialLo || nc.w.nine < e {
161 if e > nc.w.nineSpecialHi {
168 isZero = e <= nc.w.zeroSpecialHi
170 // This is the common case if we encounter a digit.
171 isZero = e == nc.w.zero
173 // Test the remaining added collation elements have a zero primary value.
174 if n := len(bufNew) - len(nc.elems); n > 1 {
175 for i := len(nc.elems) + 1; i < len(bufNew); i++ {
176 if bufNew[i].Primary() != 0 {
180 // In some rare cases, collation elements will encode runes in
181 // unicode.No as a digit. For example Ethiopic digits (U+1369 - U+1371)
182 // are not in Nd. Also some digits that clearly belong in unicode.No,
183 // like U+0C78 TELUGU FRACTION DIGIT ZERO FOR ODD POWERS OF FOUR, have
184 // collation elements indistinguishable from normal digits.
185 // Unfortunately, this means we need to make this check for nearly all
188 // TODO: check the performance impact and find something better if it is
197 func (nc *numberConverter) isDigit() bool {
199 r, _ := utf8.DecodeRune(nc.b)
200 return unicode.In(r, unicode.Nd)
202 r, _ := utf8.DecodeRuneInString(nc.s)
203 return unicode.In(r, unicode.Nd)
206 // We currently support a maximum of about 2M digits (the number of primary
207 // values). Such numbers will compare correctly against small numbers, but their
208 // comparison against other large numbers is undefined.
210 // TODO: define a proper fallback, such as comparing large numbers textually or
211 // actually allowing numbers of unlimited length.
213 // TODO: cap this to a lower number (like 100) and maybe allow a larger number
215 const maxDigits = 1<<maxPrimaryBits - 1
217 func (nc *numberConverter) update(elems []Elem) bool {
218 isZero, ok := nc.checkNextDigit(elems)
219 if nc.nDigits == 0 && isZero {
227 return nc.nDigits < maxDigits
230 // result fills in the length element for the digit sequence and returns the
231 // completed collation elements.
232 func (nc *numberConverter) result() []Elem {
233 e, _ := MakeElem(nc.nDigits, defaultSecondary, defaultTertiary, 0)
234 nc.elems[nc.lenIndex] = e