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