[ Web Proxy ]
URL:
Viewing:
https://github-redirect.dependabot.com/python/cpython/blob/main/Objects/stringlib/split.h
[Back]
[Original]
cpython/Objects/stringlib/split.h at main python/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 }}
python
cpython
Repository navigation
Code
Issues
5k+
(5k+)
Pull requests
2.8k
(2.8k)
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
cpython
/
Objects
/
stringlib
/
split.h
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
384 lines (339 loc) 10.7 KB
main
Breadcrumbs
cpython
/
Objects
/
stringlib
/
split.h
Copy path
Top
File metadata and controls
Code
Blame
384 lines (339 loc) 10.7 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/* stringlib: split implementation */
#ifndef
STRINGLIB_FASTSEARCH_H
#error
must include "stringlib/fastsearch.h" before including this module
#endif
#include
"pycore_list.h"
// _PyList_AppendTakeRef()
/* Overallocate the initial list to reduce the number of reallocs for small
split sizes. Eg, "A A A A A A A A A A".split() (10 elements) has three
resizes, to sizes 4, 8, then 16. Most observed string splits are for human
text (roughly 11 words per line) and field delimited data (usually 1-10
fields). For large strings the split algorithms are bandwidth limited
so increasing the preallocation likely will not improve things.*/
#define
MAX_PREALLOC
12
/* 5 splits gives 6 elements */
#define
PREALLOC_SIZE
(
maxsplit
) \
(maxsplit >= MAX_PREALLOC ? MAX_PREALLOC : maxsplit+1)
#define
SPLIT_APPEND
(
data
,
left
,
right
) \
sub = STRINGLIB_NEW((data) + (left), \
(right) - (left)); \
if (sub == NULL) \
goto onError; \
if (_PyList_AppendTakeRef((PyListObject *)list, sub)) \
goto onError;
#define
SPLIT_ADD
(
data
,
left
,
right
) { \
sub = STRINGLIB_NEW((data) + (left), \
(right) - (left)); \
if (sub == NULL) \
goto onError; \
if (count < MAX_PREALLOC) { \
PyList_SET_ITEM(list, count, sub); \
} else { \
if (_PyList_AppendTakeRef((PyListObject *)list, sub)) \
goto onError; \
} \
count++; }
/* Always force the list to the expected size. */
#define
FIX_PREALLOC_SIZE
(
list
) Py_SET_SIZE(list, count)
Py_LOCAL_INLINE
(
PyObject
*
)
STRINGLIB
(
split_whitespace
)(
PyObject
*
str_obj
,
const
STRINGLIB_CHAR
*
str
,
Py_ssize_t
str_len
,
Py_ssize_t
maxcount
)
{
Py_ssize_t
i
,
j
,
count
=
0
;
PyObject
*
list
=
PyList_New
(
PREALLOC_SIZE
(
maxcount
));
PyObject
*
sub
;
if
(
list
==
NULL
)
return
NULL
;
i
=
j
=
0
;
while
(
maxcount
--
>
0
) {
while
(
i
<
str_len
&&
STRINGLIB_ISSPACE
(
str
[
i
]))
i
++
;
if
(
i
==
str_len
)
break
;
j
=
i
;
i
++
;
while
(
i
<
str_len
&&
!
STRINGLIB_ISSPACE
(
str
[
i
]))
i
++
;
#if
!
STRINGLIB_MUTABLE
if
(
j
==
0
&&
i
==
str_len
&&
STRINGLIB_CHECK_EXACT
(
str_obj
)) {
/* No whitespace in str_obj, so just use it as list[0] */
Py_INCREF
(
str_obj
);
PyList_SET_ITEM
(
list
,
0
, (
PyObject
*
)
str_obj
);
count
++
;
break
;
}
#endif
SPLIT_ADD
(
str
,
j
,
i
);
}
if
(
i
<
str_len
) {
/* Only occurs when maxcount was reached */
/* Skip any remaining whitespace and copy to end of string */
while
(
i
<
str_len
&&
STRINGLIB_ISSPACE
(
str
[
i
]))
i
++
;
if
(
i
!=
str_len
)
SPLIT_ADD
(
str
,
i
,
str_len
);
}
FIX_PREALLOC_SIZE
(
list
);
return
list
;
onError
:
Py_DECREF
(
list
);
return
NULL
;
}
Py_LOCAL_INLINE
(
PyObject
*
)
STRINGLIB
(
split_char
)(
PyObject
*
str_obj
,
const
STRINGLIB_CHAR
*
str
,
Py_ssize_t
str_len
,
const
STRINGLIB_CHAR
ch
,
Py_ssize_t
maxcount
)
{
Py_ssize_t
i
,
j
,
count
=
0
;
PyObject
*
list
=
PyList_New
(
PREALLOC_SIZE
(
maxcount
));
PyObject
*
sub
;
if
(
list
==
NULL
)
return
NULL
;
i
=
j
=
0
;
while
((
j
<
str_len
)
&&
(
maxcount
--
>
0
)) {
for
(;
j
<
str_len
;
j
++
) {
/* I found that using memchr makes no difference */
if
(
str
[
j
]
==
ch
) {
SPLIT_ADD
(
str
,
i
,
j
);
i
=
j
=
j
+
1
;
break
;
}
}
}
#if
!
STRINGLIB_MUTABLE
if
(
count
==
0
&&
STRINGLIB_CHECK_EXACT
(
str_obj
)) {
/* ch not in str_obj, so just use str_obj as list[0] */
Py_INCREF
(
str_obj
);
PyList_SET_ITEM
(
list
,
0
, (
PyObject
*
)
str_obj
);
count
++
;
}
else
#endif
if
(
i
<=
str_len
) {
SPLIT_ADD
(
str
,
i
,
str_len
);
}
FIX_PREALLOC_SIZE
(
list
);
return
list
;
onError
:
Py_DECREF
(
list
);
return
NULL
;
}
Py_LOCAL_INLINE
(
PyObject
*
)
STRINGLIB
(
split
)(
PyObject
*
str_obj
,
const
STRINGLIB_CHAR
*
str
,
Py_ssize_t
str_len
,
const
STRINGLIB_CHAR
*
sep
,
Py_ssize_t
sep_len
,
Py_ssize_t
maxcount
)
{
Py_ssize_t
i
,
j
,
pos
,
count
=
0
;
PyObject
*
list
,
*
sub
;
if
(
sep_len
==
0
) {
PyErr_SetString
(
PyExc_ValueError
,
"empty separator"
);
return
NULL
;
}
else
if
(
sep_len
==
1
)
return
STRINGLIB
(
split_char
)(
str_obj
,
str
,
str_len
,
sep
[
0
],
maxcount
);
list
=
PyList_New
(
PREALLOC_SIZE
(
maxcount
));
if
(
list
==
NULL
)
return
NULL
;
i
=
j
=
0
;
while
(
maxcount
--
>
0
) {
pos
=
FASTSEARCH
(
str
+
i
,
str_len
-
i
,
sep
,
sep_len
,
-1
,
FAST_SEARCH
);
if
(
pos
<
0
)
break
;
j
=
i
+
pos
;
SPLIT_ADD
(
str
,
i
,
j
);
i
=
j
+
sep_len
;
}
#if
!
STRINGLIB_MUTABLE
if
(
count
==
0
&&
STRINGLIB_CHECK_EXACT
(
str_obj
)) {
/* No match in str_obj, so just use it as list[0] */
Py_INCREF
(
str_obj
);
PyList_SET_ITEM
(
list
,
0
, (
PyObject
*
)
str_obj
);
count
++
;
}
else
#endif
{
SPLIT_ADD
(
str
,
i
,
str_len
);
}
FIX_PREALLOC_SIZE
(
list
);
return
list
;
onError
:
Py_DECREF
(
list
);
return
NULL
;
}
Py_LOCAL_INLINE
(
PyObject
*
)
STRINGLIB
(
rsplit_whitespace
)(
PyObject
*
str_obj
,
const
STRINGLIB_CHAR
*
str
,
Py_ssize_t
str_len
,
Py_ssize_t
maxcount
)
{
Py_ssize_t
i
,
j
,
count
=
0
;
PyObject
*
list
=
PyList_New
(
PREALLOC_SIZE
(
maxcount
));
PyObject
*
sub
;
if
(
list
==
NULL
)
return
NULL
;
i
=
j
=
str_len
-
1
;
while
(
maxcount
--
>
0
) {
while
(
i
>=
0
&&
STRINGLIB_ISSPACE
(
str
[
i
]))
i
--
;
if
(
i
<
0
)
break
;
j
=
i
;
i
--
;
while
(
i
>=
0
&&
!
STRINGLIB_ISSPACE
(
str
[
i
]))
i
--
;
#if
!
STRINGLIB_MUTABLE
if
(
j
==
str_len
-
1
&&
i
<
0
&&
STRINGLIB_CHECK_EXACT
(
str_obj
)) {
/* No whitespace in str_obj, so just use it as list[0] */
Py_INCREF
(
str_obj
);
PyList_SET_ITEM
(
list
,
0
, (
PyObject
*
)
str_obj
);
count
++
;
break
;
}
#endif
SPLIT_ADD
(
str
,
i
+
1
,
j
+
1
);
}
if
(
i
>=
0
) {
/* Only occurs when maxcount was reached */
/* Skip any remaining whitespace and copy to beginning of string */
while
(
i
>=
0
&&
STRINGLIB_ISSPACE
(
str
[
i
]))
i
--
;
if
(
i
>=
0
)
SPLIT_ADD
(
str
,
0
,
i
+
1
);
}
FIX_PREALLOC_SIZE
(
list
);
if
(
PyList_Reverse
(
list
)
<
0
)
goto
onError
;
return
list
;
onError
:
Py_DECREF
(
list
);
return
NULL
;
}
Py_LOCAL_INLINE
(
PyObject
*
)
STRINGLIB
(
rsplit_char
)(
PyObject
*
str_obj
,
const
STRINGLIB_CHAR
*
str
,
Py_ssize_t
str_len
,
const
STRINGLIB_CHAR
ch
,
Py_ssize_t
maxcount
)
{
Py_ssize_t
i
,
j
,
count
=
0
;
PyObject
*
list
=
PyList_New
(
PREALLOC_SIZE
(
maxcount
));
PyObject
*
sub
;
if
(
list
==
NULL
)
return
NULL
;
i
=
j
=
str_len
-
1
;
while
((
i
>=
0
)
&&
(
maxcount
--
>
0
)) {
for
(;
i
>=
0
;
i
--
) {
if
(
str
[
i
]
==
ch
) {
SPLIT_ADD
(
str
,
i
+
1
,
j
+
1
);
j
=
i
=
i
-
1
;
break
;
}
}
}
#if
!
STRINGLIB_MUTABLE
if
(
count
==
0
&&
STRINGLIB_CHECK_EXACT
(
str_obj
)) {
/* ch not in str_obj, so just use str_obj as list[0] */
Py_INCREF
(
str_obj
);
PyList_SET_ITEM
(
list
,
0
, (
PyObject
*
)
str_obj
);
count
++
;
}
else
#endif
if
(
j
>=
-1
) {
SPLIT_ADD
(
str
,
0
,
j
+
1
);
}
FIX_PREALLOC_SIZE
(
list
);
if
(
PyList_Reverse
(
list
)
<
0
)
goto
onError
;
return
list
;
onError
:
Py_DECREF
(
list
);
return
NULL
;
}
Py_LOCAL_INLINE
(
PyObject
*
)
STRINGLIB
(
rsplit
)(
PyObject
*
str_obj
,
const
STRINGLIB_CHAR
*
str
,
Py_ssize_t
str_len
,
const
STRINGLIB_CHAR
*
sep
,
Py_ssize_t
sep_len
,
Py_ssize_t
maxcount
)
{
Py_ssize_t
j
,
pos
,
count
=
0
;
PyObject
*
list
,
*
sub
;
if
(
sep_len
==
0
) {
PyErr_SetString
(
PyExc_ValueError
,
"empty separator"
);
return
NULL
;
}
else
if
(
sep_len
==
1
)
return
STRINGLIB
(
rsplit_char
)(
str_obj
,
str
,
str_len
,
sep
[
0
],
maxcount
);
list
=
PyList_New
(
PREALLOC_SIZE
(
maxcount
));
if
(
list
==
NULL
)
return
NULL
;
j
=
str_len
;
while
(
maxcount
--
>
0
) {
pos
=
FASTSEARCH
(
str
,
j
,
sep
,
sep_len
,
-1
,
FAST_RSEARCH
);
if
(
pos
<
0
)
break
;
SPLIT_ADD
(
str
,
pos
+
sep_len
,
j
);
j
=
pos
;
}
#if
!
STRINGLIB_MUTABLE
if
(
count
==
0
&&
STRINGLIB_CHECK_EXACT
(
str_obj
)) {
/* No match in str_obj, so just use it as list[0] */
Py_INCREF
(
str_obj
);
PyList_SET_ITEM
(
list
,
0
, (
PyObject
*
)
str_obj
);
count
++
;
}
else
#endif
{
SPLIT_ADD
(
str
,
0
,
j
);
}
FIX_PREALLOC_SIZE
(
list
);
if
(
PyList_Reverse
(
list
)
<
0
)
goto
onError
;
return
list
;
onError
:
Py_DECREF
(
list
);
return
NULL
;
}
Py_LOCAL_INLINE
(
PyObject
*
)
STRINGLIB
(
splitlines
)(
PyObject
*
str_obj
,
const
STRINGLIB_CHAR
*
str
,
Py_ssize_t
str_len
,
int
keepends
)
{
/* This does not use the preallocated list because splitlines is
usually run with hundreds of newlines. The overhead of
switching between PyList_SET_ITEM and append causes about a
2-3% slowdown for that common case. A smarter implementation
could move the if check out, so the SET_ITEMs are done first
and the appends only done when the prealloc buffer is full.
That's too much work for little gain.*/
Py_ssize_t
i
;
Py_ssize_t
j
;
PyObject
*
list
=
PyList_New
(
0
);
PyObject
*
sub
;
if
(
list
==
NULL
)
return
NULL
;
for
(
i
=
j
=
0
;
i
<
str_len
; ) {
Py_ssize_t
eol
;
/* Find a line and append it */
while
(
i
<
str_len
&&
!
STRINGLIB_ISLINEBREAK
(
str
[
i
]))
i
++
;
/* Skip the line break reading CRLF as one line break */
eol
=
i
;
if
(
i
<
str_len
) {
if
(
str
[
i
]
==
'\r'
&&
i
+
1
<
str_len
&&
str
[
i
+
1
]
==
'\n'
)
i
+=
2
;
else
i
++
;
if
(
keepends
)
eol
=
i
;
}
#if
!
STRINGLIB_MUTABLE
if
(
j
==
0
&&
eol
==
str_len
&&
STRINGLIB_CHECK_EXACT
(
str_obj
)) {
/* No linebreak in str_obj, so just use it as list[0] */
if
(
PyList_Append
(
list
,
str_obj
))
goto
onError
;
break
;
}
#endif
SPLIT_APPEND
(
str
,
j
,
eol
);
j
=
i
;
}
return
list
;
onError
:
Py_DECREF
(
list
);
return
NULL
;
}
Footer
© 2026 GitHub, Inc.
Footer navigation
Terms
Privacy
Security
Status
Community
Docs
Contact
Manage cookies
Do not share my personal information
You cant perform that action at this time.
Web Proxy Viewer |
New URL
|
Original Page