~jtaylor/ubuntu/precise/python-numpy/multiarch-fix-818867

« back to all changes in this revision

Viewing changes to numpy/core/src/numpyos.c

  • Committer: Bazaar Package Importer
  • Author(s): Sandro Tosi
  • Date: 2010-10-07 10:19:13 UTC
  • mfrom: (7.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20101007101913-8b1kmt8ho4upcl9s
Tags: 1:1.4.1-5
* debian/patches/10_use_local_python.org_object.inv_sphinx.diff
  - fixed small typo in description
* debian/patches/changeset_r8364.diff
  - fix memory corruption (double free); thanks to Joseph Barillari for the
    report and to Michael Gilbert for pushing resolution; Closes: #581058

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <locale.h>
2
 
#include <stdio.h>
3
 
 
4
 
#include "numpy/npy_math.h"
5
 
 
6
 
/*
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
9
 
 * exponent.
10
 
 */
11
 
 
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
15
 
#else
16
 
#define MIN_EXPONENT_DIGITS 2
17
 
#endif
18
 
 
19
 
/*
20
 
 * Ensure that any exponent, if present, is at least MIN_EXPONENT_DIGITS
21
 
 * in length.
22
 
 */
23
 
static void
24
 
_ensure_minimum_exponent_length(char* buffer, size_t buf_size)
25
 
{
26
 
    char *p = strpbrk(buffer, "eE");
27
 
    if (p && (*(p + 1) == '-' || *(p + 1) == '+')) {
28
 
        char *start = p + 2;
29
 
        int exponent_digit_cnt = 0;
30
 
        int leading_zero_cnt = 0;
31
 
        int in_leading_zeros = 1;
32
 
        int significant_digit_cnt;
33
 
 
34
 
        /* Skip over the exponent and the sign. */
35
 
        p += 2;
36
 
 
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') {
40
 
                ++leading_zero_cnt;
41
 
            }
42
 
            if (*p != '0') {
43
 
                in_leading_zeros = 0;
44
 
            }
45
 
            ++p;
46
 
            ++exponent_digit_cnt;
47
 
        }
48
 
 
49
 
        significant_digit_cnt = exponent_digit_cnt - leading_zero_cnt;
50
 
        if (exponent_digit_cnt == MIN_EXPONENT_DIGITS) {
51
 
            /*
52
 
             * If there are 2 exactly digits, we're done,
53
 
             * regardless of what they contain
54
 
             */
55
 
        }
56
 
        else if (exponent_digit_cnt > MIN_EXPONENT_DIGITS) {
57
 
            int extra_zeros_cnt;
58
 
 
59
 
            /*
60
 
             * There are more than 2 digits in the exponent.  See
61
 
             * if we can delete some of the leading zeros
62
 
             */
63
 
            if (significant_digit_cnt < MIN_EXPONENT_DIGITS) {
64
 
                significant_digit_cnt = MIN_EXPONENT_DIGITS;
65
 
            }
66
 
            extra_zeros_cnt = exponent_digit_cnt - significant_digit_cnt;
67
 
 
68
 
            /*
69
 
             * Delete extra_zeros_cnt worth of characters from the
70
 
             * front of the exponent
71
 
             */
72
 
            assert(extra_zeros_cnt >= 0);
73
 
 
74
 
            /*
75
 
             * Add one to significant_digit_cnt to copy the
76
 
             * trailing 0 byte, thus setting the length
77
 
             */
78
 
            memmove(start, start + extra_zeros_cnt, significant_digit_cnt + 1);
79
 
        }
80
 
        else {
81
 
            /*
82
 
             * If there are fewer than 2 digits, add zeros
83
 
             * until there are 2, if there's enough room
84
 
             */
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);
89
 
            }
90
 
        }
91
 
    }
92
 
}
93
 
 
94
 
/*
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 '.'
97
 
 */
98
 
static void
99
 
_ensure_decimal_point(char* buffer, size_t buf_size)
100
 
