generated from WillAbides/goproject-tmpl
-
Notifications
You must be signed in to change notification settings - Fork 6
/
machine_helpers.go
164 lines (147 loc) · 3.16 KB
/
machine_helpers.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
package rjson
import (
"fmt"
"unicode"
"unicode/utf16"
"unicode/utf8"
)
func errUnexpectedByteInString(b byte) error {
return fmt.Errorf("unexpected byte found in string: %q", string(b))
}
const skipMaxDepth = 10_000
var (
errMaxDepth = fmt.Errorf("exceeded max depth")
errUnexpectedEOF = fmt.Errorf("unexpected end of json")
errInvalidString = fmt.Errorf("invalid json string")
errInvalidArray = fmt.Errorf("invalid json array")
errInvalidObject = fmt.Errorf("invalid json object")
errInvalidUInt = fmt.Errorf("invalid json uint")
errInvalidInt = fmt.Errorf("invalid json int")
errInvalidNumber = fmt.Errorf("invalid json number")
errNoValidToken = fmt.Errorf("no valid json token found")
errNotNull = fmt.Errorf("not null")
errNotBool = fmt.Errorf("not a boolean value")
errPOutOfRange = fmt.Errorf("p out of range")
)
func growBytesSliceCapacity(slice []byte, size int) []byte {
if cap(slice) >= size {
return slice
}
origLen := len(slice)
delta := size - cap(slice)
slice = append(slice[:cap(slice)], make([]byte, delta)...)
slice = slice[:origLen]
return slice
}
func unescapeUnicodeChar(s, data []byte) (result []byte, bytesHandled int, ok bool) {
rr := getu4(s)
if rr < 0 {
return data, 0, false
}
origLen := len(data)
data = growBytesSliceCapacity(data, origLen+4)[:origLen+4]
var w int
if utf16.IsSurrogate(rr) {
rr1 := getu4(s[6:])
if dec := utf16.DecodeRune(rr, rr1); dec != unicode.ReplacementChar {
rl := utf8.RuneLen(dec)
utf8.EncodeRune(data[origLen:], dec)
return data[:origLen+rl], 12, true
}
rr = unicode.ReplacementChar
}
w = utf8.EncodeRune(data[origLen:], rr)
return data[:origLen+w], 6, true
}
// copied from encoding/json
//
// getu4 decodes \uXXXX from the beginning of s, returning the hex value,
// or it returns -1.
func getu4(data []byte) rune {
if len(data) < 6 || data[0] != '\\' || data[1] != 'u' {
return -1
}
var r rune
for _, c := range data[2:6] {
switch {
case c >= '0' && c <= '9':
c -= '0'
case c >= 'a' && c <= 'f':
c = c - 'a' + 10
case c >= 'A' && c <= 'F':
c = c - 'A' + 10
default:
return -1
}
r = r*16 + rune(c)
}
return r
}
func skipFloatExp(data []byte, p, pe int) (int, error) {
if p == pe {
return p - 1, errInvalidNumber
}
startP := p
signed := false
for ; p < pe; p++ {
if digits[data[p]] {
continue
}
if p == startP && signBytes[data[p]] {
signed = true
continue
}
break
}
var err error
switch p - startP {
case 0:
err = errInvalidNumber
case 1:
if signed {
err = errInvalidNumber
}
}
return p - 1, err
}
func skipFloatDec(data []byte, p, pe int) (int, error) {
if p == pe {
return p - 1, errInvalidNumber
}
if !digits[data[p]] {
return p - 1, errInvalidNumber
}
p++
for ; p < pe; p++ {
if !digits[data[p]] {
break
}
}
if p == pe {
return p - 1, nil
}
if expBytes[data[p]] {
return skipFloatExp(data, p+1, pe)
}
return p - 1, nil
}
var signBytes = [256]bool{
'-': true,
'+': true,
}
var expBytes = [256]bool{
'e': true,
'E': true,
}
var digits = [256]bool{
'0': true,
'1': true,
'2': true,
'3': true,
'4': true,
'5': true,
'6': true,
'7': true,
'8': true,
'9': true,
}