22
33#include <locale.h>
44
5- #define MAX (x , y ) ((x) < (y) ? (y) : (x))
6- #define MIN (x , y ) ((x) < (y) ? (x) : (y))
5+ #ifndef STRINGLIB_IS_UNICODE
6+ # error "localeutil is specific to Unicode"
7+ #endif
78
89typedef struct {
910 const char * grouping ;
@@ -46,7 +47,7 @@ STRINGLIB(GroupGenerator_next)(STRINGLIB(GroupGenerator) *self)
4647 are optional, depending on when we're called. */
4748static void
4849STRINGLIB (fill )(STRINGLIB_CHAR * * digits_end , STRINGLIB_CHAR * * buffer_end ,
49- Py_ssize_t n_chars , Py_ssize_t n_zeros , const char * thousands_sep ,
50+ Py_ssize_t n_chars , Py_ssize_t n_zeros , STRINGLIB_CHAR * thousands_sep ,
5051 Py_ssize_t thousands_sep_len )
5152{
5253 Py_ssize_t i ;
@@ -55,15 +56,8 @@ STRINGLIB(fill)(STRINGLIB_CHAR **digits_end, STRINGLIB_CHAR **buffer_end,
5556 * buffer_end -= thousands_sep_len ;
5657
5758 /* Copy the thousands_sep chars into the buffer. */
58- #if STRINGLIB_IS_UNICODE
59- /* Convert from the char's of the thousands_sep from
60- the locale into unicode. */
61- for (i = 0 ; i < thousands_sep_len ; ++ i )
62- (* buffer_end )[i ] = thousands_sep [i ];
63- #else
64- /* No conversion, just memcpy the thousands_sep. */
65- memcpy (* buffer_end , thousands_sep , thousands_sep_len );
66- #endif
59+ memcpy (* buffer_end , thousands_sep ,
60+ thousands_sep_len * STRINGLIB_SIZEOF_CHAR );
6761 }
6862
6963 * buffer_end -= n_chars ;
@@ -76,7 +70,7 @@ STRINGLIB(fill)(STRINGLIB_CHAR **digits_end, STRINGLIB_CHAR **buffer_end,
7670}
7771
7872/**
79- * _Py_InsertThousandsGrouping :
73+ * InsertThousandsGrouping :
8074 * @buffer: A pointer to the start of a string.
8175 * @n_buffer: Number of characters in @buffer.
8276 * @digits: A pointer to the digits we're reading from. If count
@@ -106,13 +100,15 @@ STRINGLIB(fill)(STRINGLIB_CHAR **digits_end, STRINGLIB_CHAR **buffer_end,
106100 _insert_thousands_sep().
107101 **/
108102Py_ssize_t
109- _Py_InsertThousandsGrouping (STRINGLIB_CHAR * buffer ,
110- Py_ssize_t n_buffer ,
111- STRINGLIB_CHAR * digits ,
112- Py_ssize_t n_digits ,
113- Py_ssize_t min_width ,
114- const char * grouping ,
115- const char * thousands_sep )
103+ STRINGLIB (InsertThousandsGrouping )(
104+ STRINGLIB_CHAR * buffer ,
105+ Py_ssize_t n_buffer ,
106+ STRINGLIB_CHAR * digits ,
107+ Py_ssize_t n_digits ,
108+ Py_ssize_t min_width ,
109+ const char * grouping ,
110+ STRINGLIB_CHAR * thousands_sep ,
111+ Py_ssize_t thousands_sep_len )
116112{
117113 Py_ssize_t count = 0 ;
118114 Py_ssize_t n_zeros ;
@@ -124,7 +120,6 @@ _Py_InsertThousandsGrouping(STRINGLIB_CHAR *buffer,
124120 STRINGLIB_CHAR * digits_end = NULL ;
125121 Py_ssize_t l ;
126122 Py_ssize_t n_chars ;
127- Py_ssize_t thousands_sep_len = strlen (thousands_sep );
128123 Py_ssize_t remaining = n_digits ; /* Number of chars remaining to
129124 be looked at */
130125 /* A generator that returns all of the grouping widths, until it
@@ -138,9 +133,9 @@ _Py_InsertThousandsGrouping(STRINGLIB_CHAR *buffer,
138133 }
139134
140135 while ((l = STRINGLIB (GroupGenerator_next )(& groupgen )) > 0 ) {
141- l = MIN (l , MAX ( MAX (remaining , min_width ), 1 ));
142- n_zeros = MAX (0 , l - remaining );
143- n_chars = MAX (0 , MIN (remaining , l ));
136+ l = Py_MIN (l , Py_MAX ( Py_MAX (remaining , min_width ), 1 ));
137+ n_zeros = Py_MAX (0 , l - remaining );
138+ n_chars = Py_MAX (0 , Py_MIN (remaining , l ));
144139
145140 /* Use n_zero zero's and n_chars chars */
146141
@@ -168,9 +163,9 @@ _Py_InsertThousandsGrouping(STRINGLIB_CHAR *buffer,
168163 if (!loop_broken ) {
169164 /* We left the loop without using a break statement. */
170165
171- l = MAX ( MAX (remaining , min_width ), 1 );
172- n_zeros = MAX (0 , l - remaining );
173- n_chars = MAX (0 , MIN (remaining , l ));
166+ l = Py_MAX ( Py_MAX (remaining , min_width ), 1 );
167+ n_zeros = Py_MAX (0 , l - remaining );
168+ n_chars = Py_MAX (0 , Py_MIN (remaining , l ));
174169
175170 /* Use n_zero zero's and n_chars chars */
176171 count += (use_separator ? thousands_sep_len : 0 ) + n_zeros + n_chars ;
@@ -183,25 +178,3 @@ _Py_InsertThousandsGrouping(STRINGLIB_CHAR *buffer,
183178 return count ;
184179}
185180
186- /**
187- * _Py_InsertThousandsGroupingLocale:
188- * @buffer: A pointer to the start of a string.
189- * @n_digits: The number of digits in the string, in which we want
190- * to put the grouping chars.
191- *
192- * Reads thee current locale and calls _Py_InsertThousandsGrouping().
193- **/
194- Py_ssize_t
195- _Py_InsertThousandsGroupingLocale (STRINGLIB_CHAR * buffer ,
196- Py_ssize_t n_buffer ,
197- STRINGLIB_CHAR * digits ,
198- Py_ssize_t n_digits ,
199- Py_ssize_t min_width )
200- {
201- struct lconv * locale_data = localeconv ();
202- const char * grouping = locale_data -> grouping ;
203- const char * thousands_sep = locale_data -> thousands_sep ;
204-
205- return _Py_InsertThousandsGrouping (buffer , n_buffer , digits , n_digits ,
206- min_width , grouping , thousands_sep );
207- }
0 commit comments