summaryrefslogtreecommitdiff
path: root/string.c
diff options
context:
space:
mode:
authorglass <glass@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-07-10 13:35:36 +0000
committerglass <glass@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-07-10 13:35:36 +0000
commit2a2ac6236eee7c871481819d604c3ae7132e87c3 (patch)
tree6e9927716dd5ef3ed01eb038d013790e08a67cca /string.c
parent55e60d05dce136e69981f8084505834501c96fa2 (diff)
* string.c (rb_str_index): cache single byte flag and some
cosmetic changes. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41898 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'string.c')
-rw-r--r--string.c19
1 files changed, 10 insertions, 9 deletions
diff --git a/string.c b/string.c
index 16426cd2c0..d9c06660a9 100644
--- a/string.c
+++ b/string.c
@@ -2469,26 +2469,26 @@ rb_str_casecmp(VALUE str1, VALUE str2)
static long
rb_str_index(VALUE str, VALUE sub, long offset)
{
- long pos;
char *s, *sptr, *e;
- long len, slen;
+ long pos, len, slen;
+ int single_byte = single_byte_optimizable(str);
rb_encoding *enc;
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;
+
+ len = single_byte ? RSTRING_LEN(str) : str_strlen(str, enc);
slen = str_strlen(sub, enc);
if (offset < 0) {
offset += len;
if (offset < 0) return -1;
}
if (len - offset < slen) return -1;
+
s = RSTRING_PTR(str);
- e = s + RSTRING_LEN(str);
+ e = RSTRING_END(str);
if (offset) {
- offset = str_offset(s, RSTRING_END(str), offset, enc, single_byte_optimizable(str));
+ offset = str_offset(s, e, offset, enc, single_byte);
s += offset;
}
if (slen == 0) return offset;
@@ -2502,7 +2502,8 @@ rb_str_index(VALUE str, VALUE sub, long offset)
if (pos < 0) return pos;
t = rb_enc_right_char_head(s, s+pos, e, enc);
if (t == s + pos) break;
- if ((len -= t - s) <= 0) return -1;
+ len -= t - s;
+ if (len <= 0) return -1;
offset += t - s;
s = t;
}