-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
180 lines (159 loc) · 3.86 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
package uint256
// lower(c) is a lower-case letter if and only if
// c is either that lower-case letter or the equivalent upper-case letter.
// Instead of writing c == 'x' || c == 'X' one can write lower(c) == 'x'.
// Note that lower of non-letters can produce other non-letters.
func lower(c byte) byte {
return c | ('x' - 'X')
}
// underscoreOK reports whether the underscores in s are allowed.
// Checking them in this one function lets all the parsers skip over them simply.
// Underscore must appear only between digits or between a base prefix and a digit.
func underscoreOK(s string) bool {
// saw tracks the last character (class) we saw:
// ^ for beginning of number,
// 0 for a digit or base prefix,
// _ for an underscore,
// ! for none of the above.
saw := '^'
i := 0
// Optional sign.
if len(s) >= 1 && (s[0] == '-' || s[0] == '+') {
s = s[1:]
}
// Optional base prefix.
hex := false
if len(s) >= 2 && s[0] == '0' && (lower(s[1]) == 'b' || lower(s[1]) == 'o' || lower(s[1]) == 'x') {
i = 2
saw = '0' // base prefix counts as a digit for "underscore as digit separator"
hex = lower(s[1]) == 'x'
}
// Number proper.
for ; i < len(s); i++ {
// Digits are always okay.
if '0' <= s[i] && s[i] <= '9' || hex && 'a' <= lower(s[i]) && lower(s[i]) <= 'f' {
saw = '0'
continue
}
// Underscore must follow digit.
if s[i] == '_' {
if saw != '0' {
return false
}
saw = '_'
continue
}
// Underscore must also be followed by digit.
if saw == '_' {
return false
}
// Saw non-digit, non-underscore.
saw = '!'
}
return saw != '_'
}
func checkNumberS(input string) error {
const fn = "UnmarshalText"
l := len(input)
if l == 0 {
return errEmptyString(fn, input)
}
if l < 2 || input[0] != '0' ||
(input[1] != 'x' && input[1] != 'X') {
return errMissingPrefix(fn, input)
}
if l == 2 {
return errEmptyNumber(fn, input)
}
if len(input) > 3 && input[2] == '0' {
return errLeadingZero(fn, input)
}
return nil
}
// ParseUint is like ParseUint but for unsigned numbers.
//
// A sign prefix is not permitted.
func parseUint(s string, base int, bitSize int) (uint64, error) {
const fnParseUint = "ParseUint"
if s == "" {
return 0, errSyntax(fnParseUint, s)
}
base0 := base == 0
s0 := s
switch {
case 2 <= base && base <= 36:
// valid base; nothing to do
case base == 0:
// Look for octal, hex prefix.
base = 10
if s[0] == '0' {
switch {
case len(s) >= 3 && lower(s[1]) == 'b':
base = 2
s = s[2:]
case len(s) >= 3 && lower(s[1]) == 'o':
base = 8
s = s[2:]
case len(s) >= 3 && lower(s[1]) == 'x':
base = 16
s = s[2:]
default:
base = 8
s = s[1:]
}
}
default:
return 0, errInvalidBase(fnParseUint, base)
}
if bitSize == 0 {
bitSize = uintSize
} else if bitSize < 0 || bitSize > 64 {
return 0, errInvalidBitSize(fnParseUint, bitSize)
}
// Cutoff is the smallest number such that cutoff*base > maxUint64.
// Use compile-time constants for common cases.
var cutoff uint64
switch base {
case 10:
cutoff = MaxUint64/10 + 1
case 16:
cutoff = MaxUint64/16 + 1
default:
cutoff = MaxUint64/uint64(base) + 1
}
maxVal := uint64(1)<<uint(bitSize) - 1
underscores := false
var n uint64
for _, c := range []byte(s) {
var d byte
switch {
case c == '_' && base0:
underscores = true
continue
case '0' <= c && c <= '9':
d = c - '0'
case 'a' <= lower(c) && lower(c) <= 'z':
d = lower(c) - 'a' + 10
default:
return 0, errSyntax(fnParseUint, s0)
}
if d >= byte(base) {
return 0, errSyntax(fnParseUint, s0)
}
if n >= cutoff {
// n*base overflows
return maxVal, errRange(fnParseUint, s0)
}
n *= uint64(base)
n1 := n + uint64(d)
if n1 < n || n1 > maxVal {
// n+d overflows
return maxVal, errRange(fnParseUint, s0)
}
n = n1
}
if underscores && !underscoreOK(s0) {
return 0, errSyntax(fnParseUint, s0)
}
return n, nil
}