Skip to content

Commit

Permalink
Work around Go strconv's allowance of underscores in int/float litera…
Browse files Browse the repository at this point in the history
…ls (johnkerl#801)

* Function-pointerize IXS/IXSRegex to reduce runtime iffelsing

* remove IsRegexString and SuppressIXSRegex

* regression tests passing

* doc updates

* Don't use sed -I in ./configure

* Work around Go strconv's allowance of _ in int/float literals
  • Loading branch information
johnkerl committed Dec 25, 2021
1 parent 1dda1ef commit bca738a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
14 changes: 14 additions & 0 deletions internal/pkg/lib/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ func IntMin2(a, b int) int {

// TryIntFromString tries decimal, hex, octal, and binary.
func TryIntFromString(input string) (int, bool) {
// Go's strconv parses "1_2" as 12; not OK for Miller syntax. (Also not valid JSON.)
for i := 0; i < len(input); i++ {
if input[i] == '_' {
return 0, false
}
}

// Following twos-complement formatting familiar from all manners of
// languages, including C which was Miller's original implementation
// language, we want to allow 0x00....00 through 0x7f....ff as positive
Expand All @@ -115,6 +122,13 @@ func TryIntFromString(input string) (int, bool) {
}

func TryFloatFromString(input string) (float64, bool) {
// Go's strconv parses "1_2.3_4" as 12.34; not OK for Miller syntax. (Also not valid JSON.)
for i := 0; i < len(input); i++ {
if input[i] == '_' {
return 0, false
}
}

fval, err := strconv.ParseFloat(input, 64)
if err == nil {
return fval, true
Expand Down
4 changes: 4 additions & 0 deletions internal/pkg/mlrval/mlrval_is_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ func TestIsString(t *testing.T) {
assert.False(t, FromInferredType("").IsString())
assert.True(t, FromDeferredType("abc").IsString())
assert.True(t, FromInferredType("abc").IsString())
assert.True(t, FromInferredType("1_").IsString())
assert.True(t, FromInferredType("_2").IsString())
assert.True(t, FromInferredType("1_2").IsString())
assert.True(t, FromInferredType("1_2.3_4").IsString())
}

func TestIsStringOrVoid(t *testing.T) {
Expand Down

0 comments on commit bca738a

Please sign in to comment.