musl - musl - an implementation of the standard library for Linux-based systems

index : musl
musl - an implementation of the standard library for Linux-based systems
summary refs log tree commit diff
path: root/src/string/strstr.c
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2018年11月08日 15:00:02 -0500
committerRich Felker <dalias@aerifal.cx>2018年11月08日 15:22:22 -0500
commit122d67f846cb0be2c9e1c3880db9eb9545bbe38c (patch)
treea219e7810b20690c12a6ab14e47f9c57b03ac7b9 /src/string/strstr.c
parent04e18b61dfde85e34ddea15e4a7d49f24c47bb73 (diff)
downloadmusl-122d67f846cb0be2c9e1c3880db9eb9545bbe38c.tar.gz
optimize two-way strstr and memmem bad character shift
first, the condition (mem && k < p) is redundant, because mem being nonzero implies the needle is periodic with period exactly p, in which case any byte that appears in the needle must appear in the last p bytes of the needle, bounding the shift (k) by p. second, the whole point of replacing the shift k by mem (=l-p) is to prevent shifting by less than mem when discarding the memory on shift, in which case linear time could not be guaranteed. but as written, the check also replaced shifts greater than mem by mem, reducing the benefit of the shift. there is no possible benefit to this reduction of the shift; since mem is being cleared, the full shift is valid and more optimal. so only replace the shift by mem when it would be less than mem.
Diffstat (limited to 'src/string/strstr.c')
-rw-r--r--src/string/strstr.c 2
1 files changed, 1 insertions, 1 deletions
diff --git a/src/string/strstr.c b/src/string/strstr.c
index c7d66682..55ba1c7b 100644
--- a/src/string/strstr.c
+++ b/src/string/strstr.c
@@ -109,7 +109,7 @@ static char *twoway_strstr(const unsigned char *h, const unsigned char *n)
if (BITOP(byteset, h[l-1], &)) {
k = l-shift[h[l-1]];
if (k) {
- if (mem && k < p) k = l-p;
+ if (k < mem) k = mem;
h += k;
mem = 0;
continue;
generated by cgit v1.2.1 (git 2.18.0) at 2025年09月05日 23:51:56 +0000

AltStyle によって変換されたページ (->オリジナル) /