localeutil.h revision 4fd606d1f5abe38e1f42c38de1d2e895166bd0f4
/* stringlib: locale related helpers implementation */
#ifndef STRINGLIB_LOCALEUTIL_H
#define STRINGLIB_LOCALEUTIL_H
#include <locale.h>
// Prevent conflicts with EFI
#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) < (y) ? (x) : (y))
typedef struct {
const char *grouping;
char previous;
Py_ssize_t i; /* Where we're currently pointing in grouping. */
static void
{
self->i = 0;
}
/* Returns the next grouping, or 0 to signify end. */
static Py_ssize_t
{
/* Note that we don't really do much error checking here. If a
grouping string contains just CHAR_MAX, for example, then just
terminate the generator. That shouldn't happen, but at least we
fail gracefully. */
case 0:
case CHAR_MAX:
/* Stop the generator. */
return 0;
default: {
self->i++;
return (Py_ssize_t)ch;
}
}
}
/* Fill in some digits, leading zeros, and thousands separator. All
are optional, depending on when we're called. */
static void
{
Py_ssize_t i;
#endif
if (thousands_sep) {
/* Copy the thousands_sep chars into the buffer. */
/* Convert from the char's of the thousands_sep from
the locale into unicode. */
for (i = 0; i < thousands_sep_len; ++i)
(*buffer_end)[i] = thousands_sep[i];
#else
/* No conversion, just memcpy the thousands_sep. */
#endif
}
*buffer_end -= n_chars;
*digits_end -= n_chars;
*buffer_end -= n_zeros;
}
/**
* _Py_InsertThousandsGrouping:
* @buffer: A pointer to the start of a string.
* @n_buffer: Number of characters in @buffer.
* @digits: A pointer to the digits we're reading from. If count
* is non-NULL, this is unused.
* @n_digits: The number of digits in the string, in which we want
* to put the grouping chars.
* @min_width: The minimum width of the digits in the output string.
* Output will be zero-padded on the left to fill.
* @grouping: see definition in localeconv().
* @thousands_sep: see definition in localeconv().
*
* There are 2 modes: counting and filling. If @buffer is NULL,
* we are in counting mode, else filling mode.
* If counting, the required buffer size is returned.
* If filling, we know the buffer will be large enough, so we don't
* need to pass in the buffer size.
* Inserts thousand grouping characters (as defined by grouping and
* thousands_sep) into the string between buffer and buffer+n_digits.
*
* Return value: 0 on error, else 1. Note that no error can occur if
* count is non-NULL.
*
* This name won't be used, the includer of this file should define
* it to be the actual function name, based on unicode or string.
*
* As closely as possible, this code mimics the logic in decimal.py's
_insert_thousands_sep().
**/
const char *grouping,
const char *thousands_sep)
{
Py_ssize_t count = 0;
int loop_broken = 0;
int use_separator = 0; /* First time through, don't append the
separator. They only go between
groups. */
Py_ssize_t l;
be looked at */
/* A generator that returns all of the grouping widths, until it
returns 0. */
if (buffer) {
}
while ((l = _GroupGenerator_next(&groupgen)) > 0) {
/* Use n_zero zero's and n_chars chars */
/* Count only, don't do anything. */
if (buffer) {
/* Copy into the output buffer. */
}
/* Use a separator next time. */
use_separator = 1;
min_width -= l;
loop_broken = 1;
break;
}
}
if (!loop_broken) {
/* We left the loop without using a break statement. */
/* Use n_zero zero's and n_chars chars */
if (buffer) {
/* Copy into the output buffer. */
}
}
return count;
}
/**
* _Py_InsertThousandsGroupingLocale:
* @buffer: A pointer to the start of a string.
* @n_digits: The number of digits in the string, in which we want
* to put the grouping chars.
*
* Reads thee current locale and calls _Py_InsertThousandsGrouping().
**/
{
}
#endif /* STRINGLIB_LOCALEUTIL_H */