Debian Codegraph @

src/lib/util/string.c

raw symbols SCIP
1 /*
2 Authors:
3 Pavel Březina <[email protected]>
4
5 Copyright (C) 2018 Red Hat
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <ctype.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <regex.h>
26
27 #include "common/common.h"
28 #include "lib/util/string.h"
29 #include "lib/util/string_array.h"
30
31 bool
32 string_is_empty(const char *str)
33 {
34 return str == NULL || str[0] == '\0';
35 }
36
37 const char *
38 string_trim_left_const(const char *str)
39 {
40 if (str == NULL) {
41 return NULL;
42 }
43
44 while (isspace(*str)) {
45 str++;
46 }
47
48 return str;
49 }
50
51 char *
52 string_trim_left(const char *str)
53 {
54 str = string_trim_left_const(str);
55 if (str == NULL) {
56 return NULL;
57 }
58
59 return strdup(str);
60 }
61
62 char *
63 string_trim_right(const char *str)
64 {
65 const char *end;
66
67 if (str == NULL) {
68 return NULL;
69 }
70
71 end = str + strlen(str) - 1;
72 while (end > str && isspace(*end)) {
73 end--;
74 }
75
76 return strndup(str, end - str + 1);
77 }
78
79 char *
80 string_trim(const char *str)
81 {
82 return string_trim_right(string_trim_left_const(str));
83 }
84
85 char *
86 string_trim_noempty(const char *str)
87 {
88 char *trimmed = string_trim(str);
89
90 if (trimmed != NULL && trimmed[0] == '\0') {
91 free(trimmed);
92 return NULL;
93 }
94
95 return trimmed;
96 }
97
98 static errno_t
99 string_explode_get_token(const char *str,
100 size_t len,
101 unsigned int flags,
102 char **_token)
103 {
104 char *token;
105 char *tmp;
106
107 token = strndup(str, len);
108 if (token == NULL) {
109 return ENOMEM;
110 }
111
112 if (flags & STRING_EXPLODE_TRIM_LEFT && flags & STRING_EXPLODE_TRIM_RIGHT) {
113 tmp = string_trim(token);
114 free(token);
115 if (tmp == NULL) {
116 return ENOMEM;
117 }
118
119 token = tmp;
120 } else if (flags & STRING_EXPLODE_TRIM_LEFT) {
121 tmp = string_trim_left(token);
122 free(token);
123 if (tmp == NULL) {
124 return ENOMEM;
125 }
126
127 token = tmp;
128 } else if (flags & STRING_EXPLODE_TRIM_RIGHT) {
129 tmp = string_trim_right(token);
130 free(token);
131 if (tmp == NULL) {
132 return ENOMEM;
133 }
134
135 token = tmp;
136 }
137
138 if (flags & STRING_EXPLODE_SKIP_EMPTY && string_is_empty(token)) {
139 *_token = NULL;
140 free(token);
141 return EOK;
142 }
143
144 if (flags & STRING_EXPLODE_SKIP_COMMENT && token[0] == '#') {
145 *_token = NULL;
146 free(token);
147 return EOK;
148 }
149
150 *_token = token;
151
152 return EOK;
153 }
154
155 static char **
156 string_explode_add_value(char **array,
157 const char *value,
158 size_t len,
159 unsigned int flags)
160 {
161 char *token;
162 errno_t ret;
163
164 ret = string_explode_get_token(value, len, flags, &token);
165 if (ret != EOK) {
166 string_array_free(array);
167 return NULL;
168 }
169
170 if (token == NULL) {
171 return array;
172 }
173
174 array = string_array_add_value(array, token, false);
175 free(token);
176
177 return array;
178 }
179
180 char **
181 string_explode(const char *str, char delimiter, unsigned int flags)
182 {
183 const char *remainder;
184 const char *pos;
185 char **array;
186 size_t len;
187
188 array = string_array_create(1);
189 if (array == NULL) {
190 return NULL;
191 }
192
193 remainder = str;
194 while ((pos = strchr(remainder, delimiter)) != NULL) {
195 len = pos - remainder;
196 array = string_explode_add_value(array, remainder, len, flags);
197 if (array == NULL) {
198 return NULL;
199 }
200
201 remainder = pos + 1;
202 }
203
204 if (string_is_empty(remainder)) {
205 /* Add empty line if string end with delimiter. */
206 if (remainder != str && *(remainder - 1) == delimiter
207 && !(flags & STRING_EXPLODE_SKIP_EMPTY)) {
208 return string_array_add_value(array, "", false);
209 }
210
211 return array;
212 }
213
214 return string_explode_add_value(array, remainder, strlen(remainder), flags);
215 }
216
217 char *
218 string_implode(const char **array, char delimiter)
219 {
220 const char delimiter_str[] = {delimiter, '\0'};
221 size_t len = 0;
222 char *tmp;
223 char *str;
224 int i;
225
226 for (i = 0; array[i] != NULL; i++) {
227 len += strlen(array[i]) + 1;
228 }
229
230 if (len == 0) {
231 return strdup("");
232 }
233
234 str = malloc_zero_array(char, len + 1);
235 if (str == NULL) {
236 return NULL;
237 }
238
239 tmp = str;
240 for (i = 0; array[i + 1] != NULL; i++) {
241 strcat(tmp, array[i]);
242 strcat(tmp, delimiter_str);
243 tmp += strlen(array[i]) + 1;
244 }
245
246 strcat(tmp, array[i]);
247
248 return str;
249 }
250
251 void
252 string_replace_position(char *str, size_t start, size_t end, const char *with)
253 {
254 size_t len;
255 size_t i;
256
257 len = strlen(with);
258
259 if (len > end - start) {
260 return;
261 }
262
263 memcpy(str + start, with, len);
264
265 for (i = start + len; i < end; i++) {
266 str[i] = '\0';
267 }
268 }
269
270 void
271 string_remove_line(char *beginning, char *str, size_t inner_position)
272 {
273 char *left;
274
275 /* str may not be the beginning of the line so we need to refer
276 * to iterate until we reach the beginning */
277 for (left = str + inner_position; left != beginning; left--) {
278 if (*(left - 1) == '\n') {
279 break;
280 }
281 }
282
283 /* Remove the whole line that is in front of our string and then iterate
284 * to the line end or string end. */
285 for (; left < str || *left != '\0'; left++) {
286 if (*left == '\n') {
287 *left = '\0';
288 break;
289 }
290
291 *left = '\0';
292 }
293 }
294
295 void
296 string_remove_range(char *str, size_t from, size_t to)
297 {
298 char *pos;
299
300 for (pos = str + from; *pos != '\0' && pos - str != to; pos++) {
301 *pos = '\0';
302 }
303 }
304
305 void
306 string_remove_remainder(char *str, size_t from)
307 {
308 char *pos;
309
310 for (pos = str + from; *pos != '\0'; pos++) {
311 *pos = '\0';
312 }
313 }
314
315 void
316 string_replace_shake(char *str, size_t original_length)
317 {
318 size_t pos;
319 size_t i;
320
321 for (i = 0, pos = 0; i < original_length; i++) {
322 if (str[i] != '\0') {
323 str[pos] = str[i];
324 pos++;
325 }
326 }
327
328 for (; pos < original_length; pos++) {
329 str[pos] = '\0';
330 }
331 }
332
333 static int
334 min3(unsigned int a, unsigned int b, unsigned int c)
335 {
336 if (a < b && a < c) {
337 return a;
338 } else if (b < a && b < c) {
339 return b;
340 }
341
342 return c;
343 }
344
345 int
346 string_levenshtein(const char *a, const char *b)
347 {
348 unsigned int len_a = strlen(a);
349 unsigned int len_b = strlen(b);
350 unsigned int x;
351 unsigned int y;
352 unsigned int last_diag;
353 unsigned int old_diag;
354 unsigned int column[len_a + 1];
355
356 memset(column, 0, (len_a + 1) * sizeof(unsigned int));
357
358 for (y = 1; y <= len_a; y++) {
359 column[y] = y;
360 }
361
362 for (x = 1; x <= len_b; x++) {
363 column[0] = x;
364 for (y = 1, last_diag = x - 1; y <= len_a; y++) {
365 old_diag = column[y];
366 column[y] = min3(column[y] + 1, column[y - 1] + 1,
367 last_diag + (a[y - 1] == b[x - 1] ? 0 : 1));
368 last_diag = old_diag;
369 }
370 }
371
372 return column[len_a];
373 }