Add Musl multibyte functions

These are standard functions that are needed to help support the Skull
language. Note that normally this codebase uses libc/str/thompike.h

See #105
This commit is contained in:
Justine Tunney 2021-03-06 09:52:29 -08:00
parent cfd453d125
commit 11ec99931b
28 changed files with 1064 additions and 131 deletions

View file

@ -1,36 +1,71 @@
/*-*- 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.
Musl Libc
Copyright © 2005-2014 Rich Felker, et al.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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/errno.h"
#include "libc/limits.h"
#include "libc/macros.internal.h"
#include "libc/str/mb.internal.h"
#include "libc/str/str.h"
#include "libc/str/tpdecode.internal.h"
compatfn int mbtowc(wchar_t *wc, const char *s, size_t n) {
asm(".ident\t\"\\n\\n\
Musl libc (MIT License)\\n\
Copyright 2005-2014 Rich Felker, et. al.\"");
asm(".include \"libc/disclaimer.inc\"");
int mbtowc(wchar_t *restrict wc, const char *restrict src, size_t n) {
unsigned c;
const unsigned char *s = (const void *)src;
wchar_t dummy;
if (!s) return 0;
_Alignas(8) char alt[ROUNDUP(MB_CUR_MAX, 8)];
if (n < MB_CUR_MAX) {
memset(alt, 0, sizeof(alt));
memcpy(alt, s, n);
s = &alt[0];
if (!n) goto ilseq;
if (!wc) wc = &dummy;
if (*s < 0x80) return !!(*wc = *s);
if (MB_CUR_MAX == 1) return (*wc = CODEUNIT(*s)), 1;
if (*s - SA > SB - SA) goto ilseq;
c = kMbBittab[*s++ - SA];
/* Avoid excessive checks against n: If shifting the state n-1
* times does not clear the high bit, then the value of n is
* insufficient to read a character */
if (n < 4 && ((c << (6 * n - 6)) & (1U << 31))) goto ilseq;
if (OOB(c, *s)) goto ilseq;
c = c << 6 | *s++ - 0x80;
if (!(c & (1U << 31))) {
*wc = c;
return 2;
}
wint_t wi;
int rc = tpdecode(s, &wi);
if (wc) *wc = (wchar_t)wi;
return rc;
if (*s - 0x80u >= 0x40) goto ilseq;
c = c << 6 | *s++ - 0x80;
if (!(c & (1U << 31))) {
*wc = c;
return 3;
}
if (*s - 0x80u >= 0x40) goto ilseq;
*wc = c << 6 | *s++ - 0x80;
return 4;
ilseq:
errno = EILSEQ;
return -1;
}