Debian Codegraph @

v2/unsigned.go

raw symbols SCIP
1 // Copyright 2017 Google Inc. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package getopt
6
7 import (
8 "fmt"
9 "strconv"
10 "sync"
11 )
12
13 type unsigned uint64
14
15 type UnsignedLimit struct {
16 Base int // Base for conversion as per strconv.ParseInt
17 Bits int // Number of bits as per strconv.ParseInt
18 Min uint64 // Minimum allowed value if both Min and Max are not 0
19 Max uint64 // Maximum allowed value if both Min and Max are not 0
20 }
21
22 var (
23 unsignedLimitsMu sync.Mutex
24 unsignedLimits = make(map[*unsigned]*UnsignedLimit)
25 )
26
27 func (n *unsigned) Set(value string, opt Option) error {
28 unsignedLimitsMu.Lock()
29 l := unsignedLimits[n]
30 unsignedLimitsMu.Unlock()
31 if l == nil {
32 return fmt.Errorf("no limits defined for %s", opt.Name())
33 }
34 v, err := strconv.ParseUint(value, l.Base, l.Bits)
35 if err != nil {
36 if e, ok := err.(*strconv.NumError); ok {
37 switch e.Err {
38 case strconv.ErrRange:
39 err = fmt.Errorf("value out of range: %s", value)
40 case strconv.ErrSyntax:
41 err = fmt.Errorf("not a valid number: %s", value)
42 }
43 }
44 return err
45 }
46 if l.Min != 0 || l.Max != 0 {
47 if v < l.Min {
48 return fmt.Errorf("value out of range (<%v): %s", l.Min, value)
49 }
50 if v > l.Max {
51 return fmt.Errorf("value out of range (>%v): %s", l.Max, value)
52 }
53 }
54 *n = unsigned(v)
55 return nil
56 }
57
58 func (n *unsigned) String() string {
59 unsignedLimitsMu.Lock()
60 l := unsignedLimits[n]
61 unsignedLimitsMu.Unlock()
62 if l != nil && l.Base != 0 {
63 return strconv.FormatUint(uint64(*n), l.Base)
64 }
65 return strconv.FormatUint(uint64(*n), 10)
66 }
67
68 // Unsigned creates an option that is stored in a uint64 and is
69 // constrained by the limits pointed to by l. The Max and Min values are only
70 // used if at least one of the values are not 0. If Base is 0, the base is
71 // implied by the string's prefix: base 16 for "0x", base 8 for "0", and base
72 // 10 otherwise.
73 func Unsigned(name rune, value uint64, l *UnsignedLimit, helpvalue ...string) *uint64 {
74 CommandLine.unsignedOption(&value, "", name, l, helpvalue...)
75 return &value
76 }
77
78 func (s *Set) Unsigned(name rune, value uint64, l *UnsignedLimit, helpvalue ...string) *uint64 {
79 s.unsignedOption(&value, "", name, l, helpvalue...)
80 return &value
81 }
82
83 func UnsignedLong(name string, short rune, value uint64, l *UnsignedLimit, helpvalue ...string) *uint64 {
84 CommandLine.unsignedOption(&value, name, short, l, helpvalue...)
85 return &value
86 }
87
88 func (s *Set) UnsignedLong(name string, short rune, value uint64, l *UnsignedLimit, helpvalue ...string) *uint64 {
89 s.unsignedOption(&value, name, short, l, helpvalue...)
90 return &value
91 }
92
93 func (s *Set) unsignedOption(p *uint64, name string, short rune, l *UnsignedLimit, helpvalue ...string) {
94 opt := s.FlagLong((*unsigned)(p), name, short, helpvalue...)
95 if l.Base > 36 || l.Base == 1 || l.Base < 0 {
96 fmt.Fprintf(stderr, "invalid base for %s: %d\n", opt.Name(), l.Base)
97 exit(1)
98 }
99 if l.Bits < 0 || l.Bits > 64 {
100 fmt.Fprintf(stderr, "invalid bit size for %s: %d\n", opt.Name(), l.Bits)
101 exit(1)
102 }
103 if l.Min > l.Max {
104 fmt.Fprintf(stderr, "min greater than max for %s\n", opt.Name())
105 exit(1)
106 }
107 lim := *l
108 unsignedLimitsMu.Lock()
109 unsignedLimits[(*unsigned)(p)] = &lim
110 unsignedLimitsMu.Unlock()
111 }