summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--stage3/string.c32
-rw-r--r--stage3/string.h4
2 files changed, 36 insertions, 0 deletions
diff --git a/stage3/string.c b/stage3/string.c
index ce776ad..c97330a 100644
--- a/stage3/string.c
+++ b/stage3/string.c
@@ -1,6 +1,7 @@
#include "string.h"
#include "memory.h"
#include "heap.h"
+#include "math.h"
isize str_cmp(str s1, str s2)
{
@@ -54,6 +55,37 @@ usize str_parse_num(str s, u8 base, u64 *x)
return s.len;
}
+usize str_parse_dbl(str s, double *x)
+{
+ *x = 0.0;
+ str iter = s;
+
+ bool neg = false;
+ if (iter.len >= 1 && iter.data[0] == '-') {
+ neg = true;
+ iter = str_advance(iter, 1);
+ }
+
+ u64 tmp;
+ usize adv = str_parse_num(iter, 10, &tmp);
+ if (!adv)
+ return 0;
+
+ iter = str_advance(iter, adv);
+ *x = tmp;
+
+ if (iter.len >= 1 && iter.data[0] == '.'
+ && (adv = str_parse_num(str_advance(iter, 1), 10, &tmp))) {
+ *x += (double) tmp / (double) ipow(10, adv);
+ iter = str_advance(iter, 1+adv);
+ }
+
+ if (neg)
+ *x = -*x;
+
+ return s.len - iter.len;
+}
+
str str_walk(str *s, str sep)
{
if (s->len == 0)
diff --git a/stage3/string.h b/stage3/string.h
index 1a28494..c5ceabe 100644
--- a/stage3/string.h
+++ b/stage3/string.h
@@ -17,6 +17,10 @@ usize str_find(str s, str tokens);
// resulting number is stored in *x
usize str_parse_num(str s, u8 base, u64 *x);
+// parses a double and returns number of chars processed
+// resulting number is stored in *x
+usize str_parse_dbl(str s, double *x);
+
// this is a splitting function
// returns the next non-empty substring of *s that is delimited by the tokens in sep
// the returned string does not contain any of the separators