mlibc: backport inttypes fixes

This commit is contained in:
Matheus
2026-04-24 00:26:09 -03:00
parent 7f94276b0b
commit 53635c3990
+121 -4
View File
@@ -4,13 +4,130 @@ index 8edf44b..c13a2c1 100644
+++ mlibc-workdir/options/ansi/generic/time.cpp
@@ -598,7 +598,7 @@ int nanosleep(const struct timespec *req, struct timespec *rem) {
int e = mlibc::sys_sleep(&tmp.tv_sec, &tmp.tv_nsec);
if (!e)
return 0;
if (!e)
return 0;
- else if (e == EINTR)
+ else if (e == EINTR && rem)
*rem = tmp;
*rem = tmp;
errno = e;
errno = e;
diff --git mlibc-clean/options/internal/include/mlibc/strtol.hpp mlibc-workdir/options/internal/include/mlibc/strtol.hpp
index a77d9d0..9f3fe08 100644
--- mlibc-clean/options/internal/include/mlibc/strtol.hpp
+++ mlibc-workdir/options/internal/include/mlibc/strtol.hpp
@@ -4,6 +4,7 @@
#include <type_traits>
#include <ctype.h>
#include <wctype.h>
+#include <errno.h>
#include <limits.h>
namespace mlibc {
diff --git mlibc-clean/options/ansi/generic/inttypes.cpp mlibc-workdir/options/ansi/generic/inttypes.cpp
index 6478680..1ca08f4 100644
--- mlibc-clean/options/ansi/generic/inttypes.cpp
+++ mlibc-workdir/options/ansi/generic/inttypes.cpp
@@ -1,11 +1,7 @@
-
-#include <ctype.h>
#include <inttypes.h>
-#include <string.h>
+#include <mlibc/strtol.hpp>
#include <bits/ensure.h>
#include <mlibc/debug.hpp>
-
-static const char *__mlibc_digits = "0123456789abcdefghijklmnopqrstuvwxyz";
intmax_t imaxabs(intmax_t num) {
@@ -16,83 +14,17 @@ imaxdiv_t imaxdiv(intmax_t number, intmax_t denom) {
return r;
}
-template <class T> T strtoxmax(const char *it, char **out, int base) {
- T v = 0;
- bool negate = false;
- const unsigned char *s = (const unsigned char *)it;
- int c;
-
- if(std::is_signed_v<T>) {
- if(*s == '+') {
- s++;
- }else if(*s == '-') {
- negate = true;
- s++;
- }
- }
-
- do {
- c = *s++;
- } while (isspace(c));
- if ((base == 0 || base == 16) && c == '0' && (*s == 'x' || *s == 'X')) {
- c = s[1];
- s += 2;
- base = 16;
- }
- if (base == 0)
- base = c == '0' ? 8 : 10;
-
- if(base == 8) {
- if(*it != 0)
- goto parse_digits;
- it++;
- }else if(base == 16) {
- if(*it != 0)
- goto parse_digits;
- it++;
- if(*it != 'x' && *it != 'X')
- goto parse_digits;
- it++;
- }
-
-parse_digits:
- while(*it) {
- if(isspace(*it)) {
- it++;
- continue;
- }
-
- __ensure(base <= 10); // TODO: For base > 10 we need to implement tolower().
- //auto c = strchr(__mlibc_digits, tolower(*it));
- auto c = strchr(__mlibc_digits, *it);
- if(!c || (c - __mlibc_digits) >= base)
- break;
- v = v * base + (c - __mlibc_digits);
- it++;
- }
-
- if(std::is_signed_v<T>) {
- if(negate)
- v = -v;
- }
-
- if(out)
- *out = const_cast<char *>(it);
- return v;
-}
-
-intmax_t strtoimax(const char *it, char **out, int base) {
- // TODO: This function has to check for overflow!
- return strtoxmax<intmax_t>(it, out, base);
+intmax_t strtoimax(const char *string, char **end, int base) {
+ return mlibc::stringToInteger<intmax_t, char>(string, end, base);
}
-uintmax_t strtoumax(const char *it, char **out, int base) {
- return strtoxmax<uintmax_t>(it, out, base);
+uintmax_t strtoumax(const char *string, char **end, int base) {
+ return mlibc::stringToInteger<uintmax_t, char>(string, end, base);
}
intmax_t wcstoimax(const wchar_t *__restrict, wchar_t **__restrict, int) {
__ensure(!"Not implemented");
__builtin_unreachable();
}
uintmax_t wcstoumax(const wchar_t *__restrict, wchar_t **__restrict, int) {
__ensure(!"Not implemented");
__builtin_unreachable();
}
diff --git mlibc-clean/options/ansi/generic/wchar.cpp mlibc-workdir/options/ansi/generic/wchar.cpp
index b44ebc2..9339316 100644
--- mlibc-clean/options/ansi/generic/wchar.cpp