2 Copyright (C) 2012, 2013, 2014, 2015, 2016, 2017
3 Ben Kibbey <bjk@luxsci.net>
5 This file is part of pwmd.
7 Pwmd 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 2 of the License, or
10 (at your option) any later version.
12 Pwmd 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.
17 You should have received a copy of the GNU General Public License
18 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
28 #include "util-string.h"
32 string_free (struct string_s
*s
, int with_data
)
44 string_erase (struct string_s
*s
, ssize_t pos
, ssize_t len
)
52 s
->len
= s
->len
- pos
;
58 memmove (&s
->str
[pos
], &s
->str
[pos
+ len
], n
);
64 /* Be careful about allocations since other string_ functions may
65 * realloc the 'str' pointer. */
67 string_new_content (char *str
)
69 struct string_s
*s
= xcalloc (1, sizeof (struct string_s
));
72 s
->len
= strlen (s
->str
);
73 s
->allocated
= s
->len
+ 1;
78 string_new (const char *str
)
80 struct string_s
*s
= xcalloc (1, sizeof (struct string_s
));
84 s
->str
= str_dup (str
);
91 s
->len
= strlen (s
->str
);
92 s
->allocated
= s
->len
+ 1;
99 string_append (struct string_s
*s
, const char *str
)
101 size_t len
= strlen (str
);
103 if (s
->allocated
< len
+ s
->len
+ 1)
105 char *p
= xrealloc (s
->str
, (len
+ s
->len
+ 1) * sizeof (char));
111 s
->allocated
= s
->len
+ len
+ 1;
114 memcpy (&s
->str
[s
->len
], str
, len
);
121 string_truncate (struct string_s
*s
, size_t n
)
135 string_prepend (struct string_s
*s
, const char *str
)
137 size_t len
= strlen (str
);
139 if (s
->allocated
< s
->len
+ len
+ 1)
141 char *p
= xrealloc (s
->str
, (len
+ s
->len
+ 1) * sizeof (char));
147 s
->allocated
= s
->len
+ len
+ 1;
150 memmove (&s
->str
[len
], s
->str
, s
->len
);
151 memcpy (s
->str
, str
, len
);
158 string_append_printf (struct string_s
*s
, const char *fmt
, ...)
165 len
= str_vasprintf (&buf
, fmt
, ap
);
171 s
= string_append (s
, buf
);
177 string_insert_c (struct string_s
*s
, ssize_t pos
, char c
)
179 size_t len
= s
->len
+ 2;
181 if (pos
>= s
->allocated
)
184 if (s
->allocated
< len
)
186 char *p
= xrealloc (s
->str
, len
* sizeof (char));
195 memmove (&s
->str
[pos
+ 1], &s
->str
[pos
], s
->len
- pos
);
197 s
->len
= pos
+ 1 == s
->allocated
? pos
+ 1 : s
->len
+ 1;
203 strv_length (char **a
)
213 /* 'str' must already be allocated. */
215 strv_cat (char **a
, char *str
)
217 int len
= a
? strv_length (a
) : 0;
220 dst
= xrealloc (a
, (len
+ 2) * sizeof (char *));
230 strv_printf (char ***array
, const char *fmt
, ...)
238 ret
= str_vasprintf (&buf
, fmt
, ap
);
244 a
= strv_cat (*array
, buf
);
260 for (p
= a
; p
&& *p
; p
++)
267 strv_dup (char **src
)
272 for (p
= src
; p
&& *p
; p
++)
275 char *xp
= str_dup (*p
);
283 tmp
= strv_cat (dst
, xp
);
298 strv_catv (char **dst
, char **src
)
310 for (p
= src
; p
&& *p
; p
++)
313 char *xp
= str_dup (*p
);
321 tmp
= strv_cat (d
, xp
);
345 while (len
&& isspace (str
[--len
]))
348 while (p
&& *p
&& isspace (*p
))
355 str_split_common (const char *src
, const char *delim
, int count
, int ws
)
359 const char *dp
= delim
;
360 char *str
= str_dup (src
);
362 size_t dlen
= strlen (delim
);
363 size_t pos
= 0, lastpos
= 0;
379 size_t len
= pos
- lastpos
- dlen
+ 1;
382 xp
= xmalloc (len
+ 1);
390 memcpy (xp
, &str
[lastpos
], len
);
397 tmp
= strv_cat (dst
, s
);
409 if (count
> 0 && index
+1 == count
&& *(p
+ 1))
411 if (!strv_printf (&dst
, "%s", p
+ 1))
437 if (!strv_printf (&dst
, "%s", p
))
448 /* Like str_split() but trims whitespace between 'delim'. */
450 str_split_ws (const char *str
, const char *delim
, int count
)
452 return str_split_common (str
, delim
, count
, 1);
456 str_split (const char *str
, const char *delim
, int count
)
458 return str_split_common (str
, delim
, count
, 0);
462 strv_join (const char *delim
, char **a
)
466 struct string_s
*s
= string_new ("");
471 for (p
= a
; p
&& *p
; p
++)
475 sp
= string_append_printf (s
, "%s%s", *p
, delim
476 && *(p
+ 1) ? delim
: "");
496 for (p
= str
; *p
; p
++)
506 str_chomp (char *str
)
508 int len
= strlen (str
);
509 char *p
= str
+ len
- 1;
511 while (len
&& isspace (*p
))
524 memmove (str
, p
, len
);
530 str_dup (const char *str
)
537 t
= xmalloc ((len
+ 1) * sizeof (char));
541 for (c
= 0; c
< len
; c
++)
549 str_asprintf (const char *fmt
, ...)
556 len
= str_vasprintf (&result
, fmt
, ap
);
558 return len
== -1 ? NULL
: result
;
562 str_vasprintf (char **result
, const char *fmt
, va_list ap
)
569 len
= vsnprintf (NULL
, 0, fmt
, cp
);
572 buf
= xmalloc (++len
);
577 len
= vsnprintf (buf
, len
, fmt
, cp
);