{
101
 
    int insert_count = 0;
102
 
    char* chars_to_insert;
103
 
 
104
 
    /* search for the first non-digit character */
105
 
    char *p = buffer;
106
 
    if (*p == '-' || *p == '+')
107
 
        /*
108
 
         * Skip leading sign, if present.  I think this could only
109
 
         * ever be '-', but it can't hurt to check for both.
110
 
         */
111
 
        ++p;
112
 
    while (*p && isdigit(Py_CHARMASK(*p))) {
113
 
        ++p;
114
 
    }
115
 
    if (*p == '.') {
116
 
        if (isdigit(Py_CHARMASK(*(p+1)))) {
117
 
            /*
118
 
             * Nothing to do, we already have a decimal
119
 
             * point and a digit after it.
120
 
             */
121
 
        }
122
 
        else {
123
 
            /*
124
 
             * We have a decimal point, but no following
125
 
             * digit.  Insert a zero after the decimal.
126
 
             */
127
 
            ++p;
128
 
            chars_to_insert = "0";
129
 
            insert_count = 1;
130
 
        }
131
 
    }
132
 
    else {
133
 
        chars_to_insert = ".0";
134
 
        insert_count = 2;
135
 
    }
136
 
    if (insert_count) {
137
 
        size_t buf_len = strlen(buffer);
138
 
        if (buf_len + insert_count + 1 >= buf_size) {
139
 
            /*
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.
143
 
             */
144
 
        }
145
 
        else {
146
 
            memmove(p + insert_count, p, buffer + strlen(buffer) - p + 1);
147
 
            memcpy(p, chars_to_insert, insert_count);
148
 
        }
149
 
    }
150
 
}
151
 
 
152
 
/* see FORMATBUFLEN in unicodeobject.c */
153
 
#define FLOAT_FORMATBUFLEN 120
154
 
 
155
 
/*
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.
159
 
 */
160
 
static void
161
 
_change_decimal_from_locale_to_dot(char* buffer)
162
 
{
163
 
    struct lconv *locale_data = localeconv();
164
 
    const char *decimal_point = locale_data->decimal_point;
165
 
 
166
 
    if (decimal_point[0] != '.' || decimal_point[1] != 0) {
167
 
        size_t decimal_point_len = strlen(decimal_point);
168
 
 
169
 
        if (*buffer == '+' || *buffer == '-') {
170
 
            buffer++;
171
 
        }
172
 
        while (isdigit(Py_CHARMASK(*buffer))) {
173
 
            buffer++;
174
 
        }
175
 
        if (strncmp(buffer, decimal_point, decimal_point_len) == 0) {
176
 
            *buffer = '.';
177
 
            buffer++;
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;
183
 
            }
184
 
        }
185
 
    }
186
 
}
187
 
 
188
 
/*
189
 
 * Check that the format string is a valid one for NumPyOS_ascii_format*
190
 
 */
191
 
static int
192
 
_check_ascii_format(const char *format)
193
 
{
194
 
    char format_char;
195
 
    size_t format_len = strlen(format);
196
 
 
197
 
    /* The last character in the format string must be the format char */
198
 
    format_char = format[format_len - 1];
199
 
 
200
 
    if (format[0] != '%') {
201
 
        return -1;
202
 
    }
203
 
 
204
 
    /*
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.
209
 
     */
210
 
    if (strpbrk(format + 1, "'l%")) {
211
 
        return -1;
212
 
    }
213
 
 
214
 
    /*
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.
219
 
     */
220
 
    if (!(format_char == 'e' || format_char == 'E'
221
 
          || format_char == 'f' || format_char == 'F'
222
 
          || format_char == 'g' || format_char == 'G')) {
223
 
        return -1;
224
 
    }
225
 
 
226
 
    return 0;
227
 
}
228
 
 
229
 
/*
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
233
 
 * PyOS_ascii_formatd
234
 
 */
235
 
static char*
236
 
_fix_ascii_format(char* buf, size_t buflen, int decimal)
237
 
{
238
 
    /*
239
 
     * Get the current locale, and find the decimal point string.
240
 
     * Convert that string back to a dot.
241
 
     */
242
 
    _change_decimal_from_locale_to_dot(buf);
243
 
 
244
 
    /*
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
250
 
     */
251
 
    _ensure_minimum_exponent_length(buf, buflen);
252
 
 
253
 
    if (decimal != 0) {
254
 
        _ensure_decimal_point(buf, buflen);
255
 
    }
256
 
 
257
 
    return buf;
258
 
}
259
 
 
260
 
/*
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
265
 
 *      converting.
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
269
 
 *      PyOS_ascii_formatd
270
 
 *
271
 
 * This is similar to PyOS_ascii_formatd in python > 2.6, except that it does
272
 
 * not handle 'n', and handles nan / inf.
273
 
 *
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'.
277
 
 *
278
 
 * Return value: The pointer to the buffer with the converted string.
279
 
 */
280
 
#define _ASCII_FORMAT(type, suffix, print_type)                         \
281
 
    static char*                                                        \
282
 
    NumPyOS_ascii_format ## suffix(char *buffer, size_t buf_size,       \
283
 
                                   const char *format,                  \
284
 
                                   type val, int decimal)               \
