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
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.
3434 **/
3535int
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 ;
0 commit comments