@@ -252,98 +252,98 @@ static size_t string_count_tokens(const char *str, const char *delim)
252252
253253struct string_list * string_split (const char * str , const char * delim )
254254{
255- char * save = NULL ;
256- char * copy = NULL ;
257- const char * tmp = NULL ;
255+ const char * p = str ;
258256 struct string_list * list = string_list_new ();
259-
260257 if (!list )
261258 return NULL ;
262259
263- if (!(copy = strdup (str )))
264- goto error ;
265-
266- /* Pre-size the element array to avoid repeated reallocs.
267- * We scan the original string (not the copy, since strtok
268- * mutates it) to count tokens. */
269- {
270- size_t token_count = string_count_tokens (str , delim );
271- if (token_count > list -> cap )
272- {
273- if (!string_list_capacity (list , token_count ))
274- goto error ;
275- }
276- }
277-
278- tmp = strtok_r (copy , delim , & save );
279- while (tmp )
260+ while (* p )
280261 {
281262 union string_list_elem_attr attr ;
282- size_t tok_len ;
263+ const char * tok ;
283264
284- attr .i = 0 ;
285- tok_len = strlen (tmp );
265+ while (* p && strchr (delim , * p ))
266+ p ++ ;
267+ if (!* p )
268+ break ;
286269
287- if (!string_list_append_n (list , tmp , tok_len , attr ))
288- goto error ;
270+ tok = p ;
271+ while (* p && !strchr (delim , * p ))
272+ p ++ ;
289273
290- tmp = strtok_r (NULL , delim , & save );
274+ attr .i = 0 ;
275+ if (!string_list_append_n (list , tok , p - tok , attr ))
276+ goto error ;
291277 }
292278
293- free (copy );
294279 return list ;
295280
296281error :
297282 string_list_free (list );
298- free (copy );
299283 return NULL ;
300284}
301285
302286bool string_split_noalloc (struct string_list * list ,
303287 const char * str , const char * delim )
304288{
305- char * save = NULL ;
306- char * copy = NULL ;
307- const char * tmp = NULL ;
289+ size_t _len ;
290+ const char * end ;
291+ const char * p = str ;
308292
309- if (!list )
293+ if (!list || ! str || ! delim || ! * delim )
310294 return false;
311295
312- if (!(copy = strdup (str )))
313- return false;
296+ /* Compute delimiter length once. */
297+ {
298+ const char * d = delim ;
299+ while (* d )
300+ d ++ ;
301+ _len = d - delim ;
302+ }
314303
315304 /* Pre-size to avoid repeated reallocs. */
316305 {
317- size_t token_count = string_count_tokens (str , delim );
318- if (token_count > list -> cap )
306+ size_t __len = string_count_tokens (str , delim );
307+ if (__len > list -> cap )
319308 {
320- if (!string_list_capacity (list , token_count ))
321- {
322- free (copy );
309+ if (!string_list_capacity (list , __len ))
323310 return false;
324- }
325311 }
326312 }
327313
328- tmp = strtok_r (copy , delim , & save );
329- while (tmp )
314+ while (* p )
330315 {
331316 union string_list_elem_attr attr ;
332- size_t tok_len ;
317+ size_t __len ;
333318
334- attr .i = 0 ;
335- tok_len = strlen ( tmp );
319+ attr .i = 0 ;
320+ end = strstr ( p , delim );
336321
337- if (! string_list_append_n ( list , tmp , tok_len , attr ) )
322+ if (end )
338323 {
339- free (copy );
340- return false;
324+ __len = end - p ;
325+ if (__len > 0 )
326+ {
327+ if (!string_list_append_n (list , p , __len , attr ))
328+ return false;
329+ }
330+ p = end + _len ;
331+ }
332+ else
333+ {
334+ const char * s = p ;
335+ while (* s )
336+ s ++ ;
337+ __len = s - p ;
338+ if (__len > 0 )
339+ {
340+ if (!string_list_append_n (list , p , __len , attr ))
341+ return false;
342+ }
343+ break ;
341344 }
342-
343- tmp = strtok_r (NULL , delim , & save );
344345 }
345346
346- free (copy );
347347 return true;
348348}
349349
0 commit comments