summaryrefslogtreecommitdiffstats
path: root/string.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-02-03 05:34:16 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-02-03 05:34:16 +0000
commite25e3851e39e64c39d9f9e2c69d03d9e6973c88e (patch)
treeb7b065aabe639d272c8e53defbe59daac51bbf23 /string.c
parentc4a05cd4cd6c1360ceee3547e8eb8c3a4430eb71 (diff)
downloadruby-e25e3851e39e64c39d9f9e2c69d03d9e6973c88e.tar.gz
ruby-e25e3851e39e64c39d9f9e2c69d03d9e6973c88e.tar.xz
ruby-e25e3851e39e64c39d9f9e2c69d03d9e6973c88e.zip
* re.c (rb_memsearch): algolithm body of String#index.
* error.c (Init_Exception): "to_str" removed. * eval.c (eval): should not rely on Exception#to_str * eval.c (compile_error): ditto. * error.c (err_append): ditto. * hash.c (rb_hash_merge): Hash#merge, non destructive "update". now there's also Hash#merge! which is an alias to "update". git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@3431 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'string.c')
-rw-r--r--string.c40
1 files changed, 3 insertions, 37 deletions
diff --git a/string.c b/string.c
index c864f1fc1..90449023c 100644
--- a/string.c
+++ b/string.c
@@ -838,48 +838,14 @@ rb_str_index(str, sub, offset)
VALUE str, sub;
long offset;
{
- char *s, *e, *p;
- long len;
- int d, hx, hy, i;
-
if (offset < 0) {
offset += RSTRING(str)->len;
if (offset < 0) return -1;
}
if (RSTRING(str)->len - offset < RSTRING(sub)->len) return -1;
- s = RSTRING(str)->ptr+offset;
- p = RSTRING(sub)->ptr;
- len = RSTRING(sub)->len;
- if (len == 0) return offset;
- e = RSTRING(str)->ptr + RSTRING(str)->len - len + 1;
-
- /* seach using Karp-Rabin algolithm described in:
-
- EXACT STRING MATCHING ALGORITHMS
- http://www-igm.univ-mlv.fr/~lecroq/string/index.html
- */
-
-#define KR_REHASH(a, b, h) ((((h) - (a)*d) << 1) + (b))
-
- /* Preprocessing */
- /* computes d = 2^(m-1) with
- the left-shift operator */
- for (d = i = 1; i < len; ++i)
- d = (d<<1);
-
- for (hy = hx = i = 0; i < len; ++i) {
- hx = ((hx<<1) + p[i]);
- hy = ((hy<<1) + s[i]);
- }
-
- /* Searching */
- while (s < e) {
- if (hx == hy && rb_memcmp(p, s, len) == 0)
- return (s-(RSTRING(str)->ptr));
- hy = KR_REHASH(*s, *(s+len), hy);
- s++;
- }
- return -1;
+ if (RSTRING(sub)->len == 0) return offset;
+ return rb_memsearch(RSTRING(sub)->ptr, RSTRING(sub)->len,
+ RSTRING(str)->ptr+offset, RSTRING(str)->len-offset);
}
static VALUE