-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcollate_string.go
More file actions
54 lines (51 loc) · 909 Bytes
/
collate_string.go
File metadata and controls
54 lines (51 loc) · 909 Bytes
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
package gson
func suffixEncodeString(s []byte, code []byte) int {
n := 0
for _, x := range s {
code[n] = x
n++
if x == Terminator {
code[n] = 1
n++
}
}
code[n] = Terminator
return n + 1
}
func suffixDecodeString(code []byte, text []byte) (int, int) {
for i, j := 0, 0; i < len(code); i++ {
x := code[i]
if x == Terminator {
i++
switch x = code[i]; x {
case 1:
text[j] = 0
j++
case Terminator:
return i + 1, j
default:
panic("collate decode invalid escape sequence")
}
continue
}
text[j] = x
j++
}
panic("collate decode invalid string")
}
func encodedStringSize(code []byte) int {
for i := 0; i < len(code); {
if code[i] == Terminator {
switch code[i+1] {
case 1:
i++
case Terminator:
return i
default:
panic("collate decode invalid escape sequence")
}
}
i++
}
panic("collate decode invalid string")
}