1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
|
/*
* Embedded Linux library
* Copyright (C) 2011-2014 Intel Corporation
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#define _GNU_SOURCE
#include <string.h>
#include "strv.h"
#include "private.h"
#include "useful.h"
/**
* SECTION:strv
* @short_description: String array functions
*
* String array functions
*/
/**
* l_strfreev:
* @strlist: String list to free
*
* Frees a list of strings
**/
LIB_EXPORT void l_strfreev(char **strlist)
{
l_strv_free(strlist);
}
/**
* l_strsplit:
* @str: String to split
* @sep: The delimiter character
*
* Splits a string into pieces which do not contain the delimiter character.
* As a special case, an empty string is returned as an empty array, e.g.
* an array with just the NULL element.
*
* Note that this function only works with ASCII delimiters.
*
* Returns: A newly allocated %NULL terminated string array. This array
* should be freed using l_strfreev().
**/
LIB_EXPORT char **l_strsplit(const char *str, const char sep)
{
int len;
int i;
const char *p;
char **ret;
if (unlikely(!str))
return NULL;
if (str[0] == '\0')
return l_new(char *, 1);
for (p = str, len = 1; *p; p++)
if (*p == sep)
len += 1;
ret = l_new(char *, len + 1);
i = 0;
p = str;
len = 0;
while (p[len]) {
if (p[len] != sep) {
len += 1;
continue;
}
ret[i++] = l_strndup(p, len);
p += len + 1;
len = 0;
}
ret[i++] = l_strndup(p, len);
return ret;
}
/**
* l_strsplit_set:
* @str: String to split
* @separators: A set of delimiters
*
* Splits a string into pieces which do not contain the delimiter characters
* that can be found in @separators.
* As a special case, an empty string is returned as an empty array, e.g.
* an array with just the NULL element.
*
* Note that this function only works with ASCII delimiters.
*
* Returns: A newly allocated %NULL terminated string array. This array
* should be freed using l_strfreev().
**/
LIB_EXPORT char **l_strsplit_set(const char *str, const char *separators)
{
int len;
int i;
const char *p;
char **ret;
bool sep_table[256];
if (unlikely(!str))
return NULL;
if (str[0] == '\0')
return l_new(char *, 1);
memset(sep_table, 0, sizeof(sep_table));
for (p = separators; *p; p++)
sep_table[(unsigned char) *p] = true;
for (p = str, len = 1; *p; p++)
if (sep_table[(unsigned char) *p] == true)
len += 1;
ret = l_new(char *, len + 1);
i = 0;
p = str;
len = 0;
while (p[len]) {
if (sep_table[(unsigned char) p[len]] != true) {
len += 1;
continue;
}
ret[i++] = l_strndup(p, len);
p += len + 1;
len = 0;
}
ret[i++] = l_strndup(p, len);
return ret;
}
/**
* l_strjoinv:
* @str_array: a %NULL terminated array of strings to join
* @delim: Delimiting character
*
* Joins strings contanied in the @str_array into one long string delimited
* by @delim.
*
* Returns: A newly allocated string that should be freed using l_free()
*/
LIB_EXPORT char *l_strjoinv(char **str_array, const char delim)
{
size_t len = 0;
unsigned int i;
char *ret;
char *p;
if (unlikely(!str_array))
return NULL;
if (!str_array[0])
return l_strdup("");
for (i = 0; str_array[i]; i++)
len += strlen(str_array[i]);
len += 1 + i - 1;
ret = l_malloc(len);
p = stpcpy(ret, str_array[0]);
for (i = 1; str_array[i]; i++) {
*p++ = delim;
p = stpcpy(p, str_array[i]);
}
return ret;
}
/**
* l_strv_new:
*
* Returns: new empty string array
**/
LIB_EXPORT char **l_strv_new(void)
{
return l_new(char *, 1);
}
/**
* l_strv_free:
* @str_array: a %NULL terminated array of strings
*
* Frees strings in @str_array and @str_array itself
**/
LIB_EXPORT void l_strv_free(char **str_array)
{
if (likely(str_array)) {
int i;
for (i = 0; str_array[i]; i++)
l_free(str_array[i]);
l_free(str_array);
}
}
/**
* l_strv_length:
* @str_array: a %NULL terminated array of strings
*
* Returns: the number of strings in @str_array
*/
LIB_EXPORT unsigned int l_strv_length(char **str_array)
{
unsigned int i = 0;
if (unlikely(!str_array))
return 0;
while (str_array[i])
i += 1;
return i;
}
/**
* l_strv_contains:
* @str_array: a %NULL terminated array of strings
* @item: An item to search for, must be not %NULL
*
* Returns: #true if @str_array contains item
*/
LIB_EXPORT bool l_strv_contains(char **str_array, const char *item)
{
unsigned int i = 0;
if (unlikely(!str_array || !item))
return false;
while (str_array[i]) {
if (!strcmp(str_array[i], item))
return true;
i += 1;
}
return false;
}
/**
* l_strv_append:
* @str_array: a %NULL terminated array of strings or %NULL
* @str: A string to be appended at the end of @str_array
*
* Returns: New %NULL terminated array of strings with @str added
*/
LIB_EXPORT char **l_strv_append(char **str_array, const char *str)
{
char **ret;
unsigned int i, len;
if (unlikely(!str))
return str_array;
len = l_strv_length(str_array);
ret = l_new(char *, len + 2);
for (i = 0; i < len; i++)
ret[i] = str_array[i];
ret[i] = l_strdup(str);
l_free(str_array);
return ret;
}
LIB_EXPORT char **l_strv_append_printf(char **str_array,
const char *format, ...)
{
va_list args;
char **ret;
va_start(args, format);
ret = l_strv_append_vprintf(str_array, format, args);
va_end(args);
return ret;
}
LIB_EXPORT char **l_strv_append_vprintf(char **str_array,
const char *format, va_list args)
{
char **ret;
unsigned int i, len;
if (unlikely(!format))
return str_array;
len = l_strv_length(str_array);
ret = l_new(char *, len + 2);
for (i = 0; i < len; i++)
ret[i] = str_array[i];
ret[i] = l_strdup_vprintf(format, args);
l_free(str_array);
return ret;
}
/**
* l_strv_copy:
* @str_array: a %NULL terminated array of strings or %NULL
*
* Returns: An independent copy of @str_array.
*/
LIB_EXPORT char **l_strv_copy(char **str_array)
{
int i, len;
char **copy;
if (unlikely(!str_array))
return NULL;
for (len = 0; str_array[len]; len++);
copy = l_malloc(sizeof(char *) * (len + 1));
for (i = len; i >= 0; i--)
copy[i] = l_strdup(str_array[i]);
return copy;
}
/**
* l_strv_eq:
* @a: a %NULL terminated array of strings or %NULL
* @b: another %NULL terminated array of strings or %NULL
*
* Returns: Whether @a and @b's contents are identical, including the
* order, or @a and @b are both %NULL.
*/
LIB_EXPORT bool l_strv_eq(char **a, char **b)
{
if (!a || !b)
return a == b;
for (; *a; a++, b++)
if (!*b || strcmp(*a, *b))
return false;
return !*b;
}
|