From 86307f52ee1b3c5aa76e2fd6ee118e681dd76905 Mon Sep 17 00:00:00 2001 From: matz Date: Thu, 15 Jul 1999 07:59:59 +0000 Subject: 990715 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_3@497 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- string.c | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) (limited to 'string.c') diff --git a/string.c b/string.c index f191bd1f4d..b9a0918edc 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)); } -- cgit v1.2.3