-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwlite_wcstod.c
138 lines (129 loc) · 4.11 KB
/
wlite_wcstod.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* wlite: A 16-bit Unicode based tiny <wchar.h> library with CJK features
* Copyright (C) 2022 Eido INOUE
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Lesser Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdbool.h>
#include <errno.h> // errno, EILSEQ, ERANGE / FIXME: freestanding
#include <locale.h> // FIXME: freestanding
#include "wlite_config.h" // wchar_t, NULL, size_t
#include "wlite_wchar.h" // prototypes
#include "wlite_wctype.h"
#include "wlite_stdlib.h"
// https://codereview.stackexchange.com/questions/44869/implement-strtod-parsing
// https://linux.die.net/man/3/wcstod
/*
* FIXME
* If the correct value is out of the range of representable values for the type,
* a positive or negative HUGE_VAL is returned, and errno is set to ERANGE.
*
* If the correct value would cause underflow, the function returns a value whose
* magnitude is no greater than the smallest normalized positive number and
* sets errno to ERANGE.
*/
double
wlite_wcstod(const wchar_t *str, wchar_t **endptr) {
double result = 0.0;
wchar_t signedResult = L'\0';
wchar_t signedExponent = L'\0';
int decimals = 0;
bool isExponent = false;
bool hasExponent = false;
bool hasResult = false;
// exponent is logically int but is coded as double so that its eventual
// overflow detection can be the same as for double result
double exponent = 0;
wchar_t c;
for (; L'\0' != (c = *str); ++str) {
if ((c >= L'0') && (c <= L'9')) {
int digit = c - L'0';
if (isExponent) {
exponent = (10 * exponent) + digit;
hasExponent = true;
}
else if (decimals == 0) {
result = (10 * result) + digit;
hasResult = true;
}
else {
result += (double) digit / decimals;
decimals *= 10;
}
continue;
}
if (c == L'.') {
if (!hasResult) {
// don't allow leading '.'
break;
}
if (isExponent) {
// don't allow decimal places in exponent
break;
}
if (decimals != 0) {
// this is the 2nd time we've found a '.'
break;
}
decimals = 10;
continue;
}
if ((c == L'-') || (c == L'+')) {
if (isExponent) {
if (signedExponent || (exponent != 0))
break;
else
signedExponent = c;
}
else {
if (signedResult || (result != 0))
break;
else
signedResult = c;
}
continue;
}
if (c == L'E') {
if (!hasResult) {
// don't allow leading 'E'
break;
}
if (isExponent)
break;
else
isExponent = true;
continue;
}
// else unexpected character
break;
}
if (isExponent && !hasExponent) {
while (*str != L'E')
--str;
}
if (!hasResult && signedResult)
--str;
if (endptr)
*endptr = (wchar_t *) str;
for (; exponent != 0; --exponent) {
if (signedExponent == L'-')
result /= 10;
else
result *= 10;
}
if (signedResult == L'-') {
result = -result; // in IEEE 754, +0.0 != -0.0
}
return result;
}