285
 
    {                                                                   \
286
 
        if (npy_isfinite(val)) {                                        \
287
 
            if(_check_ascii_format(format)) {                           \
288
 
                return NULL;                                            \
289
 
            }                                                           \
290
 
            PyOS_snprintf(buffer, buf_size, format, (print_type)val);   \
291
 
            return _fix_ascii_format(buffer, buf_size, decimal);        \
292
 
        }                                                               \
293
 
        else if (npy_isnan(val)){                                       \
294
 
            if (buf_size < 4) {                                         \
295
 
                return NULL;                                            \
296
 
            }                                                           \
297
 
            strcpy(buffer, "nan");                                      \
298
 
        }                                                               \
299
 
        else {                                                          \
300
 
            if (npy_signbit(val)) {                                     \
301
 
                if (buf_size < 5) {                                     \
302
 
                    return NULL;                                        \
303
 
                }                                                       \
304
 
                strcpy(buffer, "-inf");                                 \
305
 
            }                                                           \
306
 
            else {                                                      \
307
 
                if (buf_size < 4) {                                     \
308
 
                    return NULL;                                        \
309
 
                }                                                       \
310
 
                strcpy(buffer, "inf");                                  \
311
 
            }                                                           \
312
 
        }                                                               \
313
 
        return buffer;                                                  \
314
 
    }
315
 
 
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)
320
 
#else
321
 
_ASCII_FORMAT(long double, l, double)
322
 
#endif
323
 
 
324
 
/*
325
 
 * NumPyOS_ascii_isspace:
326
 
 *
327
 
 * Same as isspace under C locale
328
 
 */
329
 
static int
330
 
NumPyOS_ascii_isspace(char c)
331
 
{
332
 
    return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t'
333
 
                    || c == '\v';
334
 
}
335
 
 
336
 
 
337
 
/*
338
 
 * NumPyOS_ascii_isalpha:
339
 
 *
340
 
 * Same as isalpha under C locale
341
 
 */
342
 
static int
343
 
NumPyOS_ascii_isalpha(char c)
344
 
{
345
 
    return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
346
 
}
347
 
 
348
 
 
349
 
/*
350
 
 * NumPyOS_ascii_isdigit:
351
 
 *
352
 
 * Same as isdigit under C locale
353
 
 */
354
 
static int
355
 
NumPyOS_ascii_isdigit(char c)
356
 
{
357
 
    return (c >= '0' && c <= '9');
358
 
}
359
 
 
360
 
 
361
 
/*
362
 
 * NumPyOS_ascii_isalnum:
363
 
 *
364
 
 * Same as isalnum under C locale
365
 
 */
366
 
static int
367
 
NumPyOS_ascii_isalnum(char c)
368
 
{
369
 
    return NumPyOS_ascii_isdigit(c) || NumPyOS_ascii_isalpha(c);
370
 
}
371
 
 
372
 
 
373
 
/*
374
 
 * NumPyOS_ascii_tolower:
375
 
 *
376
 
 * Same as tolower under C locale
377
 
 */
378
 
static char
379
 
NumPyOS_ascii_tolower(char c)
380
 
{
381
 
    if (c >= 'A' && c <= 'Z') {
382
 
        return c + ('a'-'A');
383
 
    }
384
 
    return c;
385
 
}
386
 
 
387
 
 
388
 
/*
389
 
 * NumPyOS_ascii_strncasecmp:
390
 
 *
391
 
 * Same as strncasecmp under C locale
392
 
 */
393
 
static int
394
 
NumPyOS_ascii_strncasecmp(const char* s1, const char* s2, size_t len)
395
 
{
396
 
    int diff;
397
 
    while (len > 0 && *s1 != '\0' && *s2 != '\0') {
398
 
        diff = ((int)NumPyOS_ascii_tolower(*s1)) -
399
 
            ((int)NumPyOS_ascii_tolower(*s2));
400
 
        if (diff != 0) {
401
 
            return diff;
402
 
        }
403
 
        ++s1;
404
 
        ++s2;
405
 
        --len;
406
 
    }
407
 
    if (len > 0) {
408
 
        return ((int)*s1) - ((int)*s2);
409
 
    }
410
 
    return 0;
411
 
}
412
 
 
413
 
 
414
 
/*
415
 
 * NumPyOS_ascii_strtod:
416
 
 *
417
 
 * Work around bugs in PyOS_ascii_strtod
418
 
 */
419
 
static double
420
 
NumPyOS_ascii_strtod(const char *s, char** endptr)
421
 
