Ignore:
Timestamp:
Mar 19, 2014, 11:11:30 AM (11 years ago)
Author:
dmik
Message:

python: Update vendor to 2.7.6.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/vendor/current/Python/strtod.c

    r2 r388  
     1#include <stdio.h>
     2#include <string.h>
     3
    14#include "pyconfig.h"
    25
     
    1518*/
    1619
    17 /*  This is an implementation of the strtod() function described in the 
     20/*  This is an implementation of the strtod() function described in the
    1821    System V manuals, with a different name to avoid linker problems.
    1922    All that str2dbl() does itself is check that the argument is well-formed
     
    2326    There are two reasons why this should be provided to the net:
    2427    (a) some UNIX systems do not yet have strtod(), or do not have it
    25         available in the BSD "universe" (but they do have atof()).
     28    available in the BSD "universe" (but they do have atof()).
    2629    (b) some of the UNIX systems that *do* have it get it wrong.
    27         (some crash with large arguments, some assign the wrong *ptr value).
     30    (some crash with large arguments, some assign the wrong *ptr value).
    2831    There is a reason why *we* are providing it: we need a correct version
    2932    of strtod(), and if we give this one away maybe someone will look for
    3033    mistakes in it and fix them for us (:-).
    3134*/
    32    
     35
    3336/*  The following constants are machine-specific.  MD{MIN,MAX}EXPT are
    3437    integers and MD{MIN,MAX}FRAC are strings such that
    35         0.${MDMAXFRAC}e${MDMAXEXPT} is the largest representable double,
    36         0.${MDMINFRAC}e${MDMINEXPT} is the smallest representable +ve double
     38    0.${MDMAXFRAC}e${MDMAXEXPT} is the largest representable double,
     39    0.${MDMINFRAC}e${MDMINEXPT} is the smallest representable +ve double
    3740    MD{MIN,MAX}FRAC must not have any trailing zeros.
    3841    The values here are for IEEE-754 64-bit floats.
     
    4548    we can't always trust that stuff to be there or to be correct.
    4649*/
    47 static  int     MDMINEXPT       = -323;
    48 static  char    MDMINFRAC[]     = "494065645841246544";
    49 static  double  ZERO            = 0.0;
     50static  int     MDMINEXPT       = -323;
     51static  char    MDMINFRAC[]     = "494065645841246544";
     52static  double  ZERO            = 0.0;
    5053
    51 static  int     MDMAXEXPT       = 309;
    52 static  char    MDMAXFRAC[]     = "17976931348623157";
    53 static  double  HUGE            = 1.7976931348623157e308;
     54static  int     MDMAXEXPT       = 309;
     55static  char    MDMAXFRAC[]     = "17976931348623157";
     56static  double  HUGE            = 1.7976931348623157e308;
    5457
    55 extern  double  atof(const char *);             /* Only called when result known to be ok */
     58extern  double  atof(const char *);             /* Only called when result known to be ok */
    5659
    5760#ifdef HAVE_ERRNO_H
    5861#include <errno.h>
    5962#endif
    60 extern  int     errno;
     63extern  int     errno;
    6164
    6265double strtod(char *str, char **ptr)
    6366{
    64         int sign, scale, dotseen;
    65         int esign, expt;
    66         char *save;
    67         register char *sp, *dp;
    68         register int c;
    69         char *buforg, *buflim;
    70         char buffer[64];                /* 45-digit significant + */
    71                                         /* 13-digit exponent */
    72         sp = str;
    73         while (*sp == ' ') sp++;
    74         sign = 1;
    75         if (*sp == '-') sign -= 2, sp++;
    76         dotseen = 0, scale = 0;
    77         dp = buffer;   
    78         *dp++ = '0'; *dp++ = '.';
    79         buforg = dp, buflim = buffer+48;
    80         for (save = sp; c = *sp; sp++)
    81             if (c == '.') {
    82                 if (dotseen) break;
    83                 dotseen++;
    84             } else
    85             if ((unsigned)(c-'0') > (unsigned)('9'-'0')) {
    86                 break;
    87             } else
    88             if (c == '0') {
    89                 if (dp != buforg) {
    90                     /* This is not the first digit, so we want to keep it */
    91                     if (dp < buflim) *dp++ = c;
    92                     if (!dotseen) scale++;
    93                 } else {
    94                     /* No non-zero digits seen yet */
    95                     /* If a . has been seen, scale must be adjusted */
    96                     if (dotseen) scale--;
    97                 }
    98             } else {
    99                 /* This is a nonzero digit, so we want to keep it */
    100                 if (dp < buflim) *dp++ = c;
    101                 /* If it precedes a ., scale must be adjusted */
    102                 if (!dotseen) scale++;
    103             }
    104         if (sp == save) {
    105             if (ptr) *ptr = str;
    106             errno = EDOM;               /* what should this be? */
    107             return ZERO;
    108         }
    109        
    110         while (dp > buforg && dp[-1] == '0') --dp;
    111         if (dp == buforg) *dp++ = '0';
    112         *dp = '\0';
    113         /*  Now the contents of buffer are
    114             +--+--------+-+--------+
    115             |0.|fraction|\|leftover|
    116             +--+--------+-+--------+
    117                         ^dp points here
    118             where fraction begins with 0 iff it is "0", and has at most
    119             45 digits in it, and leftover is at least 16 characters.
    120         */
    121         save = sp, expt = 0, esign = 1;
    122         do {
    123             c = *sp++;
    124             if (c != 'e' && c != 'E') break;
    125             c = *sp++;
    126             if (c == '-') esign -= 2, c = *sp++; else
    127             if (c == '+' /* || c == ' ' */ ) c = *sp++;
    128             if ((unsigned)(c-'0') > (unsigned)('9'-'0')) break;
    129             while (c == '0') c = *sp++;
    130             for (; (unsigned)(c-'0') <= (unsigned)('9'-'0'); c = *sp++)
    131                 expt = expt*10 + c-'0';     
    132             if (esign < 0) expt = -expt;
    133             save = sp-1;
    134         } while (0);
    135         if (ptr) *ptr = save;
    136         expt += scale;
    137         /*  Now the number is sign*0.fraction*10**expt  */
    138         errno = ERANGE;
    139         if (expt > MDMAXEXPT) {
    140             return HUGE*sign;
    141         } else
    142         if (expt == MDMAXEXPT) {
    143             if (strcmp(buforg, MDMAXFRAC) > 0) return HUGE*sign;
    144         } else
    145         if (expt < MDMINEXPT) {
    146             return ZERO*sign;
    147         } else
    148         if (expt == MDMINEXPT) {
    149             if (strcmp(buforg, MDMINFRAC) < 0) return ZERO*sign;
    150         }
    151         /*  We have now established that the number can be  */
    152         /*  represented without overflow or underflow  */
    153         (void) sprintf(dp, "E%d", expt);
    154         errno = 0;
    155         return atof(buffer)*sign;
     67    int sign, scale, dotseen;
     68    int esign, expt;
     69    char *save;
     70    register char *sp, *dp;
     71    register int c;
     72    char *buforg, *buflim;
     73    char buffer[64];                    /* 45-digit significant + */
     74                    /* 13-digit exponent */
     75    sp = str;
     76    while (*sp == ' ') sp++;
     77    sign = 1;
     78    if (*sp == '-') sign -= 2, sp++;
     79    dotseen = 0, scale = 0;
     80    dp = buffer;
     81    *dp++ = '0'; *dp++ = '.';
     82    buforg = dp, buflim = buffer+48;
     83    for (save = sp; (c = *sp); sp++)
     84        if (c == '.') {
     85        if (dotseen) break;
     86        dotseen++;
     87        } else
     88        if ((unsigned)(c-'0') > (unsigned)('9'-'0')) {
     89        break;
     90        } else
     91        if (c == '0') {
     92        if (dp != buforg) {
     93            /* This is not the first digit, so we want to keep it */
     94            if (dp < buflim) *dp++ = c;
     95            if (!dotseen) scale++;
     96        } else {
     97            /* No non-zero digits seen yet */
     98            /* If a . has been seen, scale must be adjusted */
     99            if (dotseen) scale--;
     100        }
     101        } else {
     102        /* This is a nonzero digit, so we want to keep it */
     103        if (dp < buflim) *dp++ = c;
     104        /* If it precedes a ., scale must be adjusted */
     105        if (!dotseen) scale++;
     106        }
     107    if (sp == save) {
     108        if (ptr) *ptr = str;
     109        errno = EDOM;                   /* what should this be? */
     110        return ZERO;
     111    }
     112
     113    while (dp > buforg && dp[-1] == '0') --dp;
     114    if (dp == buforg) *dp++ = '0';
     115    *dp = '\0';
     116    /*  Now the contents of buffer are
     117        +--+--------+-+--------+
     118        |0.|fraction|\|leftover|
     119        +--+--------+-+--------+
     120                    ^dp points here
     121        where fraction begins with 0 iff it is "0", and has at most
     122        45 digits in it, and leftover is at least 16 characters.
     123    */
     124    save = sp, expt = 0, esign = 1;
     125    do {
     126        c = *sp++;
     127        if (c != 'e' && c != 'E') break;
     128        c = *sp++;
     129        if (c == '-') esign -= 2, c = *sp++; else
     130        if (c == '+' /* || c == ' ' */ ) c = *sp++;
     131        if ((unsigned)(c-'0') > (unsigned)('9'-'0')) break;
     132        while (c == '0') c = *sp++;
     133        for (; (unsigned)(c-'0') <= (unsigned)('9'-'0'); c = *sp++)
     134        expt = expt*10 + c-'0';
     135        if (esign < 0) expt = -expt;
     136        save = sp-1;
     137    } while (0);
     138    if (ptr) *ptr = save;
     139    expt += scale;
     140    /*  Now the number is sign*0.fraction*10**expt  */
     141    errno = ERANGE;
     142    if (expt > MDMAXEXPT) {
     143        return HUGE*sign;
     144    } else
     145    if (expt == MDMAXEXPT) {
     146        if (strcmp(buforg, MDMAXFRAC) > 0) return HUGE*sign;
     147    } else
     148    if (expt < MDMINEXPT) {
     149        return ZERO*sign;
     150    } else
     151    if (expt == MDMINEXPT) {
     152        if (strcmp(buforg, MDMINFRAC) < 0) return ZERO*sign;
     153    }
     154    /*  We have now established that the number can be  */
     155    /*  represented without overflow or underflow  */
     156    (void) sprintf(dp, "E%d", expt);
     157    errno = 0;
     158    return atof(buffer)*sign;
    156159}
Note: See TracChangeset for help on using the changeset viewer.