-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
185 lines (154 loc) · 3.7 KB
/
utils.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
180
181
182
183
184
185
package timex
func isDigit(c byte) bool { return c >= '0' && c <= '9' }
func fromDigit(c byte) int { return int(c - '0') }
func toDigit(n int) byte { return byte(n) + '0' }
// match reports whether s1 and s2 match ignoring case.
// It is assumed s1 and s2 are the same length.
func match(s1, s2 string) bool {
for i := 0; i < len(s1); i++ {
c1 := s1[i]
c2 := s2[i]
if c1 != c2 {
// Switch to lower-case.
c1 |= 'a' - 'A'
c2 |= 'a' - 'A'
if c1 != c2 || c1 < 'a' || c1 > 'z' {
return false
}
}
}
return true
}
// searchName reports whether the prefix of value exist in names.
// It returns the index and left string.
func searchName(names []string, value string) (int, string, bool) {
for i, name := range names {
if len(value) >= len(name) && match(value[:len(name)], name) {
return i, value[len(name):], true
}
}
return -1, value, false
}
// atoi converts a string to integer with minimum and maximum digit length.
func atoi(s string, min, max int) (int, string, bool) {
// Optimization for the most common scenario.
if min == 2 && max == 2 && len(s) >= 2 {
if !isDigit(s[0]) || !isDigit(s[1]) {
goto SLOW
}
n := fromDigit(s[0])*1e1 + fromDigit(s[1])
return n, s[2:], true
}
if min == 4 && max == 4 && len(s) >= 4 {
if !isDigit(s[0]) || !isDigit(s[1]) || !isDigit(s[2]) || !isDigit(s[3]) {
goto SLOW
}
n := fromDigit(s[0])*1e3 + fromDigit(s[1])*1e2 + fromDigit(s[2])*1e1 + fromDigit(s[3])
return n, s[4:], true
}
SLOW:
var negative bool
if len(s) > 0 && (s[0] == '-' || s[0] == '+') {
negative = s[0] == '-'
s = s[1:]
}
var n, index int
for ; index < len(s) && index < max; index++ {
c := s[index]
if !isDigit(c) {
break
}
n = n*10 + fromDigit(c)
}
if index < min {
return 0, "", false
}
if negative {
n = -n
}
return n, s[index:], true
}
// appendInt appends the decimal form of integer with specified minimum digit length.
// If specified digit width is zero, the original form of integer will be followed.
func appendInt(b []byte, n int, min int) []byte {
if n < 0 {
b = append(b, '-')
n = -n
}
// Optimization for the most common scenario.
switch {
case min == 2 && n < 1e2:
return append(b, toDigit(n/1e1), toDigit(n%1e1))
case min == 4 && n < 1e4:
return append(b, toDigit(n/1e3), toDigit(n/1e2%1e1), toDigit(n/1e1%1e1), toDigit(n%1e1))
}
var width int
if n == 0 {
width = 1
}
for i := n; i > 0; i /= 10 {
width++
}
if min > width {
width = min
}
if len(b)+width <= cap(b) {
b = b[:len(b)+width]
} else {
b = append(b, make([]byte, width)...)
}
for i := 0; i < width; i++ {
index := len(b) - 1 - i
next := n / 10
b[index] = toDigit(n - next*10)
n = next
}
return b
}
// atof converts a string to integer and its fraction part with specific digit length.
func atof(s string, min, max, fraction int) (int, int, string, bool) {
m, s, ok := atoi(s, min, max)
if !ok || len(s) == 0 || s[0] != '.' {
return m, 0, s, ok
}
s = s[1:]
var n, index int
for ; index < len(s) && index < fraction; index++ {
c := s[index]
if !isDigit(c) {
break
}
n = n*10 + fromDigit(c)
}
for i := index; i < fraction; i++ {
n *= 10
}
return m, n, s[index:], true
}
// appendFraction appends the fractional part of integer with specified maximum digit length.
func appendFraction(b []byte, n int, max int) []byte {
if n == 0 {
return b
}
width := max
for ; n%10 == 0; n /= 10 {
width--
}
if width <= 0 {
return b
}
width++ // Decimal point.
if len(b)+width <= cap(b) {
b = b[:len(b)+width]
} else {
b = append(b, make([]byte, width)...)
}
b[len(b)-width] = '.'
for i := 0; i < width-1; i++ {
index := len(b) - 1 - i
next := n / 10
b[index] = toDigit(n - next*10)
n = next
}
return b
}