-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathiterator_skip_str.go
84 lines (80 loc) · 1.4 KB
/
iterator_skip_str.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
package jzon
func (it *Iterator) skipU4() error {
remain := 4
for {
i := it.head
for ; i < it.tail; i++ {
c := it.buffer[i]
u4v := hexValue[c]
if u4v == invalidHex {
return InvalidUnicodeCharError{c: c}
}
if remain == 1 {
it.head = i + 1
return nil
}
remain--
}
it.head = i
if err := it.readMore(); err != nil {
return err
}
}
}
func (it *Iterator) skipEscapedChar() error {
c, err := it.nextByte()
if err != nil {
return err
}
escaped := escapeMap[c]
if escaped != noEscape {
it.head++
return nil
}
if c != 'u' {
return InvalidEscapeCharError{c: c}
}
it.head++
return it.skipU4()
}
// internal, call only after a '"' is consumed
func skipString(it *Iterator, _ byte) error {
for {
i := it.head
for i < it.tail {
c := it.buffer[i]
if c == '"' {
it.head = i + 1
return nil
} else if c == '\\' {
it.head = i + 1
err := it.skipEscapedChar()
if err != nil {
return err
}
i = it.head
} else if c < ' ' { // json.org
return InvalidStringCharError{c: c}
} else {
i++
}
}
// i == it.tail
it.head = i
if err := it.readMore(); err != nil {
return err
}
}
}
// SkipString skips a string
func (it *Iterator) SkipString() error {
c, err := it.nextToken()
if err != nil {
return err
}
if c != '"' {
return UnexpectedByteError{got: c, exp: '"'}
}
it.head++
return skipString(it, c)
}