blob: 31fed349caa57b13468c79da5bbd29c81b792094 [file] [log] [blame]
Victor Stinner6f5fa1b2018-11-26 14:17:01 +01001/* _PyUnicode_InsertThousandsGrouping() helper functions */
Eric Smith0923d1d2009-04-16 20:16:10 +00002
3typedef struct {
4 const char *grouping;
5 char previous;
6 Py_ssize_t i; /* Where we're currently pointing in grouping. */
Victor Stinner6f5fa1b2018-11-26 14:17:01 +01007} GroupGenerator;
8
Eric Smith0923d1d2009-04-16 20:16:10 +00009
10static void
Victor Stinner6f5fa1b2018-11-26 14:17:01 +010011GroupGenerator_init(GroupGenerator *self, const char *grouping)
Eric Smith0923d1d2009-04-16 20:16:10 +000012{
13 self->grouping = grouping;
14 self->i = 0;
15 self->previous = 0;
16}
17
Victor Stinner6f5fa1b2018-11-26 14:17:01 +010018
Eric Smith0923d1d2009-04-16 20:16:10 +000019/* Returns the next grouping, or 0 to signify end. */
20static Py_ssize_t
Victor Stinner6f5fa1b2018-11-26 14:17:01 +010021GroupGenerator_next(GroupGenerator *self)
Eric Smith0923d1d2009-04-16 20:16:10 +000022{
23 /* Note that we don't really do much error checking here. If a
24 grouping string contains just CHAR_MAX, for example, then just
25 terminate the generator. That shouldn't happen, but at least we
26 fail gracefully. */
27 switch (self->grouping[self->i]) {
28 case 0:
29 return self->previous;
30 case CHAR_MAX:
31 /* Stop the generator. */
32 return 0;
33 default: {
34 char ch = self->grouping[self->i];
35 self->previous = ch;
36 self->i++;
37 return (Py_ssize_t)ch;
38 }
39 }
40}
41
Victor Stinner6f5fa1b2018-11-26 14:17:01 +010042
Eric Smith0923d1d2009-04-16 20:16:10 +000043/* Fill in some digits, leading zeros, and thousands separator. All
44 are optional, depending on when we're called. */
45static void
Victor Stinner6f5fa1b2018-11-26 14:17:01 +010046InsertThousandsGrouping_fill(_PyUnicodeWriter *writer, Py_ssize_t *buffer_pos,
47 PyObject *digits, Py_ssize_t *digits_pos,
48 Py_ssize_t n_chars, Py_ssize_t n_zeros,
49 PyObject *thousands_sep, Py_ssize_t thousands_sep_len,
50 Py_UCS4 *maxchar)
Eric Smith0923d1d2009-04-16 20:16:10 +000051{
Victor Stinner6f5fa1b2018-11-26 14:17:01 +010052 if (!writer) {
53 /* if maxchar > 127, maxchar is already set */
54 if (*maxchar == 127 && thousands_sep) {
55 Py_UCS4 maxchar2 = PyUnicode_MAX_CHAR_VALUE(thousands_sep);
56 *maxchar = Py_MAX(*maxchar, maxchar2);
57 }
58 return;
59 }
Eric Smith0923d1d2009-04-16 20:16:10 +000060
61 if (thousands_sep) {
Victor Stinner6f5fa1b2018-11-26 14:17:01 +010062 *buffer_pos -= thousands_sep_len;
Eric Smith0923d1d2009-04-16 20:16:10 +000063
64 /* Copy the thousands_sep chars into the buffer. */
Victor Stinner6f5fa1b2018-11-26 14:17:01 +010065 _PyUnicode_FastCopyCharacters(writer->buffer, *buffer_pos,
66 thousands_sep, 0,
67 thousands_sep_len);
Eric Smith0923d1d2009-04-16 20:16:10 +000068 }
69
Victor Stinner6f5fa1b2018-11-26 14:17:01 +010070 *buffer_pos -= n_chars;
71 *digits_pos -= n_chars;
72 _PyUnicode_FastCopyCharacters(writer->buffer, *buffer_pos,
73 digits, *digits_pos,
74 n_chars);
Eric Smith0923d1d2009-04-16 20:16:10 +000075
Victor Stinner6f5fa1b2018-11-26 14:17:01 +010076 if (n_zeros) {
77 *buffer_pos -= n_zeros;
78 enum PyUnicode_Kind kind = PyUnicode_KIND(writer->buffer);
79 void *data = PyUnicode_DATA(writer->buffer);
80 FILL(kind, data, '0', *buffer_pos, n_zeros);
81 }
Eric Smith0923d1d2009-04-16 20:16:10 +000082}