summaryrefslogtreecommitdiff
path: root/re.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
commit0c90036fd14fd5aef136afccfb38e42c4d3c01e3 (patch)
treeb7b065aabe639d272c8e53defbe59daac51bbf23 /re.c
parente58e5cb5caf8aeac402ebe2db617c20beca2c0eb (diff)
* 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: svn+ssh://ci.ruby-lang.org/ruby/trunk@3431 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 're.c')
-rw-r--r--re.c55
1 files changed, 54 insertions, 1 deletions
diff --git a/re.c b/re.c
index 3ddfb30..8828119 100644
--- a/re.c
+++ b/re.c
@@ -96,6 +96,59 @@ rb_memcmp(p1, p2, len)
return rb_memcicmp(p1, p2, len);
}
+int
+rb_memsearch(x0, m, y0, n)
+ char *x0, *y0;
+ long m, n;
+{
+ unsigned char *x = x0, *y = y0;
+ unsigned char *s, *e;
+ long d, i;
+ unsigned long hx, hy;
+
+#define KR_REHASH(a, b, h) ((((h) - (a)*d) << 1) + (b))
+
+ s = y; e = s + n - m + 1;
+
+ /* Preprocessing */
+ /* computes d = 2^(m-1) with
+ the left-shift operator */
+ for (d = i = 1; i < m; ++i)
+ d = (d<<1);
+
+ if (ruby_ignorecase) {
+ /* Prepare hash value */
+ for (hy = hx = i = 0; i < m; ++i) {
+ hx = ((hx<<1) + casetable[x[i]]);
+ hy = ((hy<<1) + casetable[s[i]]);
+ }
+ /* Searching */
+ while (s < e) {
+ if (hx == hy && rb_memcicmp(x, s, m) == 0) {
+ return s-y;
+ }
+ hy = KR_REHASH(casetable[*s], casetable[*(s+m)], hy);
+ s++;
+ }
+ }
+ else {
+ /* Prepare hash value */
+ for (hy = hx = i = 0; i < m; ++i) {
+ hx = ((hx<<1) + x[i]);
+ hy = ((hy<<1) + s[i]);
+ }
+ /* Searching */
+ while (s < e) {
+ if (hx == hy && memcmp(x, s, m) == 0) {
+ return s-y;
+ }
+ hy = KR_REHASH(*s, *(s+m), hy);
+ s++;
+ }
+ }
+ return -1;
+}
+
#define REG_CASESTATE FL_USER0
#define KCODE_NONE 0
#define KCODE_EUC FL_USER1
@@ -469,7 +522,7 @@ rb_reg_kcode_m(re)
case KCODE_UTF8:
kcode = "utf8"; break;
default:
- rb_bug("unknow kcode - should not happen");
+ rb_bug("unknown kcode - should not happen");
break;
}
return rb_str_new2(kcode);