Skip to content
Snippets Groups Projects
Commit bfb8819c authored by Paul Sokolovsky's avatar Paul Sokolovsky
Browse files

objstr: Make .split() support bytes.

parent 5e5d69b3
No related branches found
No related tags found
No related merge requests found
...@@ -411,6 +411,7 @@ STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) { ...@@ -411,6 +411,7 @@ STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) {
#define is_ws(c) ((c) == ' ' || (c) == '\t') #define is_ws(c) ((c) == ' ' || (c) == '\t')
STATIC mp_obj_t str_split(uint n_args, const mp_obj_t *args) { STATIC mp_obj_t str_split(uint n_args, const mp_obj_t *args) {
const mp_obj_type_t *self_type = mp_obj_get_type(args[0]);
machine_int_t splits = -1; machine_int_t splits = -1;
mp_obj_t sep = mp_const_none; mp_obj_t sep = mp_const_none;
if (n_args > 1) { if (n_args > 1) {
...@@ -432,7 +433,7 @@ STATIC mp_obj_t str_split(uint n_args, const mp_obj_t *args) { ...@@ -432,7 +433,7 @@ STATIC mp_obj_t str_split(uint n_args, const mp_obj_t *args) {
while (s < top && splits != 0) { while (s < top && splits != 0) {
const byte *start = s; const byte *start = s;
while (s < top && !is_ws(*s)) s++; while (s < top && !is_ws(*s)) s++;
mp_obj_list_append(res, mp_obj_new_str(start, s - start, false)); mp_obj_list_append(res, str_new(self_type, start, s - start));
if (s >= top) { if (s >= top) {
break; break;
} }
...@@ -443,7 +444,7 @@ STATIC mp_obj_t str_split(uint n_args, const mp_obj_t *args) { ...@@ -443,7 +444,7 @@ STATIC mp_obj_t str_split(uint n_args, const mp_obj_t *args) {
} }
if (s < top) { if (s < top) {
mp_obj_list_append(res, mp_obj_new_str(s, top - s, false)); mp_obj_list_append(res, str_new(self_type, s, top - s));
} }
} else { } else {
...@@ -467,7 +468,7 @@ STATIC mp_obj_t str_split(uint n_args, const mp_obj_t *args) { ...@@ -467,7 +468,7 @@ STATIC mp_obj_t str_split(uint n_args, const mp_obj_t *args) {
} }
s++; s++;
} }
mp_obj_list_append(res, mp_obj_new_str(start, s - start, false)); mp_obj_list_append(res, str_new(self_type, start, s - start));
if (s >= top) { if (s >= top) {
break; break;
} }
......
...@@ -26,3 +26,5 @@ print("abcabc".split("bc")) ...@@ -26,3 +26,5 @@ print("abcabc".split("bc"))
print("abcabc".split("bc", 0)) print("abcabc".split("bc", 0))
print("abcabc".split("bc", 1)) print("abcabc".split("bc", 1))
print("abcabc".split("bc", 2)) print("abcabc".split("bc", 2))
print(b"abcabc".split(b"bc", 2))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment