From 6c12b69fdac5b38410fb37e94f1546e3c119685e Mon Sep 17 00:00:00 2001 From: nobu Date: Mon, 16 Jul 2007 09:29:35 +0000 Subject: [PATCH] * string.c (rb_str_rindex_m): accept string-like object convertible with #to_str method, as well as rb_str_index_m. [ruby-core:11692] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12805 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 ++++ string.c | 78 ++++++++++++++++++++++++++++++------------------------- 2 files changed, 47 insertions(+), 36 deletions(-) diff --git a/ChangeLog b/ChangeLog index 5068eafe38..8a1d43fb15 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Mon Jul 16 18:29:33 2007 Nobuyoshi Nakada + + * string.c (rb_str_rindex_m): accept string-like object convertible + with #to_str method, as well as rb_str_index_m. [ruby-core:11692] + Mon Jul 16 07:17:28 2007 Nobuyoshi Nakada * insns.def (getspecial): lfp_svar_get() requires int for special diff --git a/string.c b/string.c index 32adcfd392..204e98bf88 100644 --- a/string.c +++ b/string.c @@ -1238,27 +1238,26 @@ rb_str_index_m(int argc, VALUE *argv, VALUE str) pos = rb_reg_search(sub, str, pos, 0); break; - case T_FIXNUM: - { - int c = FIX2INT(sub); - long len = RSTRING_LEN(str); - char *p = RSTRING_PTR(str); + case T_FIXNUM: { + int c = FIX2INT(sub); + long len = RSTRING_LEN(str); + char *p = RSTRING_PTR(str); - for (;pos= 0) return LONG2NUM(pos); break; + default: { + VALUE tmp; + + tmp = rb_check_string_type(sub); + if (NIL_P(tmp)) { + rb_raise(rb_eTypeError, "type mismatch: %s given", + rb_obj_classname(sub)); + } + sub = tmp; + } + /* fall through */ case T_STRING: pos = rb_str_rindex(str, sub, pos); if (pos >= 0) return LONG2NUM(pos); break; - case T_FIXNUM: - { - int c = FIX2INT(sub); - char *p = RSTRING_PTR(str) + pos; - char *pbeg = RSTRING_PTR(str); + case T_FIXNUM: { + int c = FIX2INT(sub); + char *p = RSTRING_PTR(str) + pos; + char *pbeg = RSTRING_PTR(str); - if (pos == RSTRING_LEN(str)) { - if (pos == 0) return Qnil; - --p; - } - while (pbeg <= p) { - if ((unsigned char)*p == c) - return LONG2NUM((char*)p - RSTRING_PTR(str)); - p--; - } - return Qnil; + if (pos == RSTRING_LEN(str)) { + if (pos == 0) return Qnil; + --p; + } + while (pbeg <= p) { + if ((unsigned char)*p == c) + return LONG2NUM((char*)p - RSTRING_PTR(str)); + p--; + } + return Qnil; } - - default: - rb_raise(rb_eTypeError, "type mismatch: %s given", - rb_obj_classname(sub)); } return Qnil; } @@ -4223,7 +4228,7 @@ rb_str_crypt(VALUE str, VALUE salt) VALUE rb_str_intern(VALUE s) { - volatile VALUE str = s; + VALUE str = RB_GC_GUARD(s); ID id; if (OBJ_TAINTED(str) && rb_safe_level() >= 1) { @@ -4860,6 +4865,7 @@ rb_to_id(VALUE name) RSTRING_PTR(rb_inspect(name))); } name = tmp; + /* fall through */ case T_STRING: name = rb_str_intern(name); /* fall through */