-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
126 lines (98 loc) · 1.96 KB
/
util.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
package jsonextract
import (
"bytes"
"io"
"strconv"
"unicode"
"unicode/utf8"
)
// Convert []rune to []byte
func runesToUTF8(rs []rune) []byte {
size := 0
for _, r := range rs {
size += utf8.RuneLen(r)
}
bs := make([]byte, size)
count := 0
for _, r := range rs {
count += utf8.EncodeRune(bs[count:], r)
}
return bs
}
var validEsc = []rune{'"', '\\', '/', 'b', 'f', 'n', 'r', 't', 'r', 'u'}
// Check if char is valid escape
func isCharValidEscape(char rune) bool {
for _, c := range validEsc {
if char == c {
return true
}
}
return false
}
var hex = []rune{'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'b', 'c', 'd', 'e', 'f', 'A', 'B', 'C', 'D', 'E', 'F'}
// Check if a rune is a hexa char
func isCharHex(char rune) bool {
for _, c := range hex {
if char == c {
return true
}
}
return false
}
// quote rune to string converted to []rune
func quoteRune(char rune) []rune {
rns := []rune(strconv.QuoteRune(char))
return rns[1 : len(rns)-1]
}
var endNum = []rune{'}', ']', ',', ' '}
// check if char numeric ending
func isCharEndNum(char rune) bool {
for _, c := range endNum {
if char == c {
return true
}
}
if unicode.IsControl(char) || char == ' ' {
return true
}
return false
}
// check if exponent valid
func isExpValid(r reader) (rune, error) {
char, _, err := r.ReadRune()
if err != nil {
if err == io.EOF {
return 0, errInvalid
}
return 0, err
}
if !unicode.IsNumber(char) && char != '+' && char != '-' {
return 0, errInvalid
}
return char, nil
}
func isCharMinOrPlus(char rune) bool {
if char == '-' || char == '+' {
return true
}
return false
}
func getParent(json *JSON) *JSON {
for json.parent != nil {
json = json.parent
}
return json
}
// read runes from bytes.Reader
func readAllRunes(r *bytes.Reader) []rune {
size := r.Size()
rns := make([]rune, size)
for i := int64(0); i < size; i++ {
r, _, err := r.ReadRune()
if err != nil {
break
}
rns[i] = r
}
return rns
}