summaryrefslogtreecommitdiff
path: root/string.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-07-16 09:29:35 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-07-16 09:29:35 +0000
commit6c12b69fdac5b38410fb37e94f1546e3c119685e (patch)
treebe1390af8f8ea365ab70e313cb75d9b0623d2a6e /string.c
parentc2b34e34cbca5d8c10f6ed9af96b9ff1d998e6a5 (diff)
* 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
Diffstat (limited to 'string.c')
-rw-r--r--string.c84
1 files changed, 45 insertions, 39 deletions
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);
-
- for (;pos<len;pos++) {
- if ((unsigned char)p[pos] == c) return LONG2NUM(pos);
- }
- return Qnil;
+ case T_FIXNUM: {
+ int c = FIX2INT(sub);
+ long len = RSTRING_LEN(str);
+ char *p = RSTRING_PTR(str);
+
+ for (;pos<len;pos++) {
+ if ((unsigned char)p[pos] == c) return LONG2NUM(pos);
+ }
+ return Qnil;
}
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;
+ 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:
@@ -1351,32 +1350,38 @@ rb_str_rindex_m(int argc, VALUE *argv, VALUE str)
if (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);
-
- 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;
- }
+ case T_FIXNUM: {
+ int c = FIX2INT(sub);
+ char *p = RSTRING_PTR(str) + pos;
+ char *pbeg = RSTRING_PTR(str);
- default:
- rb_raise(rb_eTypeError, "type mismatch: %s given",
- rb_obj_classname(sub));
+ 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;
+ }
}
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 */