Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bfb8819

Browse files
committed
objstr: Make .split() support bytes.
1 parent 5e5d69b commit bfb8819

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

py/objstr.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) {
411411
#define is_ws(c) ((c) == ' ' || (c) == '\t')
412412

413413
STATIC mp_obj_t str_split(uint n_args, const mp_obj_t *args) {
414+
const mp_obj_type_t *self_type = mp_obj_get_type(args[0]);
414415
machine_int_t splits = -1;
415416
mp_obj_t sep = mp_const_none;
416417
if (n_args > 1) {
@@ -432,7 +433,7 @@ STATIC mp_obj_t str_split(uint n_args, const mp_obj_t *args) {
432433
while (s < top && splits != 0) {
433434
const byte *start = s;
434435
while (s < top && !is_ws(*s)) s++;
435-
mp_obj_list_append(res, mp_obj_new_str(start, s - start, false));
436+
mp_obj_list_append(res, str_new(self_type, start, s - start));
436437
if (s >= top) {
437438
break;
438439
}
@@ -443,7 +444,7 @@ STATIC mp_obj_t str_split(uint n_args, const mp_obj_t *args) {
443444
}
444445

445446
if (s < top) {
446-
mp_obj_list_append(res, mp_obj_new_str(s, top - s, false));
447+
mp_obj_list_append(res, str_new(self_type, s, top - s));
447448
}
448449

449450
} else {
@@ -467,7 +468,7 @@ STATIC mp_obj_t str_split(uint n_args, const mp_obj_t *args) {
467468
}
468469
s++;
469470
}
470-
mp_obj_list_append(res, mp_obj_new_str(start, s - start, false));
471+
mp_obj_list_append(res, str_new(self_type, start, s - start));
471472
if (s >= top) {
472473
break;
473474
}

tests/basics/string_split.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@
2626
print("abcabc".split("bc", 0))
2727
print("abcabc".split("bc", 1))
2828
print("abcabc".split("bc", 2))
29+
30+
print(b"abcabc".split(b"bc", 2))

0 commit comments

Comments
 (0)