Skip to content

Commit

Permalink
support conversion into symbols.
Browse files Browse the repository at this point in the history
  • Loading branch information
cesare committed Jan 9, 2009
1 parent dcfa36b commit a291253
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
19 changes: 19 additions & 0 deletions ext/yaml/construct.c
Expand Up @@ -115,6 +115,21 @@ static VALUE get_fixnum_by_regexp(VALUE rstring) {
return Qundef;
}

static VALUE get_symbol(VALUE rstring) {
const char* pattern = "^:.+$";
const VALUE regexp = rb_reg_new(pattern, strlen(pattern), 0);
const char* str;

if (rb_reg_match(regexp, rstring) == Qnil) {
return Qundef;
}

str = RSTRING_PTR(rstring);
str++; /* length of rstring is determined more than 2 bytes, as it matches regexp above */

return ID2SYM(rb_intern(str));
}

static VALUE construct_scalar_node(yaml_document_t* document, yaml_node_t* node) {
VALUE value;
const char* str;
Expand All @@ -133,6 +148,10 @@ static VALUE construct_scalar_node(yaml_document_t* document, yaml_node_t* node)
}

rstring = rb_str_new2(str);
value = get_symbol(rstring);
if (value != Qundef) {
return value;
}

value = get_fixnum_by_regexp(rstring);
if (value != Qundef) {
Expand Down
13 changes: 13 additions & 0 deletions spec/construct_spec.rb
Expand Up @@ -54,5 +54,18 @@
YAML::LibYAML.load(input).should == expect
end
end

{
':symbol' => :symbol,
':123' => :"123",
':test of symbols' => :"test of symbols",
':@attribute' => :"@attribute",
':@@cattr' => :"@@cattr",
':$variable' => :'$variable',
}.each do |str, expected|
it "should interpret '#{str}' as a symbol" do
YAML::LibYAML.load(str).should == expected
end
end
end
end

0 comments on commit a291253

Please sign in to comment.