aboutsummaryrefslogtreecommitdiffstats
path: root/src/string.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2019-12-18 20:05:18 -0500
committerDavid Robillard <d@drobilla.net>2020-06-21 18:12:03 +0200
commitedf4beae75709472bfc29a824afdd0184fc65337 (patch)
tree2bec6280a93c84536190bf76b8eb6cf6e4cd7716 /src/string.c
parent654d0cffd006573d7eedba19fbf2e627f04737b3 (diff)
downloadserd-edf4beae75709472bfc29a824afdd0184fc65337.tar.gz
serd-edf4beae75709472bfc29a824afdd0184fc65337.tar.bz2
serd-edf4beae75709472bfc29a824afdd0184fc65337.zip
Add support for parsing NaN, INF, and -INF
Diffstat (limited to 'src/string.c')
-rw-r--r--src/string.c18
1 files changed, 14 insertions, 4 deletions
diff --git a/src/string.c b/src/string.c
index 635588ae..488d0ae3 100644
--- a/src/string.c
+++ b/src/string.c
@@ -114,6 +114,19 @@ serd_strtod(const char* str, size_t* end)
{
double result = 0.0;
+#define SET_END(index) if (end) { *end = index; }
+
+ if (!strcmp(str, "NaN")) {
+ SET_END(3);
+ return NAN;
+ } else if (!strcmp(str, "-INF")) {
+ SET_END(4);
+ return -INFINITY;
+ } else if (!strcmp(str, "INF")) {
+ SET_END(3);
+ return INFINITY;
+ }
+
// Point s at the first non-whitespace character
const char* s = str;
while (is_space(*s)) { ++s; }
@@ -146,10 +159,7 @@ serd_strtod(const char* str, size_t* end)
result *= pow(10, expt * expt_sign);
}
- if (end) {
- *end = s - str;
- }
-
+ SET_END(s - str);
return result * sign;
}