1 /* Copyright (c) 2003-2004, Roger Dingledine
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2021, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
7 * \file smartlist_split.c
8 * \brief Split a string into a smartlist_t of substrings.
11 #include "lib/smartlist_core/smartlist_core.h"
12 #include "lib/smartlist_core/smartlist_split.h"
14 #include "lib/err/torerr.h"
15 #include "lib/string/util_string.h"
16 #include "lib/string/compat_ctype.h"
17 #include "lib/malloc/malloc.h"
22 * Split a string <b>str</b> along all occurrences of <b>sep</b>,
23 * appending the (newly allocated) split strings, in order, to
24 * <b>sl</b>. Return the number of strings added to <b>sl</b>.
26 * If <b>flags</b>&SPLIT_SKIP_SPACE is true, remove initial and
27 * trailing space from each entry.
28 * If <b>flags</b>&SPLIT_IGNORE_BLANK is true, remove any entries
30 * If <b>flags</b>&SPLIT_STRIP_SPACE is true, strip spaces from each
33 * If <b>max</b>\>0, divide the string into no more than <b>max</b> pieces. If
34 * <b>sep</b> is NULL, split on any sequence of horizontal space.
37 smartlist_split_string(smartlist_t
*sl
, const char *str
, const char *sep
,
40 const char *cp
, *end
, *next
;
48 if (flags
&SPLIT_SKIP_SPACE
) {
49 while (TOR_ISSPACE(*cp
)) ++cp
;
52 if (max
>0 && n
== max
-1) {
53 end
= strchr(cp
,'\0');
57 end
= strchr(cp
,'\0');
59 for (end
= cp
; *end
&& *end
!= '\t' && *end
!= ' '; ++end
)
68 next
= end
+strlen(sep
);
71 while (*next
== '\t' || *next
== ' ')
75 if (flags
&SPLIT_SKIP_SPACE
) {
76 while (end
> cp
&& TOR_ISSPACE(*(end
-1)))
79 if (end
!= cp
|| !(flags
&SPLIT_IGNORE_BLANK
)) {
80 char *string
= tor_strndup(cp
, end
-cp
);
81 if (flags
&SPLIT_STRIP_SPACE
)
82 tor_strstrip(string
, " ");
83 smartlist_add(sl
, string
);