@@ -4952,6 +4952,9 @@ static Value builtin_split(Interpreter* interp, Value* args, int argc, Expr** ar
49524952 if (argc >= 2 ) {
49534953 EXPECT_STR (args [1 ], "SPLIT" , interp , line , col );
49544954 sep = args [1 ].as .s ;
4955+ if (sep [0 ] == '\0' ) {
4956+ RUNTIME_ERROR (interp , "SPLIT expects a non-empty delimiter" , line , col );
4957+ }
49554958 }
49564959 const char * s = args [0 ].as .s ;
49574960 // simple separator: if sep==NULL split on whitespace, else split on sep exactly
@@ -4984,16 +4987,15 @@ static Value builtin_split(Interpreter* interp, Value* args, int argc, Expr** ar
49844987 free (piece );
49854988 cur = found + seplen ;
49864989 }
4987- // last piece
4988- if (* cur != '\0' ) {
4989- if (count + 1 > cap ) { cap *= 2 ; items = realloc (items , sizeof (Value ) * cap ); }
4990- items [count ++ ] = value_str (cur );
4991- }
4990+ // last piece, including empty trailing fields
4991+ size_t len = strlen (cur );
4992+ char * piece = malloc (len + 1 );
4993+ memcpy (piece , cur , len );
4994+ piece [len ] = '\0' ;
4995+ if (count + 1 > cap ) { cap *= 2 ; items = realloc (items , sizeof (Value ) * cap ); }
4996+ items [count ++ ] = value_str (piece );
4997+ free (piece );
49924998 free (copy );
4993- if (count == 0 ) {
4994- free (items );
4995- return value_tns_new (TYPE_STR , 1 , (const size_t []){0 });
4996- }
49974999 size_t shape [1 ] = { count };
49985000 Value out = value_tns_from_values (TYPE_STR , 1 , shape , items , count );
49995001 for (size_t i = 0 ; i < count ; i ++ ) value_free (items [i ]);
0 commit comments