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>2023-12-02 18:49:06 -0500
commita970f06aba98736223214a6fa995f4e82acd7132 (patch)
treefc243e4bf26f4c1f95df6f62abdd6740d87d8afd /src/string.c
parent44feb2724a8fe34992999867f5b6468228b6fc01 (diff)
downloadserd-a970f06aba98736223214a6fa995f4e82acd7132.tar.gz
serd-a970f06aba98736223214a6fa995f4e82acd7132.tar.bz2
serd-a970f06aba98736223214a6fa995f4e82acd7132.zip
[WIP] 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 5281d500..ec608f25 100644
--- a/src/string.c
+++ b/src/string.c
@@ -3,6 +3,7 @@
#include "string_utils.h"
+#include "exess/exess.h"
#include "serd/memory.h"
#include "serd/node.h"
#include "serd/status.h"
@@ -115,68 +116,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;
}