Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
6 changes: 4 additions & 2 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,13 @@ func (parser *jsonParser) error(stream *Stream) error {
func TestJsonParser(t *testing.T) {
parser := newJSONParser()

data, err := parser.Parse([]byte(`{"one": 1, "two": "three", "four": [5, "six", 7.8, {}]}`))
data, err := parser.Parse([]byte(`{"one": 1, "two": "three", "four": [5, "six", 7.8, -9, -99.9, {}], "five-5": "5-five", "6-six": "six-6"}`))
require.NoError(t, err)
require.Equal(t, map[string]interface{}{
"one": int64(1),
"two": "three",
"four": []interface{}{int64(5), "six", 7.8, map[string]interface{}{}},
"four": []interface{}{int64(5), "six", 7.8, int64(-9), -99.9, map[string]interface{}{}},
"five-5": "5-five",
"6-six": "six-6",
}, data)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/bzick/tokenizer
module github.com/kettek/tokenizer
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you change back to github.com/bzick/tokenizer?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


go 1.13

Expand Down
10 changes: 10 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,16 @@ func (p *parsing) parseNumber() bool {
} else {
break
}
} else if !hasExp && p.curr == '-' { // Allow starting numbers from a negative sign.
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I analyzed this and realized it breaks backward compatibility. Let's make it a separate setting, something like "negative number parsing" settings.

if isNumberByte(nextByte) {
if start == -1 {
start = p.pos
} else {
break
}
} else {
break
}
} else {
break
}
Expand Down
3 changes: 3 additions & 0 deletions tokenizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func TestTokenize(t *testing.T) {
t.Run("integers", func(t *testing.T) {
integers := []item{
{int64(1), Token{key: TokenInteger, value: []byte("1")}},
{int64(-1), Token{key: TokenInteger, value: []byte("-1")}},
{int64(123456), Token{key: TokenInteger, value: []byte("123456")}},
{int64(123456), Token{key: TokenInteger, value: []byte("123_456")}},
}
Expand All @@ -63,10 +64,12 @@ func TestTokenize(t *testing.T) {
{2.3, Token{key: TokenFloat, value: []byte("2.3")}},
{2.0, Token{key: TokenFloat, value: []byte("2.")}},
{0.2, Token{key: TokenFloat, value: []byte(".2")}},
{-2.3, Token{key: TokenFloat, value: []byte("-2.3")}},
{2.3e4, Token{key: TokenFloat, value: []byte("2.3e4")}},
{2.3e-4, Token{key: TokenFloat, value: []byte("2.3e-4")}},
{2.3e+4, Token{key: TokenFloat, value: []byte("2.3E+4")}},
{2e4, Token{key: TokenFloat, value: []byte("2e4")}},
{-1e1, Token{key: TokenFloat, value: []byte("-1e1")}},
}
for _, v := range floats {
t.Run(string(v.token.value), func(t *testing.T) {
Expand Down