Skip to content

Commit

Permalink
allow numbers with underscore chars
Browse files Browse the repository at this point in the history
Add support for numbers like 12_346_798 and 1.344_543_344

This is a convenience enhancement, which improves readability of long numbers in MetricsQL queries.
For example, 1_234_567_890 is easier to read than 1234567890.

These numbers are supported by Go - see golang/go#28493
  • Loading branch information
valyala committed Aug 30, 2023
1 parent 2892124 commit dd4d4c2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func scanPositiveNumber(s string) (string, error) {
}
return s[:i], nil
}
for i < len(s) && isDecimalChar(s[i]) {
for i < len(s) && isDecimalCharOrUnderscore(s[i]) {
i++
}

Expand All @@ -258,7 +258,7 @@ func scanPositiveNumber(s string) (string, error) {
// Scan fractional part. It cannot be empty.
i++
j := i
for j < len(s) && isDecimalChar(s[j]) {
for j < len(s) && isDecimalCharOrUnderscore(s[j]) {
j++
}
i = j
Expand Down Expand Up @@ -673,6 +673,10 @@ func isDecimalChar(ch byte) bool {
return ch >= '0' && ch <= '9'
}

func isDecimalCharOrUnderscore(ch byte) bool {
return isDecimalChar(ch) || ch == '_'
}

func isHexChar(ch byte) bool {
return isDecimalChar(ch) || ch >= 'a' && ch <= 'f' || ch >= 'A' && ch <= 'F'
}
Expand Down
5 changes: 5 additions & 0 deletions lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ func TestScanPositiveNumberSuccess(t *testing.T) {
f("2.3tb", "2.3tb")
f("3tI", "3tI")
f("4.5TIB ", "4.5TIB")

// number with underscores - see https://github.com/golang/go/issues/28493
f("1_2_334", "1_2_334")
f("1_2.3_34_5", "1_2.3_34_5")
f("1_2.3_34_5e8", "1_2.3_34_5e8")
}

func TestScanPositiveNumberFailure(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ func TestParseSuccess(t *testing.T) {
// numberExpr
same(`1`)
same(`123.`)
same(`1_234`)
same(`1_2_34.56_78_9`)
another(`-123.`, `-123`)
same(`foo - 123.`)
same(`12.e+4`)
Expand Down

0 comments on commit dd4d4c2

Please sign in to comment.