Skip to content

Commit

Permalink
Json fixes regarding float exponent
Browse files Browse the repository at this point in the history
  • Loading branch information
Ary Borenszweig committed Oct 20, 2014
1 parent 011cf90 commit c1df6ce
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
2 changes: 2 additions & 0 deletions spec/std/json/lexer_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,12 @@ describe "Json::Lexer" do
it_lexes_json_float "0.123", 0.123
it_lexes_json_float "1234.567", 1234.567
it_lexes_json_float "0e1", 0
it_lexes_json_float "0E1", 0
it_lexes_json_float "0.1e1", 0.1e1
it_lexes_json_float "0e+12", 0
it_lexes_json_float "0e-12", 0
it_lexes_json_float "1e2", 1e2
it_lexes_json_float "1E2", 1e2
it_lexes_json_float "1e+12", 1e12
it_lexes_json_float "1.2e-3", 1.2e-3
it_lexes_json_float "9.91343313498688", 9.91343313498688
Expand Down
14 changes: 8 additions & 6 deletions src/json/lexer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -200,19 +200,21 @@ abstract class Json::Lexer
next_char
end

if current_char == '0'
case current_char
when '0'
next_char
if current_char == '.'
case current_char
when '.'
consume_float(negative, integer)
elsif current_char == 'e'
when 'e', 'E'
consume_exponent(negative, integer.to_f64)
elsif '0' <= current_char <= '9' || current_char == 'e' || current_char == 'E'
when '0' .. '9'
unexpected_char
else
@token.type = :INT
@token.int_value = 0_i64
end
elsif '1' <= current_char <= '9'
when '1' .. '9'
integer = (current_char.ord - '0'.ord).to_i64
char = next_char
while '0' <= char <= '9'
Expand All @@ -224,7 +226,7 @@ abstract class Json::Lexer
case char
when '.'
consume_float(negative, integer)
when 'e'
when 'e', 'E'
consume_exponent(negative, integer.to_f64)
else
@token.type = :INT
Expand Down

0 comments on commit c1df6ce

Please sign in to comment.