Skip to content

Commit ca2e266

Browse files
author
eric.smith
committed
Now that PyOS_ascii_formatd supports the 'n' format, simplify the float formatting code to just call it.
git-svn-id: http://svn.python.org/projects/python/trunk@60910 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 2e1586e commit ca2e266

1 file changed

Lines changed: 15 additions & 40 deletions

File tree

Objects/stringlib/formatter.h

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,16 @@ parse_internal_render_format_spec(PyObject *format_spec,
130130
}
131131
else if (end-ptr >= 1 && is_alignment_token(ptr[0])) {
132132
format->align = ptr[0];
133-
ptr++;
133+
++ptr;
134134
}
135135

136136
/* Parse the various sign options */
137137
if (end-ptr >= 1 && is_sign_element(ptr[0])) {
138138
format->sign = ptr[0];
139-
ptr++;
139+
++ptr;
140140
#if ALLOW_PARENS_FOR_SIGN
141141
if (end-ptr >= 1 && ptr[0] == ')') {
142-
ptr++;
142+
++ptr;
143143
}
144144
#endif
145145
}
@@ -150,7 +150,7 @@ parse_internal_render_format_spec(PyObject *format_spec,
150150
if (format->align == '\0') {
151151
format->align = '=';
152152
}
153-
ptr++;
153+
++ptr;
154154
}
155155

156156
/* XXX add error checking */
@@ -165,7 +165,7 @@ parse_internal_render_format_spec(PyObject *format_spec,
165165

166166
/* Parse field precision */
167167
if (end-ptr && ptr[0] == '.') {
168-
ptr++;
168+
++ptr;
169169

170170
/* XXX add error checking */
171171
specified_width = get_integer(&ptr, end, &format->precision);
@@ -189,7 +189,7 @@ parse_internal_render_format_spec(PyObject *format_spec,
189189

190190
if (end-ptr == 1) {
191191
format->type = ptr[0];
192-
ptr++;
192+
++ptr;
193193
}
194194

195195
return 1;
@@ -570,7 +570,7 @@ format_int_or_long_internal(PyObject *value, const InternalFormatSpec *format,
570570
/* if X, convert to uppercase */
571571
if (format->type == 'X') {
572572
Py_ssize_t t;
573-
for (t = 0; t < n_digits; t++)
573+
for (t = 0; t < n_digits; ++t)
574574
p[t + n_leading_chars] = STRINGLIB_TOUPPER(p[t + n_leading_chars]);
575575
}
576576

@@ -596,37 +596,20 @@ strtounicode(Py_UNICODE *buffer, const char *charbuffer)
596596
{
597597
register Py_ssize_t i;
598598
Py_ssize_t len = strlen(charbuffer);
599-
for (i = len - 1; i >= 0; i--)
599+
for (i = len - 1; i >= 0; --i)
600600
buffer[i] = (Py_UNICODE) charbuffer[i];
601601

602602
return len;
603603
}
604604
#endif
605605

606-
/* the callback function to call to do the actual float formatting.
607-
it matches the definition of PyOS_ascii_formatd */
608-
typedef char*
609-
(*DoubleSnprintfFunction)(char *buffer, size_t buf_len,
610-
const char *format, double d);
611-
612-
/* just a wrapper to make PyOS_snprintf look like DoubleSnprintfFunction */
613-
static char*
614-
snprintf_double(char *buffer, size_t buf_len, const char *format, double d)
615-
{
616-
PyOS_snprintf(buffer, buf_len, format, d);
617-
return NULL;
618-
}
619-
620606
/* see FORMATBUFLEN in unicodeobject.c */
621607
#define FLOAT_FORMATBUFLEN 120
622608

623609
/* much of this is taken from unicodeobject.c */
624-
/* use type instead of format->type, so that it can be overridden by
625-
format_number() */
626610
static PyObject *
627-
_format_float(STRINGLIB_CHAR type, PyObject *value,
628-
const InternalFormatSpec *format,
629-
DoubleSnprintfFunction snprintf)
611+
format_float_internal(PyObject *value,
612+
const InternalFormatSpec *format)
630613
{
631614
/* fmt = '%.' + `prec` + `type` + '%%'
632615
worst case length = 2 + 10 (len of INT_MAX) + 1 + 2 = 15 (use 20)*/
@@ -658,6 +641,7 @@ _format_float(STRINGLIB_CHAR type, PyObject *value,
658641
char* trailing = "";
659642
STRINGLIB_CHAR *p;
660643
NumberFieldWidths spec;
644+
STRINGLIB_CHAR type = format->type;
661645

662646
#if STRINGLIB_IS_UNICODE
663647
Py_UNICODE unicodebuf[FLOAT_FORMATBUFLEN];
@@ -692,8 +676,8 @@ _format_float(STRINGLIB_CHAR type, PyObject *value,
692676
PyOS_snprintf(fmt, sizeof(fmt), "%%.%" PY_FORMAT_SIZE_T "d%c", precision,
693677
(char)type);
694678

695-
/* call the passed in function to do the actual formatting */
696-
snprintf(charbuf, sizeof(charbuf), fmt, x);
679+
/* do the actual formatting */
680+
PyOS_ascii_formatd(charbuf, sizeof(charbuf), fmt, x);
697681

698682
/* adding trailing to fmt with PyOS_snprintf doesn't work, not
699683
sure why. we'll just concatentate it here, no harm done. we
@@ -719,8 +703,8 @@ _format_float(STRINGLIB_CHAR type, PyObject *value,
719703
and skip it */
720704
sign = p[0];
721705
if (sign == '-') {
722-
p++;
723-
n_digits--;
706+
++p;
707+
--n_digits;
724708
}
725709

726710
calc_number_widths(&spec, sign, n_digits, format);
@@ -743,15 +727,6 @@ _format_float(STRINGLIB_CHAR type, PyObject *value,
743727
done:
744728
return result;
745729
}
746-
747-
static PyObject *
748-
format_float_internal(PyObject *value, const InternalFormatSpec *format)
749-
{
750-
if (format->type == 'n')
751-
return _format_float('f', value, format, snprintf_double);
752-
else
753-
return _format_float(format->type, value, format, PyOS_ascii_formatd);
754-
}
755730
#endif /* FORMAT_FLOAT */
756731

757732
/************************************************************************/

0 commit comments

Comments
 (0)