Skip to content

Commit 8b7faa3

Browse files
committed
objstr: split(None): Fix whitespace properly.
1 parent 6eb7530 commit 8b7faa3

2 files changed

Lines changed: 5 additions & 4 deletions

File tree

py/objstr.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,6 @@ STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) {
454454
return mp_obj_new_str_from_vstr(self_type, &vstr);
455455
}
456456

457-
#define is_ws(c) ((c) == ' ' || (c) == '\t')
458457
enum {SPLIT = 0, KEEP = 1, SPLITLINES = 2};
459458

460459
STATIC inline mp_obj_t str_split_internal(mp_uint_t n_args, const mp_obj_t *args, int type) {
@@ -476,15 +475,15 @@ STATIC inline mp_obj_t str_split_internal(mp_uint_t n_args, const mp_obj_t *args
476475
// sep not given, so separate on whitespace
477476

478477
// Initial whitespace is not counted as split, so we pre-do it
479-
while (s < top && is_ws(*s)) s++;
478+
while (s < top && unichar_isspace(*s)) s++;
480479
while (s < top && splits != 0) {
481480
const byte *start = s;
482-
while (s < top && !is_ws(*s)) s++;
481+
while (s < top && !unichar_isspace(*s)) s++;
483482
mp_obj_list_append(res, mp_obj_new_str_of_type(self_type, start, s - start));
484483
if (s >= top) {
485484
break;
486485
}
487-
while (s < top && is_ws(*s)) s++;
486+
while (s < top && unichar_isspace(*s)) s++;
488487
if (splits > 0) {
489488
splits--;
490489
}

tests/basics/string_split.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
print(" a b c ".split(None, 1))
77
print(" a b c ".split(None, 0))
88
print(" a b c ".split(None, -1))
9+
print("foo\n\t\x07\v\nbar".split())
10+
print("foo\nbar\n".split())
911

1012
# empty separator should fail
1113
try:

0 commit comments

Comments
 (0)