summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorglass <glass@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-07-31 07:53:08 +0000
committerglass <glass@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-07-31 07:53:08 +0000
commit8b126d59b3b28a5c4b6de6e106a93d1aaa3d2174 (patch)
tree3818dc7ffe6f0d76479b1de7873779f86b640826
parentfc3b9361d486f24b134af595538d4f156c5ec757 (diff)
* string.c (rb_str_rindex): refactoring and avoid to call str_nth() if
pos == 0. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42268 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog5
-rw-r--r--string.c28
2 files changed, 22 insertions, 11 deletions
diff --git a/ChangeLog b/ChangeLog
index 05c5dc2bcd..6babc2619a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Wed Jul 31 16:43:30 2013 Masaki Matsushita <glass.saga@gmail.com>
+
+ * string.c (rb_str_rindex): refactoring and avoid to call str_nth() if
+ pos == 0.
+
Wed Jul 31 14:41:36 2013 Akinori MUSHA <knu@iDaemons.org>
* lib/set.rb: [DOC] Add a couple of notes on Hash as storage.
diff --git a/string.c b/string.c
index e7f9b44324..e09852cdb9 100644
--- a/string.c
+++ b/string.c
@@ -2673,27 +2673,33 @@ rb_str_rindex(VALUE str, VALUE sub, long pos)
long len, slen;
char *s, *sbeg, *e, *t;
rb_encoding *enc;
- int singlebyte = single_byte_optimizable(str);
+ int singlebyte;
enc = rb_enc_check(str, sub);
- if (is_broken_string(sub)) {
- return -1;
- }
- len = str_strlen(str, enc);
+ if (is_broken_string(sub)) return -1;
+ singlebyte = single_byte_optimizable(str);
+ len = singlebyte ? RSTRING_LEN(str) : str_strlen(str, enc);
slen = str_strlen(sub, enc);
+
/* substring longer than string */
if (len < slen) return -1;
- if (len - pos < slen) {
- pos = len - slen;
- }
- if (len == 0) {
- return pos;
- }
+ if (len - pos < slen) pos = len - slen;
+ if (len == 0) return pos;
+
sbeg = RSTRING_PTR(str);
e = RSTRING_END(str);
t = RSTRING_PTR(sub);
slen = RSTRING_LEN(sub);
+
+ if (pos == 0) {
+ if (memcmp(sbeg, t, slen) == 0)
+ return 0;
+ else
+ return -1;
+ }
+
s = str_nth(sbeg, e, pos, enc, singlebyte);
+
while (s) {
if (memcmp(s, t, slen) == 0) {
return pos;