-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatof.go
52 lines (41 loc) · 1.28 KB
/
atof.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
package slicestrconv
import (
"strconv"
"strings"
)
// ParseFloat32Slice parses and returns the []float32 slice represented by the string s.
func ParseFloat32Slice(s string, base int) ([]float32, 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 []float32
for _, v := range values {
value, err := strconv.ParseFloat(strings.TrimSpace(v), base)
if err != nil {
return nil, err
}
theSlice = append(theSlice, float32(value))
}
return theSlice, nil
}
// ParseFloat64Slice parses and returns the []float64 slice represented by the string s.
func ParseFloat64Slice(s string, base int) ([]float64, 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 []float64
for _, v := range values {
value, err := strconv.ParseFloat(strings.TrimSpace(v), base)
if err != nil {
return nil, err
}
theSlice = append(theSlice, value)
}
return theSlice, nil
}