1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* string.c (rb_str_sub_bang, str_gsub): allows hash for replacement.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15487 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2008-02-15 09:23:55 +00:00
parent 8b09f7015a
commit a05337f14d
2 changed files with 20 additions and 4 deletions

View file

@ -1,3 +1,7 @@
Fri Feb 15 18:23:54 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
* string.c (rb_str_sub_bang, str_gsub): allows hash for replacement.
Fri Feb 15 17:12:41 2008 Yukihiro Matsumoto <matz@ruby-lang.org> Fri Feb 15 17:12:41 2008 Yukihiro Matsumoto <matz@ruby-lang.org>
* string.c (str_strlen): use search_nonascii() for performance. * string.c (str_strlen): use search_nonascii() for performance.

View file

@ -2783,7 +2783,7 @@ get_pat(VALUE pat, int quote)
static VALUE static VALUE
rb_str_sub_bang(int argc, VALUE *argv, VALUE str) rb_str_sub_bang(int argc, VALUE *argv, VALUE str)
{ {
VALUE pat, repl, match; VALUE pat, repl, match, hash = Qnil;
struct re_registers *regs; struct re_registers *regs;
int iter = 0; int iter = 0;
int tainted = 0; int tainted = 0;
@ -2794,7 +2794,10 @@ rb_str_sub_bang(int argc, VALUE *argv, VALUE str)
} }
else if (argc == 2) { else if (argc == 2) {
repl = argv[1]; repl = argv[1];
hash = rb_check_convert_type(argv[1], T_HASH, "Hash", "to_hash");
if (NIL_P(hash)) {
StringValue(repl); StringValue(repl);
}
if (OBJ_TAINTED(repl)) tainted = 1; if (OBJ_TAINTED(repl)) tainted = 1;
} }
else { else {
@ -2818,6 +2821,9 @@ rb_str_sub_bang(int argc, VALUE *argv, VALUE str)
str_frozen_check(str); str_frozen_check(str);
rb_backref_set(match); 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 { else {
repl = rb_reg_regsub(repl, str, regs, pat); repl = rb_reg_regsub(repl, str, regs, pat);
} }
@ -2904,7 +2910,7 @@ rb_str_sub(int argc, VALUE *argv, VALUE str)
static VALUE static VALUE
str_gsub(int argc, VALUE *argv, VALUE str, int bang) 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; struct re_registers *regs;
long beg, n; long beg, n;
long offset, blen, slen, len; long offset, blen, slen, len;
@ -2920,7 +2926,10 @@ str_gsub(int argc, VALUE *argv, VALUE str, int bang)
break; break;
case 2: case 2:
repl = argv[1]; repl = argv[1];
hash = rb_check_convert_type(argv[1], T_HASH, "Hash", "to_hash");
if (NIL_P(hash)) {
StringValue(repl); StringValue(repl);
}
if (OBJ_TAINTED(repl)) tainted = 1; if (OBJ_TAINTED(repl)) tainted = 1;
break; break;
default: default:
@ -2956,6 +2965,9 @@ str_gsub(int argc, VALUE *argv, VALUE str, int bang)
} }
rb_backref_set(match); 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 { else {
val = rb_reg_regsub(repl, str, regs, pat); val = rb_reg_regsub(repl, str, regs, pat);
} }