summaryrefslogtreecommitdiffstats
path: root/Objects/stringlib/localeutil.h
diff options
context:
space:
mode:
authorSergey B Kirpichev <skirpichev@gmail.com>2025-02-25 15:27:07 (GMT)
committerGitHub <noreply@github.com>2025-02-25 15:27:07 (GMT)
commitf39a07be47cd9219eaf0e538ae32ad8239c88e66 (patch)
tree1b57568378401b44c9ffd8d3f237b13694842b5c /Objects/stringlib/localeutil.h
parentfa6a8140dd2a72da6df2a7dfafbf07045debf64d (diff)
downloadcpython-f39a07be47cd9219eaf0e538ae32ad8239c88e66.zip
cpython-f39a07be47cd9219eaf0e538ae32ad8239c88e66.tar.gz
cpython-f39a07be47cd9219eaf0e538ae32ad8239c88e66.tar.bz2
gh-87790: support thousands separators for formatting fractional part of floats (#125304)
```pycon >>> f"{123_456.123_456:_._f}" # Whole and fractional '123_456.123_456' >>> f"{123_456.123_456:_f}" # Integer component only '123_456.123456' >>> f"{123_456.123_456:._f}" # Fractional component only '123456.123_456' >>> f"{123_456.123_456:.4_f}" # with precision '123456.1_235' ```
Diffstat (limited to 'Objects/stringlib/localeutil.h')
-rw-r--r--Objects/stringlib/localeutil.h27
1 files changed, 21 insertions, 6 deletions
diff --git a/Objects/stringlib/localeutil.h b/Objects/stringlib/localeutil.h
index d77715e..a4ab701 100644
--- a/Objects/stringlib/localeutil.h
+++ b/Objects/stringlib/localeutil.h
@@ -47,7 +47,7 @@ InsertThousandsGrouping_fill(_PyUnicodeWriter *writer, Py_ssize_t *buffer_pos,
PyObject *digits, Py_ssize_t *digits_pos,
Py_ssize_t n_chars, Py_ssize_t n_zeros,
PyObject *thousands_sep, Py_ssize_t thousands_sep_len,
- Py_UCS4 *maxchar)
+ Py_UCS4 *maxchar, int forward)
{
if (!writer) {
/* if maxchar > 127, maxchar is already set */
@@ -59,24 +59,39 @@ InsertThousandsGrouping_fill(_PyUnicodeWriter *writer, Py_ssize_t *buffer_pos,
}
if (thousands_sep) {
- *buffer_pos -= thousands_sep_len;
-
+ if (!forward) {
+ *buffer_pos -= thousands_sep_len;
+ }
/* Copy the thousands_sep chars into the buffer. */
_PyUnicode_FastCopyCharacters(writer->buffer, *buffer_pos,
thousands_sep, 0,
thousands_sep_len);
+ if (forward) {
+ *buffer_pos += thousands_sep_len;
+ }
}
- *buffer_pos -= n_chars;
- *digits_pos -= n_chars;
+ if (!forward) {
+ *buffer_pos -= n_chars;
+ *digits_pos -= n_chars;
+ }
_PyUnicode_FastCopyCharacters(writer->buffer, *buffer_pos,
digits, *digits_pos,
n_chars);
+ if (forward) {
+ *buffer_pos += n_chars;
+ *digits_pos += n_chars;
+ }
if (n_zeros) {
- *buffer_pos -= n_zeros;
+ if (!forward) {
+ *buffer_pos -= n_zeros;
+ }
int kind = PyUnicode_KIND(writer->buffer);
void *data = PyUnicode_DATA(writer->buffer);
unicode_fill(kind, data, '0', *buffer_pos, n_zeros);
+ if (forward) {
+ *buffer_pos += n_zeros;
+ }
}
}