diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-07-16 09:29:35 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-07-16 09:29:35 +0000 |
commit | 8af410bdb530e5fc98b0703e6ad5c9d5a4145340 (patch) | |
tree | 71b0d94a290020c6bf8a87d578f50901fc0bd572 | |
parent | 12d19d3887bb9fab958f05cf4f0132bfd6e9bfe1 (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
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | string.c | 79 |
2 files changed, 47 insertions, 37 deletions
@@ -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 05:45:53 2007 Nobuyoshi Nakada <nobu@ruby-lang.org> * sprintf.c (rb_f_sprintf): more checks for format argument. @@ -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; } |