MCPcopy
hub / github.com/uber/aresdb / parseInt

Method parseInt

query/expr/parser.go:45–68  ·  view source on GitHub ↗

parseInt parses a string and returns an integer literal.

(min, max int)

Source from the content-addressed store, hash-verified

43
44// parseInt parses a string and returns an integer literal.
45func (p *Parser) parseInt(min, max int) (int, error) {
46 tok, pos, lit := p.scanIgnoreWhitespace()
47 if tok != NUMBER {
48 return 0, newParseError(tokstr(tok, lit), []string{"number"}, pos)
49 }
50
51 // Return an error if the number has a fractional part.
52 if strings.Contains(lit, ".") {
53 return 0, &ParseError{Message: "number must be an integer", Pos: pos}
54 }
55
56 // Convert string to int.
57 n, err := strconv.Atoi(lit)
58 if err != nil {
59 return 0, &ParseError{Message: err.Error(), Pos: pos}
60 } else if min > n || n > max {
61 return 0, &ParseError{
62 Message: fmt.Sprintf("invalid value %d: must be %d <= n <= %d", n, min, max),
63 Pos: pos,
64 }
65 }
66
67 return n, nil
68}
69
70// parseUInt32 parses a string and returns a 32-bit unsigned integer literal.
71func (p *Parser) parseUInt32() (uint32, error) {

Callers

nothing calls this directly

Calls 4

scanIgnoreWhitespaceMethod · 0.95
newParseErrorFunction · 0.85
tokstrFunction · 0.85
ErrorMethod · 0.65

Tested by

no test coverage detected