2 Copyright (C) 2012-2021 Ben Kibbey <bjk@luxsci.net>
4 This file is part of pwmd.
6 Pwmd is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 2 of the License, or
9 (at your option) any later version.
11 Pwmd is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
27 #include "util-string.h"
31 string_free (struct string_s
*s
, int with_data
)
43 string_erase (struct string_s
*s
, ssize_t pos
, ssize_t len
)
51 s
->len
= s
->len
- pos
;
57 memmove (&s
->str
[pos
], &s
->str
[pos
+ len
], n
);
63 /* Be careful about allocations since other string_ functions may
64 * realloc the 'str' pointer. */
66 string_new_content (char *str
)
68 struct string_s
*s
= xcalloc (1, sizeof (struct string_s
));
71 s
->len
= strlen (s
->str
);
72 s
->allocated
= s
->len
+ 1;
77 string_new (const char *str
)
79 struct string_s
*s
= xcalloc (1, sizeof (struct string_s
));
83 s
->str
= str_dup (str
);
90 s
->len
= strlen (s
->str
);
91 s
->allocated
= s
->len
+ 1;
98 string_append (struct string_s
*s
, const char *str
)
100 size_t len
= strlen (str
);
102 if (s
->allocated
< len
+ s
->len
+ 1)
104 char *p
= xrealloc (s
->str
, (len
+ s
->len
+ 1) * sizeof (char));
110 s
->allocated
= s
->len
+ len
+ 1;
113 memcpy (&s
->str
[s
->len
], str
, len
);
120 string_truncate (struct string_s
*s
, size_t n
)
134 string_prepend (struct string_s
*s
, const char *str
)
136 size_t len
= strlen (str
);
138 if (s
->allocated
< s
->len
+ len
+ 1)
140 char *p
= xrealloc (s
->str
, (len
+ s
->len
+ 1) * sizeof (char));
146 s
->allocated
= s
->len
+ len
+ 1;
149 memmove (&s
->str
[len
], s
->str
, s
->len
);
150 memcpy (s
->str
, str
, len
);
157 string_append_printf (struct string_s
*s
, const char *fmt
, ...)
164 len
= str_vasprintf (&buf
, fmt
, ap
);
170 s
= string_append (s
, buf
);
176 string_insert_c (struct string_s
*s
, ssize_t pos
, char c
)
178 size_t len
= s
->len
+ 2;
180 if (pos
>= s
->allocated
)
183 if (s
->allocated
< len
)
185 char *p
= xrealloc (s
->str
, len
* sizeof (char));
194 memmove (&s
->str
[pos
+ 1], &s
->str
[pos
], s
->len
- pos
);
196 s
->len
= pos
+ 1 == s
->allocated
? pos
+ 1 : s
->len
+ 1;
202 strv_length (char **a
)
212 /* 'str' must already be allocated. */
214 strv_cat (char **a
, char *str
)
216 int len
= a
? strv_length (a
) : 0;
219 dst
= xrealloc (a
, (len
+ 2) * sizeof (char *));
229 strv_printf (char ***array
, const char *fmt
, ...)
237 ret
= str_vasprintf (&buf
, fmt
, ap
);
243 a
= strv_cat (*array
, buf
);
259 for (p
= a
; p
&& *p
; p
++)
266 strv_dup (char **src
)
271 for (p
= src
; p
&& *p
; p
++)
274 char *xp
= str_dup (*p
);
282 tmp
= strv_cat (dst
, xp
);
297 strv_catv (char **dst
, char **src
)
309 for (p
= src
; p
&& *p
; p
++)
312 char *xp
= str_dup (*p
);
320 tmp
= strv_cat (d
, xp
);
344 while (len
&& isspace (str
[--len
]))
347 while (p
&& *p
&& isspace (*p
))
354 str_split_common (const char *src
, const char *delim
, int count
, int ws
)
358 const char *dp
= delim
;
359 char *str
= str_dup (src
);
361 size_t dlen
= strlen (delim
);
362 size_t pos
= 0, lastpos
= 0;
378 size_t len
= pos
- lastpos
- dlen
+ 1;
381 xp
= xmalloc (len
+ 1);
389 memcpy (xp
, &str
[lastpos
], len
);
396 tmp
= strv_cat (dst
, s
);
408 if (count
> 0 && index
+1 == count
&& *(p
+ 1))
410 if (!strv_printf (&dst
, "%s", p
+ 1))
436 if (!strv_printf (&dst
, "%s", p
))
447 /* Like str_split() but trims whitespace between 'delim'. */
449 str_split_ws (const char *str
, const char *delim
, int count
)
451 return str_split_common (str
, delim
, count
, 1);
455 str_split (const char *str
, const char *delim
, int count
)
457 return str_split_common (str
, delim
, count
, 0);
461 strv_join (const char *delim
, char **a
)
465 struct string_s
*s
= string_new ("");
470 for (p
= a
; p
&& *p
; p
++)
474 sp
= string_append_printf (s
, "%s%s", *p
, delim
475 && *(p
+ 1) ? delim
: "");
495 for (p
= str
; *p
; p
++)
505 str_chomp (char *str
)
507 int len
= strlen (str
);
508 char *p
= str
+ len
- 1;
510 while (len
&& isspace (*p
))
523 memmove (str
, p
, len
);
529 str_dup (const char *str
)
536 t
= xmalloc ((len
+ 1) * sizeof (char));
540 for (c
= 0; c
< len
; c
++)
548 str_asprintf (const char *fmt
, ...)
555 len
= str_vasprintf (&result
, fmt
, ap
);
557 return len
== -1 ? NULL
: result
;
561 str_vasprintf (char **result
, const char *fmt
, va_list ap
)
568 len
= vsnprintf (NULL
, 0, fmt
, cp
);
571 buf
= xmalloc (++len
);
576 len2
= vsnprintf (buf
, len
, fmt
, cp
);