From 8ff5320574e74868a613c3bcc2e640032d1578a2 Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Mon, 9 Nov 2026 08:27:59 -0400 Subject: [PATCH] MT#55283 consolidate str_to_d F-up f54da0bce Change-Id: Ib1dced9fe351b250c084669acf2566574e1baefd --- lib/str.h | 35 ++++++++++------------------------- 1 file changed, 10 insertions(+), 25 deletions(-) diff --git a/lib/str.h b/lib/str.h index 740b974b6..ebb40be78 100644 --- a/lib/str.h +++ b/lib/str.h @@ -8,6 +8,7 @@ #include #include #include +#include #include "compat.h" #include "containers.h" @@ -385,8 +386,9 @@ INLINE void str_swap(str *a, str *b) { #define MAX_LONG_LONG_LEN 21 // assume 64 bits #define MAX_LONG_LEN 21 // assume 64 bits #define MAX_INT_LEN 11 // assume 32 bits +#define MAX_DOUBLE_LEN 63 -#define str_to_x(type, name, max_len, fn, min_val, max_val) \ +#define str_to_xx(type, name, max_len, fn, min_val, min_cmp, max_val1, max_val2, ...) \ INLINE type name(const str *s, type def) { \ char *ep; \ char buf[max_len + 1]; \ @@ -398,43 +400,26 @@ INLINE void str_swap(str *a, str *b) { memcpy(buf, s->s, s->len); \ buf[s->len] = '\0'; \ errno = 0; \ - ret = fn(buf, &ep, 10); \ + ret = fn(buf, &ep, ## __VA_ARGS__); \ if (*ep != '\0') \ return def; \ - if (ret == max_val && errno == ERANGE) \ + if ((ret == max_val1 || ret == max_val2) && errno == ERANGE) \ return def; \ - if (ret == min_val && errno == ERANGE) \ + if (ret min_cmp min_val && errno == ERANGE) \ return def; \ return ret; \ } +#define str_to_x(type, name, max_len, fn, min_val, max_val) \ + str_to_xx(type, name, max_len, fn, min_val, ==, max_val, max_val, 10) + str_to_x(long long, str_to_ll, MAX_LONG_LONG_LEN, strtoll, LLONG_MIN, LLONG_MAX) str_to_x(unsigned long long, str_to_ull, MAX_LONG_LONG_LEN, strtoll, ULLONG_MAX, ULLONG_MAX) str_to_x(long, str_to_l, MAX_LONG_LEN, strtol, LONG_MIN, LONG_MAX) str_to_x(unsigned long, str_to_ul, MAX_LONG_LEN, strtol, ULONG_MAX, ULONG_MAX) str_to_x(int, str_to_i, MAX_INT_LEN, strtol, INT_MIN, INT_MAX) str_to_x(unsigned int, str_to_u, MAX_INT_LEN, strtoul, INT_MAX, INT_MAX) - -#define MAX_DOUBLE_LEN 63 - -INLINE double str_to_d(const str *s, double def) { - char *ep; - char buf[MAX_DOUBLE_LEN + 1]; - double ret; - if (s->len <= 0) - return def; - if (s->len > MAX_DOUBLE_LEN) - return def; - memcpy(buf, s->s, s->len); - buf[s->len] = '\0'; - errno = 0; - ret = strtod(buf, &ep); - if (*ep != '\0') - return def; - if (errno == ERANGE) - return def; - return ret; -} +str_to_xx(double, str_to_d, MAX_DOUBLE_LEN, strtod, DBL_MIN, <=, +HUGE_VAL, -HUGE_VAL) INLINE bool str_token(str *new_token, str *ori_and_remainder, int sep) {