4
#include "numpy/npy_math.h"
7
* From the C99 standard, section 7.19.6: The exponent always contains at least
8
* two digits, and only as many more digits as necessary to represent the
12
/* We force 3 digits on windows for python < 2.6 for compatibility reason */
13
#if defined(MS_WIN32) && (PY_VERSION_HEX < 0x02060000)
14
#define MIN_EXPONENT_DIGITS 3
16
#define MIN_EXPONENT_DIGITS 2
20
* Ensure that any exponent, if present, is at least MIN_EXPONENT_DIGITS
24
_ensure_minimum_exponent_length(char* buffer, size_t buf_size)
26
char *p = strpbrk(buffer, "eE");
27
if (p && (*(p + 1) == '-' || *(p + 1) == '+')) {
29
int exponent_digit_cnt = 0;
30
int leading_zero_cnt = 0;
31
int in_leading_zeros = 1;
32
int significant_digit_cnt;
34
/* Skip over the exponent and the sign. */
37
/* Find the end of the exponent, keeping track of leading zeros. */
38
while (*p && isdigit(Py_CHARMASK(*p))) {
39
if (in_leading_zeros && *p == '0') {
49
significant_digit_cnt = exponent_digit_cnt - leading_zero_cnt;
50
if (exponent_digit_cnt == MIN_EXPONENT_DIGITS) {
52
* If there are 2 exactly digits, we're done,
53
* regardless of what they contain
56
else if (exponent_digit_cnt > MIN_EXPONENT_DIGITS) {
60
* There are more than 2 digits in the exponent. See
61
* if we can delete some of the leading zeros
63
if (significant_digit_cnt < MIN_EXPONENT_DIGITS) {
64
significant_digit_cnt = MIN_EXPONENT_DIGITS;
66
extra_zeros_cnt = exponent_digit_cnt - significant_digit_cnt;
69
* Delete extra_zeros_cnt worth of characters from the
70
* front of the exponent
72
assert(extra_zeros_cnt >= 0);
75
* Add one to significant_digit_cnt to copy the
76
* trailing 0 byte, thus setting the length
78
memmove(start, start + extra_zeros_cnt, significant_digit_cnt + 1);
82
* If there are fewer than 2 digits, add zeros
83
* until there are 2, if there's enough room
85
int zeros = MIN_EXPONENT_DIGITS - exponent_digit_cnt;
86
if (start + zeros + exponent_digit_cnt + 1 < buffer + buf_size) {
87
memmove(start + zeros, start, exponent_digit_cnt + 1);
88
memset(start, '0', zeros);
95
* Ensure that buffer has a decimal point in it. The decimal point
96
* will not be in the current locale, it will always be '.'
99
_ensure_decimal_point(char* buffer, size_t buf_size)
101
int insert_count = 0;
102
char* chars_to_insert;
104
/* search for the first non-digit character */
106
if (*p == '-' || *p == '+')
108
* Skip leading sign, if present. I think this could only
109
* ever be '-', but it can't hurt to check for both.
112
while (*p && isdigit(Py_CHARMASK(*p))) {
116
if (isdigit(Py_CHARMASK(*(p+1)))) {
118
* Nothing to do, we already have a decimal
119
* point and a digit after it.
124
* We have a decimal point, but no following
125
* digit. Insert a zero after the decimal.
128
chars_to_insert = "0";
133
chars_to_insert = ".0";
137
size_t buf_len = strlen(buffer);
138
if (buf_len + insert_count + 1 >= buf_size) {
140
* If there is not enough room in the buffer
141
* for the additional text, just skip it. It's
142
* not worth generating an error over.
146
memmove(p + insert_count, p, buffer + strlen(buffer) - p + 1);
147
memcpy(p, chars_to_insert, insert_count);
152
/* see FORMATBUFLEN in unicodeobject.c */
153
#define FLOAT_FORMATBUFLEN 120
156
* Given a string that may have a decimal point in the current
157
* locale, change it back to a dot. Since the string cannot get
158
* longer, no need for a maximum buffer size parameter.
161
_change_decimal_from_locale_to_dot(char* buffer)
163
struct lconv *locale_data = localeconv();
164
const char *decimal_point = locale_data->decimal_point;
166
if (decimal_point[0] != '.' || decimal_point[1] != 0) {
167
size_t decimal_point_len = strlen(decimal_point);
169
if (*buffer == '+' || *buffer == '-') {
172
while (isdigit(Py_CHARMASK(*buffer))) {
175
if (strncmp(buffer, decimal_point, decimal_point_len) == 0) {
178
if (decimal_point_len > 1) {
179
/* buffer needs to get smaller */
180
size_t rest_len = strlen(buffer + (decimal_point_len - 1));
181
memmove(buffer, buffer + (decimal_point_len - 1), rest_len);
182
buffer[rest_len] = 0;
189
* Check that the format string is a valid one for NumPyOS_ascii_format*
192
_check_ascii_format(const char *format)
195
size_t format_len = strlen(format);
197
/* The last character in the format string must be the format char */
198
format_char = format[format_len - 1];
200
if (format[0] != '%') {
205
* I'm not sure why this test is here. It's ensuring that the format
206
* string after the first character doesn't have a single quote, a
207
* lowercase l, or a percent. This is the reverse of the commented-out
208
* test about 10 lines ago.
210
if (strpbrk(format + 1, "'l%")) {
215
* Also curious about this function is that it accepts format strings
216
* like "%xg", which are invalid for floats. In general, the
217
* interface to this function is not very good, but changing it is
218
* difficult because it's a public API.
220
if (!(format_char == 'e' || format_char == 'E'
221
|| format_char == 'f' || format_char == 'F'
222
|| format_char == 'g' || format_char == 'G')) {
230
* Fix the generated string: make sure the decimal is ., that exponent has a
231
* minimal number of digits, and that it has a decimal + one digit after that
232
* decimal if decimal argument != 0 (Same effect that 'Z' format in
236
_fix_ascii_format(char* buf, size_t buflen, int decimal)
239
* Get the current locale, and find the decimal point string.
240
* Convert that string back to a dot.
242
_change_decimal_from_locale_to_dot(buf);
245
* If an exponent exists, ensure that the exponent is at least
246
* MIN_EXPONENT_DIGITS digits, providing the buffer is large enough
247
* for the extra zeros. Also, if there are more than
248
* MIN_EXPONENT_DIGITS, remove as many zeros as possible until we get
249
* back to MIN_EXPONENT_DIGITS
251
_ensure_minimum_exponent_length(buf, buflen);
254
_ensure_decimal_point(buf, buflen);
261
* NumPyOS_ascii_format*:
262
* - buffer: A buffer to place the resulting string in
263
* - buf_size: The length of the buffer.
264
* - format: The printf()-style format to use for the code to use for
266
* - value: The value to convert
267
* - decimal: if != 0, always has a decimal, and at leasat one digit after
268
* the decimal. This has the same effect as passing 'Z' in the origianl
271
* This is similar to PyOS_ascii_formatd in python > 2.6, except that it does
272
* not handle 'n', and handles nan / inf.
274
* Converts a #gdouble to a string, using the '.' as decimal point. To format
275
* the number you pass in a printf()-style format string. Allowed conversion
276
* specifiers are 'e', 'E', 'f', 'F', 'g', 'G'.
278
* Return value: The pointer to the buffer with the converted string.
280
#define _ASCII_FORMAT(type, suffix, print_type) \
282
NumPyOS_ascii_format ## suffix(char *buffer, size_t buf_size, \
283
const char *format, \
284
type val, int decimal) \
286
if (npy_isfinite(val)) { \
287
if(_check_ascii_format(format)) { \
290
PyOS_snprintf(buffer, buf_size, format, (print_type)val); \
291
return _fix_ascii_format(buffer, buf_size, decimal); \
293
else if (npy_isnan(val)){ \
294
if (buf_size < 4) { \
297
strcpy(buffer, "nan"); \
300
if (npy_signbit(val)) { \
301
if (buf_size < 5) { \
304
strcpy(buffer, "-inf"); \
307
if (buf_size < 4) { \
310
strcpy(buffer, "inf"); \
316
_ASCII_FORMAT(float, f, float)
317
_ASCII_FORMAT(double, d, double)
318
#ifndef FORCE_NO_LONG_DOUBLE_FORMATTING
319
_ASCII_FORMAT(long double, l, long double)
321
_ASCII_FORMAT(long double, l, double)
325
* NumPyOS_ascii_isspace:
327
* Same as isspace under C locale
330
NumPyOS_ascii_isspace(char c)
332
return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t'
338
* NumPyOS_ascii_isalpha:
340
* Same as isalpha under C locale
343
NumPyOS_ascii_isalpha(char c)
345
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
350
* NumPyOS_ascii_isdigit:
352
* Same as isdigit under C locale
355
NumPyOS_ascii_isdigit(char c)
357
return (c >= '0' && c <= '9');
362
* NumPyOS_ascii_isalnum:
364
* Same as isalnum under C locale
367
NumPyOS_ascii_isalnum(char c)
369
return NumPyOS_ascii_isdigit(c) || NumPyOS_ascii_isalpha(c);
374
* NumPyOS_ascii_tolower:
376
* Same as tolower under C locale
379
NumPyOS_ascii_tolower(char c)
381
if (c >= 'A' && c <= 'Z') {
382
return c + ('a'-'A');
389
* NumPyOS_ascii_strncasecmp:
391
* Same as strncasecmp under C locale
394
NumPyOS_ascii_strncasecmp(const char* s1, const char* s2, size_t len)
397
while (len > 0 && *s1 != '\0' && *s2 != '\0') {
398
diff = ((int)NumPyOS_ascii_tolower(*s1)) -
399
((int)NumPyOS_ascii_tolower(*s2));
408
return ((int)*s1) - ((int)*s2);
415
* NumPyOS_ascii_strtod:
417
* Work around bugs in PyOS_ascii_strtod
420
NumPyOS_ascii_strtod(const char *s, char** endptr)
422
struct lconv *locale_data = localeconv();
423
const char *decimal_point = locale_data->decimal_point;
424
size_t decimal_point_len = strlen(decimal_point);
426
char buffer[FLOAT_FORMATBUFLEN+1];
432
while (NumPyOS_ascii_isspace(*s)) {
439
* Recognize POSIX inf/nan representations on all platforms.
447
else if (*p == '+') {
450
if (NumPyOS_ascii_strncasecmp(p, "nan", 3) == 0) {
454
while (NumPyOS_ascii_isalnum(*p) || *p == '_') {
461
if (endptr != NULL) {
466
else if (NumPyOS_ascii_strncasecmp(p, "inf", 3) == 0) {
468
if (NumPyOS_ascii_strncasecmp(p, "inity", 5) == 0) {
471
if (endptr != NULL) {
474
return result*NPY_INFINITY;
481
* At least Python versions <= 2.5.2 and <= 2.6.1
483
* Fails to do best-efforts parsing of strings of the form "1<DP>234"
484
* where <DP> is the decimal point under the foreign locale.
486
if (decimal_point[0] != '.' || decimal_point[1] != 0) {
488
if (*p == '+' || *p == '-') {
491
while (*p >= '0' && *p <= '9') {
494
if (strncmp(p, decimal_point, decimal_point_len) == 0) {
496
if (n > FLOAT_FORMATBUFLEN) {
497
n = FLOAT_FORMATBUFLEN;
499
memcpy(buffer, s, n);
501
result = PyOS_ascii_strtod(buffer, &q);
502
if (endptr != NULL) {
503
*endptr = (char*)(s + (q - buffer));
510
return PyOS_ascii_strtod(s, endptr);
515
* NumPyOS_ascii_ftolf:
517
* * value: Place to store the value read
519
* Similar to PyOS_ascii_strtod, except that it reads input from a file.
521
* Similarly to fscanf, this function always consumes leading whitespace,
522
* and any text that could be the leading part in valid input.
524
* Return value: similar to fscanf.
525
* * 0 if no number read,
526
* * 1 if a number read,
527
* * EOF if end-of-file met before reading anything.
530
NumPyOS_ascii_ftolf(FILE *fp, double *value)
532
char buffer[FLOAT_FORMATBUFLEN + 1];
539
* Pass on to PyOS_ascii_strtod the leftmost matching part in regexp
541
* \s*[+-]? ( [0-9]*\.[0-9]+([eE][+-]?[0-9]+)
542
* | nan ( \([:alphanum:_]*\) )?
546
* case-insensitively.
548
* The "do { ... } while (0)" wrapping in macros ensures that they behave
549
* properly eg. in "if ... else" structures.
552
#define END_MATCH() \
555
#define NEXT_CHAR() \
557
if (c == EOF || endp >= buffer + FLOAT_FORMATBUFLEN) \
563
#define MATCH_ALPHA_STRING_NOCASE(string) \
565
for (p=(string); *p!='\0' && (c==*p || c+('a'-'A')==*p); ++p) \
567
if (*p != '\0') END_MATCH(); \
570
#define MATCH_ONE_OR_NONE(condition) \
571
do { if (condition) NEXT_CHAR(); } while (0)
573
#define MATCH_ONE_OR_MORE(condition) \
576
while (condition) { NEXT_CHAR(); ok = 1; } \
577
if (!ok) END_MATCH(); \
580
#define MATCH_ZERO_OR_MORE(condition) \
581
while (condition) { NEXT_CHAR(); }
583
/* 1. emulate fscanf EOF handling */
588
/* 2. consume leading whitespace unconditionally */
589
while (NumPyOS_ascii_isspace(c)) {
593
/* 3. start reading matching input to buffer */
596
/* 4.1 sign (optional) */
597
MATCH_ONE_OR_NONE(c == '+' || c == '-');
599
/* 4.2 nan, inf, infinity; [case-insensitive] */
600
if (c == 'n' || c == 'N') {
602
MATCH_ALPHA_STRING_NOCASE("an");
604
/* accept nan([:alphanum:_]*), similarly to strtod */
607
MATCH_ZERO_OR_MORE(NumPyOS_ascii_isalnum(c) || c == '_');
614
else if (c == 'i' || c == 'I') {
616
MATCH_ALPHA_STRING_NOCASE("nfinity");
621
MATCH_ZERO_OR_MORE(NumPyOS_ascii_isdigit(c));
625
MATCH_ONE_OR_MORE(NumPyOS_ascii_isdigit(c));
629
if (c == 'e' || c == 'E') {
631
MATCH_ONE_OR_NONE(c == '+' || c == '-');
632
MATCH_ONE_OR_MORE(NumPyOS_ascii_isdigit(c));
642
/* 5. try to convert buffer. */
643
*value = NumPyOS_ascii_strtod(buffer, &p);
645
/* return 1 if something read, else 0 */
646
return (buffer == p) ? 0 : 1;
651
#undef MATCH_ALPHA_STRING_NOCASE
652
#undef MATCH_ONE_OR_NONE
653
#undef MATCH_ONE_OR_MORE
654
#undef MATCH_ZERO_OR_MORE