aboutsummaryrefslogtreecommitdiffstats
path: root/src/string.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2021-04-15 18:34:17 -0400
committerDavid Robillard <d@drobilla.net>2022-01-13 23:03:41 -0500
commitc90c662f85ca1a36794a0404b2101a72de020ca3 (patch)
tree9e4e8abb5a70e955f0236aa9ebeccdbcba02c33f /src/string.c
parentf074a83f20e04e360704a190ab5f4e646f4272b7 (diff)
downloadserd-c90c662f85ca1a36794a0404b2101a72de020ca3.tar.gz
serd-c90c662f85ca1a36794a0404b2101a72de020ca3.tar.bz2
serd-c90c662f85ca1a36794a0404b2101a72de020ca3.zip
Use exess for reading and writing numeric and binary literals
Diffstat (limited to 'src/string.c')
-rw-r--r--src/string.c67
1 files changed, 7 insertions, 60 deletions
diff --git a/src/string.c b/src/string.c
index 3a750c4f..c5b1d6f7 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,68 +106,14 @@ serd_strlen(const char* const str, SerdNodeFlags* const flags)
return strlen(str);
}
-static double
-read_sign(const char** const sptr)
-{
- double sign = 1.0;
-
- switch (**sptr) {
- case '-':
- sign = -1.0;
- ++(*sptr);
- break;
- case '+':
- ++(*sptr);
- break;
- default:
- break;
- }
-
- return sign;
-}
-
double
-serd_strtod(const char* const str, char** const endptr)
+serd_strtod(const char* const str, const char** const 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;
}