forked from att/gopkgs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
atoll.go
211 lines (170 loc) · 4.96 KB
/
atoll.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// vi: sw=4 ts=4:
/*
---------------------------------------------------------------------------
Copyright (c) 2013-2015 AT&T Intellectual Property
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
---------------------------------------------------------------------------
*/
/*
Mnemonic: atoll
Absrtract: a clike atoll that doesn't error when it encounters
a non-digit; returning 0 if there are no digits. This supports
0x<hex> or 0<octal> values and should parse them stopping
conversion at the first non-appropriate 'digit'. This also allows
a lead +/-.
There is an extension on the C functions... if the value is
postfixed with M/K/G or m/k/g the return value will be
'expanded' accordingly with the capitalised values being
powrs of 10 (e.g. MB) and the lower case indicating powers
of 2 (e.g. MiB). This might cause unwanted side effects should
the goal be to take a string 200grains and capture just the
value; these functions will interpret the leading g incorrectly.
Input can be either a string, pointer to string, or a byte array.
If the string/array passed in does not begin with a valid digit
or sign, or is a pointer that is nil, a value of 0 is returned
rather than an error.
*/
package clike
import (
"strconv"
)
/*
Convert a string or an array of bytes into a 64 bit integer.
*/
func Atoll( objx interface{} ) (v int64) {
var (
i int
buf []byte
)
v = 0 // ensure all early returns have a value of 0
if objx == nil {
return
}
switch objx.( type ) { // place into a container we can deal with
case []byte:
buf = objx.([]byte)
case string:
buf = []byte( objx.(string) )
case *string:
bp := objx.( *string )
if bp == nil {
return 0
}
buf = []byte( *bp );
default:
return // who knows, but it doesn't convert
}
if len( buf ) < 1 {
return
}
i = 0
if buf[i] == '-' || buf[i] == '+' {
i++
}
if buf[i] == '0' {
if len( buf ) > 1 && buf[i+1] == 'x' {
i += 2
for ; i < len(buf) && ((buf[i] >= '0' && buf[i] <= '9') || (buf[i] >= 'a' && buf[i] <= 'f') ); i++ {} // find last digit
} else {
i++
for ; i < len(buf) && (buf[i] >= '0' && buf[i] <= '7'); i++ {} // find last octal digit
}
} else {
for ; i < len(buf) && (buf[i] >= '0' && buf[i] <= '9'); i++ {} // find last digit
}
if i > 0 {
v, _ = strconv.ParseInt( string( buf[0:i] ), 0, 64 )
}
if i < len( buf ) {
switch string( buf[i:] ) {
case "M", "MB":
v *= 1000000
case "G", "GB":
v *= 1000000000
case "K", "KB":
v *= 1000
case "m", "MiB":
v *= 1048576
case "g", "GiB":
v *= 1073741824
case "k", "KiB":
v *= 1024
default: break;
}
}
return
}
/*
Convert to unsigned 64bit.
*/
func Atoull( objx interface{} ) (v uint64) {
var (
i int
buf []byte
)
v = 0 // ensure all early returns have a value of 0
if objx == nil {
return
}
switch objx.( type ) { // place into a container we can deal with
case []byte:
buf = objx.([]byte)
case string:
buf = []byte( objx.(string) )
case *string:
bp := objx.( *string )
if bp == nil {
return 0
}
buf = []byte( *bp );
default:
return // who knows, but it doesn't convert
}
if len( buf ) < 1 {
return
}
i = 0
if buf[i] == '-' || buf[i] == '+' {
i++
}
if buf[i] == '0' {
if len( buf ) > 1 && buf[i+1] == 'x' {
i += 2
for ; i < len(buf) && ((buf[i] >= '0' && buf[i] <= '9') || (buf[i] >= 'a' && buf[i] <= 'f') ); i++ {} // find last digit
} else {
i++
for ; i < len(buf) && (buf[i] >= '0' && buf[i] <= '7'); i++ {} // find last octal digit
}
} else {
for ; i < len(buf) && (buf[i] >= '0' && buf[i] <= '9'); i++ {} // find last digit
}
if i > 0 {
v, _ = strconv.ParseUint( string( buf[0:i] ), 0, 64 )
}
if i < len( buf ) {
switch string( buf[i:] ) {
case "M", "MB":
v *= 1000000
case "G", "GB":
v *= 1000000000
case "K", "KB":
v *= 1000
case "m", "MiB":
v *= 1048576
case "g", "GiB":
v *= 1073741824
case "k", "KiB":
v *= 1024
default: break;
}
}
return
}