-
Notifications
You must be signed in to change notification settings - Fork 0
/
int_width.go
69 lines (52 loc) · 1.19 KB
/
int_width.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
package main
import (
"fmt"
"strconv"
"strings"
"golang.org/x/exp/slices"
)
type intWidth struct {
permitted []uint
notDefault bool
values []uint
}
func newIntWidth(permitted []uint) *intWidth {
r := &intWidth{}
r.permitted = append(r.permitted, permitted...)
r.values = append(r.values, permitted...)
return r
}
func sliceToString(s []uint) string {
var t []string
for _, v := range s {
t = append(t, strconv.Itoa(int(v)))
}
return strings.Join(t, ", ")
}
func (i *intWidth) Set(s string) error {
if !i.notDefault {
i.values = nil
i.notDefault = true
}
parsed_signed, err := strconv.Atoi(s)
if err != nil {
return err
}
parsed := uint(parsed_signed)
if !slices.Contains(i.permitted, parsed) {
return fmt.Errorf("%d is not supported (only %s)", parsed, sliceToString(i.permitted))
}
for _, v := range i.values {
if v == parsed {
return fmt.Errorf("%d is already specified", parsed)
}
}
i.values = append(i.values, uint(parsed))
return nil
}
func (i intWidth) String() string {
return fmt.Sprintf("%v", sliceToString(i.values))
}
func (i intWidth) Type() string {
return fmt.Sprintf("list of uint widths, must be %s", sliceToString(i.permitted))
}