Mercurial > cpython-lru_cache-weakref
annotate Objects/stringlib/localeutil.h @ 87690:66c1c9f32567 default tip
Add use_weakref option to functools.lru_cache
The use_weakref option makes lru_cache create weak references to the function
arguments when creating the cache key, which prevents them from staying in the
memory forever even after all other references to them are gone.
This is a proof of concept for http://bugs.python.org/issue19859
| author | Radomir Dopieralski <devel@sheep.art.pl> |
|---|---|
| date | Tue, 03 Dec 2013 16:02:48 +0100 |
| parents | 890d5c8dc59f |
| children |
| 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 #include <locale.h> |
|
ef4ec968fdb6
Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
4 |
|
75231
f89e2f4cda88
Issue #13706: Fix format(int, "n") for locale with non-ASCII thousands separator
Victor Stinner <victor.stinner@haypocalc.com>
parents:
72475
diff
changeset
|
5 #ifndef STRINGLIB_IS_UNICODE |
|
f89e2f4cda88
Issue #13706: Fix format(int, "n") for locale with non-ASCII thousands separator
Victor Stinner <victor.stinner@haypocalc.com>
parents:
72475
diff
changeset
|
6 # error "localeutil is specific to Unicode" |
|
f89e2f4cda88
Issue #13706: Fix format(int, "n") for locale with non-ASCII thousands separator
Victor Stinner <victor.stinner@haypocalc.com>
parents:
72475
diff
changeset
|
7 #endif |
|
53187
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
8 |
|
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
9 typedef struct { |
|
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
10 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
|
11 char previous; |
|
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
12 Py_ssize_t i; /* Where we're currently pointing in grouping. */ |
| 72475 | 13 } STRINGLIB(GroupGenerator); |
|
53187
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
14 |
|
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
15 static void |
| 72475 | 16 STRINGLIB(GroupGenerator_init)(STRINGLIB(GroupGenerator) *self, const char *grouping) |
|
53187
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
17 { |
|
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
18 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
|
19 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
|
20 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
|
21 } |
|
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
22 |
|
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
23 /* 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
|
24 static Py_ssize_t |
| 72475 | 25 STRINGLIB(GroupGenerator_next)(STRINGLIB(GroupGenerator) *self) |
|
53187
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
26 { |
|
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
27 /* 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
|
28 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
|
29 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
|
30 fail gracefully. */ |
|
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
31 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
|
32 case 0: |
|
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
33 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
|
34 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
|
35 /* 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
|
36 return 0; |
|
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
37 default: { |
|
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
38 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
|
39 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
|
40 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 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
|
42 } |
|
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
43 } |
|
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 /* 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
|
47 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
|
48 static void |
| 72475 | 49 STRINGLIB(fill)(STRINGLIB_CHAR **digits_end, STRINGLIB_CHAR **buffer_end, |
|
75231
f89e2f4cda88
Issue #13706: Fix format(int, "n") for locale with non-ASCII thousands separator
Victor Stinner <victor.stinner@haypocalc.com>
parents:
72475
diff
changeset
|
50 Py_ssize_t n_chars, Py_ssize_t n_zeros, STRINGLIB_CHAR* thousands_sep, |
|
53187
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
51 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
|
52 { |
|
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 i; |
|
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 (thousands_sep) { |
|
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
56 *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
|
57 |
|
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
58 /* Copy the thousands_sep chars into the buffer. */ |
|
75231
f89e2f4cda88
Issue #13706: Fix format(int, "n") for locale with non-ASCII thousands separator
Victor Stinner <victor.stinner@haypocalc.com>
parents:
72475
diff
changeset
|
59 memcpy(*buffer_end, thousands_sep, |
|
f89e2f4cda88
Issue #13706: Fix format(int, "n") for locale with non-ASCII thousands separator
Victor Stinner <victor.stinner@haypocalc.com>
parents:
72475
diff
changeset
|
60 thousands_sep_len * STRINGLIB_SIZEOF_CHAR); |
|
53187
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 |
|
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
63 *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
|
64 *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
|
65 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
|
66 |
|
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 -= n_zeros; |
| 72475 | 68 for (i = 0; i < n_zeros; i++) |
| 69 (*buffer_end)[i] = '0'; | |
|
53187
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
70 } |
|
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
71 |
|
46909
ef4ec968fdb6
Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
72 /** |
|
75231
f89e2f4cda88
Issue #13706: Fix format(int, "n") for locale with non-ASCII thousands separator
Victor Stinner <victor.stinner@haypocalc.com>
parents:
72475
diff
changeset
|
73 * InsertThousandsGrouping: |
|
46909
ef4ec968fdb6
Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
74 * @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
|
75 * @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
|
76 * @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
|
77 * is non-NULL, this is unused. |
|
48358
098173e844d1
Merged revisions 64491 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
46909
diff
changeset
|
78 * @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
|
79 * 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
|
80 * @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
|
81 * 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
|
82 * @grouping: see definition in localeconv(). |
|
28096ed73153
Added ',' thousands grouping to int.__format__. See PEP 378.
Eric Smith <eric@trueblade.com>
parents:
48358
diff
changeset
|
83 * @thousands_sep: see definition in localeconv(). |
|
46909
ef4ec968fdb6
Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
84 * |
|
53187
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
85 * 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
|
86 * 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
|
87 * 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
|
88 * 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
|
89 * 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
|
90 * 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
|
91 * 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
|
92 * |
|
ef4ec968fdb6
Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
93 * 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
|
94 * count is non-NULL. |
|
ef4ec968fdb6
Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
95 * |
|
ef4ec968fdb6
Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
96 * 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
|
97 * 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
|
98 * |
|
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
99 * 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
|
100 _insert_thousands_sep(). |
|
46909
ef4ec968fdb6
Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
101 **/ |
|
77536
890d5c8dc59f
Make private function static (from `make smelly`)
Antoine Pitrou <solipsis@pitrou.net>
parents:
75231
diff
changeset
|
102 static Py_ssize_t |
|
75231
f89e2f4cda88
Issue #13706: Fix format(int, "n") for locale with non-ASCII thousands separator
Victor Stinner <victor.stinner@haypocalc.com>
parents:
72475
diff
changeset
|
103 STRINGLIB(InsertThousandsGrouping)( |
|
f89e2f4cda88
Issue #13706: Fix format(int, "n") for locale with non-ASCII thousands separator
Victor Stinner <victor.stinner@haypocalc.com>
parents:
72475
diff
changeset
|
104 STRINGLIB_CHAR *buffer, |
|
f89e2f4cda88
Issue #13706: Fix format(int, "n") for locale with non-ASCII thousands separator
Victor Stinner <victor.stinner@haypocalc.com>
parents:
72475
diff
changeset
|
105 Py_ssize_t n_buffer, |
|
f89e2f4cda88
Issue #13706: Fix format(int, "n") for locale with non-ASCII thousands separator
Victor Stinner <victor.stinner@haypocalc.com>
parents:
72475
diff
changeset
|
106 STRINGLIB_CHAR *digits, |
|
f89e2f4cda88
Issue #13706: Fix format(int, "n") for locale with non-ASCII thousands separator
Victor Stinner <victor.stinner@haypocalc.com>
parents:
72475
diff
changeset
|
107 Py_ssize_t n_digits, |
|
f89e2f4cda88
Issue #13706: Fix format(int, "n") for locale with non-ASCII thousands separator
Victor Stinner <victor.stinner@haypocalc.com>
parents:
72475
diff
changeset
|
108 Py_ssize_t min_width, |
|
f89e2f4cda88
Issue #13706: Fix format(int, "n") for locale with non-ASCII thousands separator
Victor Stinner <victor.stinner@haypocalc.com>
parents:
72475
diff
changeset
|
109 const char *grouping, |
|
f89e2f4cda88
Issue #13706: Fix format(int, "n") for locale with non-ASCII thousands separator
Victor Stinner <victor.stinner@haypocalc.com>
parents:
72475
diff
changeset
|
110 STRINGLIB_CHAR *thousands_sep, |
|
f89e2f4cda88
Issue #13706: Fix format(int, "n") for locale with non-ASCII thousands separator
Victor Stinner <victor.stinner@haypocalc.com>
parents:
72475
diff
changeset
|
111 Py_ssize_t thousands_sep_len) |
|
46909
ef4ec968fdb6
Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
112 { |
|
53187
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
113 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
|
114 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
|
115 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
|
116 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
|
117 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
|
118 groups. */ |
|
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
119 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
|
120 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
|
121 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
|
122 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
|
123 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
|
124 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
|
125 /* 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
|
126 returns 0. */ |
| 72475 | 127 STRINGLIB(GroupGenerator) groupgen; |
| 128 STRINGLIB(GroupGenerator_init)(&groupgen, grouping); | |
|
52773
28096ed73153
Added ',' thousands grouping to int.__format__. See PEP 378.
Eric Smith <eric@trueblade.com>
parents:
48358
diff
changeset
|
129 |
|
53187
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
130 if (buffer) { |
|
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
131 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
|
132 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
|
133 } |
|
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
134 |
| 72475 | 135 while ((l = STRINGLIB(GroupGenerator_next)(&groupgen)) > 0) { |
|
75231
f89e2f4cda88
Issue #13706: Fix format(int, "n") for locale with non-ASCII thousands separator
Victor Stinner <victor.stinner@haypocalc.com>
parents:
72475
diff
changeset
|
136 l = Py_MIN(l, Py_MAX(Py_MAX(remaining, min_width), 1)); |
|
f89e2f4cda88
Issue #13706: Fix format(int, "n") for locale with non-ASCII thousands separator
Victor Stinner <victor.stinner@haypocalc.com>
parents:
72475
diff
changeset
|
137 n_zeros = Py_MAX(0, l - remaining); |
|
f89e2f4cda88
Issue #13706: Fix format(int, "n") for locale with non-ASCII thousands separator
Victor Stinner <victor.stinner@haypocalc.com>
parents:
72475
diff
changeset
|
138 n_chars = Py_MAX(0, Py_MIN(remaining, l)); |
|
53187
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
139 |
|
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
140 /* 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
|
141 |
|
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
142 /* 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
|
143 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
|
144 |
|
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
145 if (buffer) { |
|
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
146 /* Copy into the output buffer. */ |
| 72475 | 147 STRINGLIB(fill)(&digits_end, &buffer_end, n_chars, n_zeros, |
|
53187
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
148 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
|
149 } |
|
46909
ef4ec968fdb6
Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
150 |
|
53187
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
151 /* 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
|
152 use_separator = 1; |
|
46909
ef4ec968fdb6
Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
153 |
|
53187
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
154 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
|
155 min_width -= l; |
|
46909
ef4ec968fdb6
Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
156 |
|
53187
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
157 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
|
158 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
|
159 break; |
|
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
160 } |
|
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
161 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
|
162 } |
|
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
163 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
|
164 /* 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
|
165 |
|
75231
f89e2f4cda88
Issue #13706: Fix format(int, "n") for locale with non-ASCII thousands separator
Victor Stinner <victor.stinner@haypocalc.com>
parents:
72475
diff
changeset
|
166 l = Py_MAX(Py_MAX(remaining, min_width), 1); |
|
f89e2f4cda88
Issue #13706: Fix format(int, "n") for locale with non-ASCII thousands separator
Victor Stinner <victor.stinner@haypocalc.com>
parents:
72475
diff
changeset
|
167 n_zeros = Py_MAX(0, l - remaining); |
|
f89e2f4cda88
Issue #13706: Fix format(int, "n") for locale with non-ASCII thousands separator
Victor Stinner <victor.stinner@haypocalc.com>
parents:
72475
diff
changeset
|
168 n_chars = Py_MAX(0, Py_MIN(remaining, l)); |
|
46909
ef4ec968fdb6
Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
169 |
|
53187
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
170 /* 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
|
171 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
|
172 if (buffer) { |
|
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
173 /* Copy into the output buffer. */ |
| 72475 | 174 STRINGLIB(fill)(&digits_end, &buffer_end, n_chars, n_zeros, |
|
53187
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
175 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
|
176 } |
|
53187
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
177 } |
|
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
52773
diff
changeset
|
178 return count; |
|
52773
28096ed73153
Added ',' thousands grouping to int.__format__. See PEP 378.
Eric Smith <eric@trueblade.com>
parents:
48358
diff
changeset
|
179 } |
|
46909
ef4ec968fdb6
Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
180 |
