Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,11 @@ Loop:
}
}

// A lone "." has no digits. The numeric grammar requires at least one.
if len(buf) == 0 {
return ConversionSyntax
}

if big || z.compact == c.Inflated {
z.unscaled.SetString(string(buf), 10)
z.compact = c.Inflated
Expand Down Expand Up @@ -300,6 +305,10 @@ func (z *Big) scanExponent(r io.ByteScanner) error {

ch, err = r.ReadByte()
if err != nil {
if err == io.EOF {
// "1e" has an exponent mark and no digits.
return ConversionSyntax
}
return err
}
var neg bool
Expand All @@ -318,6 +327,7 @@ func (z *Big) scanExponent(r io.ByteScanner) error {
}

var exp uint64
digits := 0
for {
ch, err := r.ReadByte()
if err != nil {
Expand Down Expand Up @@ -347,6 +357,12 @@ func (z *Big) scanExponent(r io.ByteScanner) error {
return Overflow
}
exp = v
digits++
}

// "1e+" and "1e-" have a sign and no digits.
if digits == 0 {
return ConversionSyntax
}

if neg {
Expand Down
31 changes: 29 additions & 2 deletions scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ func BenchmarkBig_SetString(b *testing.B) {
globOk = ok
}



func TestUnmarshalTextTwice(t *testing.T) {
x := &Big{}

Expand All @@ -64,6 +62,35 @@ func TestUnmarshalTextTwice(t *testing.T) {
}
}

func TestSetStringTruncatedNumeric(t *testing.T) {
for _, s := range []string{"1e", "1E", "1e+", "1e-", "-1e", "1.2e", "."} {
x, ok := new(Big).SetString(s)
if !ok {
t.Fatalf("%s: SetString returned false", s)
}
if !x.IsNaN(0) {
t.Fatalf("%s = %s, want NaN", s, x)
}
if x.Context.Conditions&ConversionSyntax == 0 {
t.Fatalf("%s: missing ConversionSyntax", s)
}
}

kept := []struct{ in, want string }{
{"1e2", "1E+2"},
{"1e+2", "1E+2"},
{"1e-1", "0.1"},
{"1.", "1"},
{".5", "0.5"},
{"10.00", "10.00"},
}
for _, tc := range kept {
x, ok := new(Big).SetString(tc.in)
if !ok || x.String() != tc.want {
t.Fatalf("%s = %s ok=%v, want %s", tc.in, x, ok, tc.want)
}
}
}

func TestSetStringTwice(t *testing.T) {
x := &Big{}
Expand Down