Skip to content

Commit

Permalink
Quote the numbers even if they are out of range (#593)
Browse files Browse the repository at this point in the history
* Quote the numbers even if they are out of range

#586

* Keep the original interface of the ToNumber function
  • Loading branch information
shuheiktgw authored Dec 16, 2024
1 parent e1d8782 commit 61bc6c1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
6 changes: 3 additions & 3 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,12 +301,12 @@ func checkLineBreak(t *token.Token) bool {
}
// Due to the way that comment parsing works its assumed that when a null value does not have new line in origin
// it was squashed therefore difference is ignored.
//foo:
// foo:
// bar:
// # comment
// baz: 1
//becomes
//foo:
// becomes
// foo:
// bar: null # comment
//
// baz: 1
Expand Down
39 changes: 30 additions & 9 deletions token/token.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package token

import (
"errors"
"fmt"
"strconv"
"strings"
Expand Down Expand Up @@ -539,15 +540,35 @@ type NumberValue struct {
}

func ToNumber(value string) *NumberValue {
if len(value) == 0 {
num, err := toNumber(value)
if err != nil {
return nil
}
return num
}

func isNumber(value string) bool {
num, err := toNumber(value)
if err != nil {
var numErr *strconv.NumError
if errors.As(err, &numErr) && errors.Is(numErr.Err, strconv.ErrRange) {
return true
}
return false
}
return num != nil
}

func toNumber(value string) (*NumberValue, error) {
if len(value) == 0 {
return nil, nil
}
if strings.HasPrefix(value, "_") {
return nil
return nil, nil
}
dotCount := strings.Count(value, ".")
if dotCount > 1 {
return nil
return nil, nil
}

isNegative := strings.HasPrefix(value, "-")
Expand Down Expand Up @@ -589,19 +610,19 @@ func ToNumber(value string) *NumberValue {
if typ == NumberTypeFloat {
f, err := strconv.ParseFloat(text, 64)
if err != nil {
return nil
return nil, err
}
v = f
} else if isNegative {
i, err := strconv.ParseInt(text, base, 64)
if err != nil {
return nil
return nil, err
}
v = i
} else {
u, err := strconv.ParseUint(text, base, 64)
if err != nil {
return nil
return nil, err
}
v = u
}
Expand All @@ -610,7 +631,7 @@ func ToNumber(value string) *NumberValue {
Type: typ,
Value: v,
Text: text,
}
}, nil
}

// This is a subset of the formats permitted by the regular expression
Expand All @@ -635,15 +656,15 @@ func isTimestamp(value string) bool {
return false
}

// IsNeedQuoted whether need quote for passed string or not
// IsNeedQuoted checks whether the value needs quote for passed string or not
func IsNeedQuoted(value string) bool {
if value == "" {
return true
}
if _, exists := reservedEncKeywordMap[value]; exists {
return true
}
if num := ToNumber(value); num != nil {
if isNumber(value) {
return true
}
first := value[0]
Expand Down
4 changes: 4 additions & 0 deletions token/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ func TestIsNeedQuoted(t *testing.T) {
"",
"true",
"1.234",
"0b11111111111111111111111111111111111111111111111111111111111111111",
"0o7777777777777777777777777777777777777777",
"999999999999999999999999999999999999999999",
"0xffffffffffffffffffffffffffffffffffffffff",
"1:1",
"2001-12-15T02:59:43.1Z",
"2001-12-14t21:59:43.10-05:00",
Expand Down

0 comments on commit 61bc6c1

Please sign in to comment.