ruby-changes:3997
From: ko1@a...
Date: Fri, 15 Feb 2008 18:24:37 +0900 (JST)
Subject: [ruby-changes:3997] nobu - Ruby:r15487 (trunk): * string.c (rb_str_sub_bang, str_gsub): allows hash for replacement.
nobu 2008-02-15 18:23:55 +0900 (Fri, 15 Feb 2008) New Revision: 15487 Modified files: trunk/ChangeLog trunk/string.c Log: * string.c (rb_str_sub_bang, str_gsub): allows hash for replacement. http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/string.c?r1=15487&r2=15486&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=15487&r2=15486&diff_format=u Index: ChangeLog =================================================================== --- ChangeLog (revision 15486) +++ ChangeLog (revision 15487) @@ -1,3 +1,7 @@ +Fri Feb 15 18:23:54 2008 Nobuyoshi Nakada <nobu@r...> + + * string.c (rb_str_sub_bang, str_gsub): allows hash for replacement. + Fri Feb 15 17:12:41 2008 Yukihiro Matsumoto <matz@r...> * string.c (str_strlen): use search_nonascii() for performance. Index: string.c =================================================================== --- string.c (revision 15486) +++ string.c (revision 15487) @@ -2783,7 +2783,7 @@ static VALUE rb_str_sub_bang(int argc, VALUE *argv, VALUE str) { - VALUE pat, repl, match; + VALUE pat, repl, match, hash = Qnil; struct re_registers *regs; int iter = 0; int tainted = 0; @@ -2794,7 +2794,10 @@ } else if (argc == 2) { repl = argv[1]; - StringValue(repl); + hash = rb_check_convert_type(argv[1], T_HASH, "Hash", "to_hash"); + if (NIL_P(hash)) { + StringValue(repl); + } if (OBJ_TAINTED(repl)) tainted = 1; } else { @@ -2818,6 +2821,9 @@ str_frozen_check(str); rb_backref_set(match); } + else if (!NIL_P(hash)) { + repl = rb_hash_aref(hash, rb_str_subseq(str, BEG(0), END(0) - BEG(0))); + } else { repl = rb_reg_regsub(repl, str, regs, pat); } @@ -2904,7 +2910,7 @@ static VALUE str_gsub(int argc, VALUE *argv, VALUE str, int bang) { - VALUE pat, val, repl, match, dest; + VALUE pat, val, repl, match, dest, hash = Qnil; struct re_registers *regs; long beg, n; long offset, blen, slen, len; @@ -2920,7 +2926,10 @@ break; case 2: repl = argv[1]; - StringValue(repl); + hash = rb_check_convert_type(argv[1], T_HASH, "Hash", "to_hash"); + if (NIL_P(hash)) { + StringValue(repl); + } if (OBJ_TAINTED(repl)) tainted = 1; break; default: @@ -2956,6 +2965,9 @@ } rb_backref_set(match); } + else if (!NIL_P(hash)) { + val = rb_hash_aref(hash, rb_str_subseq(str, BEG(0), END(0) - BEG(0))); + } else { val = rb_reg_regsub(repl, str, regs, pat); } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/