add "scan helper getc" and rework strtod, etc. to use it
[musl] / src / stdlib / strtod.c
1 #include <stdlib.h>
2 #include "shgetc.h"
3 #include "floatscan.h"
4 #include "stdio_impl.h"
5
6 static long double strtox(const char *s, char **p, int prec)
7 {
8         char *t = (char *)s;
9         while (isspace(*t)) t++;
10         FILE f = {
11                 .buf = (void *)t, .rpos = (void *)t,
12                 .rend = (void *)-1, .lock = -1
13         };
14         shlim(&f, 0);
15         long double y = __floatscan(&f, -1, prec, 1);
16         off_t cnt = shcnt(&f);
17         if (p) *p = cnt ? t + cnt : (char *)s;
18         return y;
19 }
20
21 float strtof(const char *s, char **p)
22 {
23         return strtox(s, p, 0);
24 }
25
26 double strtod(const char *s, char **p)
27 {
28         return strtox(s, p, 1);
29 }
30
31 long double strtold(const char *s, char **p)
32 {
33         return strtox(s, p, 2);
34 }