{
422
 
    struct lconv *locale_data = localeconv();
423
 
    const char *decimal_point = locale_data->decimal_point;
424
 
    size_t decimal_point_len = strlen(decimal_point);
425
 
 
426
 
    char buffer[FLOAT_FORMATBUFLEN+1];
427
 
    const char *p;
428
 
    char *q;
429
 
    size_t n;
430
 
    double result;
431
 
 
432
 
    while (NumPyOS_ascii_isspace(*s)) {
433
 
        ++s;
434
 
    }
435
 
 
436
 
    /*
437
 
     * ##1
438
 
     *
439
 
     * Recognize POSIX inf/nan representations on all platforms.
440
 
     */
441
 
    p = s;
442
 
    result = 1.0;
443
 
    if (*p == '-') {
444
 
        result = -1.0;
445
 
        ++p;
446
 
    }
447
 
    else if (*p == '+') {
448
 
        ++p;
449
 
    }
450
 
    if (NumPyOS_ascii_strncasecmp(p, "nan", 3) == 0) {
451
 
        p += 3;
452
 
        if (*p == '(') {
453
 
            ++p;
454
 
            while (NumPyOS_ascii_isalnum(*p) || *p == '_') {
455
 
                ++p;
456
 
            }
457
 
            if (*p == ')') {
458
 
                ++p;
459
 
            }
460
 
        }
461
 
        if (endptr != NULL) {
462
 
            *endptr = (char*)p;
463
 
        }
464
 
        return NPY_NAN;
465
 
    }
466
 
    else if (NumPyOS_ascii_strncasecmp(p, "inf", 3) == 0) {
467
 
        p += 3;
468
 
        if (NumPyOS_ascii_strncasecmp(p, "inity", 5) == 0) {
469
 
            p += 5;
470
 
        }
471
 
        if (endptr != NULL) {
472
 
            *endptr = (char*)p;
473
 
        }
474
 
        return result*NPY_INFINITY;
475
 
    }
476
 
    /* End of ##1 */
477
 
 
478
 
    /*
479
 
     * ## 2
480
 
     *
481
 
     * At least Python versions <= 2.5.2 and <= 2.6.1
482
 
     *
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.
485
 
     */
486
 
    if (decimal_point[0] != '.' || decimal_point[1] != 0) {
487
 
        p = s;
488
 
        if (*p == '+' || *p == '-') {
489
 
            ++p;
490
 
        }
491
 
        while (*p >= '0' && *p <= '9') {
492
 
            ++p;
493
 
        }
494
 
        if (strncmp(p, decimal_point, decimal_point_len) == 0) {
495
 
            n = (size_t)(p - s);
496
 
            if (n > FLOAT_FORMATBUFLEN) {
497
 
                n = FLOAT_FORMATBUFLEN;
498
 
            }
499
 
            memcpy(buffer, s, n);
500
 
            buffer[n] = '\0';
501
 
            result = PyOS_ascii_strtod(buffer, &q);
502
 
            if (endptr != NULL) {
503
 
                *endptr = (char*)(s + (q - buffer));
504
 
            }
505
 
            return result;
506
 
        }
507
 
    }
508
 
    /* End of ##2 */
509
 
 
510
 
    return PyOS_ascii_strtod(s, endptr);
511
 
}
512
 
 
513
 
 
514
 
/*
515
 
 * NumPyOS_ascii_ftolf:
516
 
 *      * fp: FILE pointer
517
 
 *      * value: Place to store the value read
518
 
 *
519
 
 * Similar to PyOS_ascii_strtod, except that it reads input from a file.
520
 
 *
521
 
 * Similarly to fscanf, this function always consumes leading whitespace,
522
 
 * and any text that could be the leading part in valid input.
523
 
 *
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.
528
 
 */
529
 
static int
530
 
NumPyOS_ascii_ftolf(FILE *fp, double *value)
531
 
