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
commit8af410bdb530e5fc98b0703e6ad5c9d5a4145340 (patch)
tree71b0d94a290020c6bf8a87d578f50901fc0bd572 /string.c
parent12d19d3887bb9fab958f05cf4f0132bfd6e9bfe1 (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/branches/ruby_1_8@12805 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'string.c')
-rw-r--r--string.c79
1 files changed, 42 insertions, 37 deletions
diff --git a/string.c b/string.c
index 08c958d8e4..ea87e491cb 100644
--- a/string.c
+++ b/string.c
@@ -1129,27 +1129,26 @@ rb_str_index_m(argc, argv, str)
pos = rb_reg_search(sub, str, pos, 0);
break;
- case T_FIXNUM:
- {
- int c = FIX2INT(sub);
- long len = RSTRING(str)->len;
- unsigned char *p = (unsigned char*)RSTRING(str)->ptr;
-
- for (;pos<len;pos++) {
- if (p[pos] == c) return LONG2NUM(pos);
- }
- return Qnil;
+ case T_FIXNUM: {
+ int c = FIX2INT(sub);
+ long len = RSTRING(str)->len;
+ unsigned char *p = (unsigned char*)RSTRING(str)->ptr;
+
+ for (;pos<len;pos++) {
+ if (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:
@@ -1247,31 +1246,37 @@ rb_str_rindex_m(argc, argv, 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);
- unsigned char *p = (unsigned char*)RSTRING(str)->ptr + pos;
- unsigned char *pbeg = (unsigned char*)RSTRING(str)->ptr;
-
- if (pos == RSTRING(str)->len) {
- if (pos == 0) return Qnil;
- --p;
- }
- while (pbeg <= p) {
- if (*p == c) return LONG2NUM((char*)p - RSTRING(str)->ptr);
- p--;
- }
- return Qnil;
- }
+ case T_FIXNUM: {
+ int c = FIX2INT(sub);
+ unsigned char *p = (unsigned char*)RSTRING(str)->ptr + pos;
+ unsigned char *pbeg = (unsigned char*)RSTRING(str)->ptr;
- default:
- rb_raise(rb_eTypeError, "type mismatch: %s given",
- rb_obj_classname(sub));
+ if (pos == RSTRING(str)->len) {
+ if (pos == 0) return Qnil;
+ --p;
+ }
+ while (pbeg <= p) {
+ if (*p == c) return LONG2NUM((char*)p - RSTRING(str)->ptr);
+ p--;
+ }
+ return Qnil;
+ }
}
return Qnil;
}