annotate Objects/stringlib/localeutil.h @ 78069:a8aa77458e12 3.2

Add c_size_t and c_ssize_t to the ctypes summary table.
author Antoine Pitrou <solipsis@pitrou.net>
date Thu, 12 Jul 2012 20:31:50 +0200
parents b653eab13b73
children 8beaa9a37387
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
46909
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
diff changeset
1 /* stringlib: locale related helpers implementation */
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
diff changeset
2
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
diff changeset
3 #ifndef STRINGLIB_LOCALEUTIL_H
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
diff changeset
4 #define STRINGLIB_LOCALEUTIL_H
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
diff changeset
5
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
diff changeset
6 #include <locale.h>
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
diff changeset
7
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
8 #define MAX(x, y) ((x) < (y) ? (y) : (x))
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
9 #define MIN(x, y) ((x) < (y) ? (x) : (y))
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
10
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
11 typedef struct {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
12 const char *grouping;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
13 char previous;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
14 Py_ssize_t i; /* Where we're currently pointing in grouping. */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
15 } GroupGenerator;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
16
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
17 static void
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
18 _GroupGenerator_init(GroupGenerator *self, const char *grouping)
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
19 {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
20 self->grouping = grouping;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
21 self->i = 0;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
22 self->previous = 0;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
23 }
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
24
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
25 /* Returns the next grouping, or 0 to signify end. */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
26 static Py_ssize_t
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
27 _GroupGenerator_next(GroupGenerator *self)
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
28 {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
29 /* Note that we don't really do much error checking here. If a
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
30 grouping string contains just CHAR_MAX, for example, then just
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
31 terminate the generator. That shouldn't happen, but at least we
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
32 fail gracefully. */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
33 switch (self->grouping[self->i]) {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
34 case 0:
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
35 return self->previous;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
36 case CHAR_MAX:
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
37 /* Stop the generator. */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
38 return 0;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
39 default: {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
40 char ch = self->grouping[self->i];
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
41 self->previous = ch;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
42 self->i++;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
43 return (Py_ssize_t)ch;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
44 }
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
45 }
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
46 }
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
47
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
48 /* Fill in some digits, leading zeros, and thousands separator. All
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
49 are optional, depending on when we're called. */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
50 static void
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
51 fill(STRINGLIB_CHAR **digits_end, STRINGLIB_CHAR **buffer_end,
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
52 Py_ssize_t n_chars, Py_ssize_t n_zeros, const char* thousands_sep,
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
53 Py_ssize_t thousands_sep_len)
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
54 {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
55 #if STRINGLIB_IS_UNICODE
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
56 Py_ssize_t i;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
57 #endif
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
58
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
59 if (thousands_sep) {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
60 *buffer_end -= thousands_sep_len;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
61
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
62 /* Copy the thousands_sep chars into the buffer. */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
63 #if STRINGLIB_IS_UNICODE
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
64 /* Convert from the char's of the thousands_sep from
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
65 the locale into unicode. */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
66 for (i = 0; i < thousands_sep_len; ++i)
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
67 (*buffer_end)[i] = thousands_sep[i];
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
68 #else
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
69 /* No conversion, just memcpy the thousands_sep. */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
70 memcpy(*buffer_end, thousands_sep, thousands_sep_len);
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
71 #endif
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
72 }
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
73
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
74 *buffer_end -= n_chars;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
75 *digits_end -= n_chars;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
76 memcpy(*buffer_end, *digits_end, n_chars * sizeof(STRINGLIB_CHAR));
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
77
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
78 *buffer_end -= n_zeros;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
79 STRINGLIB_FILL(*buffer_end, '0', n_zeros);
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
80 }
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
81
46909
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
diff changeset
82 /**
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
diff changeset
83 * _Py_InsertThousandsGrouping:
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
diff changeset
84 * @buffer: A pointer to the start of a string.
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
85 * @n_buffer: Number of characters in @buffer.
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
86 * @digits: A pointer to the digits we're reading from. If count
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
87 * is non-NULL, this is unused.
48358
098173e844d1 Merged revisions 64491 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 46909
diff changeset
88 * @n_digits: The number of digits in the string, in which we want
098173e844d1 Merged revisions 64491 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 46909
diff changeset
89 * to put the grouping chars.
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
90 * @min_width: The minimum width of the digits in the output string.
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
91 * Output will be zero-padded on the left to fill.
52773
28096ed73153 Added ',' thousands grouping to int.__format__. See PEP 378.
Eric Smith <eric@trueblade.com>
parents: 48358
diff changeset
92 * @grouping: see definition in localeconv().
28096ed73153 Added ',' thousands grouping to int.__format__. See PEP 378.
Eric Smith <eric@trueblade.com>
parents: 48358
diff changeset
93 * @thousands_sep: see definition in localeconv().
46909
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
diff changeset
94 *
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
95 * There are 2 modes: counting and filling. If @buffer is NULL,
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
96 * we are in counting mode, else filling mode.
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
97 * If counting, the required buffer size is returned.
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
98 * If filling, we know the buffer will be large enough, so we don't
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
99 * need to pass in the buffer size.
52773
28096ed73153 Added ',' thousands grouping to int.__format__. See PEP 378.
Eric Smith <eric@trueblade.com>
parents: 48358
diff changeset
100 * Inserts thousand grouping characters (as defined by grouping and
28096ed73153 Added ',' thousands grouping to int.__format__. See PEP 378.
Eric Smith <eric@trueblade.com>
parents: 48358
diff changeset
101 * thousands_sep) into the string between buffer and buffer+n_digits.
46909
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
diff changeset
102 *
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
diff changeset
103 * Return value: 0 on error, else 1. Note that no error can occur if
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
diff changeset
104 * count is non-NULL.
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
diff changeset
105 *
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
diff changeset
106 * This name won't be used, the includer of this file should define
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
diff changeset
107 * it to be the actual function name, based on unicode or string.
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
108 *
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
109 * As closely as possible, this code mimics the logic in decimal.py's
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
110 _insert_thousands_sep().
46909
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
diff changeset
111 **/
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
112 Py_ssize_t
46909
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
diff changeset
113 _Py_InsertThousandsGrouping(STRINGLIB_CHAR *buffer,
52773
28096ed73153 Added ',' thousands grouping to int.__format__. See PEP 378.
Eric Smith <eric@trueblade.com>
parents: 48358
diff changeset
114 Py_ssize_t n_buffer,
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
115 STRINGLIB_CHAR *digits,
52773
28096ed73153 Added ',' thousands grouping to int.__format__. See PEP 378.
Eric Smith <eric@trueblade.com>
parents: 48358
diff changeset
116 Py_ssize_t n_digits,
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
117 Py_ssize_t min_width,
52773
28096ed73153 Added ',' thousands grouping to int.__format__. See PEP 378.
Eric Smith <eric@trueblade.com>
parents: 48358
diff changeset
118 const char *grouping,
28096ed73153 Added ',' thousands grouping to int.__format__. See PEP 378.
Eric Smith <eric@trueblade.com>
parents: 48358
diff changeset
119 const char *thousands_sep)
46909
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
diff changeset
120 {
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
121 Py_ssize_t count = 0;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
122 Py_ssize_t n_zeros;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
123 int loop_broken = 0;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
124 int use_separator = 0; /* First time through, don't append the
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
125 separator. They only go between
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
126 groups. */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
127 STRINGLIB_CHAR *buffer_end = NULL;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
128 STRINGLIB_CHAR *digits_end = NULL;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
129 Py_ssize_t l;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
130 Py_ssize_t n_chars;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
131 Py_ssize_t thousands_sep_len = strlen(thousands_sep);
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
132 Py_ssize_t remaining = n_digits; /* Number of chars remaining to
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
133 be looked at */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
134 /* A generator that returns all of the grouping widths, until it
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
135 returns 0. */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
136 GroupGenerator groupgen;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
137 _GroupGenerator_init(&groupgen, grouping);
52773
28096ed73153 Added ',' thousands grouping to int.__format__. See PEP 378.
Eric Smith <eric@trueblade.com>
parents: 48358
diff changeset
138
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
139 if (buffer) {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
140 buffer_end = buffer + n_buffer;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
141 digits_end = digits + n_digits;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
142 }
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
143
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
144 while ((l = _GroupGenerator_next(&groupgen)) > 0) {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
145 l = MIN(l, MAX(MAX(remaining, min_width), 1));
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
146 n_zeros = MAX(0, l - remaining);
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
147 n_chars = MAX(0, MIN(remaining, l));
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
148
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
149 /* Use n_zero zero's and n_chars chars */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
150
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
151 /* Count only, don't do anything. */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
152 count += (use_separator ? thousands_sep_len : 0) + n_zeros + n_chars;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
153
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
154 if (buffer) {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
155 /* Copy into the output buffer. */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
156 fill(&digits_end, &buffer_end, n_chars, n_zeros,
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
157 use_separator ? thousands_sep : NULL, thousands_sep_len);
52773
28096ed73153 Added ',' thousands grouping to int.__format__. See PEP 378.
Eric Smith <eric@trueblade.com>
parents: 48358
diff changeset
158 }
46909
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
diff changeset
159
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
160 /* Use a separator next time. */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
161 use_separator = 1;
46909
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
diff changeset
162
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
163 remaining -= n_chars;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
164 min_width -= l;
46909
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
diff changeset
165
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
166 if (remaining <= 0 && min_width <= 0) {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
167 loop_broken = 1;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
168 break;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
169 }
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
170 min_width -= thousands_sep_len;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
171 }
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
172 if (!loop_broken) {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
173 /* We left the loop without using a break statement. */
52773
28096ed73153 Added ',' thousands grouping to int.__format__. See PEP 378.
Eric Smith <eric@trueblade.com>
parents: 48358
diff changeset
174
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
175 l = MAX(MAX(remaining, min_width), 1);
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
176 n_zeros = MAX(0, l - remaining);
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
177 n_chars = MAX(0, MIN(remaining, l));
46909
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
diff changeset
178
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
179 /* Use n_zero zero's and n_chars chars */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
180 count += (use_separator ? thousands_sep_len : 0) + n_zeros + n_chars;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
181 if (buffer) {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
182 /* Copy into the output buffer. */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
183 fill(&digits_end, &buffer_end, n_chars, n_zeros,
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
184 use_separator ? thousands_sep : NULL, thousands_sep_len);
52773
28096ed73153 Added ',' thousands grouping to int.__format__. See PEP 378.
Eric Smith <eric@trueblade.com>
parents: 48358
diff changeset
185 }
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
186 }
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
187 return count;
52773
28096ed73153 Added ',' thousands grouping to int.__format__. See PEP 378.
Eric Smith <eric@trueblade.com>
parents: 48358
diff changeset
188 }
46909
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
diff changeset
189
52773
28096ed73153 Added ',' thousands grouping to int.__format__. See PEP 378.
Eric Smith <eric@trueblade.com>
parents: 48358
diff changeset
190 /**
28096ed73153 Added ',' thousands grouping to int.__format__. See PEP 378.
Eric Smith <eric@trueblade.com>
parents: 48358
diff changeset
191 * _Py_InsertThousandsGroupingLocale:
28096ed73153 Added ',' thousands grouping to int.__format__. See PEP 378.
Eric Smith <eric@trueblade.com>
parents: 48358
diff changeset
192 * @buffer: A pointer to the start of a string.
28096ed73153 Added ',' thousands grouping to int.__format__. See PEP 378.
Eric Smith <eric@trueblade.com>
parents: 48358
diff changeset
193 * @n_digits: The number of digits in the string, in which we want
28096ed73153 Added ',' thousands grouping to int.__format__. See PEP 378.
Eric Smith <eric@trueblade.com>
parents: 48358
diff changeset
194 * to put the grouping chars.
28096ed73153 Added ',' thousands grouping to int.__format__. See PEP 378.
Eric Smith <eric@trueblade.com>
parents: 48358
diff changeset
195 *
28096ed73153 Added ',' thousands grouping to int.__format__. See PEP 378.
Eric Smith <eric@trueblade.com>
parents: 48358
diff changeset
196 * Reads thee current locale and calls _Py_InsertThousandsGrouping().
28096ed73153 Added ',' thousands grouping to int.__format__. See PEP 378.
Eric Smith <eric@trueblade.com>
parents: 48358
diff changeset
197 **/
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
198 Py_ssize_t
52773
28096ed73153 Added ',' thousands grouping to int.__format__. See PEP 378.
Eric Smith <eric@trueblade.com>
parents: 48358
diff changeset
199 _Py_InsertThousandsGroupingLocale(STRINGLIB_CHAR *buffer,
28096ed73153 Added ',' thousands grouping to int.__format__. See PEP 378.
Eric Smith <eric@trueblade.com>
parents: 48358
diff changeset
200 Py_ssize_t n_buffer,
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
201 STRINGLIB_CHAR *digits,
52773
28096ed73153 Added ',' thousands grouping to int.__format__. See PEP 378.
Eric Smith <eric@trueblade.com>
parents: 48358
diff changeset
202 Py_ssize_t n_digits,
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
203 Py_ssize_t min_width)
52773
28096ed73153 Added ',' thousands grouping to int.__format__. See PEP 378.
Eric Smith <eric@trueblade.com>
parents: 48358
diff changeset
204 {
28096ed73153 Added ',' thousands grouping to int.__format__. See PEP 378.
Eric Smith <eric@trueblade.com>
parents: 48358
diff changeset
205 struct lconv *locale_data = localeconv();
28096ed73153 Added ',' thousands grouping to int.__format__. See PEP 378.
Eric Smith <eric@trueblade.com>
parents: 48358
diff changeset
206 const char *grouping = locale_data->grouping;
28096ed73153 Added ',' thousands grouping to int.__format__. See PEP 378.
Eric Smith <eric@trueblade.com>
parents: 48358
diff changeset
207 const char *thousands_sep = locale_data->thousands_sep;
28096ed73153 Added ',' thousands grouping to int.__format__. See PEP 378.
Eric Smith <eric@trueblade.com>
parents: 48358
diff changeset
208
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
209 return _Py_InsertThousandsGrouping(buffer, n_buffer, digits, n_digits,
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
210 min_width, grouping, thousands_sep);
46909
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
diff changeset
211 }
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
diff changeset
212 #endif /* STRINGLIB_LOCALEUTIL_H */