mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-22 02:20:31 +00:00
Get GNU MPFR and MPC tests to pass
This change fixes more issues with our scanf() function.
This commit is contained in:
parent
63a1636e1f
commit
6ef2a471e4
37 changed files with 389 additions and 865 deletions
|
@ -131,7 +131,7 @@ imaxdiv_t imaxdiv(intmax_t, intmax_t) pureconst;
|
|||
#endif
|
||||
|
||||
#if (__GNUC__ * 100 + __GNUC_MINOR__ >= 406 || defined(__llvm__)) && \
|
||||
!defined(__STRICT_ANSI__)
|
||||
!defined(__STRICT_ANSI__) && defined(_COSMO_SOURCE)
|
||||
int128_t i128abs(int128_t)
|
||||
libcesque pureconst;
|
||||
int128_t strtoi128(const char *, char **, int) paramsnonnull((1));
|
||||
|
|
|
@ -42,11 +42,6 @@ size_t uint64toarray_radix16(uint64_t, char[hasatleast 17]);
|
|||
size_t uint64toarray_fixed16(uint64_t, char[hasatleast 17], uint8_t);
|
||||
size_t uint64toarray_radix8(uint64_t, char[hasatleast 24]);
|
||||
|
||||
#ifndef __STRICT_ANSI__
|
||||
size_t int128toarray_radix10(int128_t, char *);
|
||||
size_t uint128toarray_radix10(uint128_t, char *);
|
||||
#endif
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* _COSMO_SOURCE */
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
|
|
|
@ -1,53 +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 │
|
||||
│ │
|
||||
│ 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/itoa.h"
|
||||
#include "libc/limits.h"
|
||||
#include "libc/mem/reverse.internal.h"
|
||||
|
||||
uint128_t __udivmodti4(uint128_t, uint128_t, uint128_t *);
|
||||
|
||||
/**
|
||||
* Converts unsigned 128-bit integer to string.
|
||||
* @param a needs at least 40 bytes
|
||||
* @return bytes written w/o nul
|
||||
*/
|
||||
dontinline size_t uint128toarray_radix10(uint128_t i, char *a) {
|
||||
size_t j;
|
||||
uint128_t rem;
|
||||
j = 0;
|
||||
do {
|
||||
i = __udivmodti4(i, 10, &rem);
|
||||
a[j++] = rem + '0';
|
||||
} while (i > 0);
|
||||
a[j] = '\0';
|
||||
reverse(a, j);
|
||||
return j;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts signed 128-bit integer to string.
|
||||
* @param a needs at least 41 bytes
|
||||
* @return bytes written w/o nul
|
||||
*/
|
||||
size_t int128toarray_radix10(int128_t i, char *a) {
|
||||
if (i >= 0) return uint128toarray_radix10(i, a);
|
||||
*a++ = '-';
|
||||
return 1 + uint128toarray_radix10(-(uint128_t)i, a);
|
||||
}
|
|
@ -4,15 +4,11 @@
|
|||
#ifdef _COSMO_SOURCE
|
||||
COSMOPOLITAN_C_START_
|
||||
|
||||
#define sleb64 __sleb64
|
||||
#define zleb64 __zleb64
|
||||
#define uleb64 __uleb64
|
||||
#define unzleb64 __unzleb64
|
||||
#define unuleb64 __unuleb64
|
||||
#define sleb128 __sleb128
|
||||
#define zleb128 __zleb128
|
||||
#define uleb128 __uleb128
|
||||
#define unsleb128 __unsleb128
|
||||
#define sleb64 __sleb64
|
||||
#define zleb64 __zleb64
|
||||
#define uleb64 __uleb64
|
||||
#define unzleb64 __unzleb64
|
||||
#define unuleb64 __unuleb64
|
||||
|
||||
char *sleb64(char *, int64_t);
|
||||
char *zleb64(char[hasatleast 10], int64_t);
|
||||
|
@ -20,13 +16,6 @@ char *uleb64(char[hasatleast 10], uint64_t);
|
|||
int unzleb64(const char *, size_t, int64_t *);
|
||||
int unuleb64(char *, size_t, uint64_t *);
|
||||
|
||||
#ifndef __STRICT_ANSI__
|
||||
char *sleb128(char *, int128_t);
|
||||
char *zleb128(char *, int128_t);
|
||||
char *uleb128(char *, uint128_t);
|
||||
int unsleb128(const void *, size_t, int128_t *);
|
||||
#endif /* __STRICT_ANSI__ */
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* _COSMO_SOURCE */
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
|
|
|
@ -1,36 +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 │
|
||||
│ │
|
||||
│ 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/leb128.h"
|
||||
|
||||
/**
|
||||
* Encodes signed leb128 integer.
|
||||
*/
|
||||
char *sleb128(char *p, int128_t x) {
|
||||
int c;
|
||||
for (;;) {
|
||||
c = x & 127;
|
||||
x >>= 7;
|
||||
if ((x == 0 && !(c & 64)) || (x == -1 && (c & 64))) {
|
||||
*p++ = c;
|
||||
return p;
|
||||
} else {
|
||||
*p++ = c | 128;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,61 +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 │
|
||||
│ │
|
||||
│ 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/stdckdint.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/str/tab.internal.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 (ckd_mul(&x, x, base) || ckd_add(&x, x, c * d)) {
|
||||
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;
|
||||
}
|
|
@ -1,54 +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 │
|
||||
│ │
|
||||
│ 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"
|
||||
#include "libc/str/tab.internal.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;
|
||||
}
|
|
@ -1,35 +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 │
|
||||
│ │
|
||||
│ 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/leb128.h"
|
||||
|
||||
/**
|
||||
* Encodes unsigned leb128 integer.
|
||||
*/
|
||||
char *uleb128(char *p, uint128_t x) {
|
||||
int c;
|
||||
for (;;) {
|
||||
c = x & 127;
|
||||
if (!(x >>= 7)) {
|
||||
*p++ = c;
|
||||
return p;
|
||||
} else {
|
||||
*p++ = c | 128;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,44 +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 │
|
||||
│ │
|
||||
│ 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/leb128.h"
|
||||
|
||||
/**
|
||||
* Decodes a GNU-style varint from a buffer.
|
||||
*
|
||||
* The GNU Assembler is able to encode numbers this way, since it's used
|
||||
* by the DWARF debug format.
|
||||
*/
|
||||
int unsleb128(const void *buf, size_t size, int128_t *out) {
|
||||
int b;
|
||||
int128_t r, w;
|
||||
unsigned char c;
|
||||
const unsigned char *p, *pe;
|
||||
pe = (p = buf) + size;
|
||||
r = b = 0;
|
||||
do {
|
||||
if (size && p == pe) return -1;
|
||||
c = *p++;
|
||||
w = c & 0x7f;
|
||||
r |= w << b;
|
||||
b += 7;
|
||||
} while (c & 0x80);
|
||||
if (c & 0x40) r |= -1ull << b;
|
||||
if (out) *out = r;
|
||||
return p - (const unsigned char *)buf;
|
||||
}
|
|
@ -1,61 +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 │
|
||||
│ │
|
||||
│ 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/stdckdint.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/str/tab.internal.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 (ckd_mul(&x, x, base) || ckd_add(&x, x, c * d)) {
|
||||
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;
|
||||
}
|
|
@ -1,54 +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 │
|
||||
│ │
|
||||
│ 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"
|
||||
#include "libc/str/tab.internal.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;
|
||||
}
|
|
@ -1,39 +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 │
|
||||
│ │
|
||||
│ 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/leb128.h"
|
||||
|
||||
/**
|
||||
* Encodes signed integer to array.
|
||||
*/
|
||||
char *zleb128(char *p, int128_t x) {
|
||||
int c;
|
||||
uint128_t u;
|
||||
u = x;
|
||||
u <<= 1;
|
||||
u ^= x >> 127;
|
||||
for (;;) {
|
||||
c = u & 127;
|
||||
if (!(u >>= 7)) {
|
||||
*p++ = c;
|
||||
return p;
|
||||
} else {
|
||||
*p++ = c | 128;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue