-
Notifications
You must be signed in to change notification settings - Fork 1
/
codec.go
351 lines (308 loc) · 8.91 KB
/
codec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package conust
import (
"strings"
)
// Codec can transform strings to and from the Conust format.
//
// It has EncodeToken and DecodeToken functions to transform simple numbers to and from the Conust format.
//
// There is also EncodeMixedText, a convenience function, that encodes each group of decimal numbers
// and returns the resulting string. So that for example the strings "Item 20" and "Item 100" become
// "Item 722" and "Item 731" which sort as the numeric value in them would naturally imply.
type Codec struct {
builder strings.Builder
}
// EncodeToken turns the input number into the alphanumerically sortable Conust string.
// If the input hase a base higher than 10 and contains letter characters, it must be lowercased.
// Note that if you want to incorporate the generated token into a string, and the token is not at
// the very end of it, then you will need to add a space character after the token to ensure correct
// sorting of the string.
// EncodeMixedText does that automatically
func (c *Codec) EncodeToken(input string) (out string, ok bool) {
if input == "" {
return "", true
}
if !c.isValidInput(input) {
return "", false
}
positive := c.getPositivity(input)
decimalPointPos := c.getDecimalPointPos(input)
sStartPos := c.getSignificantStartPos(input)
sEndPos := c.getSignificantEndPos(input)
if sStartPos == sEndPos {
return zeroOutput, true
}
magnitude, magnitudePositive := c.getMagnitudeParams(len(input), sStartPos, sEndPos, decimalPointPos)
c.builder.Reset()
c.builder.Grow(c.calculateEncodedSize(positive, magnitude, sStartPos, sEndPos, decimalPointPos))
c.builder.WriteByte(c.encodeSign(positive, magnitudePositive))
c.writeMagnitude(positive, magnitudePositive, magnitude)
if sStartPos < decimalPointPos && decimalPointPos < sEndPos {
c.writeDigits(positive, input[sStartPos:decimalPointPos])
c.writeDigits(positive, input[decimalPointPos+1:sEndPos])
} else {
c.writeDigits(positive, input[sStartPos:sEndPos])
}
if !positive {
c.builder.WriteByte(negativeNumberTerminator)
}
return c.builder.String(), true
}
// DecodeToken turns a Conust string back into its normal representation. The output will not reconstruct
// leading and trailing zeros. The plus sign for positive numbers is omitted as well.
func (c *Codec) DecodeToken(input string) (out string, ok bool) {
if input == "" {
return "", true
}
if input == zeroOutput {
return zeroInput, true
}
if len(input) < 3 {
return "", false
}
positive, magnitudePositive, ok := c.decodeSigns(input)
if !ok {
return "", false
}
magnitude, sStartPos, ok := c.decodeMagnitude(input, positive, magnitudePositive)
if !ok {
return "", false
}
encodedLength := len(input)
if !positive {
if input[encodedLength-1] != negativeNumberTerminator {
return "", false
}
encodedLength--
}
significantPartLength := encodedLength - sStartPos
for i := sStartPos; i < encodedLength; i++ {
if !isDigit(input[i]) {
return "", false
}
}
c.builder.Reset()
c.builder.Grow(c.calculateDecodedLength(positive, magnitudePositive, magnitude, significantPartLength))
if !positive {
c.builder.WriteByte(minusByte)
}
if !magnitudePositive {
c.builder.WriteByte(digit0)
c.builder.WriteByte(decimalPoint)
for i := 0; i < magnitude; i++ {
c.builder.WriteByte(digit0)
}
c.writeDigits(positive, input[sStartPos:encodedLength])
} else {
if magnitude >= significantPartLength {
c.writeDigits(positive, input[sStartPos:encodedLength])
for i := 0; i < magnitude-significantPartLength; i++ {
c.builder.WriteByte(digit0)
}
} else {
c.writeDigits(positive, input[sStartPos:sStartPos+magnitude])
c.builder.WriteByte(decimalPoint)
c.writeDigits(positive, input[sStartPos+magnitude:encodedLength])
}
}
return c.builder.String(), true
}
// EncodeMixedText is a convinience function that replaces all groups of decimal numbers of the input
// with Conust strings also surrounding them with spaces (if not already present) to ensure the expected ordering
func (c *Codec) EncodeMixedText(input string) (out string, ok bool) {
insideNumber := false
donePartEnd := 0
var b strings.Builder
ok = true
b.Grow(len(input) + 6)
for i := 0; i < len(input); i++ {
if input[i] >= digit0 && input[i] <= digit9 {
if !insideNumber {
b.Write([]byte(input[donePartEnd:i]))
donePartEnd = i
insideNumber = true
if i > 0 && input[i-1] != inTextSeparator {
b.WriteByte(inTextSeparator)
}
}
continue
}
if insideNumber {
encoded, encOk := c.EncodeToken(input[donePartEnd:i])
if encOk {
b.WriteString(encoded)
} else {
b.WriteString(input[donePartEnd:i])
ok = false
}
insideNumber = false
donePartEnd = i
if input[i] != inTextSeparator {
b.WriteByte(inTextSeparator)
}
}
}
if !insideNumber {
b.WriteString(input[donePartEnd:])
} else {
encoded, encOk := c.EncodeToken(input[donePartEnd:])
if encOk {
b.WriteString(encoded)
} else {
b.WriteString(input[donePartEnd:])
ok = false
}
}
out = b.String()
return
}
func (c *Codec) isValidInput(input string) bool {
if !isSignByte(input[0]) && !isDigit(input[0]) {
return false
}
decimalPointAlreadyFound := false
for i := 1; i < len(input); i++ {
if isDigit(input[i]) {
continue
}
if decimalPointAlreadyFound {
return false
}
if input[i] == decimalPoint {
decimalPointAlreadyFound = true
continue
}
return false
}
return true
}
func (c *Codec) getPositivity(input string) (positive bool) {
return input[0] != minusByte
}
func (c *Codec) getSignificantStartPos(input string) int {
i := 0
for ; i < len(input); i++ {
if isDigit(input[i]) && input[i] != digit0 {
return i
}
}
return -1
}
func (c *Codec) getSignificantEndPos(input string) int {
i := len(input) - 1
for ; i >= 0; i-- {
if isDigit(input[i]) && input[i] != digit0 {
return i + 1
}
}
return -1
}
func (c *Codec) getDecimalPointPos(input string) int {
return strings.IndexByte(input, decimalPoint)
}
func (c *Codec) getMagnitudeParams(inputLength int, sStartPos int, sEndPos int, decimalPointPos int) (magnitude int, magnitudePositive bool) {
if decimalPointPos < 0 {
magnitude = inputLength - sStartPos
magnitudePositive = true
} else if decimalPointPos < sStartPos {
magnitude = sStartPos - (decimalPointPos + 1)
magnitudePositive = false
} else {
magnitude = decimalPointPos - sStartPos
magnitudePositive = true
}
return
}
func (c *Codec) calculateEncodedSize(positive bool, magnitude int, sStartPos int, sEndPos int, decimalPointPos int) int {
length := 2 + (magnitude / maxMagnitudeDigitValue) + sEndPos - sStartPos
if !positive {
length++
}
if sStartPos < decimalPointPos && decimalPointPos < sEndPos {
length--
}
return length
}
func (c *Codec) encodeSign(positive bool, magnitudePositive bool) byte {
if positive {
if magnitudePositive {
return signPositiveMagPositive
}
return signPositiveMagNegative
}
if magnitudePositive {
return signNegativeMagPositive
}
return signNegativeMagNegative
}
func (c *Codec) writeMagnitude(positive bool, magnitudePositive bool, magnitude int) {
reverseDigits := positive != magnitudePositive
for ; magnitude > maxMagnitudeDigitValue; magnitude -= maxMagnitudeDigitValue {
if reverseDigits {
c.builder.WriteByte(intToReversedDigit(maxDigitValue))
} else {
c.builder.WriteByte(intToDigit(maxDigitValue))
}
}
if reverseDigits {
c.builder.WriteByte(intToReversedDigit(magnitude))
} else {
c.builder.WriteByte(intToDigit(magnitude))
}
}
func (c *Codec) writeDigits(positive bool, digits string) {
if positive {
c.builder.WriteString(digits)
} else {
for i := 0; i < len(digits); i++ {
c.builder.WriteByte(reverseDigit(digits[i]))
}
}
}
func (c *Codec) decodeSigns(in string) (positive bool, magnitudePositive bool, ok bool) {
switch in[0] {
case signPositiveMagPositive:
return true, true, true
case signPositiveMagNegative:
return true, false, true
case signNegativeMagNegative:
return false, false, true
case signNegativeMagPositive:
return false, true, true
default:
return false, false, false
}
}
func (c *Codec) decodeMagnitude(in string, positive bool, magnitudePositive bool) (magnitude int, significantPartPos int, ok bool) {
reverseDigits := positive != magnitudePositive
var digitValue int
for i := 1; i < len(in); i++ {
if reverseDigits {
digitValue = reversedDigitToInt(in[i])
} else {
digitValue = digitToInt(in[i])
}
if digitValue == maxDigitValue {
magnitude += maxMagnitudeDigitValue
} else {
magnitude += digitValue
significantPartPos = i + 1
ok = true
return
}
}
return 0, 0, false
}
func (c *Codec) calculateDecodedLength(positive bool, magnitudePositive bool, magnitude int, significantPartLength int) int {
var signLength int
if !positive {
signLength = 1
}
if magnitudePositive {
if magnitude >= significantPartLength {
return signLength + magnitude
}
return signLength + significantPartLength + 1
}
return signLength + 2 + magnitude + significantPartLength
}