2020-06-15 14:18:57 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
|
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
2020-06-15 14:18:57 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2021-10-04 13:24:56 +00:00
|
|
|
#include "libc/dce.h"
|
|
|
|
#include "libc/intrin/asan.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/str/str.h"
|
|
|
|
|
2021-10-04 13:24:56 +00:00
|
|
|
typedef char xmm_t __attribute__((__vector_size__(16), __aligned__(16)));
|
|
|
|
|
2020-06-15 14:18:57 +00:00
|
|
|
/**
|
|
|
|
* Searches for substring.
|
|
|
|
*
|
|
|
|
* @param haystack is the search area, as a NUL-terminated string
|
|
|
|
* @param needle is the desired substring, also NUL-terminated
|
|
|
|
* @return pointer to first substring within haystack, or NULL
|
2021-10-04 13:24:56 +00:00
|
|
|
* @note this implementation goes fast in practice but isn't hardened
|
|
|
|
* against pathological cases, and therefore shouldn't be used on
|
|
|
|
* untrustworthy data
|
2020-06-15 14:18:57 +00:00
|
|
|
* @asyncsignalsafe
|
|
|
|
* @see memmem()
|
|
|
|
*/
|
2021-10-04 13:24:56 +00:00
|
|
|
noasan char *strstr(const char *haystack, const char *needle) {
|
|
|
|
xmm_t *p;
|
2021-02-07 14:11:44 +00:00
|
|
|
size_t i;
|
2021-10-04 13:24:56 +00:00
|
|
|
unsigned k, m;
|
|
|
|
xmm_t v, n, z = {0};
|
|
|
|
if (IsAsan()) __asan_verify(needle, 1);
|
|
|
|
if (IsAsan()) __asan_verify(haystack, 1);
|
|
|
|
if (haystack == needle || !*needle) return haystack;
|
|
|
|
n = (xmm_t){*needle, *needle, *needle, *needle, *needle, *needle,
|
|
|
|
*needle, *needle, *needle, *needle, *needle, *needle,
|
|
|
|
*needle, *needle, *needle, *needle};
|
2021-02-07 14:11:44 +00:00
|
|
|
for (;;) {
|
2021-10-04 13:24:56 +00:00
|
|
|
k = (uintptr_t)haystack & 15;
|
|
|
|
p = (const xmm_t *)((uintptr_t)haystack & -16);
|
|
|
|
v = *p;
|
2021-10-08 15:11:51 +00:00
|
|
|
m = __builtin_ia32_pmovmskb128((v == z) | (v == n));
|
2021-10-04 13:24:56 +00:00
|
|
|
m >>= k;
|
|
|
|
m <<= k;
|
|
|
|
while (!m) {
|
|
|
|
v = *++p;
|
2021-10-08 15:11:51 +00:00
|
|
|
m = __builtin_ia32_pmovmskb128((v == z) | (v == n));
|
2021-10-04 13:24:56 +00:00
|
|
|
}
|
|
|
|
haystack = (const char *)p + __builtin_ctzl(m);
|
|
|
|
for (i = 0;; ++i) {
|
2021-02-07 14:11:44 +00:00
|
|
|
if (!needle[i]) return (/*unconst*/ char *)haystack;
|
|
|
|
if (!haystack[i]) break;
|
|
|
|
if (needle[i] != haystack[i]) break;
|
|
|
|
}
|
|
|
|
if (!*haystack++) break;
|
|
|
|
}
|
2021-10-04 13:24:56 +00:00
|
|
|
return 0;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|