Skip to content

Commit 6d7e7a7

Browse files
committed
Merged revisions 64491 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r64491 | eric.smith | 2008-06-23 20:42:10 -0400 (Mon, 23 Jun 2008) | 1 line Modified interface to _Py_[String|Unicode]InsertThousandsGrouping, in anticipation of fixing issue 3140. ........
1 parent 7d84055 commit 6d7e7a7

File tree

5 files changed

+33
-26
lines changed

5 files changed

+33
-26
lines changed

Include/bytesobject.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ PyAPI_FUNC(int) PyBytes_AsStringAndSize(
9191
see Objects/stringlib/localeutil.h */
9292

9393
PyAPI_FUNC(int) _PyBytes_InsertThousandsGrouping(char *buffer,
94-
Py_ssize_t len,
95-
char *plast,
94+
Py_ssize_t n_buffer,
95+
Py_ssize_t n_digits,
9696
Py_ssize_t buf_size,
9797
Py_ssize_t *count,
9898
int append_zero_char);

Include/unicodeobject.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,8 +1458,8 @@ PyAPI_FUNC(PyObject *) _PyUnicode_XStrip(
14581458
see Objects/stringlib/localeutil.h */
14591459

14601460
PyAPI_FUNC(int) _PyUnicode_InsertThousandsGrouping(Py_UNICODE *buffer,
1461-
Py_ssize_t len,
1462-
Py_UNICODE *plast,
1461+
Py_ssize_t n_buffer,
1462+
Py_ssize_t n_digits,
14631463
Py_ssize_t buf_size,
14641464
Py_ssize_t *count,
14651465
int append_zero_char);

Objects/stringlib/formatter.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -563,8 +563,7 @@ format_int_or_long_internal(PyObject *value, const InternalFormatSpec *format,
563563
if (format->type == 'n')
564564
/* Compute how many additional chars we need to allocate
565565
to hold the thousands grouping. */
566-
STRINGLIB_GROUPING(pnumeric_chars, n_digits,
567-
pnumeric_chars+n_digits,
566+
STRINGLIB_GROUPING(NULL, n_digits, n_digits,
568567
0, &n_grouping_chars, 0);
569568

570569
/* Allocate a new string to hold the result */
@@ -592,8 +591,7 @@ format_int_or_long_internal(PyObject *value, const InternalFormatSpec *format,
592591
/* We know this can't fail, since we've already
593592
reserved enough space. */
594593
STRINGLIB_CHAR *pstart = p + n_leading_chars;
595-
int r = STRINGLIB_GROUPING(pstart, n_digits,
596-
pstart + n_digits,
594+
int r = STRINGLIB_GROUPING(pstart, n_digits, n_digits,
597595
spec.n_total+n_grouping_chars-n_leading_chars,
598596
NULL, 0);
599597
assert(r);

Objects/stringlib/localeutil.h

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
/**
99
* _Py_InsertThousandsGrouping:
1010
* @buffer: A pointer to the start of a string.
11-
* @len: The length of the string.
12-
* @plast: A pointer to the end of of the digits in the string. This
13-
* may be before the end of the string (if the string contains
14-
* decimals, for example).
11+
* @n_buffer: The length of the string.
12+
* @n_digits: The number of digits in the string, in which we want
13+
* to put the grouping chars.
1514
* @buf_size: The maximum size of the buffer pointed to by buffer.
1615
* @count: If non-NULL, points to a variable that will receive the
1716
* number of characters we need to insert (and no formatting
@@ -21,10 +20,11 @@
2120
* string.
2221
*
2322
* Inserts thousand grouping characters (as defined in the current
24-
* locale) into the string between buffer and plast. If count is
25-
* non-NULL, don't do any formatting, just count the number of
26-
* characters to insert. This is used by the caller to appropriately
27-
* resize the buffer, if needed.
23+
* locale) into the string between buffer and buffer+n_digits. If
24+
* count is non-NULL, don't do any formatting, just count the number
25+
* of characters to insert. This is used by the caller to
26+
* appropriately resize the buffer, if needed. If count is non-NULL,
27+
* buffer can be NULL (it is not dereferenced at all in that case).
2828
*
2929
* Return value: 0 on error, else 1. Note that no error can occur if
3030
* count is non-NULL.
@@ -34,8 +34,8 @@
3434
**/
3535
int
3636
_Py_InsertThousandsGrouping(STRINGLIB_CHAR *buffer,
37-
Py_ssize_t len,
38-
STRINGLIB_CHAR *plast,
37+
Py_ssize_t n_buffer,
38+
Py_ssize_t n_digits,
3939
Py_ssize_t buf_size,
4040
Py_ssize_t *count,
4141
int append_zero_char)
@@ -44,34 +44,43 @@ _Py_InsertThousandsGrouping(STRINGLIB_CHAR *buffer,
4444
const char *grouping = locale_data->grouping;
4545
const char *thousands_sep = locale_data->thousands_sep;
4646
Py_ssize_t thousands_sep_len = strlen(thousands_sep);
47-
STRINGLIB_CHAR *pend = buffer + len; /* current end of buffer */
48-
STRINGLIB_CHAR *pmax = buffer + buf_size; /* max of buffer */
47+
STRINGLIB_CHAR *pend = NULL; /* current end of buffer */
48+
STRINGLIB_CHAR *pmax = NULL; /* max of buffer */
4949
char current_grouping;
50+
Py_ssize_t remaining = n_digits; /* Number of chars remaining to
51+
be looked at */
5052

5153
/* Initialize the character count, if we're just counting. */
5254
if (count)
5355
*count = 0;
56+
else {
57+
/* We're not just counting, we're modifying buffer */
58+
pend = buffer + n_buffer;
59+
pmax = buffer + buf_size;
60+
}
5461

55-
/* Starting at plast and working right-to-left, keep track of
62+
/* Starting at the end and working right-to-left, keep track of
5663
what grouping needs to be added and insert that. */
5764
current_grouping = *grouping++;
5865

5966
/* If the first character is 0, perform no grouping at all. */
6067
if (current_grouping == 0)
6168
return 1;
6269

63-
while (plast - buffer > current_grouping) {
70+
while (remaining > current_grouping) {
6471
/* Always leave buffer and pend valid at the end of this
6572
loop, since we might leave with a return statement. */
6673

67-
plast -= current_grouping;
74+
remaining -= current_grouping;
6875
if (count) {
6976
/* We're only counting, not touching the memory. */
7077
*count += thousands_sep_len;
7178
}
7279
else {
7380
/* Do the formatting. */
7481

82+
STRINGLIB_CHAR *plast = buffer + remaining;
83+
7584
/* Is there room to insert thousands_sep_len chars? */
7685
if (pmax - pend < thousands_sep_len)
7786
/* No room. */
@@ -111,7 +120,7 @@ _Py_InsertThousandsGrouping(STRINGLIB_CHAR *buffer,
111120
if (append_zero_char) {
112121
/* Append a zero character to mark the end of the string,
113122
if there's room. */
114-
if (pend - plast < 1)
123+
if (pend - (buffer + remaining) < 1)
115124
/* No room, error. */
116125
return 0;
117126
*pend = 0;

Python/pystrtod.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,8 @@ add_thousands_grouping(char* buffer, size_t buf_size)
364364
/* At this point, p points just past the right-most character we
365365
want to format. We need to add the grouping string for the
366366
characters between buffer and p. */
367-
return _PyBytes_InsertThousandsGrouping(buffer, len, p,
368-
buf_size, NULL, 1);
367+
return _PyBytes_InsertThousandsGrouping(buffer, len, p-buffer,
368+
buf_size, NULL, 1);
369369
}
370370

371371
/* see FORMATBUFLEN in unicodeobject.c */

0 commit comments

Comments
 (0)