aboutsummaryrefslogtreecommitdiff
path: root/src/stdlib/nums.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2025-08-16 17:21:01 -0400
committerBruce Hill <bruce@bruce-hill.com>2025-08-16 17:21:01 -0400
commitc72b0406a32ffc3f04324f7b6c321486762fca41 (patch)
tree244e51c858890ea2ffb8c74a2c33c81b79de376e /src/stdlib/nums.c
parent849fd423a759edf1b58b548a6148c177a6f8cd71 (diff)
Improved parsing and prefix/suffix matching using a `remainder`
parameter
Diffstat (limited to 'src/stdlib/nums.c')
-rw-r--r--src/stdlib/nums.c23
1 files changed, 17 insertions, 6 deletions
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)