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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
|
#include <locale.h>
#include <stdio.h>
/* From the C99 standard, section 7.19.6: The exponent always contains at least
two digits, and only as many more digits as necessary to represent the
exponent.
*/
/* We force 3 digits on windows for python < 2.6 for compatibility reason */
#if defined(MS_WIN32) && (PY_VERSION_HEX < 0x02060000)
#define MIN_EXPONENT_DIGITS 3
#else
#define MIN_EXPONENT_DIGITS 2
#endif
/* Ensure that any exponent, if present, is at least MIN_EXPONENT_DIGITS
in length. */
static void
_ensure_minimum_exponent_length(char* buffer, size_t buf_size)
{
char *p = strpbrk(buffer, "eE");
if (p && (*(p + 1) == '-' || *(p + 1) == '+')) {
char *start = p + 2;
int exponent_digit_cnt = 0;
int leading_zero_cnt = 0;
int in_leading_zeros = 1;
int significant_digit_cnt;
/* Skip over the exponent and the sign. */
p += 2;
/* Find the end of the exponent, keeping track of leading
zeros. */
while (*p && isdigit(Py_CHARMASK(*p))) {
if (in_leading_zeros && *p == '0')
++leading_zero_cnt;
if (*p != '0')
in_leading_zeros = 0;
++p;
++exponent_digit_cnt;
}
significant_digit_cnt = exponent_digit_cnt - leading_zero_cnt;
if (exponent_digit_cnt == MIN_EXPONENT_DIGITS) {
/* If there are 2 exactly digits, we're done,
regardless of what they contain */
}
else if (exponent_digit_cnt > MIN_EXPONENT_DIGITS) {
int extra_zeros_cnt;
/* There are more than 2 digits in the exponent. See
if we can delete some of the leading zeros */
if (significant_digit_cnt < MIN_EXPONENT_DIGITS)
significant_digit_cnt = MIN_EXPONENT_DIGITS;
extra_zeros_cnt = exponent_digit_cnt - significant_digit_cnt;
/* Delete extra_zeros_cnt worth of characters from the
front of the exponent */
assert(extra_zeros_cnt >= 0);
/* Add one to significant_digit_cnt to copy the
trailing 0 byte, thus setting the length */
memmove(start, start + extra_zeros_cnt, significant_digit_cnt + 1);
}
else {
/* If there are fewer than 2 digits, add zeros
until there are 2, if there's enough room */
int zeros = MIN_EXPONENT_DIGITS - exponent_digit_cnt;
if (start + zeros + exponent_digit_cnt + 1 < buffer + buf_size) {
memmove(start + zeros, start, exponent_digit_cnt + 1);
memset(start, '0', zeros);
}
}
}
}
/* Ensure that buffer has a decimal point in it. The decimal point
will not be in the current locale, it will always be '.' */
static void
_ensure_decimal_point(char* buffer, size_t buf_size)
{
int insert_count = 0;
char* chars_to_insert;
/* search for the first non-digit character */
char *p = buffer;
if (*p == '-' || *p == '+')
/* Skip leading sign, if present. I think this could only
ever be '-', but it can't hurt to check for both. */
++p;
while (*p && isdigit(Py_CHARMASK(*p)))
++p;
if (*p == '.') {
if (isdigit(Py_CHARMASK(*(p+1)))) {
/* Nothing to do, we already have a decimal
point and a digit after it */
}
else {
/* We have a decimal point, but no following
digit. Insert a zero after the decimal. */
++p;
chars_to_insert = "0";
insert_count = 1;
}
}
else {
chars_to_insert = ".0";
insert_count = 2;
}
if (insert_count) {
size_t buf_len = strlen(buffer);
if (buf_len + insert_count + 1 >= buf_size) {
/* If there is not enough room in the buffer
for the additional text, just skip it. It's
not worth generating an error over. */
}
else {
memmove(p + insert_count, p,
buffer + strlen(buffer) - p + 1);
memcpy(p, chars_to_insert, insert_count);
}
}
}
/* see FORMATBUFLEN in unicodeobject.c */
#define FLOAT_FORMATBUFLEN 120
/* Given a string that may have a decimal point in the current
locale, change it back to a dot. Since the string cannot get
longer, no need for a maximum buffer size parameter. */
static void
_change_decimal_from_locale_to_dot(char* buffer)
{
struct lconv *locale_data = localeconv();
const char *decimal_point = locale_data->decimal_point;
if (decimal_point[0] != '.' || decimal_point[1] != 0) {
size_t decimal_point_len = strlen(decimal_point);
if (*buffer == '+' || *buffer == '-')
buffer++;
while (isdigit(Py_CHARMASK(*buffer)))
buffer++;
if (strncmp(buffer, decimal_point, decimal_point_len) == 0) {
*buffer = '.';
buffer++;
if (decimal_point_len > 1) {
/* buffer needs to get smaller */
size_t rest_len = strlen(buffer +
(decimal_point_len - 1));
memmove(buffer,
buffer + (decimal_point_len - 1),
rest_len);
buffer[rest_len] = 0;
}
}
}
}
/*
* Check that the format string is a valid one for NumPyOS_ascii_format*
*/
static int
_check_ascii_format(const char *format)
{
char format_char;
size_t format_len = strlen(format);
/* The last character in the format string must be the format char */
format_char = format[format_len - 1];
if (format[0] != '%') {
return -1;
}
/* I'm not sure why this test is here. It's ensuring that the format
string after the first character doesn't have a single quote, a
lowercase l, or a percent. This is the reverse of the commented-out
test about 10 lines ago. */
if (strpbrk(format + 1, "'l%")) {
return -1;
}
/* Also curious about this function is that it accepts format strings
like "%xg", which are invalid for floats. In general, the
interface to this function is not very good, but changing it is
difficult because it's a public API. */
if (!(format_char == 'e' || format_char == 'E' ||
format_char == 'f' || format_char == 'F' ||
format_char == 'g' || format_char == 'G')) {
return -1;
}
return 0;
}
/*
* Fix the generated string: make sure the decimal is ., that exponent has a
* minimal number of digits, and that it has a decimal + one digit after that
* decimal if decimal argument != 0 (Same effect that 'Z' format in
* PyOS_ascii_formatd
*/
static char*
_fix_ascii_format(char* buf, size_t buflen, int decimal)
{
/* Get the current locale, and find the decimal point string.
Convert that string back to a dot. */
_change_decimal_from_locale_to_dot(buf);
/* If an exponent exists, ensure that the exponent is at least
MIN_EXPONENT_DIGITS digits, providing the buffer is large enough
for the extra zeros. Also, if there are more than
MIN_EXPONENT_DIGITS, remove as many zeros as possible until we get
back to MIN_EXPONENT_DIGITS */
_ensure_minimum_exponent_length(buf, buflen);
if (decimal != 0) {
_ensure_decimal_point(buf, buflen);
}
return buf;
}
/*
* NumPyOS_ascii_format*:
* - buffer: A buffer to place the resulting string in
* - buf_size: The length of the buffer.
* - format: The printf()-style format to use for the code to use for
* converting.
* - value: The value to convert
* - decimal: if != 0, always has a decimal, and at leasat one digit after
* the decimal. This has the same effect as passing 'Z' in the origianl
* PyOS_ascii_formatd
*
* This is similar to PyOS_ascii_formatd in python > 2.6, except that it does
* not handle 'n', and handles nan / inf.
*
* Converts a #gdouble to a string, using the '.' as decimal point. To format
* the number you pass in a printf()-style format string. Allowed conversion
* specifiers are 'e', 'E', 'f', 'F', 'g', 'G'.
*
* Return value: The pointer to the buffer with the converted string.
*/
#define _ASCII_FORMAT(type, suffix, print_type) \
static char* \
NumPyOS_ascii_format ## suffix(char *buffer, size_t buf_size, \
const char *format, \
type val, int decimal) \
{ \
if (isfinite(val)) { \
if(_check_ascii_format(format)) { \
return NULL; \
} \
PyOS_snprintf(buffer, buf_size, format, (print_type)val); \
return _fix_ascii_format(buffer, buf_size, decimal); \
} \
else if (isnan(val)){ \
if (buf_size < 4) { \
return NULL; \
} \
strcpy(buffer, "nan"); \
} \
else { \
if (signbit(val)) { \
if (buf_size < 5) { \
return NULL; \
} \
strcpy(buffer, "-inf"); \
} \
else { \
if (buf_size < 4) { \
return NULL; \
} \
strcpy(buffer, "inf"); \
} \
} \
return buffer; \
}
_ASCII_FORMAT(float, f, float)
_ASCII_FORMAT(double, d, double)
#ifndef FORCE_NO_LONG_DOUBLE_FORMATTING
_ASCII_FORMAT(long double, l, long double)
#else
_ASCII_FORMAT(long double, l, double)
#endif
static double NumPyOS_PINF; /* Positive infinity */
static double NumPyOS_PZERO; /* +0 */
static double NumPyOS_NAN; /* NaN */
/* NumPyOS_init:
*
* initialize floating-point constants
*/
static void
NumPyOS_init(void) {
double mul = 1e100;
double div = 1e10;
double tmp, c;
tmp = 0;
c = mul;
for (;;) {
c *= mul;
if (c == tmp) break;
tmp = c;
}
NumPyOS_PINF = c;
tmp = 0;
c = div;
for (;;) {
c /= div;
if (c == tmp) break;
tmp = c;
}
NumPyOS_PZERO = c;
NumPyOS_NAN = NumPyOS_PINF / NumPyOS_PINF;
}
/* NumPyOS_ascii_isspace:
*
* Same as isspace under C locale
*/
static int
NumPyOS_ascii_isspace(char c)
{
return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' ||
c == '\v';
}
/* NumPyOS_ascii_isalpha:
*
* Same as isalpha under C locale
*/
static int
NumPyOS_ascii_isalpha(char c)
{
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
}
/* NumPyOS_ascii_isdigit:
*
* Same as isdigit under C locale
*/
static int
NumPyOS_ascii_isdigit(char c)
{
return (c >= '0' && c <= '9');
}
/* NumPyOS_ascii_isalnum:
*
* Same as isalnum under C locale
*/
static int
NumPyOS_ascii_isalnum(char c)
{
return NumPyOS_ascii_isdigit(c) || NumPyOS_ascii_isalpha(c);
}
/* NumPyOS_ascii_tolower:
*
* Same as tolower under C locale
*/
static char
NumPyOS_ascii_tolower(char c)
{
if (c >= 'A' && c <= 'Z')
return c + ('a'-'A');
return c;
}
/* NumPyOS_ascii_strncasecmp:
*
* Same as strncasecmp under C locale
*/
static int
NumPyOS_ascii_strncasecmp(const char* s1, const char* s2, size_t len)
{
int diff;
while (len > 0 && *s1 != '\0' && *s2 != '\0') {
diff = ((int)NumPyOS_ascii_tolower(*s1)) -
((int)NumPyOS_ascii_tolower(*s2));
if (diff != 0) return diff;
++s1;
++s2;
--len;
}
if (len > 0)
return ((int)*s1) - ((int)*s2);
return 0;
}
/* NumPyOS_ascii_strtod:
*
* Work around bugs in PyOS_ascii_strtod
*/
static double
NumPyOS_ascii_strtod(const char *s, char** endptr)
{
struct lconv *locale_data = localeconv();
const char *decimal_point = locale_data->decimal_point;
size_t decimal_point_len = strlen(decimal_point);
char buffer[FLOAT_FORMATBUFLEN+1];
const char *p;
char *q;
size_t n;
double result;
while (NumPyOS_ascii_isspace(*s)) {
++s;
}
/* ##1
*
* Recognize POSIX inf/nan representations on all platforms.
*/
p = s;
result = 1.0;
if (*p == '-') {
result = -1.0;
++p;
}
else if (*p == '+') {
++p;
}
if (NumPyOS_ascii_strncasecmp(p, "nan", 3) == 0) {
p += 3;
if (*p == '(') {
++p;
while (NumPyOS_ascii_isalnum(*p) || *p == '_') ++p;
if (*p == ')') ++p;
}
if (endptr != NULL) *endptr = (char*)p;
return NumPyOS_NAN;
}
else if (NumPyOS_ascii_strncasecmp(p, "inf", 3) == 0) {
p += 3;
if (NumPyOS_ascii_strncasecmp(p, "inity", 5) == 0)
p += 5;
if (endptr != NULL) *endptr = (char*)p;
return result*NumPyOS_PINF;
}
/* End of ##1 */
/* ## 2
*
* At least Python versions <= 2.5.2 and <= 2.6.1
*
* Fails to do best-efforts parsing of strings of the form "1<DP>234"
* where <DP> is the decimal point under the foreign locale.
*/
if (decimal_point[0] != '.' || decimal_point[1] != 0) {
p = s;
if (*p == '+' || *p == '-')
++p;
while (*p >= '0' && *p <= '9')
++p;
if (strncmp(p, decimal_point, decimal_point_len) == 0) {
n = (size_t)(p - s);
if (n > FLOAT_FORMATBUFLEN)
n = FLOAT_FORMATBUFLEN;
memcpy(buffer, s, n);
buffer[n] = '\0';
result = PyOS_ascii_strtod(buffer, &q);
if (endptr != NULL) {
*endptr = (char*)(s + (q - buffer));
}
return result;
}
}
/* End of ##2 */
return PyOS_ascii_strtod(s, endptr);
}
/*
* NumPyOS_ascii_ftolf:
* * fp: FILE pointer
* * value: Place to store the value read
*
* Similar to PyOS_ascii_strtod, except that it reads input from a file.
*
* Similarly to fscanf, this function always consumes leading whitespace,
* and any text that could be the leading part in valid input.
*
* Return value: similar to fscanf.
* * 0 if no number read,
* * 1 if a number read,
* * EOF if end-of-file met before reading anything.
*/
static int
NumPyOS_ascii_ftolf(FILE *fp, double *value)
{
char buffer[FLOAT_FORMATBUFLEN+1];
char *endp;
char *p;
int c;
int ok;
/*
* Pass on to PyOS_ascii_strtod the leftmost matching part in regexp
*
* \s*[+-]? ( [0-9]*\.[0-9]+([eE][+-]?[0-9]+)
* | nan ( \([:alphanum:_]*\) )?
* | inf(inity)?
* )
*
* case-insensitively.
*
* The "do { ... } while (0)" wrapping in macros ensures that they behave
* properly eg. in "if ... else" structures.
*/
#define END_MATCH() \
goto buffer_filled
#define NEXT_CHAR() \
do { \
if (c == EOF || endp >= buffer + FLOAT_FORMATBUFLEN) \
END_MATCH(); \
*endp++ = (char)c; \
c = getc(fp); \
} while (0)
#define MATCH_ALPHA_STRING_NOCASE(string) \
do { \
for (p=(string); *p!='\0' && (c==*p || c+('a'-'A')==*p); ++p) \
NEXT_CHAR(); \
if (*p != '\0') END_MATCH(); \
} while (0)
#define MATCH_ONE_OR_NONE(condition) \
do { if (condition) NEXT_CHAR(); } while (0)
#define MATCH_ONE_OR_MORE(condition) \
do { \
ok = 0; \
while (condition) { NEXT_CHAR(); ok = 1; } \
if (!ok) END_MATCH(); \
} while (0)
#define MATCH_ZERO_OR_MORE(condition) \
while (condition) { NEXT_CHAR(); }
/* 1. emulate fscanf EOF handling */
c = getc(fp);
if (c == EOF)
return EOF;
/* 2. consume leading whitespace unconditionally */
while (NumPyOS_ascii_isspace(c)) {
c = getc(fp);
}
/* 3. start reading matching input to buffer */
endp = buffer;
/* 4.1 sign (optional) */
MATCH_ONE_OR_NONE(c == '+' || c == '-');
/* 4.2 nan, inf, infinity; [case-insensitive] */
if (c == 'n' || c == 'N') {
NEXT_CHAR();
MATCH_ALPHA_STRING_NOCASE("an");
/* accept nan([:alphanum:_]*), similarly to strtod */
if (c == '(') {
NEXT_CHAR();
MATCH_ZERO_OR_MORE(NumPyOS_ascii_isalnum(c) || c == '_');
if (c == ')') NEXT_CHAR();
}
END_MATCH();
}
else if (c == 'i' || c == 'I') {
NEXT_CHAR();
MATCH_ALPHA_STRING_NOCASE("nfinity");
END_MATCH();
}
/* 4.3 mantissa */
MATCH_ZERO_OR_MORE(NumPyOS_ascii_isdigit(c));
if (c == '.') {
NEXT_CHAR();
MATCH_ONE_OR_MORE(NumPyOS_ascii_isdigit(c));
}
/* 4.4 exponent */
if (c == 'e' || c == 'E') {
NEXT_CHAR();
MATCH_ONE_OR_NONE(c == '+' || c == '-');
MATCH_ONE_OR_MORE(NumPyOS_ascii_isdigit(c));
}
END_MATCH();
buffer_filled:
ungetc(c, fp);
*endp = '\0';
/* 5. try to convert buffer. */
*value = NumPyOS_ascii_strtod(buffer, &p);
return (buffer == p) ? 0 : 1; /* if something was read */
}
#undef END_MATCH
#undef NEXT_CHAR
#undef MATCH_ALPHA_STRING_NOCASE
#undef MATCH_ONE_OR_NONE
#undef MATCH_ONE_OR_MORE
#undef MATCH_ZERO_OR_MORE
|