Make minor improvements

This commit is contained in:
Justine Tunney 2020-12-23 23:42:56 -08:00
parent 04caf6f9ad
commit 95b142e4e5
95 changed files with 3818 additions and 2760 deletions

View file

@ -25,7 +25,7 @@
* @param line is NULL-propagating
* @see getline
*/
char *(chomp)(char *line) {
char *chomp(char *line) {
size_t i;
for (i = strlen(line); i--;) {
if (line[i] == '\r' || line[i] == '\n') {

View file

@ -20,6 +20,9 @@
#include "libc/str/str.h"
#include "libc/str/utf16.h"
/**
* Helps runtime decode UTF-16 with slightly smaller code size.
*/
wint_t DecodeNtsUtf16(const char16_t **s) {
wint_t x, y;
for (;;) {

View file

@ -19,6 +19,10 @@
*/
#include "libc/str/str.h"
/**
* Converts ASCII hexadecimal character to integer case-insensitively.
* @return integer or 0 if c [0-9A-Fa-f]
*/
int hextoint(int c) {
if ('0' <= c && c <= '9') {
return c - '0';

View file

@ -19,6 +19,9 @@
*/
#include "libc/str/str.h"
/**
* Returns nonzero if c is printable ascii that isn't space.
*/
int isgraph(int c) {
return 0x21 <= c && c <= 0x7E;
}

View file

@ -19,6 +19,9 @@
*/
#include "libc/str/str.h"
/**
* Returns nonzero if c is printable ascii including space.
*/
int isprint(int c) {
return 0x20 <= c && c <= 0x7E;
}

View file

@ -19,6 +19,9 @@
*/
#include "libc/str/str.h"
/**
* Returns nonzero if c !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
*/
int ispunct(int c) {
return (0x21 <= c && c <= 0x7E) && !('0' <= c && c <= '9') &&
!('A' <= c && c <= 'Z') && !('a' <= c && c <= 'z');

View file

@ -24,7 +24,7 @@
compatfn int mbtowc(wchar_t *wc, const char *s, size_t n) {
if (!s) return 0;
alignas(8) char alt[ROUNDUP(MB_CUR_MAX, 8)];
_Alignas(8) char alt[ROUNDUP(MB_CUR_MAX, 8)];
if (n < MB_CUR_MAX) {
memset(alt, 0, sizeof(alt));
memcpy(alt, s, n);

View file

@ -233,37 +233,21 @@ int iswctype(wint_t, wctype_t) pureconst;
char *strsignal(int) returnsnonnull libcesque;
#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
/*───────────────────────────────────────────────────────────────────────────│─╗
cosmopolitan § strings » optimizations
*/
#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
#define explicit_bzero(STR, BYTES) \
do { \
void *Str; \
size_t Bytes; \
asm volatile("call\texplicit_bzero" \
: "=D"(Str), "=S"(Bytes) \
: "0"(STR), "1"(BYTES) \
: "rax", "rcx", "rdx", "r8", "r9", "r10", "r11", "memory", \
"cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5"); \
} while (0)
#define __memcpy_isgoodsize(SIZE) \
(__builtin_constant_p(SIZE) && ((SIZE) <= __BIGGEST_ALIGNMENT__ * 2 && \
__builtin_popcountl((unsigned)(SIZE)) == 1))
#ifdef UNBLOAT_STDARG
#define __STR_XMM_CLOBBER
#else
#define __STR_XMM_CLOBBER "xmm3", "xmm4",
#endif
#define __memcpy_isgoodsize(SIZE) \
(isconstant(SIZE) && ((SIZE) <= __BIGGEST_ALIGNMENT__ * 2 && \
__builtin_popcountl((unsigned)(SIZE)) == 1))
#define __memset_isgoodsize(SIZE) \
(isconstant(SIZE) && (((SIZE) <= __BIGGEST_ALIGNMENT__ && \
__builtin_popcountl((unsigned)(SIZE)) == 1) || \
((SIZE) % __BIGGEST_ALIGNMENT__ == 0 && \
(SIZE) / __BIGGEST_ALIGNMENT__ <= 3)))
#define __memset_isgoodsize(SIZE) \
(__builtin_constant_p(SIZE) && \
(((SIZE) <= __BIGGEST_ALIGNMENT__ && \
__builtin_popcountl((unsigned)(SIZE)) == 1) || \
((SIZE) % __BIGGEST_ALIGNMENT__ == 0 && \
(SIZE) / __BIGGEST_ALIGNMENT__ <= 3)))
#define memcpy(DEST, SRC, SIZE) \
(__memcpy_isgoodsize(SIZE) ? __builtin_memcpy(DEST, SRC, SIZE) \
@ -273,7 +257,18 @@ char *strsignal(int) returnsnonnull libcesque;
(__memset_isgoodsize(SIZE) ? __builtin_memset(DEST, BYTE, SIZE) \
: __memset(DEST, BYTE, SIZE))
#if defined(__STDC_HOSTED__) && (defined(__SSE2__) || defined(UNBLOAT_STDARG))
#if defined(__STDC_HOSTED__) && defined(__SSE2__)
#define strlen(STR) \
(__builtin_constant_p(STR) ? __builtin_strlen(STR) : ({ \
size_t LeN; \
const char *StR = (STR); \
asm("call\tstrlen" \
: "=a"(LeN) \
: "D"(StR), "m"(*(char(*)[0x7fffffff])StR) \
: "rcx", "rdx", "xmm3", "xmm4", "cc"); \
LeN; \
}))
#define memmove(DEST, SRC, SIZE) __memcpy("MemMove", (DEST), (SRC), (SIZE))
@ -291,7 +286,7 @@ char *strsignal(int) returnsnonnull libcesque;
asm("call\t" FN \
: "=m"(*(char(*)[SiZe])(DeSt)) \
: "D"(DeSt), "S"(SrC), "d"(SiZe), "m"(*(const char(*)[SiZe])(SrC)) \
: __STR_XMM_CLOBBER "rcx", "cc"); \
: "xmm3", "xmm4", "rcx", "cc"); \
DeSt; \
})
@ -302,11 +297,22 @@ char *strsignal(int) returnsnonnull libcesque;
asm("call\tMemSet" \
: "=m"(*(char(*)[SiZe])(DeSt)) \
: "D"(DeSt), "S"(BYTE), "d"(SiZe) \
: __STR_XMM_CLOBBER "rcx", "cc"); \
: "xmm3", "xmm4", "rcx", "cc"); \
DeSt; \
})
#else /* hosted/sse2/unbloat */
#define explicit_bzero(STR, BYTES) \
do { \
void *Str; \
size_t Bytes; \
asm volatile("call\texplicit_bzero" \
: "=D"(Str), "=S"(Bytes) \
: "0"(STR), "1"(BYTES) \
: "rax", "rcx", "rdx", "r8", "r9", "r10", "r11", "memory", \
"cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5"); \
} while (0)
#else /* hosted+sse2 */
#define mempcpy(DEST, SRC, SIZE) \
({ \
@ -347,7 +353,6 @@ char *strsignal(int) returnsnonnull libcesque;
})
#endif /* hosted/sse2/unbloat */
#endif /* __GNUC__ && !__STRICT_ANSI__ */
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */

View file

@ -18,6 +18,7 @@
02110-1301 USA
*/
#include "libc/bits/safemacros.internal.h"
#include "libc/macros.h"
#include "libc/str/str.h"
/**
@ -34,7 +35,7 @@ size_t strlcpy(char *d, const char *s, size_t n) {
size_t slen, actual;
slen = strlen(s);
if (n) {
actual = min(n, slen);
actual = MIN(n, slen);
memcpy(d, s, actual);
d[actual] = '\0';
}

View file

@ -1,50 +0,0 @@
/*-*- 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
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/assert.h"
#include "libc/intrin/pcmpeqb.h"
#include "libc/intrin/pmovmskb.h"
#include "libc/nexgen32e/bsf.h"
#include "libc/str/str.h"
/**
* Returns length of NUL-terminated string.
*
* @param s is non-null NUL-terminated string pointer
* @return number of bytes (excluding NUL)
* @asyncsignalsafe
*/
size_t strlen(const char *s) {
const char *p;
unsigned k, m;
uint8_t v1[16], vz[16];
k = (uintptr_t)s & 15;
p = (const char *)((uintptr_t)s & -16);
memset(vz, 0, 16);
memcpy(v1, p, 16);
pcmpeqb(v1, v1, vz);
m = pmovmskb(v1) >> k << k;
while (!m) {
p += 16;
memcpy(v1, p, 16);
pcmpeqb(v1, v1, vz);
m = pmovmskb(v1);
}
return p + bsf(m) - s;
}

View file

@ -24,7 +24,7 @@
static const char kSig[4] = "SIG";
static const char kUnknown[8] = "UNKNOWN";
alignas(1) static const char kStrSignals[][8] = {
_Alignas(char) static const char kStrSignals[][8] = {
"EXIT", "HUP", "INT", "QUIT", "ILL", "TRAP", "ABRT", "BUS",
"FPE", "KILL", "USR1", "SEGV", "USR2", "PIPE", "ALRM", "TERM",
"STKFLT", "CHLD", "CONT", "STOP", "TSTP", "TTIN", "TTOU", "URG",

View file

@ -3,6 +3,8 @@
#include "libc/nexgen32e/bsr.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
/* TODO(jart): DELETE? */
/**
* Generic Thompson-Pike Varint Decoder.
* @return number of bytes successfully consumed or -1 w/ errno
@ -12,11 +14,11 @@
forceinline int tpdecodecb(wint_t *out, int first,
int get(void *arg, uint32_t i), void *arg) {
uint32_t wc, cb, need, msb, j, i = 1;
if (unlikely((wc = first) == -1)) return -1;
while (unlikely((wc & 0b11000000) == 0b10000000)) {
if (__builtin_expect((wc = first) == -1, 0)) return -1;
while (__builtin_expect((wc & 0b11000000) == 0b10000000, 0)) {
if ((wc = get(arg, i++)) == -1) return -1;
}
if (unlikely(!(0 <= wc && wc <= 0x7F))) {
if (__builtin_expect(!(0 <= wc && wc <= 0x7F), 0)) {
msb = wc < 252 ? bsr(~wc & 0xff) : 1;
need = 7 - msb;
wc &= ((1u << msb) - 1) | 0b00000011;
@ -30,7 +32,7 @@ forceinline int tpdecodecb(wint_t *out, int first,
}
}
}
if (likely(out)) *out = (wint_t)wc;
if (__builtin_expect(!!out, 1)) *out = (wint_t)wc;
return i;
}

View file

@ -21,6 +21,8 @@
#include "libc/str/tpenc.h"
#include "libc/str/tpencode.internal.h"
/* TODO: DELETE */
/**
* Thompson-Pike Varint Encoder.
*

View file

@ -11,13 +11,14 @@ COSMOPOLITAN_C_START_
#define IsUcs2(wc) (((wc)&UTF16_MASK) != UTF16_MOAR)
#define IsUtf16Cont(wc) (((wc)&UTF16_MASK) == UTF16_CONT)
#define MergeUtf16(lo, hi) ((((lo)-0xD800) << 10) + ((hi)-0xDC00) + 0x10000)
#define EncodeUtf16(wc) \
(likely((0x0000 <= (wc) && (wc) <= 0xFFFF) || \
(0xE000 <= (wc) && (wc) <= 0xFFFF)) \
? (wc) \
: 0x10000 <= (wc) && (wc) <= 0x10FFFF \
? (((((wc)-0x10000) >> 10) + 0xD800) | \
((((wc)-0x10000) & 1023) + 0xDC00) << 16) \
#define EncodeUtf16(wc) \
(__builtin_expect(((0x0000 <= (wc) && (wc) <= 0xFFFF) || \
(0xE000 <= (wc) && (wc) <= 0xFFFF)), \
1) \
? (wc) \
: 0x10000 <= (wc) && (wc) <= 0x10FFFF \
? (((((wc)-0x10000) >> 10) + 0xD800) | \
((((wc)-0x10000) & 1023) + 0xDC00) << 16) \
: 0xFFFD)
COSMOPOLITAN_C_END_