objstr: split(None): Fix whitespace properly.
This commit is contained in:
parent
6eb7530083
commit
8b7faa31e1
|
@ -454,7 +454,6 @@ STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) {
|
||||||
return mp_obj_new_str_from_vstr(self_type, &vstr);
|
return mp_obj_new_str_from_vstr(self_type, &vstr);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define is_ws(c) ((c) == ' ' || (c) == '\t')
|
|
||||||
enum {SPLIT = 0, KEEP = 1, SPLITLINES = 2};
|
enum {SPLIT = 0, KEEP = 1, SPLITLINES = 2};
|
||||||
|
|
||||||
STATIC inline mp_obj_t str_split_internal(mp_uint_t n_args, const mp_obj_t *args, int type) {
|
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
|
||||||
// sep not given, so separate on whitespace
|
// sep not given, so separate on whitespace
|
||||||
|
|
||||||
// Initial whitespace is not counted as split, so we pre-do it
|
// Initial whitespace is not counted as split, so we pre-do it
|
||||||
while (s < top && is_ws(*s)) s++;
|
while (s < top && unichar_isspace(*s)) s++;
|
||||||
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 && !unichar_isspace(*s)) s++;
|
||||||
mp_obj_list_append(res, mp_obj_new_str_of_type(self_type, start, s - start));
|
mp_obj_list_append(res, mp_obj_new_str_of_type(self_type, start, s - start));
|
||||||
if (s >= top) {
|
if (s >= top) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
while (s < top && is_ws(*s)) s++;
|
while (s < top && unichar_isspace(*s)) s++;
|
||||||
if (splits > 0) {
|
if (splits > 0) {
|
||||||
splits--;
|
splits--;
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,8 @@ print(" a b ".split(None, 2))
|
||||||
print(" a b c ".split(None, 1))
|
print(" a b c ".split(None, 1))
|
||||||
print(" a b c ".split(None, 0))
|
print(" a b c ".split(None, 0))
|
||||||
print(" a b c ".split(None, -1))
|
print(" a b c ".split(None, -1))
|
||||||
|
print("foo\n\t\x07\v\nbar".split())
|
||||||
|
print("foo\nbar\n".split())
|
||||||
|
|
||||||
# empty separator should fail
|
# empty separator should fail
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Reference in New Issue