annotate Objects/stringlib/localeutil.h @ 65035:87f4b32905df 2.7

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