@@ -102,90 +102,28 @@ func (b *Buffer) Reset() {
102
102
103
103
// Compare returns an integer comparing the two byte slices.
104
104
// The result will be 0 if a==b, -1 if a < b, and +1 if a > b.
105
+ // Note that this is less performant than calling c.Sort() because
106
+ // a new buffer will be allocated for each call.
105
107
func (c * Collator ) Compare (a , b []byte ) int {
106
- // TODO: skip identical prefixes once we have a fast way to detect if a rune is
107
- // part of a contraction. This would lead to roughly a 10% speedup for the colcmp regtest.
108
- c .iter (0 ).SetInput (a )
109
- c .iter (1 ).SetInput (b )
110
- if res := c .compare (); res != 0 {
111
- return res
112
- }
113
- if ! c .ignore [colltab .Identity ] {
114
- return bytes .Compare (a , b )
115
- }
116
- return 0
108
+ var (
109
+ buf Buffer
110
+ kA = c .Key (& buf , a )
111
+ kB = c .Key (& buf , b )
112
+ )
113
+ return bytes .Compare (kA , kB )
117
114
}
118
115
119
116
// CompareString returns an integer comparing the two strings.
120
117
// The result will be 0 if a==b, -1 if a < b, and +1 if a > b.
118
+ // Note that this is less performant than calling c.Sort() because
119
+ // a new buffer will be allocated for each call.
121
120
func (c * Collator ) CompareString (a , b string ) int {
122
- // TODO: skip identical prefixes once we have a fast way to detect if a rune is
123
- // part of a contraction. This would lead to roughly a 10% speedup for the colcmp regtest.
124
- c .iter (0 ).SetInputString (a )
125
- c .iter (1 ).SetInputString (b )
126
- if res := c .compare (); res != 0 {
127
- return res
128
- }
129
- if ! c .ignore [colltab .Identity ] {
130
- if a < b {
131
- return - 1
132
- } else if a > b {
133
- return 1
134
- }
135
- }
136
- return 0
137
- }
138
-
139
- func compareLevel (f func (i * iter ) int , a , b * iter ) int {
140
- a .pce = 0
141
- b .pce = 0
142
- for {
143
- va := f (a )
144
- vb := f (b )
145
- if va != vb {
146
- if va < vb {
147
- return - 1
148
- }
149
- return 1
150
- } else if va == 0 {
151
- break
152
- }
153
- }
154
- return 0
155
- }
156
-
157
- func (c * Collator ) compare () int {
158
- ia , ib := c .iter (0 ), c .iter (1 )
159
- // Process primary level
160
- if c .alternate != altShifted {
161
- // TODO: implement script reordering
162
- if res := compareLevel ((* iter ).nextPrimary , ia , ib ); res != 0 {
163
- return res
164
- }
165
- } else {
166
- // TODO: handle shifted
167
- }
168
- if ! c .ignore [colltab .Secondary ] {
169
- f := (* iter ).nextSecondary
170
- if c .backwards {
171
- f = (* iter ).prevSecondary
172
- }
173
- if res := compareLevel (f , ia , ib ); res != 0 {
174
- return res
175
- }
176
- }
177
- // TODO: special case handling (Danish?)
178
- if ! c .ignore [colltab .Tertiary ] || c .caseLevel {
179
- if res := compareLevel ((* iter ).nextTertiary , ia , ib ); res != 0 {
180
- return res
181
- }
182
- if ! c .ignore [colltab .Quaternary ] {
183
- if res := compareLevel ((* iter ).nextQuaternary , ia , ib ); res != 0 {
184
- return res
185
- }
186
- }
187
- }
188
- return 0
121
+ var (
122
+ buf Buffer
123
+ kA = c .KeyFromString (& buf , a )
124
+ kB = c .KeyFromString (& buf , b )
125
+ )
126
+ return bytes .Compare (kA , kB )
189
127
}
190
128
191
129
// Key returns the collation key for str.
0 commit comments