aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Python/pystrtod.c
diff options
context:
space:
mode:
Diffstat (limited to 'Python/pystrtod.c')
-rw-r--r--Python/pystrtod.c22
1 files changed, 20 insertions, 2 deletions
diff --git a/Python/pystrtod.c b/Python/pystrtod.c
index 1b27f0a3ad3..d77b846f040 100644
--- a/Python/pystrtod.c
+++ b/Python/pystrtod.c
@@ -916,6 +916,18 @@ char * PyOS_double_to_string(double val,
(flags & Py_DTSF_ALT ? "#" : ""), precision,
format_code);
_PyOS_ascii_formatd(buf, bufsize, format, val, precision);
+
+ if (flags & Py_DTSF_NO_NEG_0 && buf[0] == '-') {
+ char *buf2 = buf + 1;
+ while (*buf2 == '0' || *buf2 == '.') {
+ ++buf2;
+ }
+ if (*buf2 == 0 || *buf2 == 'e') {
+ size_t len = buf2 - buf + strlen(buf2);
+ assert(buf[len] == 0);
+ memmove(buf, buf+1, len);
+ }
+ }
}
/* Add sign when requested. It's convenient (esp. when formatting
@@ -995,8 +1007,8 @@ static char *
format_float_short(double d, char format_code,
int mode, int precision,
int always_add_sign, int add_dot_0_if_integer,
- int use_alt_formatting, const char * const *float_strings,
- int *type)
+ int use_alt_formatting, int no_negative_zero,
+ const char * const *float_strings, int *type)
{
char *buf = NULL;
char *p = NULL;
@@ -1022,6 +1034,11 @@ format_float_short(double d, char format_code,
assert(digits_end != NULL && digits_end >= digits);
digits_len = digits_end - digits;
+ if (no_negative_zero && sign == 1 &&
+ (digits_len == 0 || (digits_len == 1 && digits[0] == '0'))) {
+ sign = 0;
+ }
+
if (digits_len && !Py_ISDIGIT(digits[0])) {
/* Infinities and nans here; adapt Gay's output,
so convert Infinity to inf and NaN to nan, and
@@ -1301,6 +1318,7 @@ char * PyOS_double_to_string(double val,
flags & Py_DTSF_SIGN,
flags & Py_DTSF_ADD_DOT_0,
flags & Py_DTSF_ALT,
+ flags & Py_DTSF_NO_NEG_0,
float_strings, type);
}
#endif // _PY_SHORT_FLOAT_REPR == 1