clang-format libc string functions imported from musl
We have adopted these and don't consider them 'contrib' code, so bring them closer to style(9). This is a followon to r315467 and r351700. MFC after: 1 week Sponsored by: The FreeBSD Foundation
This commit is contained in:
parent
f488d5b797
commit
4874ddfd37
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=367821
@ -25,30 +25,35 @@
|
|||||||
#include <sys/cdefs.h>
|
#include <sys/cdefs.h>
|
||||||
__FBSDID("$FreeBSD$");
|
__FBSDID("$FreeBSD$");
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#define SS (sizeof(size_t))
|
#define SS (sizeof(size_t))
|
||||||
#define ALIGN (sizeof(size_t)-1)
|
#define ALIGN (sizeof(size_t) - 1)
|
||||||
#define ONES ((size_t)-1/UCHAR_MAX)
|
#define ONES ((size_t)-1 / UCHAR_MAX)
|
||||||
#define HIGHS (ONES * (UCHAR_MAX/2+1))
|
#define HIGHS (ONES * (UCHAR_MAX / 2 + 1))
|
||||||
#define HASZERO(x) (((x)-ONES) & ~(x) & HIGHS)
|
#define HASZERO(x) (((x)-ONES) & ~(x)&HIGHS)
|
||||||
|
|
||||||
void *memchr(const void *src, int c, size_t n)
|
void *
|
||||||
|
memchr(const void *src, int c, size_t n)
|
||||||
{
|
{
|
||||||
const unsigned char *s = src;
|
const unsigned char *s = src;
|
||||||
c = (unsigned char)c;
|
c = (unsigned char)c;
|
||||||
#ifdef __GNUC__
|
#ifdef __GNUC__
|
||||||
for (; ((uintptr_t)s & ALIGN) && n && *s != c; s++, n--);
|
for (; ((uintptr_t)s & ALIGN) && n && *s != c; s++, n--)
|
||||||
|
;
|
||||||
if (n && *s != c) {
|
if (n && *s != c) {
|
||||||
typedef size_t __attribute__((__may_alias__)) word;
|
typedef size_t __attribute__((__may_alias__)) word;
|
||||||
const word *w;
|
const word *w;
|
||||||
size_t k = ONES * c;
|
size_t k = ONES * c;
|
||||||
for (w = (const void *)s; n>=SS && !HASZERO(*w^k); w++, n-=SS);
|
for (w = (const void *)s; n >= SS && !HASZERO(*w ^ k);
|
||||||
|
w++, n -= SS)
|
||||||
|
;
|
||||||
s = (const void *)w;
|
s = (const void *)w;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
for (; n && *s != c; s++, n--);
|
for (; n && *s != c; s++, n--)
|
||||||
|
;
|
||||||
return n ? (void *)s : 0;
|
return n ? (void *)s : 0;
|
||||||
}
|
}
|
||||||
|
@ -25,40 +25,47 @@
|
|||||||
#include <sys/cdefs.h>
|
#include <sys/cdefs.h>
|
||||||
__FBSDID("$FreeBSD$");
|
__FBSDID("$FreeBSD$");
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
static char *twobyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
|
static char *
|
||||||
|
twobyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
|
||||||
{
|
{
|
||||||
uint16_t nw = n[0]<<8 | n[1], hw = h[0]<<8 | h[1];
|
uint16_t nw = n[0] << 8 | n[1], hw = h[0] << 8 | h[1];
|
||||||
for (h+=2, k-=2; k; k--, hw = hw<<8 | *h++)
|
for (h += 2, k -= 2; k; k--, hw = hw << 8 | *h++)
|
||||||
if (hw == nw) return (char *)h-2;
|
if (hw == nw)
|
||||||
return hw == nw ? (char *)h-2 : 0;
|
return (char *)h - 2;
|
||||||
|
return hw == nw ? (char *)h - 2 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *threebyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
|
static char *
|
||||||
|
threebyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
|
||||||
{
|
{
|
||||||
uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8;
|
uint32_t nw = n[0] << 24 | n[1] << 16 | n[2] << 8;
|
||||||
uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8;
|
uint32_t hw = h[0] << 24 | h[1] << 16 | h[2] << 8;
|
||||||
for (h+=3, k-=3; k; k--, hw = (hw|*h++)<<8)
|
for (h += 3, k -= 3; k; k--, hw = (hw | *h++) << 8)
|
||||||
if (hw == nw) return (char *)h-3;
|
if (hw == nw)
|
||||||
return hw == nw ? (char *)h-3 : 0;
|
return (char *)h - 3;
|
||||||
|
return hw == nw ? (char *)h - 3 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *fourbyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
|
static char *
|
||||||
|
fourbyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
|
||||||
{
|
{
|
||||||
uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8 | n[3];
|
uint32_t nw = n[0] << 24 | n[1] << 16 | n[2] << 8 | n[3];
|
||||||
uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8 | h[3];
|
uint32_t hw = h[0] << 24 | h[1] << 16 | h[2] << 8 | h[3];
|
||||||
for (h+=4, k-=4; k; k--, hw = hw<<8 | *h++)
|
for (h += 4, k -= 4; k; k--, hw = hw << 8 | *h++)
|
||||||
if (hw == nw) return (char *)h-4;
|
if (hw == nw)
|
||||||
return hw == nw ? (char *)h-4 : 0;
|
return (char *)h - 4;
|
||||||
|
return hw == nw ? (char *)h - 4 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define MAX(a,b) ((a)>(b)?(a):(b))
|
#define MAX(a, b) ((a) > (b) ? (a) : (b))
|
||||||
#define MIN(a,b) ((a)<(b)?(a):(b))
|
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
||||||
|
|
||||||
#define BITOP(a,b,op) \
|
#define BITOP(a, b, op) \
|
||||||
((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a))))
|
((a)[(size_t)(b) / (8 * sizeof *(a))] op \
|
||||||
|
(size_t)1 << ((size_t)(b) % (8 * sizeof *(a))))
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Two Way string search algorithm, with a bad shift table applied to the last
|
* Two Way string search algorithm, with a bad shift table applied to the last
|
||||||
@ -68,25 +75,30 @@ static char *fourbyte_memmem(const unsigned char *h, size_t k, const unsigned ch
|
|||||||
* Reference: CROCHEMORE M., PERRIN D., 1991, Two-way string-matching,
|
* Reference: CROCHEMORE M., PERRIN D., 1991, Two-way string-matching,
|
||||||
* Journal of the ACM 38(3):651-675
|
* Journal of the ACM 38(3):651-675
|
||||||
*/
|
*/
|
||||||
static char *twoway_memmem(const unsigned char *h, const unsigned char *z, const unsigned char *n, size_t l)
|
static char *
|
||||||
|
twoway_memmem(const unsigned char *h, const unsigned char *z,
|
||||||
|
const unsigned char *n, size_t l)
|
||||||
{
|
{
|
||||||
size_t i, ip, jp, k, p, ms, p0, mem, mem0;
|
size_t i, ip, jp, k, p, ms, p0, mem, mem0;
|
||||||
size_t byteset[32 / sizeof(size_t)] = { 0 };
|
size_t byteset[32 / sizeof(size_t)] = { 0 };
|
||||||
size_t shift[256];
|
size_t shift[256];
|
||||||
|
|
||||||
/* Computing length of needle and fill shift table */
|
/* Computing length of needle and fill shift table */
|
||||||
for (i=0; i<l; i++)
|
for (i = 0; i < l; i++)
|
||||||
BITOP(byteset, n[i], |=), shift[n[i]] = i+1;
|
BITOP(byteset, n[i], |=), shift[n[i]] = i + 1;
|
||||||
|
|
||||||
/* Compute maximal suffix */
|
/* Compute maximal suffix */
|
||||||
ip = -1; jp = 0; k = p = 1;
|
ip = -1;
|
||||||
while (jp+k<l) {
|
jp = 0;
|
||||||
if (n[ip+k] == n[jp+k]) {
|
k = p = 1;
|
||||||
|
while (jp + k < l) {
|
||||||
|
if (n[ip + k] == n[jp + k]) {
|
||||||
if (k == p) {
|
if (k == p) {
|
||||||
jp += p;
|
jp += p;
|
||||||
k = 1;
|
k = 1;
|
||||||
} else k++;
|
} else
|
||||||
} else if (n[ip+k] > n[jp+k]) {
|
k++;
|
||||||
|
} else if (n[ip + k] > n[jp + k]) {
|
||||||
jp += k;
|
jp += k;
|
||||||
k = 1;
|
k = 1;
|
||||||
p = jp - ip;
|
p = jp - ip;
|
||||||
@ -99,14 +111,17 @@ static char *twoway_memmem(const unsigned char *h, const unsigned char *z, const
|
|||||||
p0 = p;
|
p0 = p;
|
||||||
|
|
||||||
/* And with the opposite comparison */
|
/* And with the opposite comparison */
|
||||||
ip = -1; jp = 0; k = p = 1;
|
ip = -1;
|
||||||
while (jp+k<l) {
|
jp = 0;
|
||||||
if (n[ip+k] == n[jp+k]) {
|
k = p = 1;
|
||||||
|
while (jp + k < l) {
|
||||||
|
if (n[ip + k] == n[jp + k]) {
|
||||||
if (k == p) {
|
if (k == p) {
|
||||||
jp += p;
|
jp += p;
|
||||||
k = 1;
|
k = 1;
|
||||||
} else k++;
|
} else
|
||||||
} else if (n[ip+k] < n[jp+k]) {
|
k++;
|
||||||
|
} else if (n[ip + k] < n[jp + k]) {
|
||||||
jp += k;
|
jp += k;
|
||||||
k = 1;
|
k = 1;
|
||||||
p = jp - ip;
|
p = jp - ip;
|
||||||
@ -115,26 +130,31 @@ static char *twoway_memmem(const unsigned char *h, const unsigned char *z, const
|
|||||||
k = p = 1;
|
k = p = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (ip+1 > ms+1) ms = ip;
|
if (ip + 1 > ms + 1)
|
||||||
else p = p0;
|
ms = ip;
|
||||||
|
else
|
||||||
|
p = p0;
|
||||||
|
|
||||||
/* Periodic needle? */
|
/* Periodic needle? */
|
||||||
if (memcmp(n, n+p, ms+1)) {
|
if (memcmp(n, n + p, ms + 1)) {
|
||||||
mem0 = 0;
|
mem0 = 0;
|
||||||
p = MAX(ms, l-ms-1) + 1;
|
p = MAX(ms, l - ms - 1) + 1;
|
||||||
} else mem0 = l-p;
|
} else
|
||||||
|
mem0 = l - p;
|
||||||
mem = 0;
|
mem = 0;
|
||||||
|
|
||||||
/* Search loop */
|
/* Search loop */
|
||||||
for (;;) {
|
for (;;) {
|
||||||
/* If remainder of haystack is shorter than needle, done */
|
/* If remainder of haystack is shorter than needle, done */
|
||||||
if (z-h < l) return 0;
|
if (z - h < l)
|
||||||
|
return 0;
|
||||||
|
|
||||||
/* Check last byte first; advance by shift on mismatch */
|
/* Check last byte first; advance by shift on mismatch */
|
||||||
if (BITOP(byteset, h[l-1], &)) {
|
if (BITOP(byteset, h[l - 1], &)) {
|
||||||
k = l-shift[h[l-1]];
|
k = l - shift[h[l - 1]];
|
||||||
if (k) {
|
if (k) {
|
||||||
if (mem0 && mem && k < p) k = l-p;
|
if (mem0 && mem && k < p)
|
||||||
|
k = l - p;
|
||||||
h += k;
|
h += k;
|
||||||
mem = 0;
|
mem = 0;
|
||||||
continue;
|
continue;
|
||||||
@ -146,38 +166,49 @@ static char *twoway_memmem(const unsigned char *h, const unsigned char *z, const
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Compare right half */
|
/* Compare right half */
|
||||||
for (k=MAX(ms+1,mem); k<l && n[k] == h[k]; k++);
|
for (k = MAX(ms + 1, mem); k < l && n[k] == h[k]; k++)
|
||||||
|
;
|
||||||
if (k < l) {
|
if (k < l) {
|
||||||
h += k-ms;
|
h += k - ms;
|
||||||
mem = 0;
|
mem = 0;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
/* Compare left half */
|
/* Compare left half */
|
||||||
for (k=ms+1; k>mem && n[k-1] == h[k-1]; k--);
|
for (k = ms + 1; k > mem && n[k - 1] == h[k - 1]; k--)
|
||||||
if (k <= mem) return (char *)h;
|
;
|
||||||
|
if (k <= mem)
|
||||||
|
return (char *)h;
|
||||||
h += p;
|
h += p;
|
||||||
mem = mem0;
|
mem = mem0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void *memmem(const void *h0, size_t k, const void *n0, size_t l)
|
void *
|
||||||
|
memmem(const void *h0, size_t k, const void *n0, size_t l)
|
||||||
{
|
{
|
||||||
const unsigned char *h = h0, *n = n0;
|
const unsigned char *h = h0, *n = n0;
|
||||||
|
|
||||||
/* Return immediately on empty needle */
|
/* Return immediately on empty needle */
|
||||||
if (!l) return (void *)h;
|
if (!l)
|
||||||
|
return (void *)h;
|
||||||
|
|
||||||
/* Return immediately when needle is longer than haystack */
|
/* Return immediately when needle is longer than haystack */
|
||||||
if (k<l) return 0;
|
if (k < l)
|
||||||
|
return 0;
|
||||||
|
|
||||||
/* Use faster algorithms for short needles */
|
/* Use faster algorithms for short needles */
|
||||||
h = memchr(h0, *n, k);
|
h = memchr(h0, *n, k);
|
||||||
if (!h || l==1) return (void *)h;
|
if (!h || l == 1)
|
||||||
|
return (void *)h;
|
||||||
k -= h - (const unsigned char *)h0;
|
k -= h - (const unsigned char *)h0;
|
||||||
if (k<l) return 0;
|
if (k < l)
|
||||||
if (l==2) return twobyte_memmem(h, k, n);
|
return 0;
|
||||||
if (l==3) return threebyte_memmem(h, k, n);
|
if (l == 2)
|
||||||
if (l==4) return fourbyte_memmem(h, k, n);
|
return twobyte_memmem(h, k, n);
|
||||||
|
if (l == 3)
|
||||||
|
return threebyte_memmem(h, k, n);
|
||||||
|
if (l == 4)
|
||||||
|
return fourbyte_memmem(h, k, n);
|
||||||
|
|
||||||
return twoway_memmem(h, h+k, n, l);
|
return twoway_memmem(h, h + k, n, l);
|
||||||
}
|
}
|
||||||
|
@ -25,37 +25,44 @@
|
|||||||
#include <sys/cdefs.h>
|
#include <sys/cdefs.h>
|
||||||
__FBSDID("$FreeBSD$");
|
__FBSDID("$FreeBSD$");
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
static char *twobyte_strstr(const unsigned char *h, const unsigned char *n)
|
static char *
|
||||||
|
twobyte_strstr(const unsigned char *h, const unsigned char *n)
|
||||||
{
|
{
|
||||||
uint16_t nw = n[0]<<8 | n[1], hw = h[0]<<8 | h[1];
|
uint16_t nw = n[0] << 8 | n[1], hw = h[0] << 8 | h[1];
|
||||||
for (h++; *h && hw != nw; hw = hw<<8 | *++h);
|
for (h++; *h && hw != nw; hw = hw << 8 | *++h)
|
||||||
return *h ? (char *)h-1 : 0;
|
;
|
||||||
|
return *h ? (char *)h - 1 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *threebyte_strstr(const unsigned char *h, const unsigned char *n)
|
static char *
|
||||||
|
threebyte_strstr(const unsigned char *h, const unsigned char *n)
|
||||||
{
|
{
|
||||||
uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8;
|
uint32_t nw = n[0] << 24 | n[1] << 16 | n[2] << 8;
|
||||||
uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8;
|
uint32_t hw = h[0] << 24 | h[1] << 16 | h[2] << 8;
|
||||||
for (h+=2; *h && hw != nw; hw = (hw|*++h)<<8);
|
for (h += 2; *h && hw != nw; hw = (hw | *++h) << 8)
|
||||||
return *h ? (char *)h-2 : 0;
|
;
|
||||||
|
return *h ? (char *)h - 2 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *fourbyte_strstr(const unsigned char *h, const unsigned char *n)
|
static char *
|
||||||
|
fourbyte_strstr(const unsigned char *h, const unsigned char *n)
|
||||||
{
|
{
|
||||||
uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8 | n[3];
|
uint32_t nw = n[0] << 24 | n[1] << 16 | n[2] << 8 | n[3];
|
||||||
uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8 | h[3];
|
uint32_t hw = h[0] << 24 | h[1] << 16 | h[2] << 8 | h[3];
|
||||||
for (h+=3; *h && hw != nw; hw = hw<<8 | *++h);
|
for (h += 3; *h && hw != nw; hw = hw << 8 | *++h)
|
||||||
return *h ? (char *)h-3 : 0;
|
;
|
||||||
|
return *h ? (char *)h - 3 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define MAX(a,b) ((a)>(b)?(a):(b))
|
#define MAX(a, b) ((a) > (b) ? (a) : (b))
|
||||||
#define MIN(a,b) ((a)<(b)?(a):(b))
|
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
||||||
|
|
||||||
#define BITOP(a,b,op) \
|
#define BITOP(a, b, op) \
|
||||||
((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a))))
|
((a)[(size_t)(b) / (8 * sizeof *(a))] op \
|
||||||
|
(size_t)1 << ((size_t)(b) % (8 * sizeof *(a))))
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Two Way string search algorithm, with a bad shift table applied to the last
|
* Two Way string search algorithm, with a bad shift table applied to the last
|
||||||
@ -65,7 +72,8 @@ static char *fourbyte_strstr(const unsigned char *h, const unsigned char *n)
|
|||||||
* Reference: CROCHEMORE M., PERRIN D., 1991, Two-way string-matching,
|
* Reference: CROCHEMORE M., PERRIN D., 1991, Two-way string-matching,
|
||||||
* Journal of the ACM 38(3):651-675
|
* Journal of the ACM 38(3):651-675
|
||||||
*/
|
*/
|
||||||
static char *twoway_strstr(const unsigned char *h, const unsigned char *n)
|
static char *
|
||||||
|
twoway_strstr(const unsigned char *h, const unsigned char *n)
|
||||||
{
|
{
|
||||||
const unsigned char *z;
|
const unsigned char *z;
|
||||||
size_t l, ip, jp, k, p, ms, p0, mem, mem0;
|
size_t l, ip, jp, k, p, ms, p0, mem, mem0;
|
||||||
@ -73,19 +81,23 @@ static char *twoway_strstr(const unsigned char *h, const unsigned char *n)
|
|||||||
size_t shift[256];
|
size_t shift[256];
|
||||||
|
|
||||||
/* Computing length of needle and fill shift table */
|
/* Computing length of needle and fill shift table */
|
||||||
for (l=0; n[l] && h[l]; l++)
|
for (l = 0; n[l] && h[l]; l++)
|
||||||
BITOP(byteset, n[l], |=), shift[n[l]] = l+1;
|
BITOP(byteset, n[l], |=), shift[n[l]] = l + 1;
|
||||||
if (n[l]) return 0; /* hit the end of h */
|
if (n[l])
|
||||||
|
return 0; /* hit the end of h */
|
||||||
|
|
||||||
/* Compute maximal suffix */
|
/* Compute maximal suffix */
|
||||||
ip = -1; jp = 0; k = p = 1;
|
ip = -1;
|
||||||
while (jp+k<l) {
|
jp = 0;
|
||||||
if (n[ip+k] == n[jp+k]) {
|
k = p = 1;
|
||||||
|
while (jp + k < l) {
|
||||||
|
if (n[ip + k] == n[jp + k]) {
|
||||||
if (k == p) {
|
if (k == p) {
|
||||||
jp += p;
|
jp += p;
|
||||||
k = 1;
|
k = 1;
|
||||||
} else k++;
|
} else
|
||||||
} else if (n[ip+k] > n[jp+k]) {
|
k++;
|
||||||
|
} else if (n[ip + k] > n[jp + k]) {
|
||||||
jp += k;
|
jp += k;
|
||||||
k = 1;
|
k = 1;
|
||||||
p = jp - ip;
|
p = jp - ip;
|
||||||
@ -98,14 +110,17 @@ static char *twoway_strstr(const unsigned char *h, const unsigned char *n)
|
|||||||
p0 = p;
|
p0 = p;
|
||||||
|
|
||||||
/* And with the opposite comparison */
|
/* And with the opposite comparison */
|
||||||
ip = -1; jp = 0; k = p = 1;
|
ip = -1;
|
||||||
while (jp+k<l) {
|
jp = 0;
|
||||||
if (n[ip+k] == n[jp+k]) {
|
k = p = 1;
|
||||||
|
while (jp + k < l) {
|
||||||
|
if (n[ip + k] == n[jp + k]) {
|
||||||
if (k == p) {
|
if (k == p) {
|
||||||
jp += p;
|
jp += p;
|
||||||
k = 1;
|
k = 1;
|
||||||
} else k++;
|
} else
|
||||||
} else if (n[ip+k] < n[jp+k]) {
|
k++;
|
||||||
|
} else if (n[ip + k] < n[jp + k]) {
|
||||||
jp += k;
|
jp += k;
|
||||||
k = 1;
|
k = 1;
|
||||||
p = jp - ip;
|
p = jp - ip;
|
||||||
@ -114,14 +129,17 @@ static char *twoway_strstr(const unsigned char *h, const unsigned char *n)
|
|||||||
k = p = 1;
|
k = p = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (ip+1 > ms+1) ms = ip;
|
if (ip + 1 > ms + 1)
|
||||||
else p = p0;
|
ms = ip;
|
||||||
|
else
|
||||||
|
p = p0;
|
||||||
|
|
||||||
/* Periodic needle? */
|
/* Periodic needle? */
|
||||||
if (memcmp(n, n+p, ms+1)) {
|
if (memcmp(n, n + p, ms + 1)) {
|
||||||
mem0 = 0;
|
mem0 = 0;
|
||||||
p = MAX(ms, l-ms-1) + 1;
|
p = MAX(ms, l - ms - 1) + 1;
|
||||||
} else mem0 = l-p;
|
} else
|
||||||
|
mem0 = l - p;
|
||||||
mem = 0;
|
mem = 0;
|
||||||
|
|
||||||
/* Initialize incremental end-of-haystack pointer */
|
/* Initialize incremental end-of-haystack pointer */
|
||||||
@ -130,21 +148,24 @@ static char *twoway_strstr(const unsigned char *h, const unsigned char *n)
|
|||||||
/* Search loop */
|
/* Search loop */
|
||||||
for (;;) {
|
for (;;) {
|
||||||
/* Update incremental end-of-haystack pointer */
|
/* Update incremental end-of-haystack pointer */
|
||||||
if (z-h < l) {
|
if (z - h < l) {
|
||||||
/* Fast estimate for MIN(l,63) */
|
/* Fast estimate for MIN(l,63) */
|
||||||
size_t grow = l | 63;
|
size_t grow = l | 63;
|
||||||
const unsigned char *z2 = memchr(z, 0, grow);
|
const unsigned char *z2 = memchr(z, 0, grow);
|
||||||
if (z2) {
|
if (z2) {
|
||||||
z = z2;
|
z = z2;
|
||||||
if (z-h < l) return 0;
|
if (z - h < l)
|
||||||
} else z += grow;
|
return 0;
|
||||||
|
} else
|
||||||
|
z += grow;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check last byte first; advance by shift on mismatch */
|
/* Check last byte first; advance by shift on mismatch */
|
||||||
if (BITOP(byteset, h[l-1], &)) {
|
if (BITOP(byteset, h[l - 1], &)) {
|
||||||
k = l-shift[h[l-1]];
|
k = l - shift[h[l - 1]];
|
||||||
if (k) {
|
if (k) {
|
||||||
if (k < mem) k = mem;
|
if (k < mem)
|
||||||
|
k = mem;
|
||||||
h += k;
|
h += k;
|
||||||
mem = 0;
|
mem = 0;
|
||||||
continue;
|
continue;
|
||||||
@ -156,34 +177,46 @@ static char *twoway_strstr(const unsigned char *h, const unsigned char *n)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Compare right half */
|
/* Compare right half */
|
||||||
for (k=MAX(ms+1,mem); n[k] && n[k] == h[k]; k++);
|
for (k = MAX(ms + 1, mem); n[k] && n[k] == h[k]; k++)
|
||||||
|
;
|
||||||
if (n[k]) {
|
if (n[k]) {
|
||||||
h += k-ms;
|
h += k - ms;
|
||||||
mem = 0;
|
mem = 0;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
/* Compare left half */
|
/* Compare left half */
|
||||||
for (k=ms+1; k>mem && n[k-1] == h[k-1]; k--);
|
for (k = ms + 1; k > mem && n[k - 1] == h[k - 1]; k--)
|
||||||
if (k <= mem) return (char *)h;
|
;
|
||||||
|
if (k <= mem)
|
||||||
|
return (char *)h;
|
||||||
h += p;
|
h += p;
|
||||||
mem = mem0;
|
mem = mem0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
char *strstr(const char *h, const char *n)
|
char *
|
||||||
|
strstr(const char *h, const char *n)
|
||||||
{
|
{
|
||||||
/* Return immediately on empty needle */
|
/* Return immediately on empty needle */
|
||||||
if (!n[0]) return (char *)h;
|
if (!n[0])
|
||||||
|
return (char *)h;
|
||||||
|
|
||||||
/* Use faster algorithms for short needles */
|
/* Use faster algorithms for short needles */
|
||||||
h = strchr(h, *n);
|
h = strchr(h, *n);
|
||||||
if (!h || !n[1]) return (char *)h;
|
if (!h || !n[1])
|
||||||
if (!h[1]) return 0;
|
return (char *)h;
|
||||||
if (!n[2]) return twobyte_strstr((void *)h, (void *)n);
|
if (!h[1])
|
||||||
if (!h[2]) return 0;
|
return 0;
|
||||||
if (!n[3]) return threebyte_strstr((void *)h, (void *)n);
|
if (!n[2])
|
||||||
if (!h[3]) return 0;
|
return twobyte_strstr((void *)h, (void *)n);
|
||||||
if (!n[4]) return fourbyte_strstr((void *)h, (void *)n);
|
if (!h[2])
|
||||||
|
return 0;
|
||||||
|
if (!n[3])
|
||||||
|
return threebyte_strstr((void *)h, (void *)n);
|
||||||
|
if (!h[3])
|
||||||
|
return 0;
|
||||||
|
if (!n[4])
|
||||||
|
return fourbyte_strstr((void *)h, (void *)n);
|
||||||
|
|
||||||
return twoway_strstr((void *)h, (void *)n);
|
return twoway_strstr((void *)h, (void *)n);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user