-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
117 lines (100 loc) · 2.42 KB
/
parse.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
package cliutil
import (
"fmt"
"math"
"strconv"
"strings"
"unicode"
)
// ParseKeyValue parses key value pairs.
// See https://stackoverflow.com/a/44282136
func ParseKeyValue(s string) map[string]string {
// Split the string by spaces that aren't in quotes.
lastQuote := rune(0)
parts := strings.FieldsFunc(s, func(c rune) bool {
switch {
case c == lastQuote:
lastQuote = rune(0)
return false
case lastQuote != rune(0):
return false
case unicode.In(c, unicode.Quotation_Mark):
lastQuote = c
return false
default:
return unicode.IsSpace(c)
}
})
// Build and return the key/value map.
m := make(map[string]string)
for _, part := range parts {
p := strings.Split(part, "=")
// Protect against values with no "=", treat them as a key.
if len(p) < 2 {
p = []string{p[0], ""}
}
// Trim quotes at the edges.
p[0] = strings.TrimFunc(p[0], isQuotationMark)
p[1] = strings.TrimFunc(p[1], isQuotationMark)
m[p[0]] = p[1]
}
return m
}
func isQuotationMark(c rune) bool {
return unicode.In(c, unicode.Quotation_Mark)
}
// ParseIntSlice parses a slice of integers from a string. We expect integers
// and ranges to be separated by commas, e.g., 1,2,3:5 = []int{1,2,3,4,5}.
func ParseIntSlice(s string) ([]int, error) {
if s == "" {
return []int{}, nil
}
elems := strings.Split(s, ",")
out := []int{}
for idx, elem := range elems {
i, err := parseRange(elem, idx)
if err != nil {
return []int{}, err
}
out = append(out, i...)
}
return out, nil
}
func parseRange(elem string, idx int) (out []int, err error) {
elems := strings.Split(elem, ":")
switch len(elems) {
case 1:
out = make([]int, 1)
out[0], err = strconv.Atoi(strings.TrimSpace(elems[0]))
if err != nil {
err = fmt.Errorf("value at index %v is not a number: %w", idx, err)
return
}
case 2:
r := make([]int, 2)
for iidx, elem := range elems {
r[iidx], err = strconv.Atoi(strings.TrimSpace(elem))
if err != nil {
err = fmt.Errorf("range at index %v not valid: %w", idx, err)
return
}
}
out = Sequence(r[0], r[1])
default:
err = fmt.Errorf("range at index %v not valid: %w", idx, err)
return
}
return
}
// Sequence returns a sequencce of numbers between start and end as an []int.
func Sequence(start, end int) []int {
a := make([]int, int(math.Abs(float64(start-end)))+1)
for i := range a {
if start < end {
a[i] = start + i
} else {
a[i] = start - i
}
}
return a
}