-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatoi.go
236 lines (185 loc) · 6.1 KB
/
atoi.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package slicestrconv
import (
"strconv"
"strings"
)
// ParseIntSlice parses and returns the []int slice represented by the string s.
func ParseIntSlice(s string, base int) ([]int, error) {
if !strings.HasPrefix(s, OpeningBracket) || !strings.HasSuffix(s, ClosingBracket) {
return nil, ErrInvalidSyntax
}
s = strings.TrimPrefix(s, OpeningBracket)
s = strings.TrimSuffix(s, ClosingBracket)
values := strings.Split(s, Delimiter)
var theSlice []int
for _, v := range values {
value, err := strconv.ParseInt(strings.TrimSpace(v), base, 64)
if err != nil {
return nil, err
}
theSlice = append(theSlice, int(value))
}
return theSlice, nil
}
// ParseInt8Slice parses and returns the []int8 slice represented by the string s.
func ParseInt8Slice(s string, base int) ([]int8, error) {
if !strings.HasPrefix(s, OpeningBracket) || !strings.HasSuffix(s, ClosingBracket) {
return nil, ErrInvalidSyntax
}
s = strings.TrimPrefix(s, OpeningBracket)
s = strings.TrimSuffix(s, ClosingBracket)
values := strings.Split(s, Delimiter)
var theSlice []int8
for _, v := range values {
value, err := strconv.ParseInt(strings.TrimSpace(v), base, 8)
if err != nil {
return nil, err
}
theSlice = append(theSlice, int8(value))
}
return theSlice, nil
}
// ParseInt16Slice parses and returns the []int16 slice represented by the string s.
func ParseInt16Slice(s string, base int) ([]int16, error) {
if !strings.HasPrefix(s, OpeningBracket) || !strings.HasSuffix(s, ClosingBracket) {
return nil, ErrInvalidSyntax
}
s = strings.TrimPrefix(s, OpeningBracket)
s = strings.TrimSuffix(s, ClosingBracket)
values := strings.Split(s, Delimiter)
var theSlice []int16
for _, v := range values {
value, err := strconv.ParseInt(strings.TrimSpace(v), base, 16)
if err != nil {
return nil, err
}
theSlice = append(theSlice, int16(value))
}
return theSlice, nil
}
// ParseInt32Slice parses and returns the []int32 slice represented by the string s.
func ParseInt32Slice(s string, base int) ([]int32, error) {
if !strings.HasPrefix(s, OpeningBracket) || !strings.HasSuffix(s, ClosingBracket) {
return nil, ErrInvalidSyntax
}
s = strings.TrimPrefix(s, OpeningBracket)
s = strings.TrimSuffix(s, ClosingBracket)
values := strings.Split(s, Delimiter)
var theSlice []int32
for _, v := range values {
value, err := strconv.ParseInt(strings.TrimSpace(v), base, 32)
if err != nil {
return nil, err
}
theSlice = append(theSlice, int32(value))
}
return theSlice, nil
}
// ParseInt64Slice parses and returns the []int64 slice represented by the string s.
func ParseInt64Slice(s string, base int) ([]int64, error) {
if !strings.HasPrefix(s, OpeningBracket) || !strings.HasSuffix(s, ClosingBracket) {
return nil, ErrInvalidSyntax
}
s = strings.TrimPrefix(s, OpeningBracket)
s = strings.TrimSuffix(s, ClosingBracket)
values := strings.Split(s, Delimiter)
var theSlice []int64
for _, v := range values {
value, err := strconv.ParseInt(strings.TrimSpace(v), base, 64)
if err != nil {
return nil, err
}
theSlice = append(theSlice, value)
}
return theSlice, nil
}
// ParseUintSlice parses and returns the []uint slice represented by the string s.
func ParseUintSlice(s string, base int) ([]uint, error) {
if !strings.HasPrefix(s, OpeningBracket) || !strings.HasSuffix(s, ClosingBracket) {
return nil, ErrInvalidSyntax
}
s = strings.TrimPrefix(s, OpeningBracket)
s = strings.TrimSuffix(s, ClosingBracket)
values := strings.Split(s, Delimiter)
var theSlice []uint
for _, v := range values {
value, err := strconv.ParseUint(strings.TrimSpace(v), base, 64)
if err != nil {
return nil, err
}
theSlice = append(theSlice, uint(value))
}
return theSlice, nil
}
// ParseUint8Slice parses and returns the []uint8 slice represented by the string s.
func ParseUint8Slice(s string, base int) ([]uint8, error) {
if !strings.HasPrefix(s, OpeningBracket) || !strings.HasSuffix(s, ClosingBracket) {
return nil, ErrInvalidSyntax
}
s = strings.TrimPrefix(s, OpeningBracket)
s = strings.TrimSuffix(s, ClosingBracket)
values := strings.Split(s, Delimiter)
var theSlice []uint8
for _, v := range values {
value, err := strconv.ParseUint(strings.TrimSpace(v), base, 8)
if err != nil {
return nil, err
}
theSlice = append(theSlice, uint8(value))
}
return theSlice, nil
}
// ParseUint16Slice parses and returns the []uint16 slice represented by the string s.
func ParseUint16Slice(s string, base int) ([]uint16, error) {
if !strings.HasPrefix(s, OpeningBracket) || !strings.HasSuffix(s, ClosingBracket) {
return nil, ErrInvalidSyntax
}
s = strings.TrimPrefix(s, OpeningBracket)
s = strings.TrimSuffix(s, ClosingBracket)
values := strings.Split(s, Delimiter)
var theSlice []uint16
for _, v := range values {
value, err := strconv.ParseUint(strings.TrimSpace(v), base, 16)
if err != nil {
return nil, err
}
theSlice = append(theSlice, uint16(value))
}
return theSlice, nil
}
// ParseUint32Slice parses and returns the []uint32 slice represented by the string s.
func ParseUint32Slice(s string, base int) ([]uint32, error) {
if !strings.HasPrefix(s, OpeningBracket) || !strings.HasSuffix(s, ClosingBracket) {
return nil, ErrInvalidSyntax
}
s = strings.TrimPrefix(s, OpeningBracket)
s = strings.TrimSuffix(s, ClosingBracket)
values := strings.Split(s, Delimiter)
var theSlice []uint32
for _, v := range values {
value, err := strconv.ParseUint(strings.TrimSpace(v), base, 32)
if err != nil {
return nil, err
}
theSlice = append(theSlice, uint32(value))
}
return theSlice, nil
}
// ParseUint64Slice parses and returns the []uint64 slice represented by the string s.
func ParseUint64Slice(s string, base int) ([]uint64, error) {
if !strings.HasPrefix(s, OpeningBracket) || !strings.HasSuffix(s, ClosingBracket) {
return nil, ErrInvalidSyntax
}
s = strings.TrimPrefix(s, OpeningBracket)
s = strings.TrimSuffix(s, ClosingBracket)
values := strings.Split(s, Delimiter)
var theSlice []uint64
for _, v := range values {
value, err := strconv.ParseUint(strings.TrimSpace(v), base, 64)
if err != nil {
return nil, err
}
theSlice = append(theSlice, value)
}
return theSlice, nil
}