blob: 5cab0bb1811b29e79992698abc617abb9b042c75 [file] [log] [blame]
Eric Smith5807c412008-05-11 21:00:57 +00001/* stringlib: locale related helpers implementation */
2
3#ifndef STRINGLIB_LOCALEUTIL_H
4#define STRINGLIB_LOCALEUTIL_H
5
6#include <locale.h>
7
8/**
9 * _Py_InsertThousandsGrouping:
10 * @buffer: A pointer to the start of a string.
11 * @len: The length of the string.
12 * @plast: A pointer to the end of of the digits in the string. This
13 * may be before the end of the string (if the string contains
14 * decimals, for example).
15 * @buf_size: The maximum size of the buffer pointed to by buffer.
16 * @count: If non-NULL, points to a variable that will receive the
17 * number of characters we need to insert (and no formatting
18 * will actually occur).
19 * @append_zero_char: If non-zero, put a trailing zero at the end of
20 * of the resulting string, if and only if we modified the
21 * string.
22 *
23 * Inserts thousand grouping characters (as defined in the current
24 * locale) into the string between buffer and plast. If count is
25 * non-NULL, don't do any formatting, just count the number of
26 * characters to insert. This is used by the caller to appropriately
27 * resize the buffer, if needed.
28 *
29 * Return value: 0 on error, else 1. Note that no error can occur if
30 * count is non-NULL.
31 *
32 * This name won't be used, the includer of this file should define
33 * it to be the actual function name, based on unicode or string.
34 **/
35int
36_Py_InsertThousandsGrouping(STRINGLIB_CHAR *buffer,
37 Py_ssize_t len,
38 STRINGLIB_CHAR *plast,
39 Py_ssize_t buf_size,
40 Py_ssize_t *count,
41 int append_zero_char)
42{
43 struct lconv *locale_data = localeconv();
44 const char *grouping = locale_data->grouping;
45 const char *thousands_sep = locale_data->thousands_sep;
46 Py_ssize_t thousands_sep_len = strlen(thousands_sep);
47 STRINGLIB_CHAR *pend = buffer + len; /* current end of buffer */
48 STRINGLIB_CHAR *pmax = buffer + buf_size; /* max of buffer */
49 char current_grouping;
50
51 /* Initialize the character count, if we're just counting. */
52 if (count)
53 *count = 0;
54
55 /* Starting at plast and working right-to-left, keep track of
56 what grouping needs to be added and insert that. */
57 current_grouping = *grouping++;
58
59 /* If the first character is 0, perform no grouping at all. */
60 if (current_grouping == 0)
61 return 1;
62
63 while (plast - buffer > current_grouping) {
64 /* Always leave buffer and pend valid at the end of this
65 loop, since we might leave with a return statement. */
66
67 plast -= current_grouping;
68 if (count) {
69 /* We're only counting, not touching the memory. */
70 *count += thousands_sep_len;
71 }
72 else {
73 /* Do the formatting. */
74
75 /* Is there room to insert thousands_sep_len chars? */
76 if (pmax - pend < thousands_sep_len)
77 /* No room. */
78 return 0;
79
80 /* Move the rest of the string down. */
81 memmove(plast + thousands_sep_len,
82 plast,
83 (pend - plast) * sizeof(STRINGLIB_CHAR));
84 /* Copy the thousands_sep chars into the buffer. */
85#if STRINGLIB_IS_UNICODE
86 /* Convert from the char's of the thousands_sep from
87 the locale into unicode. */
88 {
89 Py_ssize_t i;
90 for (i = 0; i < thousands_sep_len; ++i)
91 plast[i] = thousands_sep[i];
92 }
93#else
94 /* No conversion, just memcpy the thousands_sep. */
95 memcpy(plast, thousands_sep, thousands_sep_len);
96#endif
97 }
98
99 /* Adjust end pointer. */
100 pend += thousands_sep_len;
101
102 /* Move to the next grouping character, unless we're
103 repeating (which is designated by a grouping of 0). */
104 if (*grouping != 0) {
105 current_grouping = *grouping++;
106 if (current_grouping == CHAR_MAX)
107 /* We're done. */
108 break;
109 }
110 }
111 if (append_zero_char) {
112 /* Append a zero character to mark the end of the string,
113 if there's room. */
114 if (pend - plast < 1)
115 /* No room, error. */
116 return 0;
117 *pend = 0;
118 }
119 return 1;
120}
121#endif /* STRINGLIB_LOCALEUTIL_H */