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_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
This commit is contained in:
nobu 2007-07-16 09:29:35 +00:00
parent c2b34e34cb
commit 6c12b69fda
2 changed files with 47 additions and 36 deletions

View file

@ -1,3 +1,8 @@
Mon Jul 16 18:29:33 2007 Nobuyoshi Nakada <nobu@ruby-lang.org>
* 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 <nobu@ruby-lang.org> Mon Jul 16 07:17:28 2007 Nobuyoshi Nakada <nobu@ruby-lang.org>
* insns.def (getspecial): lfp_svar_get() requires int for special * insns.def (getspecial): lfp_svar_get() requires int for special

View file

@ -1238,27 +1238,26 @@ rb_str_index_m(int argc, VALUE *argv, VALUE str)
pos = rb_reg_search(sub, str, pos, 0); pos = rb_reg_search(sub, str, pos, 0);
break; break;
case T_FIXNUM: case T_FIXNUM: {
{ int c = FIX2INT(sub);
int c = FIX2INT(sub); long len = RSTRING_LEN(str);
long len = RSTRING_LEN(str); char *p = RSTRING_PTR(str);
char *p = RSTRING_PTR(str);
for (;pos<len;pos++) { for (;pos<len;pos++) {
if ((unsigned char)p[pos] == c) return LONG2NUM(pos); if ((unsigned char)p[pos] == c) return LONG2NUM(pos);
} }
return Qnil; return Qnil;
} }
default: { default: {
VALUE tmp; VALUE tmp;
tmp = rb_check_string_type(sub); tmp = rb_check_string_type(sub);
if (NIL_P(tmp)) { if (NIL_P(tmp)) {
rb_raise(rb_eTypeError, "type mismatch: %s given", rb_raise(rb_eTypeError, "type mismatch: %s given",
rb_obj_classname(sub)); rb_obj_classname(sub));
} }
sub = tmp; sub = tmp;
} }
/* fall through */ /* fall through */
case T_STRING: case T_STRING:
@ -1351,32 +1350,38 @@ rb_str_rindex_m(int argc, VALUE *argv, VALUE str)
if (pos >= 0) return LONG2NUM(pos); if (pos >= 0) return LONG2NUM(pos);
break; 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: case T_STRING:
pos = rb_str_rindex(str, sub, pos); pos = rb_str_rindex(str, sub, pos);
if (pos >= 0) return LONG2NUM(pos); if (pos >= 0) return LONG2NUM(pos);
break; break;
case T_FIXNUM: case T_FIXNUM: {
{ int c = FIX2INT(sub);
int c = FIX2INT(sub); char *p = RSTRING_PTR(str) + pos;
char *p = RSTRING_PTR(str) + pos; char *pbeg = RSTRING_PTR(str);
char *pbeg = RSTRING_PTR(str);
if (pos == RSTRING_LEN(str)) { if (pos == RSTRING_LEN(str)) {
if (pos == 0) return Qnil; if (pos == 0) return Qnil;
--p; --p;
} }
while (pbeg <= p) { while (pbeg <= p) {
if ((unsigned char)*p == c) if ((unsigned char)*p == c)
return LONG2NUM((char*)p - RSTRING_PTR(str)); return LONG2NUM((char*)p - RSTRING_PTR(str));
p--; p--;
} }
return Qnil; return Qnil;
} }
default:
rb_raise(rb_eTypeError, "type mismatch: %s given",
rb_obj_classname(sub));
} }
return Qnil; return Qnil;
} }
@ -4223,7 +4228,7 @@ rb_str_crypt(VALUE str, VALUE salt)
VALUE VALUE
rb_str_intern(VALUE s) rb_str_intern(VALUE s)
{ {
volatile VALUE str = s; VALUE str = RB_GC_GUARD(s);
ID id; ID id;
if (OBJ_TAINTED(str) && rb_safe_level() >= 1) { if (OBJ_TAINTED(str) && rb_safe_level() >= 1) {
@ -4860,6 +4865,7 @@ rb_to_id(VALUE name)
RSTRING_PTR(rb_inspect(name))); RSTRING_PTR(rb_inspect(name)));
} }
name = tmp; name = tmp;
/* fall through */
case T_STRING: case T_STRING:
name = rb_str_intern(name); name = rb_str_intern(name);
/* fall through */ /* fall through */