1 /*
2 * Copyright © 2008 Kristian Høgsberg
3 * Copyright © 2013-2015 Red Hat, Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 #include "config.h"
26
27 #include "util-strings.h"
28
29 /**
30 * Return the next word in a string pointed to by state before the first
31 * separator character. Call repeatedly to tokenize a whole string.
32 *
33 * @param state Current state
34 * @param len String length of the word returned
35 * @param separators List of separator characters
36 *
37 * @return The first word in *state, NOT null-terminated
38 */
39 static const char *
next_word(const char ** state,size_t * len,const char * separators)40 next_word(const char **state, size_t *len, const char *separators)
41 {
42 const char *next = *state;
43 size_t l;
44
45 if (!*next)
46 return NULL;
47
48 next += strspn(next, separators);
49 if (!*next) {
50 *state = next;
51 return NULL;
52 }
53
54 l = strcspn(next, separators);
55 *state = next + l;
56 *len = l;
57
58 return next;
59 }
60
61 /**
62 * Return a null-terminated string array with the tokens in the input
63 * string, e.g. "one two\tthree" with a separator list of " \t" will return
64 * an array [ "one", "two", "three", NULL ].
65 *
66 * Use strv_free() to free the array.
67 *
68 * @param in Input string
69 * @param separators List of separator characters
70 *
71 * @return A null-terminated string array or NULL on errors
72 */
73 char **
strv_from_string(const char * in,const char * separators)74 strv_from_string(const char *in, const char *separators)
75 {
76 const char *s, *word;
77 char **strv = NULL;
78 int nelems = 0, idx;
79 size_t l;
80
81 assert(in != NULL);
82
83 s = in;
84 while (next_word(&s, &l, separators) != NULL)
85 nelems++;
86
87 if (nelems == 0)
88 return NULL;
89
90 nelems++; /* NULL-terminated */
91 strv = zalloc(nelems * sizeof *strv);
92
93 idx = 0;
94
95 s = in;
96 while ((word = next_word(&s, &l, separators)) != NULL) {
97 char *copy = strndup(word, l);
98 if (!copy) {
99 strv_free(strv);
100 return NULL;
101 }
102
103 strv[idx++] = copy;
104 }
105
106 return strv;
107 }
108
109 /**
110 * Return a newly allocated string with all elements joined by the
111 * joiner, same as Python's string.join() basically.
112 * A strv of ["one", "two", "three", NULL] with a joiner of ", " results
113 * in "one, two, three".
114 *
115 * An empty strv ([NULL]) returns NULL, same for passing NULL as either
116 * argument.
117 *
118 * @param strv Input string arrray
119 * @param joiner Joiner between the elements in the final string
120 *
121 * @return A null-terminated string joining all elements
122 */
123 char *
strv_join(char ** strv,const char * joiner)124 strv_join(char **strv, const char *joiner)
125 {
126 char **s;
127 char *str;
128 size_t slen = 0;
129 size_t count = 0;
130
131 if (!strv || !joiner)
132 return NULL;
133
134 if (strv[0] == NULL)
135 return NULL;
136
137 for (s = strv, count = 0; *s; s++, count++) {
138 slen += strlen(*s);
139 }
140
141 assert(slen < 1000);
142 assert(strlen(joiner) < 1000);
143 assert(count > 0);
144 assert(count < 100);
145
146 slen += (count - 1) * strlen(joiner);
147
148 str = zalloc(slen + 1); /* trailing \0 */
149 for (s = strv; *s; s++) {
150 strcat(str, *s);
151 --count;
152 if (count > 0)
153 strcat(str, joiner);
154 }
155
156 return str;
157 }
158