summaryrefslogtreecommitdiff
path: root/string.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1999-07-15 07:59:59 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1999-07-15 07:59:59 +0000
commit86307f52ee1b3c5aa76e2fd6ee118e681dd76905 (patch)
tree4f5675ce901200d4245c6697d4e8d1c224949174 /string.c
parente12510c502a3c8a81aeda66d1867eb1e760ce533 (diff)
990715
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_3@497 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'string.c')
-rw-r--r--string.c35
1 files changed, 22 insertions, 13 deletions
diff --git a/string.c b/string.c
index f191bd1..b9a0918 100644
--- a/string.c
+++ b/string.c
@@ -631,29 +631,39 @@ rb_str_rindex(argc, argv, str)
switch (TYPE(sub)) {
case T_REGEXP:
- pos = rb_reg_search(sub, str, pos, 1);
- if (pos >= 0) return INT2NUM(pos);
+ if (RREGEXP(sub)->len) {
+ pos = rb_reg_search(sub, str, pos, 1);
+ }
+ if (pos >= 0) return INT2NUM(pos);
break;
case T_STRING:
+ len = RSTRING(sub)->len;
/* substring longer than string */
- if (pos < RSTRING(sub)->len) return Qnil;
+ if (RSTRING(str)->len < len) return Qnil;
+ if (RSTRING(str)->len - pos < len) {
+ pos = RSTRING(str)->len - len;
+ }
sbeg = RSTRING(str)->ptr;
- s = RSTRING(str)->ptr + pos - RSTRING(sub)->len;
+ s = RSTRING(str)->ptr + pos;
t = RSTRING(sub)->ptr;
- len = RSTRING(sub)->len;
- while (sbeg <= s) {
- if (*s == *t && memcmp(s, t, len) == 0) {
- return INT2NUM(s - RSTRING(str)->ptr);
+ if (len) {
+ while (sbeg <= s) {
+ if (*s == *t && memcmp(s, t, len) == 0) {
+ return INT2NUM(s - RSTRING(str)->ptr);
+ }
+ s--;
}
- s--;
+ }
+ else {
+ return INT2NUM(pos);
}
break;
case T_FIXNUM:
{
int c = FIX2INT(sub);
- char *p = RSTRING(str)->ptr + pos - 1;
+ char *p = RSTRING(str)->ptr + pos;
char *pbeg = RSTRING(str)->ptr;
while (pbeg <= p) {
@@ -1945,7 +1955,6 @@ rb_str_count(argc, argv, str)
VALUE *argv;
VALUE str;
{
- VALUE a1, a2;
char table[256];
char *s, *send;
int init = 1;
@@ -2165,7 +2174,7 @@ rb_str_each_line(argc, argv, str)
for (s = p, p += rslen; p < pend; p++) {
if (rslen == 0 && *p == '\n') {
- if (p[1] != '\n') continue;
+ if (*++p != '\n') continue;
while (*p == '\n') p++;
}
if (p[-1] == newline &&
@@ -2462,7 +2471,7 @@ rb_str_crypt(str, salt)
if (TYPE(salt) != T_STRING) salt = rb_str_to_str(salt);
if (RSTRING(salt)->len < 2)
- rb_raise(rb_eArgError, "salt too short(need >2 bytes)");
+ rb_raise(rb_eArgError, "salt too short(need >=2 bytes)");
return rb_str_new2(crypt(RSTRING(str)->ptr, RSTRING(salt)->ptr));
}