-
Notifications
You must be signed in to change notification settings - Fork 488
/
Copy pathstrconv.go
108 lines (95 loc) · 2.48 KB
/
strconv.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
package options
import (
"fmt"
"strconv"
"unicode"
"unicode/utf8"
"github.com/influxdata/flux"
"github.com/influxdata/flux/ast"
"github.com/influxdata/flux/codes"
)
// TODO(jsternberg): This file copies over code from an internal package
// because we need them from an internal package and the only way they
// are exposed is through a package that depends on the core flux parser.
// We want to avoid a dependency on the core parser so we copy these
// implementations.
//
// In the future, we should consider exposing these functions from flux
// in a non-internal package outside of the parser package.
// ParseSignedDuration is a helper wrapper around parser.ParseSignedDuration.
// We use it because we need to clear the basenode, but flux does not.
func ParseSignedDuration(text string) (*ast.DurationLiteral, error) {
// TODO(jsternberg): This is copied from an internal package in flux to break a dependency
// on the parser package where this method is exposed.
// Consider exposing this properly in flux.
if r, s := utf8.DecodeRuneInString(text); r == '-' {
d, err := parseDuration(text[s:])
if err != nil {
return nil, err
}
for i := range d {
d[i].Magnitude = -d[i].Magnitude
}
return &ast.DurationLiteral{Values: d}, nil
}
d, err := parseDuration(text)
if err != nil {
return nil, err
}
return &ast.DurationLiteral{Values: d}, nil
}
// parseDuration will convert a string into components of the duration.
func parseDuration(lit string) ([]ast.Duration, error) {
var values []ast.Duration
for len(lit) > 0 {
n := 0
for n < len(lit) {
ch, size := utf8.DecodeRuneInString(lit[n:])
if size == 0 {
panic("invalid rune in duration")
}
if !unicode.IsDigit(ch) {
break
}
n += size
}
if n == 0 {
return nil, &flux.Error{
Code: codes.Invalid,
Msg: fmt.Sprintf("invalid duration %s", lit),
}
}
magnitude, err := strconv.ParseInt(lit[:n], 10, 64)
if err != nil {
return nil, err
}
lit = lit[n:]
n = 0
for n < len(lit) {
ch, size := utf8.DecodeRuneInString(lit[n:])
if size == 0 {
panic("invalid rune in duration")
}
if !unicode.IsLetter(ch) {
break
}
n += size
}
if n == 0 {
return nil, &flux.Error{
Code: codes.Invalid,
Msg: fmt.Sprintf("duration is missing a unit: %s", lit),
}
}
unit := lit[:n]
if unit == "µs" {
unit = "us"
}
values = append(values, ast.Duration{
Magnitude: magnitude,
Unit: unit,
})
lit = lit[n:]
}
return values, nil
}