Skip to content

Commit

Permalink
Fix YAML core schema parses integer 0 (#5774)
Browse files Browse the repository at this point in the history
Parsing scalar `0` previously returned a string (`"0"`) instead of integer.
This fixes it by adding a special case for `0`. Also adds a few specs for zero
values (though binary, octal, hex were not broken).
  • Loading branch information
straight-shoota authored and RX14 committed Mar 5, 2018
1 parent 163c0cb commit 1b4261c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
4 changes: 4 additions & 0 deletions spec/std/yaml/schema/core_spec.cr
Expand Up @@ -94,17 +94,21 @@ describe YAML::Schema::Core do
end

# integer (base 10)
it_parses_scalar "0", 0
it_parses_scalar "123", 123
it_parses_scalar "+123", 123
it_parses_scalar "-123", -123

# integer (binary)
it_parses_scalar "0b0", 0
it_parses_scalar "0b10110", 0b10110

# integer (octal)
it_parses_scalar "00", 0
it_parses_scalar "0123", 0o123

# integer (hex)
it_parses_scalar "0x0", 0
it_parses_scalar "0x123abc", 0x123abc
it_parses_scalar "-0x123abc", -0x123abc

Expand Down
1 change: 1 addition & 0 deletions src/yaml/schema/core.cr
Expand Up @@ -84,6 +84,7 @@ module YAML::Schema::Core
value = parse_float?(string)
return value || string
when .starts_with?('0')
return 0_i64 if string.size == 1
value = string.to_i64?(base: 8, prefix: true)
return value || string
when .starts_with?('-'),
Expand Down

0 comments on commit 1b4261c

Please sign in to comment.