aboutsummaryrefslogtreecommitdiffstats
path: root/src/string.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2021-02-25 16:16:54 -0500
committerDavid Robillard <d@drobilla.net>2021-03-08 23:23:05 -0500
commitfc2e2b667d82f6114e339e542edd8e2ca708dc1b (patch)
treeb70f70fbb932e2f035c02a6ef0b7a1aeceeca36e /src/string.c
parentc4821c8e6bf1f81c6ea31e11ebc0fc1666e9337b (diff)
downloadserd-fc2e2b667d82f6114e339e542edd8e2ca708dc1b.tar.gz
serd-fc2e2b667d82f6114e339e542edd8e2ca708dc1b.tar.bz2
serd-fc2e2b667d82f6114e339e542edd8e2ca708dc1b.zip
WIP: Use exess for reading and writing numeric and binary literals
Diffstat (limited to 'src/string.c')
-rw-r--r--src/string.c63
1 files changed, 7 insertions, 56 deletions
diff --git a/src/string.c b/src/string.c
index d2ccea14..00b1e91a 100644
--- a/src/string.c
+++ b/src/string.c
@@ -16,6 +16,7 @@
#include "string_utils.h"
+#include "exess/exess.h"
#include "serd/serd.h"
#include <assert.h>
@@ -105,64 +106,14 @@ serd_strlen(const char* str, SerdNodeFlags* flags)
return strlen(str);
}
-static inline double
-read_sign(const char** sptr)
-{
- double sign = 1.0;
- switch (**sptr) {
- case '-':
- sign = -1.0;
- // fallthru
- case '+':
- ++(*sptr);
- // fallthru
- default:
- return sign;
- }
-}
-
double
-serd_strtod(const char* str, char** endptr)
+serd_strtod(const char* const str, const char** end)
{
- double result = 0.0;
-
- // Point s at the first non-whitespace character
- const char* s = str;
- while (is_space(*s)) {
- ++s;
- }
-
- // Read leading sign if necessary
- const double sign = read_sign(&s);
-
- // Parse integer part
- for (; is_digit(*s); ++s) {
- result = (result * 10.0) + (*s - '0');
- }
-
- // Parse fractional part
- if (*s == '.') {
- double denom = 10.0;
- for (++s; is_digit(*s); ++s) {
- result += (*s - '0') / denom;
- denom *= 10.0;
- }
- }
-
- // Parse exponent
- if (*s == 'e' || *s == 'E') {
- ++s;
- double expt = 0.0;
- double expt_sign = read_sign(&s);
- for (; is_digit(*s); ++s) {
- expt = (expt * 10.0) + (*s - '0');
- }
- result *= pow(10, expt * expt_sign);
- }
-
- if (endptr) {
- *endptr = (char*)s;
+ double value = (double)NAN;
+ const ExessResult r = exess_read_double(&value, str);
+ if (end) {
+ *end = str + r.count;
}
- return result * sign;
+ return r.status ? (double)NAN : value;
}