Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
cpython/Objects/stringlib/localeutil.h at pythoncapi · pythoncapi/cpython · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
pythoncapi
/
cpython
Public
forked from
python/cpython
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
pythoncapi
Breadcrumbs
cpython
/
Objects
/
stringlib
/
localeutil.h
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
180 lines (158 loc) · 5.75 KB
pythoncapi
Breadcrumbs
cpython
/
Objects
/
stringlib
/
localeutil.h
Copy path
Top
File metadata and controls
Code
Blame
180 lines (158 loc) · 5.75 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* stringlib: locale related helpers implementation */
#include
<locale.h>
#if
!
STRINGLIB_IS_UNICODE
# error
"localeutil.h is specific to Unicode"
#endif
typedef
struct
{
const
char
*
grouping
;
char
previous
;
Py_ssize_t
i
;
/* Where we're currently pointing in grouping. */
}
STRINGLIB
(
GroupGenerator
);
static
void
STRINGLIB
(
GroupGenerator_init
)(
STRINGLIB
(
GroupGenerator
)
*
self
,
const
char
*
grouping
)
{
self
->
grouping
=
grouping
;
self
->
i
=
0
;
self
->
previous
=
0
;
}
/* Returns the next grouping, or 0 to signify end. */
static
Py_ssize_t
STRINGLIB
(
GroupGenerator_next
)(
STRINGLIB
(
GroupGenerator
)
*
self
)
{
/* Note that we don't really do much error checking here. If a
grouping string contains just CHAR_MAX, for example, then just
terminate the generator. That shouldn't happen, but at least we
fail gracefully. */
switch
(
self
->
grouping
[
self
->
i
]) {
case
0
:
return
self
->
previous
;
case
CHAR_MAX
:
/* Stop the generator. */
return
0
;
default
: {
char
ch
=
self
->
grouping
[
self
->
i
];
self
->
previous
=
ch
;
self
->
i
++
;
return
(
Py_ssize_t
)
ch
;
}
}
}
/* Fill in some digits, leading zeros, and thousands separator. All
are optional, depending on when we're called. */
static
void
STRINGLIB
(
fill
)(
STRINGLIB_CHAR
*
*
digits_end
,
STRINGLIB_CHAR
*
*
buffer_end
,
Py_ssize_t
n_chars
,
Py_ssize_t
n_zeros
,
STRINGLIB_CHAR
*
thousands_sep
,
Py_ssize_t
thousands_sep_len
)
{
Py_ssize_t
i
;
if
(
thousands_sep
) {
*
buffer_end
-=
thousands_sep_len
;
/* Copy the thousands_sep chars into the buffer. */
memcpy
(
*
buffer_end
,
thousands_sep
,
thousands_sep_len
*
STRINGLIB_SIZEOF_CHAR
);
}
*
buffer_end
-=
n_chars
;
*
digits_end
-=
n_chars
;
memcpy
(
*
buffer_end
,
*
digits_end
,
n_chars
*
sizeof
(
STRINGLIB_CHAR
));
*
buffer_end
-=
n_zeros
;
for
(
i
=
0
;
i
<
n_zeros
;
i
++
)
(
*
buffer_end
)[
i
]
=
'0'
;
}
/**
* InsertThousandsGrouping:
* @buffer: A pointer to the start of a string.
* @n_buffer: Number of characters in @buffer.
* @digits: A pointer to the digits we're reading from. If count
* is non-NULL, this is unused.
* @n_digits: The number of digits in the string, in which we want
* to put the grouping chars.
* @min_width: The minimum width of the digits in the output string.
* Output will be zero-padded on the left to fill.
* @grouping: see definition in localeconv().
* @thousands_sep: see definition in localeconv().
*
* There are 2 modes: counting and filling. If @buffer is NULL,
* we are in counting mode, else filling mode.
* If counting, the required buffer size is returned.
* If filling, we know the buffer will be large enough, so we don't
* need to pass in the buffer size.
* Inserts thousand grouping characters (as defined by grouping and
* thousands_sep) into the string between buffer and buffer+n_digits.
*
* Return value: 0 on error, else 1. Note that no error can occur if
* count is non-NULL.
*
* This name won't be used, the includer of this file should define
* it to be the actual function name, based on unicode or string.
*
* As closely as possible, this code mimics the logic in decimal.py's
_insert_thousands_sep().
**/
static
Py_ssize_t
STRINGLIB
(
InsertThousandsGrouping
)(
STRINGLIB_CHAR
*
buffer
,
Py_ssize_t
n_buffer
,
STRINGLIB_CHAR
*
digits
,
Py_ssize_t
n_digits
,
Py_ssize_t
min_width
,
const
char
*
grouping
,
STRINGLIB_CHAR
*
thousands_sep
,
Py_ssize_t
thousands_sep_len
)
{
Py_ssize_t
count
=
0
;
Py_ssize_t
n_zeros
;
int
loop_broken
=
0
;
int
use_separator
=
0
;
/* First time through, don't append the
separator. They only go between
groups. */
STRINGLIB_CHAR
*
buffer_end
=
NULL
;
STRINGLIB_CHAR
*
digits_end
=
NULL
;
Py_ssize_t
l
;
Py_ssize_t
n_chars
;
Py_ssize_t
remaining
=
n_digits
;
/* Number of chars remaining to
be looked at */
/* A generator that returns all of the grouping widths, until it
returns 0. */
STRINGLIB
(
GroupGenerator
)
groupgen
;
STRINGLIB
(
GroupGenerator_init
)(
&
groupgen
,
grouping
);
if
(
buffer
) {
buffer_end
=
buffer
+
n_buffer
;
digits_end
=
digits
+
n_digits
;
}
while
((
l
=
STRINGLIB
(
GroupGenerator_next
)(
&
groupgen
))
>
0
) {
l
=
Py_MIN
(
l
,
Py_MAX
(
Py_MAX
(
remaining
,
min_width
),
1
));
n_zeros
=
Py_MAX
(
0
,
l
-
remaining
);
n_chars
=
Py_MAX
(
0
,
Py_MIN
(
remaining
,
l
));
/* Use n_zero zero's and n_chars chars */
/* Count only, don't do anything. */
count
+=
(
use_separator
?
thousands_sep_len
:
0
)
+
n_zeros
+
n_chars
;
if
(
buffer
) {
/* Copy into the output buffer. */
STRINGLIB
(
fill
)(
&
digits_end
,
&
buffer_end
,
n_chars
,
n_zeros
,
use_separator
?
thousands_sep
:
NULL
,
thousands_sep_len
);
}
/* Use a separator next time. */
use_separator
=
1
;
remaining
-=
n_chars
;
min_width
-=
l
;
if
(
remaining
<=
0
&&
min_width
<=
0
) {
loop_broken
=
1
;
break
;
}
min_width
-=
thousands_sep_len
;
}
if
(!
loop_broken
) {
/* We left the loop without using a break statement. */
l
=
Py_MAX
(
Py_MAX
(
remaining
,
min_width
),
1
);
n_zeros
=
Py_MAX
(
0
,
l
-
remaining
);
n_chars
=
Py_MAX
(
0
,
Py_MIN
(
remaining
,
l
));
/* Use n_zero zero's and n_chars chars */
count
+=
(
use_separator
?
thousands_sep_len
:
0
)
+
n_zeros
+
n_chars
;
if
(
buffer
) {
/* Copy into the output buffer. */
STRINGLIB
(
fill
)(
&
digits_end
,
&
buffer_end
,
n_chars
,
n_zeros
,
use_separator
?
thousands_sep
:
NULL
,
thousands_sep_len
);
}
}
return
count
;
}
You can’t perform that action at this time.