2 stringlib: locale related helpers implementation.
4 Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available under
6 the terms and conditions of the BSD License that accompanies this distribution.
7 The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 #ifndef STRINGLIB_LOCALEUTIL_H
15 #define STRINGLIB_LOCALEUTIL_H
19 // Prevent conflicts with EFI
23 #define MAX(x, y) ((x) < (y) ? (y) : (x))
24 #define MIN(x, y) ((x) < (y) ? (x) : (y))
29 Py_ssize_t i
; /* Where we're currently pointing in grouping. */
33 _GroupGenerator_init(GroupGenerator
*self
, const char *grouping
)
35 self
->grouping
= grouping
;
40 /* Returns the next grouping, or 0 to signify end. */
42 _GroupGenerator_next(GroupGenerator
*self
)
44 /* Note that we don't really do much error checking here. If a
45 grouping string contains just CHAR_MAX, for example, then just
46 terminate the generator. That shouldn't happen, but at least we
48 switch (self
->grouping
[self
->i
]) {
50 return self
->previous
;
52 /* Stop the generator. */
55 char ch
= self
->grouping
[self
->i
];
58 return (Py_ssize_t
)ch
;
63 /* Fill in some digits, leading zeros, and thousands separator. All
64 are optional, depending on when we're called. */
66 fill(STRINGLIB_CHAR
**digits_end
, STRINGLIB_CHAR
**buffer_end
,
67 Py_ssize_t n_chars
, Py_ssize_t n_zeros
, const char* thousands_sep
,
68 Py_ssize_t thousands_sep_len
)
70 #if STRINGLIB_IS_UNICODE
75 *buffer_end
-= thousands_sep_len
;
77 /* Copy the thousands_sep chars into the buffer. */
78 #if STRINGLIB_IS_UNICODE
79 /* Convert from the char's of the thousands_sep from
80 the locale into unicode. */
81 for (i
= 0; i
< thousands_sep_len
; ++i
)
82 (*buffer_end
)[i
] = thousands_sep
[i
];
84 /* No conversion, just memcpy the thousands_sep. */
85 memcpy(*buffer_end
, thousands_sep
, thousands_sep_len
);
89 *buffer_end
-= n_chars
;
90 *digits_end
-= n_chars
;
91 memcpy(*buffer_end
, *digits_end
, n_chars
* sizeof(STRINGLIB_CHAR
));
93 *buffer_end
-= n_zeros
;
94 STRINGLIB_FILL(*buffer_end
, '0', n_zeros
);
98 * _Py_InsertThousandsGrouping:
99 * @buffer: A pointer to the start of a string.
100 * @n_buffer: Number of characters in @buffer.
101 * @digits: A pointer to the digits we're reading from. If count
102 * is non-NULL, this is unused.
103 * @n_digits: The number of digits in the string, in which we want
104 * to put the grouping chars.
105 * @min_width: The minimum width of the digits in the output string.
106 * Output will be zero-padded on the left to fill.
107 * @grouping: see definition in localeconv().
108 * @thousands_sep: see definition in localeconv().
110 * There are 2 modes: counting and filling. If @buffer is NULL,
111 * we are in counting mode, else filling mode.
112 * If counting, the required buffer size is returned.
113 * If filling, we know the buffer will be large enough, so we don't
114 * need to pass in the buffer size.
115 * Inserts thousand grouping characters (as defined by grouping and
116 * thousands_sep) into the string between buffer and buffer+n_digits.
118 * Return value: 0 on error, else 1. Note that no error can occur if
121 * This name won't be used, the includer of this file should define
122 * it to be the actual function name, based on unicode or string.
124 * As closely as possible, this code mimics the logic in decimal.py's
125 _insert_thousands_sep().
128 _Py_InsertThousandsGrouping(STRINGLIB_CHAR
*buffer
,
130 STRINGLIB_CHAR
*digits
,
132 Py_ssize_t min_width
,
133 const char *grouping
,
134 const char *thousands_sep
)
136 Py_ssize_t count
= 0;
139 int use_separator
= 0; /* First time through, don't append the
140 separator. They only go between
142 STRINGLIB_CHAR
*buffer_end
= NULL
;
143 STRINGLIB_CHAR
*digits_end
= NULL
;
146 Py_ssize_t thousands_sep_len
= strlen(thousands_sep
);
147 Py_ssize_t remaining
= n_digits
; /* Number of chars remaining to
149 /* A generator that returns all of the grouping widths, until it
151 GroupGenerator groupgen
;
152 _GroupGenerator_init(&groupgen
, grouping
);
155 buffer_end
= buffer
+ n_buffer
;
156 digits_end
= digits
+ n_digits
;
159 while ((l
= _GroupGenerator_next(&groupgen
)) > 0) {
160 l
= MIN(l
, MAX(MAX(remaining
, min_width
), 1));
161 n_zeros
= MAX(0, l
- remaining
);
162 n_chars
= MAX(0, MIN(remaining
, l
));
164 /* Use n_zero zero's and n_chars chars */
166 /* Count only, don't do anything. */
167 count
+= (use_separator
? thousands_sep_len
: 0) + n_zeros
+ n_chars
;
170 /* Copy into the output buffer. */
171 fill(&digits_end
, &buffer_end
, n_chars
, n_zeros
,
172 use_separator
? thousands_sep
: NULL
, thousands_sep_len
);
175 /* Use a separator next time. */
178 remaining
-= n_chars
;
181 if (remaining
<= 0 && min_width
<= 0) {
185 min_width
-= thousands_sep_len
;
188 /* We left the loop without using a break statement. */
190 l
= MAX(MAX(remaining
, min_width
), 1);
191 n_zeros
= MAX(0, l
- remaining
);
192 n_chars
= MAX(0, MIN(remaining
, l
));
194 /* Use n_zero zero's and n_chars chars */
195 count
+= (use_separator
? thousands_sep_len
: 0) + n_zeros
+ n_chars
;
197 /* Copy into the output buffer. */
198 fill(&digits_end
, &buffer_end
, n_chars
, n_zeros
,
199 use_separator
? thousands_sep
: NULL
, thousands_sep_len
);
206 * _Py_InsertThousandsGroupingLocale:
207 * @buffer: A pointer to the start of a string.
208 * @n_digits: The number of digits in the string, in which we want
209 * to put the grouping chars.
211 * Reads thee current locale and calls _Py_InsertThousandsGrouping().
214 _Py_InsertThousandsGroupingLocale(STRINGLIB_CHAR
*buffer
,
216 STRINGLIB_CHAR
*digits
,
218 Py_ssize_t min_width
)
220 struct lconv
*locale_data
= localeconv();
221 const char *grouping
= locale_data
->grouping
;
222 const char *thousands_sep
= locale_data
->thousands_sep
;
224 return _Py_InsertThousandsGrouping(buffer
, n_buffer
, digits
, n_digits
,
225 min_width
, grouping
, thousands_sep
);
227 #endif /* STRINGLIB_LOCALEUTIL_H */