diff options
Diffstat (limited to 'py/objstr.c')
-rw-r--r-- | py/objstr.c | 36 |
1 files changed, 34 insertions, 2 deletions
diff --git a/py/objstr.c b/py/objstr.c index a2309364d7..84f872fe28 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -455,8 +455,9 @@ STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) { } #define is_ws(c) ((c) == ' ' || (c) == '\t') +enum {SPLIT = 0, KEEP = 1, SPLITLINES = 2}; -mp_obj_t mp_obj_str_split(mp_uint_t n_args, const mp_obj_t *args) { +STATIC inline mp_obj_t str_split_internal(mp_uint_t n_args, const mp_obj_t *args, int type) { const mp_obj_type_t *self_type = mp_obj_get_type(args[0]); mp_int_t splits = -1; mp_obj_t sep = mp_const_none; @@ -517,7 +518,13 @@ mp_obj_t mp_obj_str_split(mp_uint_t n_args, const mp_obj_t *args) { } s++; } - mp_obj_list_append(res, mp_obj_new_str_of_type(self_type, start, s - start)); + mp_uint_t len = s - start; + if (MP_LIKELY(!(len == 0 && s == top && (type && SPLITLINES)))) { + if (start + len != top && (type & KEEP)) { + len++; + } + mp_obj_list_append(res, mp_obj_new_str_of_type(self_type, start, len)); + } if (s >= top) { break; } @@ -531,6 +538,25 @@ mp_obj_t mp_obj_str_split(mp_uint_t n_args, const mp_obj_t *args) { return res; } +mp_obj_t mp_obj_str_split(mp_uint_t n_args, const mp_obj_t *args) { + return str_split_internal(n_args, args, SPLIT); +} + +#if MICROPY_PY_BUILTINS_STR_SPLITLINES +STATIC mp_obj_t str_splitlines(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + static const mp_arg_t allowed_args[] = { + { MP_QSTR_keepends, MP_ARG_BOOL, {.u_bool = false} }, + }; + + // parse args + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + mp_obj_t new_args[2] = {pos_args[0], MP_OBJ_NEW_QSTR(MP_QSTR__backslash_n)}; + return str_split_internal(2, new_args, SPLITLINES | (args[0].u_bool ? KEEP : 0)); +} +#endif + STATIC mp_obj_t str_rsplit(mp_uint_t n_args, const mp_obj_t *args) { if (n_args < 3) { // If we don't have split limit, it doesn't matter from which side @@ -1763,6 +1789,9 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_index_obj, 2, 4, str_index); MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_rindex_obj, 2, 4, str_rindex); MP_DEFINE_CONST_FUN_OBJ_2(str_join_obj, str_join); MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_split_obj, 1, 3, mp_obj_str_split); +#if MICROPY_PY_BUILTINS_STR_SPLITLINES +MP_DEFINE_CONST_FUN_OBJ_KW(str_splitlines_obj, 1, str_splitlines); +#endif MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_rsplit_obj, 1, 3, str_rsplit); MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_startswith_obj, 2, 3, str_startswith); MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_endswith_obj, 2, 3, str_endswith); @@ -1800,6 +1829,9 @@ STATIC const mp_map_elem_t str8_locals_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_rindex), (mp_obj_t)&str_rindex_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_join), (mp_obj_t)&str_join_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_split), (mp_obj_t)&str_split_obj }, + #if MICROPY_PY_BUILTINS_STR_SPLITLINES + { MP_OBJ_NEW_QSTR(MP_QSTR_splitlines), (mp_obj_t)&str_splitlines_obj }, + #endif { MP_OBJ_NEW_QSTR(MP_QSTR_rsplit), (mp_obj_t)&str_rsplit_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_startswith), (mp_obj_t)&str_startswith_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_endswith), (mp_obj_t)&str_endswith_obj }, |