diff options
author | Neal Norwitz <nnorwitz@gmail.com> | 2005-12-18 05:03:17 (GMT) |
---|---|---|
committer | Neal Norwitz <nnorwitz@gmail.com> | 2005-12-18 05:03:17 (GMT) |
commit | e7214a130b3fb1c4850a1e644b9e240087f6e982 (patch) | |
tree | 00383dafee9f5c89477c155e5fd76bce0c0101fd /Python | |
parent | 87b801cc2dc99caa80a442dbf02ede350d3fc342 (diff) | |
download | cpython-e7214a130b3fb1c4850a1e644b9e240087f6e982.zip cpython-e7214a130b3fb1c4850a1e644b9e240087f6e982.tar.gz cpython-e7214a130b3fb1c4850a1e644b9e240087f6e982.tar.bz2 |
Get float() to be more portable across platforms. Disable hex strings.
Diffstat (limited to 'Python')
-rw-r--r-- | Python/pystrtod.c | 73 |
1 files changed, 25 insertions, 48 deletions
diff --git a/Python/pystrtod.c b/Python/pystrtod.c index ab25799..19f25da 100644 --- a/Python/pystrtod.c +++ b/Python/pystrtod.c @@ -38,8 +38,7 @@ * Return value: the #gdouble value. **/ double -PyOS_ascii_strtod(const char *nptr, - char **endptr) +PyOS_ascii_strtod(const char *nptr, char **endptr) { char *fail_pos; double val; @@ -49,7 +48,6 @@ PyOS_ascii_strtod(const char *nptr, const char *p, *decimal_point_pos; const char *end = NULL; /* Silence gcc */ -/* g_return_val_if_fail (nptr != NULL, 0); */ assert(nptr != NULL); fail_pos = NULL; @@ -73,64 +71,36 @@ PyOS_ascii_strtod(const char *nptr, if (*p == '+' || *p == '-') p++; - if (p[0] == '0' && - (p[1] == 'x' || p[1] == 'X')) + while (ISDIGIT(*p)) + p++; + + if (*p == '.') { - p += 2; - /* HEX - find the (optional) decimal point */ + decimal_point_pos = p++; - while (ISXDIGIT(*p)) + while (ISDIGIT(*p)) p++; - if (*p == '.') - { - decimal_point_pos = p++; - - while (ISXDIGIT(*p)) - p++; - - if (*p == 'p' || *p == 'P') - p++; - if (*p == '+' || *p == '-') - p++; - while (ISDIGIT(*p)) - p++; - end = p; - } - } - else - { + if (*p == 'e' || *p == 'E') + p++; + if (*p == '+' || *p == '-') + p++; while (ISDIGIT(*p)) p++; - - if (*p == '.') - { - decimal_point_pos = p++; - - while (ISDIGIT(*p)) - p++; - - if (*p == 'e' || *p == 'E') - p++; - if (*p == '+' || *p == '-') - p++; - while (ISDIGIT(*p)) - p++; - end = p; - } + end = p; } - /* For the other cases, we need not convert the decimal point */ + /* For the other cases, we need not convert the decimal point */ } - /* Set errno to zero, so that we can distinguish zero results - and underflows */ + /* Set errno to zero, so that we can distinguish zero results + and underflows */ errno = 0; if (decimal_point_pos) { char *copy, *c; - /* We need to convert the '.' to the locale specific decimal point */ + /* We need to convert the '.' to the locale specific decimal point */ copy = malloc(end - nptr + 1 + decimal_point_len); c = copy; @@ -155,8 +125,15 @@ PyOS_ascii_strtod(const char *nptr, free(copy); } - else - val = strtod(nptr, &fail_pos); + else { + unsigned i = 0; + if (nptr[i] == '-') + i++; + if (nptr[i] == '0' && (nptr[i+1] == 'x' || nptr[i+1] == 'X')) + fail_pos = nptr; + else + val = strtod(nptr, &fail_pos); + } if (endptr) *endptr = fail_pos; |