Import C++ Standard Template Library

You can now use the hardest fastest and most dangerous language there is
with Cosmopolitan. So far about 75% of LLVM libcxx has been added. A few
breaking changes needed to be made to help this go smoothly.

- Rename nothrow to dontthrow
- Rename nodiscard to dontdiscard
- Add some libm functions, e.g. lgamma, nan, etc.
- Change intmax_t from int128 to int64 like everything else
- Introduce %jjd formatting directive for int128_t
- Introduce strtoi128(), strtou128(), etc.
- Rename bsrmax() to bsr128()

Some of the templates that should be working currently are std::vector,
std::string, std::map, std::set, std::deque, etc.
This commit is contained in:
Justine Tunney 2022-03-22 05:51:41 -07:00
parent 5022f9e920
commit 868af3f950
286 changed files with 123987 additions and 507 deletions

View file

@ -17,6 +17,7 @@ COSMOPOLITAN_C_START_
int abs(int) libcesque pureconst;
long labs(long) libcesque pureconst;
long long llabs(long long) libcesque pureconst;
intmax_t imaxabs(intmax_t) libcesque pureconst;
int atoi(const char *) paramsnonnull() libcesque;
long atol(const char *) paramsnonnull() libcesque;
long long atoll(const char *) paramsnonnull() libcesque;
@ -24,7 +25,6 @@ unsigned long strtoul(const char *, char **, int) paramsnonnull((1));
long long strtoll(const char *, char **, int) paramsnonnull((1));
unsigned long long strtoull(const char *, char **, int) paramsnonnull((1));
long long strtonum(const char *, long long, long long, const char **);
intmax_t div10(intmax_t, unsigned *) hidden;
intmax_t strtoimax(const char *, char **, int) paramsnonnull((1));
uintmax_t strtoumax(const char *, char **, int) paramsnonnull((1));
intmax_t wcstoimax(const wchar_t *, wchar_t **, int);
@ -33,18 +33,22 @@ long wcstol(const wchar_t *, wchar_t **, int);
unsigned long wcstoul(const wchar_t *, wchar_t **, int);
long strtol(const char *, char **, int) paramsnonnull((1)) libcesque;
long sizetol(const char *, long) paramsnonnull() libcesque;
long long wcstoll(const wchar_t *, wchar_t **, int);
unsigned long long wcstoull(const wchar_t *, wchar_t **, int);
int wcscoll(const wchar_t *, const wchar_t *);
size_t wcsxfrm(wchar_t *, const wchar_t *, size_t);
/*───────────────────────────────────────────────────────────────────────────│─╗
cosmopolitan § conversion » time
*/
int64_t DosDateTimeToUnix(unsigned, unsigned) nothrow;
struct timeval WindowsTimeToTimeVal(int64_t) nothrow;
struct timespec WindowsTimeToTimeSpec(int64_t) nothrow;
int64_t TimeSpecToWindowsTime(struct timespec) nothrow;
int64_t TimeValToWindowsTime(struct timeval) nothrow;
struct timeval WindowsDurationToTimeVal(int64_t) nothrow;
struct timespec WindowsDurationToTimeSpec(int64_t) nothrow;
int64_t DosDateTimeToUnix(unsigned, unsigned) dontthrow;
struct timeval WindowsTimeToTimeVal(int64_t) dontthrow;
struct timespec WindowsTimeToTimeSpec(int64_t) dontthrow;
int64_t TimeSpecToWindowsTime(struct timespec) dontthrow;
int64_t TimeValToWindowsTime(struct timeval) dontthrow;
struct timeval WindowsDurationToTimeVal(int64_t) dontthrow;
struct timespec WindowsDurationToTimeSpec(int64_t) dontthrow;
static inline struct NtFileTime MakeFileTime(int64_t x) {
return (struct NtFileTime){(uint32_t)x, (uint32_t)(x >> 32)};
@ -110,10 +114,13 @@ imaxdiv_t imaxdiv(intmax_t, intmax_t) pureconst;
#define lldiv(num, den) ((lldiv_t){(num) / (den), (num) % (den)})
#endif
#ifndef __STRICT_ANSI__
intmax_t __imaxabs(intmax_t) libcesque pureconst;
#define imaxabs(x) __imaxabs(x)
#endif /* !ANSI */
#if __GNUC__ * 100 + __GNUC_MINOR__ >= 406 || defined(__llvm__)
int128_t i128abs(int128_t) libcesque pureconst;
int128_t strtoi128(const char *, char **, int) paramsnonnull((1));
uint128_t strtou128(const char *, char **, int) paramsnonnull((1));
int128_t wcstoi128(const wchar_t *, wchar_t **, int);
uint128_t wcstou128(const wchar_t *, wchar_t **, int);
#endif /* gcc 4.6+ */
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */

View file

@ -90,7 +90,8 @@ static int __fmt_atoi(const char **str) {
* - `%ld` long (64-bit)
* - `%lu` unsigned long (64-bit)
* - `%lx` unsigned long (64-bit hexadecimal)
* - `%jd` intmax_t (128-bit)
* - `%jd` intmax_t (64-bit)
* - `%jjd` int128_t (128-bit)
*
* Width Modifiers
*
@ -291,6 +292,10 @@ hidden int __fmt(void *fn, void *arg, const char *format, va_list va) {
case 'j': /* intmax_t */
format++;
signbit = sizeof(intmax_t) * 8 - 1;
if (*format == 'j') {
format++;
signbit = sizeof(int128_t) * 8 - 1;
}
break;
case 'l':
if (format[1] == 'f' || format[1] == 'F') {

View file

@ -15,18 +15,18 @@
COSMOPOLITAN_C_START_
int snprintf(char *, size_t, const char *, ...) printfesque(3)
paramsnonnull((3)) nothrow nocallback;
paramsnonnull((3)) dontthrow nocallback;
int vsnprintf(char *, size_t, const char *, va_list)
paramsnonnull((3)) nothrow nocallback;
paramsnonnull((3)) dontthrow nocallback;
int sprintf(char *, const char *, ...) printfesque(2)
paramsnonnull((2)) nothrow nocallback frownedupon(snprintf);
paramsnonnull((2)) dontthrow nocallback frownedupon(snprintf);
int vsprintf(char *, const char *, va_list)
paramsnonnull((2)) nothrow nocallback frownedupon(vsnprintf);
paramsnonnull((2)) dontthrow nocallback frownedupon(vsnprintf);
int sscanf(const char *, const char *, ...) scanfesque(2);
int vsscanf(const char *, const char *, va_list);
int vcscanf(int (*)(void *), int (*)(int, void *), void *, const char *,
va_list);
int strerror_r(int, char *, size_t) nothrow nocallback;
int strerror_r(int, char *, size_t) dontthrow nocallback;
const char *strerror_short(int) nosideeffect;
const char *strerror_long(int) nosideeffect;
int __fmt(void *, void *, const char *, va_list) hidden;

View file

@ -1,5 +1,5 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
/*-*- 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
@ -16,8 +16,9 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/fmt/conv.h"
#include "libc/macros.internal.h"
__imaxabs:
jmp imaxabs
.endfn __imaxabs,globl
int128_t i128abs(int128_t x) {
return ABS(x);
}

View file

@ -19,6 +19,6 @@
#include "libc/fmt/conv.h"
#include "libc/macros.internal.h"
intmax_t(imaxabs)(intmax_t x) {
intmax_t imaxabs(intmax_t x) {
return ABS(x);
}

View file

@ -26,7 +26,7 @@
#define BUFFER_SIZE 144
uintmax_t __udivmodti4(uintmax_t, uintmax_t, uintmax_t *);
uint128_t __udivmodti4(uint128_t, uint128_t, uint128_t *);
static int __fmt_ntoa_format(int out(const char *, void *, size_t), void *arg,
char *buf, unsigned len, bool negative,
@ -91,9 +91,9 @@ static int __fmt_ntoa_format(int out(const char *, void *, size_t), void *arg,
}
int __fmt_ntoa2(int out(const char *, void *, size_t), void *arg,
uintmax_t value, bool neg, unsigned log2base, unsigned prec,
uint128_t value, bool neg, unsigned log2base, unsigned prec,
unsigned width, unsigned flags, const char *alphabet) {
uintmax_t remainder;
uint128_t remainder;
unsigned len, count, digit;
char buf[BUFFER_SIZE];
len = 0;
@ -132,7 +132,7 @@ int __fmt_ntoa(int out(const char *, void *, size_t), void *arg, va_list va,
unsigned long prec, unsigned long width, unsigned char flags,
const char *lang) {
bool neg;
uintmax_t value, sign;
uint128_t value, sign;
/* ignore '0' flag when prec is given */
if (flags & FLAGS_PRECISION) {

60
libc/fmt/strtoi128.c Normal file
View file

@ -0,0 +1,60 @@
/*-*- 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/errno.h"
#include "libc/fmt/conv.h"
#include "libc/fmt/strtol.internal.h"
#include "libc/limits.h"
#include "libc/str/str.h"
/**
* Decodes 128-bit signed integer from ASCII string.
*
* @param s is a non-null nul-terminated string
* @param endptr if non-null will always receive a pointer to the char
* following the last one this function processed, which is usually
* the NUL byte, or in the case of invalid strings, would point to
* the first invalid character
* @param base can be anywhere between [2,36] or 0 to auto-detect based
* on the the prefixes 0 (octal), 0x (hexadecimal), 0b (binary), or
* decimal (base 10) by default
* @return decoded saturated integer
* @see strtou128()
*/
int128_t strtoi128(const char *s, char **endptr, int base) {
char t = 0;
int d, c = *s;
int128_t x = 0;
CONSUME_SPACES(s, c);
GET_SIGN(s, c, d);
GET_RADIX(s, c, base);
if ((c = kBase36[c & 255]) && --c < base) {
if (!((t |= 1) & 2)) {
do {
if (__builtin_mul_overflow(x, base, &x) ||
__builtin_add_overflow(x, c * d, &x)) {
x = d > 0 ? INT128_MAX : INT128_MIN;
errno = ERANGE;
t |= 2;
}
} while ((c = kBase36[*++s & 255]) && --c < base);
}
}
if (t && endptr) *endptr = s;
return x;
}

View file

@ -23,7 +23,7 @@
#include "libc/str/str.h"
/**
* Decodes 128-bit signed integer from ASCII string.
* Decodes intmax_t from ASCII string.
*
* @param s is a non-null nul-terminated string
* @param endptr if non-null will always receive a pointer to the char

24
libc/fmt/strtoll_l.c Normal file
View file

@ -0,0 +1,24 @@
/*-*- 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 2022 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/fmt/conv.h"
#include "libc/unicode/locale.h"
long long strtoll_l(const char *nptr, char **endptr, int base, locale_t l) {
return strtoll(nptr, endptr, base);
}

53
libc/fmt/strtou128.c Normal file
View file

@ -0,0 +1,53 @@
/*-*- 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/fmt/conv.h"
#include "libc/fmt/strtol.internal.h"
#include "libc/str/str.h"
/**
* Decodes 128-bit unsigned integer from ASCII string.
*
* @param s is a non-null nul-terminated string
* @param endptr if non-null will always receive a pointer to the char
* following the last one this function processed, which is usually
* the NUL byte, or in the case of invalid strings, would point to
* the first invalid character
* @param base can be anywhere between [2,36] or 0 to auto-detect based
* on the the prefixes 0 (octal), 0x (hexadecimal), 0b (binary), or
* decimal (base 10) by default
* @return decoded integer mod 2¹² negated if leading `-`
* @see strtoi128()
*/
uint128_t strtou128(const char *s, char **endptr, int base) {
char t = 0;
int d, c = *s;
uint128_t x = 0;
CONSUME_SPACES(s, c);
GET_SIGN(s, c, d);
GET_RADIX(s, c, base);
if ((c = kBase36[c & 255]) && --c < base) {
t |= 1;
do {
x *= base;
x += c;
} while ((c = kBase36[*++s & 255]) && --c < base);
}
if (t && endptr) *endptr = s;
return d > 0 ? x : -x;
}

25
libc/fmt/strtoull_l.c Normal file
View file

@ -0,0 +1,25 @@
/*-*- 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 2022 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/fmt/conv.h"
#include "libc/unicode/locale.h"
unsigned long long strtoull_l(const char *nptr, char **endptr, int base,
locale_t l) {
return strtoull(nptr, endptr, base);
}

View file

@ -21,7 +21,7 @@
#include "libc/str/str.h"
/**
* Decodes 128-bit unsigned integer from ASCII string.
* Decodes uintmax_t from ASCII string.
*
* @param s is a non-null nul-terminated string
* @param endptr if non-null will always receive a pointer to the char

26
libc/fmt/swprintf.c Normal file
View file

@ -0,0 +1,26 @@
/*-*- 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 2022 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/assert.h"
#include "libc/fmt/fmt.h"
#include "libc/runtime/runtime.h"
int swprintf(wchar_t* ws, size_t n, const wchar_t* format, ...) {
assert(!"not implemented");
abort();
}

View file

@ -70,7 +70,7 @@ int vcscanf(int callback(void *), int unget(int, void *), void *arg,
}
break;
case '%': {
uintmax_t number;
uint128_t number;
void *buf;
size_t bufsize;
unsigned width = 0;
@ -117,8 +117,12 @@ int vcscanf(int callback(void *), int unget(int, void *), void *arg,
case '\'':
thousands = true;
break;
case 'j': /* 128-bit */
bits = sizeof(intmax_t) * 8;
case 'j': /* j=64-bit jj=128-bit */
if (bits < 64) {
bits = 64;
} else {
bits = 128;
}
break;
case 'l': /* long */
case 'L': /* loooong */
@ -185,7 +189,7 @@ int vcscanf(int callback(void *), int unget(int, void *), void *arg,
}
} while ((c = callback(arg)) != -1);
if (!discard) {
uintmax_t bane = (uintmax_t)1 << (bits - 1);
uint128_t bane = (uint128_t)1 << (bits - 1);
if (!(number & ~((bane - 1) | (issigned ? 0 : bane))) ||
(issigned && number == bane /* two's complement bane */)) {
++items;
@ -198,8 +202,8 @@ int vcscanf(int callback(void *), int unget(int, void *), void *arg,
}
void *out = va_arg(va, void *);
switch (bits) {
case sizeof(uintmax_t) * CHAR_BIT:
*(uintmax_t *)out = number;
case sizeof(uint128_t) * CHAR_BIT:
*(uint128_t *)out = number;
break;
case 48:
case 64:

60
libc/fmt/wcstoi128.c Normal file
View file

@ -0,0 +1,60 @@
/*-*- 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/errno.h"
#include "libc/fmt/conv.h"
#include "libc/fmt/strtol.internal.h"
#include "libc/limits.h"
#include "libc/str/str.h"
/**
* Decodes 128-bit signed integer from wide string.
*
* @param s is a non-null nul-terminated string
* @param endptr if non-null will always receive a pointer to the char
* following the last one this function processed, which is usually
* the NUL byte, or in the case of invalid strings, would point to
* the first invalid character
* @param base can be anywhere between [2,36] or 0 to auto-detect based
* on the the prefixes 0 (octal), 0x (hexadecimal), 0b (binary), or
* decimal (base 10) by default
* @return decoded saturated integer
* @see strtou128()
*/
int128_t wcstoi128(const wchar_t *s, wchar_t **endptr, int base) {
char t = 0;
int128_t x = 0;
int d, c = *s;
CONSUME_SPACES(s, c);
GET_SIGN(s, c, d);
GET_RADIX(s, c, base);
if ((c = kBase36[c & 255]) && --c < base) {
if (!((t |= 1) & 2)) {
do {
if (__builtin_mul_overflow(x, base, &x) ||
__builtin_add_overflow(x, c * d, &x)) {
x = d > 0 ? INT128_MAX : INT128_MIN;
errno = ERANGE;
t |= 2;
}
} while ((c = kBase36[*++s & 255]) && --c < base);
}
}
if (t && endptr) *endptr = s;
return x;
}

View file

@ -23,7 +23,7 @@
#include "libc/str/str.h"
/**
* Decodes 128-bit signed integer from wide string.
* Decodes intmax_t from wide string.
*
* @param s is a non-null nul-terminated string
* @param endptr if non-null will always receive a pointer to the char

View file

@ -23,7 +23,7 @@
#include "libc/str/str.h"
/**
* Decodes signed integer from wide string.
* Decodes signed long integer from wide string.
*
* @param s is a non-null nul-terminated string
* @param endptr if non-null will always receive a pointer to the char

59
libc/fmt/wcstoll.c Normal file
View file

@ -0,0 +1,59 @@
/*-*- 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/errno.h"
#include "libc/fmt/conv.h"
#include "libc/fmt/strtol.internal.h"
#include "libc/limits.h"
#include "libc/str/str.h"
/**
* Decodes signed long long integer from wide string.
*
* @param s is a non-null nul-terminated string
* @param endptr if non-null will always receive a pointer to the char
* following the last one this function processed, which is usually
* the NUL byte, or in the case of invalid strings, would point to
* the first invalid character
* @param base can be anywhere between [2,36] or 0 to auto-detect based
* on the the prefixes 0 (octal), 0x (hexadecimal), 0b (binary), or
* decimal (base 10) by default
* @return the decoded signed saturated number
*/
long long wcstoll(const wchar_t *s, wchar_t **endptr, int base) {
char t = 0;
int d, c = *s;
long long x = 0;
CONSUME_SPACES(s, c);
GET_SIGN(s, c, d);
GET_RADIX(s, c, base);
if ((c = kBase36[c & 255]) && --c < base) {
if (!((t |= 1) & 2)) {
do {
if (__builtin_mul_overflow(x, base, &x) ||
__builtin_add_overflow(x, c * d, &x)) {
x = d > 0 ? LONG_LONG_MAX : LONG_LONG_MIN;
errno = ERANGE;
t |= 2;
}
} while ((c = kBase36[*++s & 255]) && --c < base);
}
}
if (t && endptr) *endptr = s;
return x;
}

25
libc/fmt/wcstoll_l.c Normal file
View file

@ -0,0 +1,25 @@
/*-*- 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 2022 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/fmt/conv.h"
#include "libc/unicode/locale.h"
long long wcstoll_l(const wchar_t *nptr, wchar_t **endptr, int base,
locale_t l) {
return wcstoll(nptr, endptr, base);
}

53
libc/fmt/wcstou128.c Normal file
View file

@ -0,0 +1,53 @@
/*-*- 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/fmt/conv.h"
#include "libc/fmt/strtol.internal.h"
#include "libc/str/str.h"
/**
* Decodes 128-bit unsigned integer from wide string.
*
* @param s is a non-null nul-terminated string
* @param endptr if non-null will always receive a pointer to the char
* following the last one this function processed, which is usually
* the NUL byte, or in the case of invalid strings, would point to
* the first invalid character
* @param base can be anywhere between [2,36] or 0 to auto-detect based
* on the the prefixes 0 (octal), 0x (hexadecimal), 0b (binary), or
* decimal (base 10) by default
* @return decoded integer mod 2¹² negated if leading `-`
* @see strtoi128()
*/
uint128_t wcstou128(const wchar_t *s, wchar_t **endptr, int base) {
char t = 0;
int d, c = *s;
uint128_t x = 0;
CONSUME_SPACES(s, c);
GET_SIGN(s, c, d);
GET_RADIX(s, c, base);
if ((c = kBase36[c & 255]) && --c < base) {
t |= 1;
do {
x *= base;
x += c;
} while ((c = kBase36[*++s & 255]) && --c < base);
}
if (t && endptr) *endptr = s;
return d > 0 ? x : -x;
}

53
libc/fmt/wcstoull.c Normal file
View file

@ -0,0 +1,53 @@
/*-*- 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/errno.h"
#include "libc/fmt/conv.h"
#include "libc/fmt/strtol.internal.h"
#include "libc/str/str.h"
/**
* Decodes unsigned long long integer from wide string.
*
* @param s is a non-null nul-terminated string
* @param endptr if non-null will always receive a pointer to the char
* following the last one this function processed, which is usually
* the NUL byte, or in the case of invalid strings, would point to
* the first invalid character
* @param base can be anywhere between [2,36] or 0 to auto-detect based
* on the the prefixes 0 (octal), 0x (hexadecimal), 0b (binary), or
* decimal (base 10) by default
* @return decoded integer mod 2 negated if leading `-`
*/
unsigned long long wcstoull(const wchar_t *s, wchar_t **endptr, int base) {
char t = 0;
int d, c = *s;
unsigned long long x = 0;
CONSUME_SPACES(s, c);
GET_SIGN(s, c, d);
GET_RADIX(s, c, base);
if ((c = kBase36[c & 255]) && --c < base) {
t |= 1;
do {
x *= base;
x += c;
} while ((c = kBase36[*++s & 255]) && --c < base);
}
if (t && endptr) *endptr = s;
return d > 0 ? x : -x;
}

25
libc/fmt/wcstoull_l.c Normal file
View file

@ -0,0 +1,25 @@
/*-*- 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 2022 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/fmt/conv.h"
#include "libc/unicode/locale.h"
unsigned long long wcstoull_l(const wchar_t *nptr, wchar_t **endptr, int base,
locale_t l) {
return wcstoull(nptr, endptr, base);
}

View file

@ -21,7 +21,7 @@
#include "libc/str/str.h"
/**
* Decodes 128-bit unsigned integer from wide string.
* Decodes uintmax_t from wide string.
*
* @param s is a non-null nul-terminated string
* @param endptr if non-null will always receive a pointer to the char