From c72b0406a32ffc3f04324f7b6c321486762fca41 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sat, 16 Aug 2025 17:21:01 -0400 Subject: Improved parsing and prefix/suffix matching using a `remainder` parameter --- src/stdlib/nums.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) (limited to 'src/stdlib/nums.c') diff --git a/src/stdlib/nums.c b/src/stdlib/nums.c index 34fbb162..3775c8f4 100644 --- a/src/stdlib/nums.c +++ b/src/stdlib/nums.c @@ -98,14 +98,20 @@ public CONSTFUNC double Num$clamped(double x, double low, double high) { return (x <= low) ? low : (x >= high ? high : x); } -public OptionalNum_t Num$parse(Text_t text) { +public OptionalNum_t Num$parse(Text_t text, Text_t *remainder) { const char *str = Text$as_c_string(text); char *end = NULL; double d = strtod(str, &end); - if (end > str && end[0] == '\0') + if (end > str) { + if (remainder) + *remainder = Text$from_str(end); + else if (*end != '\0') + return nan("none"); return d; - else + } else { + if (remainder) *remainder = text; return nan("none"); + } } static bool Num$is_none(const void *n, const TypeInfo_t *info) @@ -203,14 +209,19 @@ public CONSTFUNC float Num32$clamped(float x, float low, float high) { return (x <= low) ? low : (x >= high ? high : x); } -public OptionalNum32_t Num32$parse(Text_t text) { +public OptionalNum32_t Num32$parse(Text_t text, Text_t *remainder) { const char *str = Text$as_c_string(text); char *end = NULL; double d = strtod(str, &end); - if (end > str && end[0] == '\0') + if (end > str && end[0] == '\0') { + if (remainder) *remainder = Text$from_str(end); + else if (*end != '\0') + return nan("none"); return d; - else + } else { + if (remainder) *remainder = text; return nan("none"); + } } static bool Num32$is_none(const void *n, const TypeInfo_t *info) -- cgit v1.2.3