@@ -464,9 +464,7 @@ STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) {
464464 return mp_obj_new_str_from_vstr (self_type , & vstr );
465465}
466466
467- enum {SPLIT = 0 , KEEP = 1 , SPLITLINES = 2 };
468-
469- STATIC inline mp_obj_t str_split_internal (mp_uint_t n_args , const mp_obj_t * args , int type ) {
467+ mp_obj_t mp_obj_str_split (size_t n_args , const mp_obj_t * args ) {
470468 const mp_obj_type_t * self_type = mp_obj_get_type (args [0 ]);
471469 mp_int_t splits = -1 ;
472470 mp_obj_t sep = mp_const_none ;
@@ -527,13 +525,7 @@ STATIC inline mp_obj_t str_split_internal(mp_uint_t n_args, const mp_obj_t *args
527525 }
528526 s ++ ;
529527 }
530- mp_uint_t sub_len = s - start ;
531- if (MP_LIKELY (!(sub_len == 0 && s == top && (type && SPLITLINES )))) {
532- if (start + sub_len != top && (type & KEEP )) {
533- sub_len ++ ;
534- }
535- mp_obj_list_append (res , mp_obj_new_str_of_type (self_type , start , sub_len ));
536- }
528+ mp_obj_list_append (res , mp_obj_new_str_of_type (self_type , start , s - start ));
537529 if (s >= top ) {
538530 break ;
539531 }
@@ -547,25 +539,49 @@ STATIC inline mp_obj_t str_split_internal(mp_uint_t n_args, const mp_obj_t *args
547539 return res ;
548540}
549541
550- mp_obj_t mp_obj_str_split (size_t n_args , const mp_obj_t * args ) {
551- return str_split_internal (n_args , args , SPLIT );
552- }
553-
554542#if MICROPY_PY_BUILTINS_STR_SPLITLINES
555543STATIC mp_obj_t str_splitlines (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
544+ enum { ARG_keepends };
556545 static const mp_arg_t allowed_args [] = {
557546 { MP_QSTR_keepends , MP_ARG_BOOL , {.u_bool = false} },
558547 };
559548
560549 // parse args
561- struct {
562- mp_arg_val_t keepends ;
563- } args ;
564- mp_arg_parse_all (n_args - 1 , pos_args + 1 , kw_args ,
565- MP_ARRAY_SIZE (allowed_args ), allowed_args , (mp_arg_val_t * )& args );
566-
567- mp_obj_t new_args [2 ] = {pos_args [0 ], MP_OBJ_NEW_QSTR (MP_QSTR__0x0a_ )};
568- return str_split_internal (2 , new_args , SPLITLINES | (args .keepends .u_bool ? KEEP : 0 ));
550+ mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
551+ mp_arg_parse_all (n_args - 1 , pos_args + 1 , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
552+
553+ const mp_obj_type_t * self_type = mp_obj_get_type (pos_args [0 ]);
554+ mp_obj_t res = mp_obj_new_list (0 , NULL );
555+
556+ GET_STR_DATA_LEN (pos_args [0 ], s , len );
557+ const byte * top = s + len ;
558+
559+ while (s < top ) {
560+ const byte * start = s ;
561+ size_t match = 0 ;
562+ while (s < top ) {
563+ if (* s == '\n' ) {
564+ match = 1 ;
565+ break ;
566+ } else if (* s == '\r' ) {
567+ if (s [1 ] == '\n' ) {
568+ match = 2 ;
569+ } else {
570+ match = 1 ;
571+ }
572+ break ;
573+ }
574+ s ++ ;
575+ }
576+ size_t sub_len = s - start ;
577+ if (args [ARG_keepends ].u_bool ) {
578+ sub_len += match ;
579+ }
580+ mp_obj_list_append (res , mp_obj_new_str_of_type (self_type , start , sub_len ));
581+ s += match ;
582+ }
583+
584+ return res ;
569585}
570586#endif
571587
0 commit comments