-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathjson_scanner.go
179 lines (157 loc) · 3.08 KB
/
json_scanner.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
package gson
import "unicode"
import "unicode/utf8"
import "unicode/utf16"
import "strconv"
var spaceCode = [256]byte{
'\t': 1,
'\n': 1,
'\v': 1,
'\f': 1,
'\r': 1,
' ': 1,
}
func skipWS(txt string, ws SpaceKind) string {
switch ws {
case UnicodeSpace:
for i, ch := range txt {
if unicode.IsSpace(ch) {
continue
}
return txt[i:]
}
return ""
case AnsiSpace:
i := 0
for i < len(txt) && spaceCode[txt[i]] == 1 {
i++
}
txt = txt[i:]
}
return txt
}
var escapeCode = [256]byte{
'"': '"',
'\\': '\\',
'/': '/',
'\'': '\'',
'b': '\b',
'f': '\f',
'n': '\n',
'r': '\r',
't': '\t',
}
func scanString(txt string, out []byte) (string, int) {
if len(txt) < 2 {
panic("scanner expectedString")
}
e := 1
for txt[e] != '"' {
c := txt[e]
if c == '\\' || c == '"' || c < ' ' {
break
}
if c < utf8.RuneSelf {
e++
continue
}
r, size := utf8.DecodeRuneInString(txt[e:])
if r == utf8.RuneError && size == 1 {
break
}
e += size
if e == len(txt) {
panic("scanner expectedString")
}
}
if txt[e] == '"' { // done we have nothing to unquote
return txt[e+1:], copy(out, txt[1:e])
}
oute := copy(out, txt[1:e]) // copy so far
loop:
for e < len(txt) {
switch c := txt[e]; {
case c == '"':
out[oute] = c
e++
break loop
case c == '\\':
if txt[e+1] == 'u' {
r := getu4(txt[e:])
if r < 0 { // invalid
panic("scanner expectedString")
}
e += 6
if utf16.IsSurrogate(r) {
nextr := getu4(txt[e:])
dec := utf16.DecodeRune(r, nextr)
if dec != unicode.ReplacementChar { // A valid pair consume
oute += utf8.EncodeRune(out[oute:], dec)
e += 6
break loop
}
// Invalid surrogate; fall back to replacement rune.
r = unicode.ReplacementChar
}
oute += utf8.EncodeRune(out[oute:], r)
} else { // escaped with " \ / ' b f n r t
out[oute] = escapeCode[txt[e+1]]
e += 2
oute++
}
case c < ' ': // control character is invalid
panic("scanner expectedString")
case c < utf8.RuneSelf: // ASCII
out[oute] = c
oute++
e++
default: // coerce to well-formed UTF-8
r, size := utf8.DecodeRuneInString(txt[e:])
e += size
oute += utf8.EncodeRune(out[oute:], r)
}
}
if out[oute] == '"' {
return txt[e:], oute
}
panic("scanner expectedString")
}
// getu4 decodes \uXXXX from the beginning of s, returning the hex value,
// or it returns -1.
func getu4(s string) rune {
if len(s) < 6 || s[0] != '\\' || s[1] != 'u' {
return -1
}
r, err := strconv.ParseUint(s[2:6], 16, 64)
if err != nil {
return -1
}
return rune(r)
}
var intCheck = [256]byte{}
var digitCheck = [256]byte{}
var numCheck = [256]byte{}
var fltCheck = [256]byte{}
func init() {
for i := 48; i <= 57; i++ {
intCheck[i] = 1
numCheck[i] = 1
}
intCheck['-'] = 1
intCheck['+'] = 1
intCheck['.'] = 1
intCheck['e'] = 1
intCheck['E'] = 1
numCheck['-'] = 1
numCheck['+'] = 1
numCheck['.'] = 1
fltCheck['.'] = 1
fltCheck['e'] = 1
fltCheck['E'] = 1
for i := 48; i <= 57; i++ {
digitCheck[i] = 1
}
digitCheck['-'] = 1
digitCheck['+'] = 1
digitCheck['.'] = 1
}