{
532
 
    char buffer[FLOAT_FORMATBUFLEN + 1];
533
 
    char *endp;
534
 
    char *p;
535
 
    int c;
536
 
    int ok;
537
 
 
538
 
    /*
539
 
     * Pass on to PyOS_ascii_strtod the leftmost matching part in regexp
540
 
     *
541
 
     *     \s*[+-]? ( [0-9]*\.[0-9]+([eE][+-]?[0-9]+)
542
 
     *              | nan  (  \([:alphanum:_]*\) )?
543
 
     *              | inf(inity)?
544
 
     *              )
545
 
     *
546
 
     * case-insensitively.
547
 
     *
548
 
     * The "do { ... } while (0)" wrapping in macros ensures that they behave
549
 
     * properly eg. in "if ... else" structures.
550
 
     */
551
 
 
552
 
#define END_MATCH()                                                         \
553
 
        goto buffer_filled
554
 
 
555
 
#define NEXT_CHAR()                                                         \
556
 
        do {                                                                \
557
 
            if (c == EOF || endp >= buffer + FLOAT_FORMATBUFLEN)            \
558
 
                END_MATCH();                                                \
559
 
            *endp++ = (char)c;                                              \
560
 
            c = getc(fp);                                                   \
561
 
        } while (0)
562
 
 
563
 
#define MATCH_ALPHA_STRING_NOCASE(string)                                   \
564
 
        do {                                                                \
565
 
            for (p=(string); *p!='\0' && (c==*p || c+('a'-'A')==*p); ++p)   \
566
 
                NEXT_CHAR();                                                \
567
 
            if (*p != '\0') END_MATCH();                                    \
568
 
        } while (0)
569
 
 
570
 
#define MATCH_ONE_OR_NONE(condition)                                        \
571
 
        do { if (condition) NEXT_CHAR(); } while (0)
572
 
 
573
 
#define MATCH_ONE_OR_MORE(condition)                                        \
574
 
        do {                                                                \
575
 
            ok = 0;                                                         \
576
 
            while (condition) { NEXT_CHAR(); ok = 1; }                      \
577
 
            if (!ok) END_MATCH();                                           \
578
 
        } while (0)
579
 
 
580
 
#define MATCH_ZERO_OR_MORE(condition)                                       \
581
 
        while (condition) { NEXT_CHAR(); }
582
 
 
583
 
    /* 1. emulate fscanf EOF handling */
584
 
    c = getc(fp);
585
 
    if (c == EOF) {
586
 
        return EOF;
587
 
    }
588
 
    /* 2. consume leading whitespace unconditionally */
589
 
    while (NumPyOS_ascii_isspace(c)) {
590
 
        c = getc(fp);
591
 
    }
592
 
 
593
 
    /* 3. start reading matching input to buffer */
594
 
    endp = buffer;
595
 
 
596
 
    /* 4.1 sign (optional) */
597
 
    MATCH_ONE_OR_NONE(c == '+' || c == '-');
598
 
 
599
 
    /* 4.2 nan, inf, infinity; [case-insensitive] */
600
 
    if (c == 'n' || c == 'N') {
601
 
        NEXT_CHAR();
602
 
        MATCH_ALPHA_STRING_NOCASE("an");
603
 
 
604
 
        /* accept nan([:alphanum:_]*), similarly to strtod */
605
 
        if (c == '(') {
606
 
            NEXT_CHAR();
607
 
            MATCH_ZERO_OR_MORE(NumPyOS_ascii_isalnum(c) || c == '_');
608
 
            if (c == ')') {
609
 
                NEXT_CHAR();
610
 
            }
611
 
        }
612
 
        END_MATCH();
613
 
    }
614
 
    else if (c == 'i' || c == 'I') {
615
 
        NEXT_CHAR();
616
 
        MATCH_ALPHA_STRING_NOCASE("nfinity");
617
 
        END_MATCH();
618
 
    }
619
 
 
620
 
    /* 4.3 mantissa */
621
 
    MATCH_ZERO_OR_MORE(NumPyOS_ascii_isdigit(c));
622
 
 
623
 
    if (c == '.') {
624
 
        NEXT_CHAR();
625
 
        MATCH_ONE_OR_MORE(NumPyOS_ascii_isdigit(c));
626
 
    }
627
 
 
628
 
    /* 4.4 exponent */
629
 
    if (c == 'e' || c == 'E') {
630
 
        NEXT_CHAR();
631
 
        MATCH_ONE_OR_NONE(c == '+' || c == '-');
632
 
        MATCH_ONE_OR_MORE(NumPyOS_ascii_isdigit(c));
633
 
    }
634
 
 
635
 
    END_MATCH();
636
 
 
637
 
buffer_filled:
638
 
 
639
 
    ungetc(c, fp);
640
 
    *endp = '\0';
641
 
 
642
 
    /* 5. try to convert buffer. */
643
 
    *value = NumPyOS_ascii_strtod(buffer, &p);
644
 
 
645
 
    /* return 1 if something read, else 0 */
646
 
    return (buffer == p) ? 0 : 1;
647
 
}
648
 
 
649
 
#undef END_MATCH
650
 
#undef NEXT_CHAR
651
 
#undef MATCH_ALPHA_STRING_NOCASE
652
 
#undef MATCH_ONE_OR_NONE
653
 
#undef MATCH_ONE_OR_MORE
654
 
#undef MATCH_ZERO_OR_MORE