Skip to content
This repository has been archived by the owner on Dec 15, 2019. It is now read-only.

Commit

Permalink
Support scientific notation floats
Browse files Browse the repository at this point in the history
  • Loading branch information
cep21 committed Dec 4, 2014
1 parent 6966f50 commit 2c8fc55
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
44 changes: 43 additions & 1 deletion scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ func (s *scanner) scanNumber() (int, []byte, error) {
return 0, nil, err
} else if s.c != '.' {
s.unread()
return TNUMBER, s.scratch[0:n], nil
// We can't just return. The number could be 1e-1
return s.scanScientific(n)
}
s.scratch[n] = '.'
n++
Expand All @@ -207,6 +208,47 @@ func (s *scanner) scanNumber() (int, []byte, error) {
}
n++

return s.scanScientific(n)
}

func (s *scanner) scanScientific(n int) (int, []byte, error) {
// Read scientific
if err := s.read(); err == io.EOF {
return TNUMBER, s.scratch[0:n], nil
} else if err != nil {
return 0, nil, err
} else if s.c != 'e' && s.c != 'E' {
s.unread()
return TNUMBER, s.scratch[0:n], nil
}
s.scratch[n] = 'e'
n++

// Read sign
if err := s.read(); err == io.EOF {
return TNUMBER, s.scratch[0:n], nil
} else if err != nil {
return 0, nil, err
} else if s.c != '-' && s.c != '+' {
s.unread()
} else if s.c == '-' { // don't bother adding the +
s.scratch[n] = '-'
n++
}

if err := s.read(); err != nil {
return 0, nil, err
}

// Read whole number.
if err := s.scanDigits(&n); err == io.EOF {
return TNUMBER, s.scratch[0:n], nil
} else if err != nil {
fmt.Println("Bad?")
return 0, nil, err
}
n++

return TNUMBER, s.scratch[0:n], nil
}

Expand Down
26 changes: 26 additions & 0 deletions scanner/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"strconv"
)

// Ensures that a positive number can be scanned.
Expand All @@ -33,6 +34,31 @@ func TestScanFloat(t *testing.T) {
assert.Equal(t, string(b), "120.12931")
}

// Ensures that a fractional number in scientific notation can be scanned.
func TestScanFloatScientific(t *testing.T) {
tok, b, err := NewScanner(strings.NewReader("10.1e01")).Scan()
assert.NoError(t, err)
assert.Equal(t, tok, TNUMBER)
assert.Equal(t, string(b), "10.1e01")

tok, b, err = NewScanner(strings.NewReader("10.1e-01")).Scan()
assert.NoError(t, err)
assert.Equal(t, tok, TNUMBER)
assert.Equal(t, string(b), "10.1e-01")

tok, b, err = NewScanner(strings.NewReader("10.1e+01")).Scan()
assert.NoError(t, err)
assert.Equal(t, tok, TNUMBER)
assert.Equal(t, string(b), "10.1e01")
f, _ := strconv.ParseFloat(string(b), 64)
assert.Equal(t, 10.1e+01, f)

tok, b, err = NewScanner(strings.NewReader("-1e1")).Scan()
assert.NoError(t, err)
assert.Equal(t, tok, TNUMBER)
assert.Equal(t, string(b), "-1e1")
}

// Ensures that a quoted string can be scanned.
func TestScanString(t *testing.T) {
tok, b, err := NewScanner(strings.NewReader(`"hello world"`)).Scan()
Expand Down

0 comments on commit 2c8fc55

Please sign in to comment.