1 /* stringlib: locale related helpers implementation */
3 #ifndef STRINGLIB_LOCALEUTIL_H
4 #define STRINGLIB_LOCALEUTIL_H
8 // Prevent conflicts with EFI
12 #define MAX(x, y) ((x) < (y) ? (y) : (x))
13 #define MIN(x, y) ((x) < (y) ? (x) : (y))
18 Py_ssize_t i
; /* Where we're currently pointing in grouping. */
22 _GroupGenerator_init(GroupGenerator
*self
, const char *grouping
)
24 self
->grouping
= grouping
;
29 /* Returns the next grouping, or 0 to signify end. */
31 _GroupGenerator_next(GroupGenerator
*self
)
33 /* Note that we don't really do much error checking here. If a
34 grouping string contains just CHAR_MAX, for example, then just
35 terminate the generator. That shouldn't happen, but at least we
37 switch (self
->grouping
[self
->i
]) {
39 return self
->previous
;
41 /* Stop the generator. */
44 char ch
= self
->grouping
[self
->i
];
47 return (Py_ssize_t
)ch
;
52 /* Fill in some digits, leading zeros, and thousands separator. All
53 are optional, depending on when we're called. */
55 fill(STRINGLIB_CHAR
**digits_end
, STRINGLIB_CHAR
**buffer_end
,
56 Py_ssize_t n_chars
, Py_ssize_t n_zeros
, const char* thousands_sep
,
57 Py_ssize_t thousands_sep_len
)
59 #if STRINGLIB_IS_UNICODE
64 *buffer_end
-= thousands_sep_len
;
66 /* Copy the thousands_sep chars into the buffer. */
67 #if STRINGLIB_IS_UNICODE
68 /* Convert from the char's of the thousands_sep from
69 the locale into unicode. */
70 for (i
= 0; i
< thousands_sep_len
; ++i
)
71 (*buffer_end
)[i
] = thousands_sep
[i
];
73 /* No conversion, just memcpy the thousands_sep. */
74 memcpy(*buffer_end
, thousands_sep
, thousands_sep_len
);
78 *buffer_end
-= n_chars
;
79 *digits_end
-= n_chars
;
80 memcpy(*buffer_end
, *digits_end
, n_chars
* sizeof(STRINGLIB_CHAR
));
82 *buffer_end
-= n_zeros
;
83 STRINGLIB_FILL(*buffer_end
, '0', n_zeros
);
87 * _Py_InsertThousandsGrouping:
88 * @buffer: A pointer to the start of a string.
89 * @n_buffer: Number of characters in @buffer.
90 * @digits: A pointer to the digits we're reading from. If count
91 * is non-NULL, this is unused.
92 * @n_digits: The number of digits in the string, in which we want
93 * to put the grouping chars.
94 * @min_width: The minimum width of the digits in the output string.
95 * Output will be zero-padded on the left to fill.
96 * @grouping: see definition in localeconv().
97 * @thousands_sep: see definition in localeconv().
99 * There are 2 modes: counting and filling. If @buffer is NULL,
100 * we are in counting mode, else filling mode.
101 * If counting, the required buffer size is returned.
102 * If filling, we know the buffer will be large enough, so we don't
103 * need to pass in the buffer size.
104 * Inserts thousand grouping characters (as defined by grouping and
105 * thousands_sep) into the string between buffer and buffer+n_digits.
107 * Return value: 0 on error, else 1. Note that no error can occur if
110 * This name won't be used, the includer of this file should define
111 * it to be the actual function name, based on unicode or string.
113 * As closely as possible, this code mimics the logic in decimal.py's
114 _insert_thousands_sep().
117 _Py_InsertThousandsGrouping(STRINGLIB_CHAR
*buffer
,
119 STRINGLIB_CHAR
*digits
,
121 Py_ssize_t min_width
,
122 const char *grouping
,
123 const char *thousands_sep
)
125 Py_ssize_t count
= 0;
128 int use_separator
= 0; /* First time through, don't append the
129 separator. They only go between
131 STRINGLIB_CHAR
*buffer_end
= NULL
;
132 STRINGLIB_CHAR
*digits_end
= NULL
;
135 Py_ssize_t thousands_sep_len
= strlen(thousands_sep
);
136 Py_ssize_t remaining
= n_digits
; /* Number of chars remaining to
138 /* A generator that returns all of the grouping widths, until it
140 GroupGenerator groupgen
;
141 _GroupGenerator_init(&groupgen
, grouping
);
144 buffer_end
= buffer
+ n_buffer
;
145 digits_end
= digits
+ n_digits
;
148 while ((l
= _GroupGenerator_next(&groupgen
)) > 0) {
149 l
= MIN(l
, MAX(MAX(remaining
, min_width
), 1));
150 n_zeros
= MAX(0, l
- remaining
);
151 n_chars
= MAX(0, MIN(remaining
, l
));
153 /* Use n_zero zero's and n_chars chars */
155 /* Count only, don't do anything. */
156 count
+= (use_separator
? thousands_sep_len
: 0) + n_zeros
+ n_chars
;
159 /* Copy into the output buffer. */
160 fill(&digits_end
, &buffer_end
, n_chars
, n_zeros
,
161 use_separator
? thousands_sep
: NULL
, thousands_sep_len
);
164 /* Use a separator next time. */
167 remaining
-= n_chars
;
170 if (remaining
<= 0 && min_width
<= 0) {
174 min_width
-= thousands_sep_len
;
177 /* We left the loop without using a break statement. */
179 l
= MAX(MAX(remaining
, min_width
), 1);
180 n_zeros
= MAX(0, l
- remaining
);
181 n_chars
= MAX(0, MIN(remaining
, l
));
183 /* Use n_zero zero's and n_chars chars */
184 count
+= (use_separator
? thousands_sep_len
: 0) + n_zeros
+ n_chars
;
186 /* Copy into the output buffer. */
187 fill(&digits_end
, &buffer_end
, n_chars
, n_zeros
,
188 use_separator
? thousands_sep
: NULL
, thousands_sep_len
);
195 * _Py_InsertThousandsGroupingLocale:
196 * @buffer: A pointer to the start of a string.
197 * @n_digits: The number of digits in the string, in which we want
198 * to put the grouping chars.
200 * Reads thee current locale and calls _Py_InsertThousandsGrouping().
203 _Py_InsertThousandsGroupingLocale(STRINGLIB_CHAR
*buffer
,
205 STRINGLIB_CHAR
*digits
,
207 Py_ssize_t min_width
)
209 struct lconv
*locale_data
= localeconv();
210 const char *grouping
= locale_data
->grouping
;
211 const char *thousands_sep
= locale_data
->thousands_sep
;
213 return _Py_InsertThousandsGrouping(buffer
, n_buffer
, digits
, n_digits
,
214 min_width
, grouping
, thousands_sep
);
216 #endif /* STRINGLIB_LOCALEUTIL_H */