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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
/***********************************************************************
proj_strtod: Convert string to double, accepting underscore separators
Thomas Knudsen, 2017-01-17/09-19
************************************************************************
Conventionally, PROJ.4 does not honor locale settings, consistently
behaving as if LC_ALL=C.
For this to work, we have, for many years, been using other solutions
than the C standard library strtod/atof functions for converting strings
to doubles.
In the early versions of proj, iirc, a gnu version of strtod was used,
mostly to work around cases where the same system library was used for
C and Fortran linking, hence making strtod accept "D" and "d" as
exponentiation indicators, following Fortran Double Precision constant
syntax. This broke the proj angular syntax accepting a "d" to mean
"degree": 12d34'56", meaning 12 degrees 34 minutes and 56 seconds.
With an explicit MIT licence, PROJ.4 could not include GPL code any
longer, and apparently at some time, the GPL code was replaced by the
current C port of a GDAL function (in pj_strtod.c), which reads the
LC_NUMERIC setting and, behind the back of the user, momentarily changes
the conventional '.' delimiter to whatever the locale requires, then
calls the system supplied strtod.
While this requires a minimum amount of coding, it only solves one
problem, and not in a very generic way.
Another problem, I would like to see solved, is the handling of underscores
as generic delimiters. This is getting popular in a number of programming
languages (Ada, C++, C#, D, Java, Julia, Perl 5, Python, Rust, etc.
cf. e.g. https://www.python.org/dev/peps/pep-0515/), and in our case of
handling numbers being in the order of magnitude of the Earth's dimensions,
and a resolution of submillimetre, i.e. having 10 or more significant digits,
splitting the "wall of digits" into smaller chunks is of immense value.
Hence this reimplementation of strtod, which hardcodes '.' as indicator of
numeric fractions, and accepts '_' anywhere in a numerical string sequence:
So a typical northing value can be written
6_098_907.8250 m
rather than
6098907.8250 m
which, in my humble opinion, is well worth the effort.
While writing this code, I took ample inspiration from Michael Ringgaard's
strtod version over at http://www.jbox.dk/sanos/source/lib/strtod.c.html,
and Yasuhiro Matsumoto's public domain version over at
https://gist.github.com/mattn/1890186. The code below is, however, not
copied from any of the two mentioned - it is a reimplementation, and
probably suffers from its own set of bugs. So for now, it is intended
not as a replacement of pj_strtod, but only as an experimental piece of
code for use in an experimental new transformation program, cct.
************************************************************************
Thomas Knudsen, thokn@sdfe.dk, 2017-01-17/2017-09-18
************************************************************************
* Copyright (c) 2017 Thomas Knudsen & SDFE
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
***********************************************************************/
#include <string.h> /* for strchr */
#include <errno.h>
#include <ctype.h>
#include <float.h> /* for HUGE_VAL */
#include <math.h> /* for pow() */
double proj_strtod(const char *str, char **endptr);
double proj_atof(const char *str);
double proj_strtod(const char *str, char **endptr) {
double number = 0, integral_part = 0;
int exponent = 0;
int fraction_is_nonzero = 0;
int sign = 0;
char *p = (char *) str;
int n = 0;
int num_digits_total = 0;
int num_digits_after_comma = 0;
int num_prefixed_zeros = 0;
if (0==str) {
errno = EFAULT;
if (endptr)
*endptr = p;
return HUGE_VAL;
}
/* First skip leading whitespace */
while (isspace(*p))
p++;
/* Empty string? */
if (0==*p) {
errno = EINVAL;
if (endptr)
*endptr = p;
return HUGE_VAL;
}
/* Then handle optional prefixed sign and skip prefix zeros */
switch (*p) {
case '-':
sign = -1, p++; break;
case '+':
sign = 1, p++; break;
default:
if (isdigit(*p) || '_'==*p || '.'==*p)
break;
if (endptr)
*endptr = p;
errno = EINVAL;
return HUGE_VAL;
}
/* skip prefixed zeros */
while ('0'==*p || '_'==*p)
p++;
/* Now expect a (potentially zero-length) string of digits */
while (isdigit(*p) || ('_'==*p)) {
if ('_'==*p) {
p++;
continue;
}
number = number * 10. + (*p - '0');
p++;
num_digits_total++;
}
integral_part = number;
/* Do we have a fractional part? */
if ('.'==*p) {
p++;
/* keep on skipping prefixed zeros (i.e. allow writing 1e-20 */
/* as 0.00000000000000000001 without losing precision) */
if (0==integral_part)
while ('0'==*p || '_'==*p) {
if ('0'==*p)
num_prefixed_zeros++;
p++;
}
/* if the next character is nonnumeric, we have reached the end */
if (0==strchr ("0123456789eE+-", *p))
return integral_part;
while (isdigit(*p) || '_'==*p) {
/* Don't let pathologically long fractions destroy precision */
if ('_'==*p || num_digits_total > 17) {
p++;
continue;
}
number = number * 10. + (*p - '0');
if (*p!='0')
fraction_is_nonzero = 1;
p++;
num_digits_total++;
num_digits_after_comma++;
}
/* Avoid having long zero-tails (4321.000...000) destroy precision */
if (fraction_is_nonzero)
exponent = -(num_digits_after_comma + num_prefixed_zeros);
else
number = integral_part;
}
/* non-digit */
if (0==num_digits_total) {
errno = EINVAL;
if (endptr)
*endptr = p;
return HUGE_VAL;
}
if (sign==-1)
number = -number;
/* Do we have an exponent part? */
if (*p == 'e' || *p == 'E') {
p++;
/* Does it have a sign? */
sign = 0;
if ('-'==*p)
sign = -1;
if ('+'==*p)
sign = +1;
if (0==sign) {
if (!isdigit(*p) && *p!='_') {
if (endptr)
*endptr = p;
return HUGE_VAL;
}
}
else
p++;
/* Go on and read the exponent */
n = 0;
while (isdigit(*p) || '_'==*p) {
if ('_'==*p) {
p++;
continue;
}
n = n * 10 + (*p - '0');
p++;
}
if (-1==sign)
n = -n;
exponent += n;
}
if ((exponent < DBL_MIN_EXP) || (exponent > DBL_MAX_EXP)) {
errno = ERANGE;
if (endptr)
*endptr = p;
return HUGE_VAL;
}
/* on some platforms pow() is very slow - so don't call it if exponent==0 */
if (exponent)
number *= pow (10, exponent);
/* Did we run into an infinity? */
if (fabs(number) > DBL_MAX)
errno = ERANGE;
if (endptr)
*endptr = p;
return number;
}
double proj_atof(const char *str) {
return proj_strtod(str, (void *) 0);
}
#ifdef TEST
#include <string.h>
int main (int argc, char **argv) {
double res;
char *endptr;
if (argc < 2)
return 0;
res = proj_strtod (argv[1], &endptr);
printf ("res = %20.15g. Rest = [%s], errno = %d\n", res, endptr, (int) errno);
return 0;
}
#endif
|