mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-04 02:08:30 +00:00
Add more missing libc functionality
This commit is contained in:
parent
cf93ecbbb2
commit
a8cf0f7e89
74 changed files with 6981 additions and 105 deletions
|
@ -118,6 +118,7 @@ wchar_t *wcschr(const wchar_t *, wchar_t) strlenesque;
|
|||
wchar_t *wmemchr(const wchar_t *, wchar_t, size_t) strlenesque;
|
||||
wchar_t *wcschrnul(const wchar_t *, wchar_t) strlenesque returnsnonnull;
|
||||
char *strstr(const char *, const char *) strlenesque;
|
||||
char *strcasestr(const char *, const char *) strlenesque;
|
||||
char16_t *strstr16(const char16_t *, const char16_t *) strlenesque;
|
||||
wchar_t *wcsstr(const wchar_t *, const wchar_t *) strlenesque;
|
||||
void *rawwmemchr(const void *, wchar_t) strlenesque returnsnonnull;
|
||||
|
|
70
libc/str/strcasestr.c
Normal file
70
libc/str/strcasestr.c
Normal file
|
@ -0,0 +1,70 @@
|
|||
/*-*- 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 │
|
||||
│ │
|
||||
│ 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. │
|
||||
│ │
|
||||
│ 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. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/dce.h"
|
||||
#include "libc/intrin/asan.internal.h"
|
||||
#include "libc/str/str.h"
|
||||
|
||||
typedef char xmm_t __attribute__((__vector_size__(16), __aligned__(16)));
|
||||
|
||||
/**
|
||||
* Searches for substring case-insensitively.
|
||||
*
|
||||
* @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
|
||||
* @note this implementation goes fast in practice but isn't hardened
|
||||
* against pathological cases, and therefore shouldn't be used on
|
||||
* untrustworthy data
|
||||
* @asyncsignalsafe
|
||||
* @see strstr()
|
||||
*/
|
||||
noasan char *strcasestr(const char *haystack, const char *needle) {
|
||||
char c;
|
||||
xmm_t *p;
|
||||
size_t i;
|
||||
unsigned k, m;
|
||||
xmm_t v, n1, n2, z = {0};
|
||||
if (IsAsan()) __asan_verify(needle, 1);
|
||||
if (IsAsan()) __asan_verify(haystack, 1);
|
||||
if (haystack == needle || !*needle) return haystack;
|
||||
c = *needle;
|
||||
n1 = (xmm_t){c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c};
|
||||
c = kToLower[c & 255];
|
||||
n2 = (xmm_t){c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c};
|
||||
for (;;) {
|
||||
k = (uintptr_t)haystack & 15;
|
||||
p = (const xmm_t *)((uintptr_t)haystack & -16);
|
||||
v = *p;
|
||||
m = __builtin_ia32_pmovmskb128((v == z) | (v == n1) | (v == n2));
|
||||
m >>= k;
|
||||
m <<= k;
|
||||
while (!m) {
|
||||
v = *++p;
|
||||
m = __builtin_ia32_pmovmskb128((v == z) | (v == n1) | (v == n2));
|
||||
}
|
||||
haystack = (const char *)p + __builtin_ctzl(m);
|
||||
for (i = 0;; ++i) {
|
||||
if (!needle[i]) return (/*unconst*/ char *)haystack;
|
||||
if (!haystack[i]) break;
|
||||
if (kToLower[needle[i] & 255] != kToLower[haystack[i] & 255]) break;
|
||||
}
|
||||
if (!*haystack++) break;
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -32,6 +32,7 @@ typedef char xmm_t __attribute__((__vector_size__(16), __aligned__(16)));
|
|||
* against pathological cases, and therefore shouldn't be used on
|
||||
* untrustworthy data
|
||||
* @asyncsignalsafe
|
||||
* @see strcasestr()
|
||||
* @see memmem()
|
||||
*/
|
||||
noasan char *strstr(const char *haystack, const char *needle) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue