mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
string.c (mrb_memsearch): move simple search from mrb_memsearch_qs()
This commit is contained in:
+30
-31
@@ -454,39 +454,23 @@ str_index_str_by_char(mrb_state *mrb, mrb_value str, mrb_value sub, mrb_int pos)
|
||||
static inline mrb_int
|
||||
mrb_memsearch_qs(const unsigned char *xs, mrb_int m, const unsigned char *ys, mrb_int n)
|
||||
{
|
||||
if (n + m < MRB_QS_SHORT_STRING_LENGTH) {
|
||||
const unsigned char *y = ys;
|
||||
const unsigned char *ye = ys+n-m+1;
|
||||
const unsigned char *y = ys;
|
||||
ptrdiff_t qstable[256];
|
||||
|
||||
for (;;) {
|
||||
y = (const unsigned char*)memchr(y, xs[0], (size_t)(ye-y));
|
||||
if (y == NULL) return -1;
|
||||
if (memcmp(xs, y, m) == 0) {
|
||||
return (mrb_int)(y - ys);
|
||||
}
|
||||
y++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
else {
|
||||
const unsigned char *y = ys;
|
||||
ptrdiff_t qstable[256];
|
||||
|
||||
/* Preprocessing */
|
||||
for (int i = 0; i < 256; i++)
|
||||
qstable[i] = m + 1;
|
||||
|
||||
const unsigned char *x = xs, *xe = xs + m;
|
||||
for (; x < xe; x++)
|
||||
qstable[*x] = xe - x;
|
||||
|
||||
/* Searching */
|
||||
for (; y + m <= ys + n; y += qstable[y[m]]) {
|
||||
if (*xs == *y && memcmp(xs, y, m) == 0)
|
||||
return (mrb_int)(y - ys);
|
||||
}
|
||||
return -1;
|
||||
/* Preprocessing */
|
||||
for (int i = 0; i < 256; i++)
|
||||
qstable[i] = m + 1;
|
||||
|
||||
const unsigned char *x = xs, *xe = xs + m;
|
||||
for (; x < xe; x++)
|
||||
qstable[*x] = xe - x;
|
||||
|
||||
/* Searching */
|
||||
for (; y + m <= ys + n; y += qstable[y[m]]) {
|
||||
if (*xs == *y && memcmp(xs, y, m) == 0)
|
||||
return (mrb_int)(y - ys);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static mrb_int
|
||||
@@ -509,6 +493,21 @@ mrb_memsearch(const void *x0, mrb_int m, const void *y0, mrb_int n)
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
if (n + m < MRB_QS_SHORT_STRING_LENGTH) {
|
||||
const unsigned char *ys = y0;
|
||||
const unsigned char *y = ys;
|
||||
const unsigned char *ye = y0+n-m+1;
|
||||
|
||||
for (;;) {
|
||||
y = (const unsigned char*)memchr(y, x[0], (size_t)(ye-ys));
|
||||
if (y == NULL) return -1;
|
||||
if (memcmp(x, y, m) == 0) {
|
||||
return (mrb_int)(y - ys);
|
||||
}
|
||||
y++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
return mrb_memsearch_qs((const unsigned char*)x0, m, (const unsigned char*)y0, n);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user