03670b0bf03ce6682ba2d34220b4f5058f9f0e60
[musl] / src / stdlib / wcstod.c
1 #include "shgetc.h"
2 #include "floatscan.h"
3 #include "stdio_impl.h"
4
5 /* This read function heavily cheats. It knows:
6  *  (1) len will always be 1
7  *  (2) non-ascii characters don't matter */
8
9 static size_t do_read(FILE *f, unsigned char *buf, size_t len)
10 {
11         size_t i;
12         const wchar_t *wcs = f->cookie;
13
14         if (!wcs[0]) wcs=L"@";
15         for (i=0; i<f->buf_size && wcs[i]; i++)
16                 f->buf[i] = wcs[i] < 128 ? wcs[i] : '@';
17         f->rpos = f->buf;
18         f->rend = f->buf + i;
19         f->cookie = (void *)(wcs+i);
20
21         if (i && len) {
22                 *buf = *f->rpos++;
23                 return 1;
24         }
25         return 0;
26 }
27
28 static long double wcstox(const wchar_t *s, wchar_t **p, int prec)
29 {
30         wchar_t *t = (wchar_t *)s;
31         unsigned char buf[64];
32         FILE f = {0};
33         f.flags = 0;
34         f.rpos = f.rend = 0;
35         f.buf = buf + 4;
36         f.buf_size = sizeof buf - 4;
37         f.lock = -1;
38         f.read = do_read;
39         while (iswspace(*t)) t++;
40         f.cookie = (void *)t;
41         shlim(&f, 0);
42         long double y = __floatscan(&f, prec, 1);
43         if (p) {
44                 size_t cnt = shcnt(&f);
45                 *p = cnt ? t + cnt : (wchar_t *)s;
46         }
47         return y;
48 }
49
50 float wcstof(const wchar_t *restrict s, wchar_t **restrict p)
51 {
52         return wcstox(s, p, 0);
53 }
54
55 double wcstod(const wchar_t *restrict s, wchar_t **restrict p)
56 {
57         return wcstox(s, p, 1);
58 }
59
60 long double wcstold(const wchar_t *restrict s, wchar_t **restrict p)
61 {
62         return wcstox(s, p, 2);
63 }