diff --git a/ChangeLog b/ChangeLog index 738e08e63d..0e61d7ccc6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +Mon Jul 20 19:00:58 2009 Nobuyoshi Nakada + + * compile.c (compile_dstr_fragments): reduced needless literal. + + * parse.y (xstring, regexp, dsym, literal_concat, evstr2dstr): + literal at the top of dstr is no longer needed if it is empty, + since concatstrings and toregexp always create new strings. + Mon Jul 20 12:51:39 2009 wanabe * lib/matrix.rb (Matrix#rank): revert a part of r20859 to avoid @@ -27,7 +35,7 @@ Sun Jul 19 20:41:24 2009 Tadayoshi Funaba Sun Jul 19 17:32:37 2009 Nobuyoshi Nakada - * io.c (io_read): should taint the result. [ruby-dev:38826] + * io.c (io_read): should taint the result. [ruby-dev:38826] Sun Jul 19 11:00:14 2009 Nobuyoshi Nakada diff --git a/compile.c b/compile.c index 636ad31c4c..94c157ebe0 100644 --- a/compile.c +++ b/compile.c @@ -2143,11 +2143,14 @@ compile_dstr_fragments(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int *cntp { NODE *list = node->nd_next; VALUE lit = node->nd_lit; - int cnt = 1; + int cnt = 0; debugp_param("nd_lit", lit); - hide_obj(lit); - ADD_INSN1(ret, nd_line(node), putobject, lit); + if (!NIL_P(lit)) { + hide_obj(lit); + cnt++; + ADD_INSN1(ret, nd_line(node), putobject, lit); + } while (list) { COMPILE(ret, "each string", list->nd_head); diff --git a/parse.y b/parse.y index 4636b4de30..3e4bc729b2 100644 --- a/parse.y +++ b/parse.y @@ -3806,7 +3806,7 @@ xstring : tXSTRING_BEG xstring_contents tSTRING_END nd_set_type(node, NODE_DXSTR); break; default: - node = NEW_NODE(NODE_DXSTR, STR_NEW0(), 1, NEW_LIST(node)); + node = NEW_NODE(NODE_DXSTR, Qnil, 1, NEW_LIST(node)); break; } } @@ -3835,7 +3835,7 @@ regexp : tREGEXP_BEG xstring_contents tREGEXP_END } break; default: - node = NEW_NODE(NODE_DSTR, STR_NEW0(), 1, NEW_LIST(node)); + node = NEW_NODE(NODE_DSTR, Qnil, 1, NEW_LIST(node)); case NODE_DSTR: if (options & RE_OPTION_ONCE) { nd_set_type(node, NODE_DREGX_ONCE); @@ -3844,7 +3844,7 @@ regexp : tREGEXP_BEG xstring_contents tREGEXP_END nd_set_type(node, NODE_DREGX); } node->nd_cflag = options & RE_OPTION_MASK; - reg_fragment_check(node->nd_lit, options); + if (!NIL_P(node->nd_lit)) reg_fragment_check(node->nd_lit, options); for (list = node->nd_next; list; list = list->nd_next) { if (nd_type(list->nd_head) == NODE_STR) { reg_fragment_check(list->nd_head->nd_lit, options); @@ -4081,7 +4081,7 @@ dsym : tSYMBEG xstring_contents tSTRING_END nd_set_type($$, NODE_LIT); break; default: - $$ = NEW_NODE(NODE_DSYM, STR_NEW0(), 1, NEW_LIST($$)); + $$ = NEW_NODE(NODE_DSYM, Qnil, 1, NEW_LIST($$)); break; } } @@ -7809,7 +7809,7 @@ literal_concat_gen(struct parser_params *parser, NODE *head, NODE *tail) htype = nd_type(head); if (htype == NODE_EVSTR) { - NODE *node = NEW_DSTR(STR_NEW0()); + NODE *node = NEW_DSTR(Qnil); head = list_append(node, head); } switch (nd_type(tail)) { @@ -7858,7 +7858,7 @@ static NODE * evstr2dstr_gen(struct parser_params *parser, NODE *node) { if (nd_type(node) == NODE_EVSTR) { - node = list_append(NEW_DSTR(STR_NEW0()), node); + node = list_append(NEW_DSTR(Qnil), node); } return node; } diff --git a/test/ruby/test_literal.rb b/test/ruby/test_literal.rb index e87769a6c8..52f7704940 100644 --- a/test/ruby/test_literal.rb +++ b/test/ruby/test_literal.rb @@ -59,6 +59,10 @@ class TestRubyLiteral < Test::Unit::TestCase assert_equal '16', "#{2 ** 4}" s = "string" assert_equal s, "#{s}" + a = 'Foo' + b = "#{a}" << 'Bar' + assert_equal('Foo', a, 'r3842') + assert_equal('FooBar', b, 'r3842') end def test_dsymbol