Skip to content

Commit

Permalink
Parse integers with underscores
Browse files Browse the repository at this point in the history
  • Loading branch information
k0kubun committed Aug 8, 2022
1 parent a7345b3 commit 8170ad6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/yaml.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,28 @@ void raise_parser_problem(mrb_state *mrb, yaml_parser_t *parser)
problem_str, problem_line, problem_col);
}

// Parse 123_456 as 123456
static long long
parse_yaml_int(const char *str, char *is_int)
{
*is_int = 0;
long long num = 0;
if (strlen(str) == 0) return num;

for (int i = 0; i < strlen(str); i++) {
if (0 < i && i < strlen(str) - 1 && str[i] == '_') continue;
if ('0' <= str[i] && str[i] <= '9') {
num *= 10;
num += str[i] - '0';
} else {
return num; // not integer
}
}

*is_int = 1;
return num;
}

mrb_value
node_to_value_with_aliases(mrb_state *mrb,
yaml_document_t *document, yaml_node_t *node, int use_scalar_aliases)
Expand Down Expand Up @@ -215,8 +237,9 @@ node_to_value_with_aliases(mrb_state *mrb,
}

/* Check if it is a Fixnum */
ll = strtoll(str, &endptr, 0);
if (str != endptr && *endptr == '\0')
char is_int;
ll = parse_yaml_int(str, &is_int);
if (is_int)
return mrb_fixnum_value(ll);

/* Check if it is a Float */
Expand Down
4 changes: 4 additions & 0 deletions test/yaml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@
assert_equal 5, YAML.load('5')
end

assert('YAML load fixnum with underscore') do
assert_equal 123456, YAML.load('123_456')
end

assert('YAML load fixnum as string') do
assert_equal '5', YAML.load('"5"')
end
Expand Down

0 comments on commit 8170ad6

Please sign in to comment.