Commit Graph

93 Commits

Author SHA1 Message Date
Lennart Poettering
e8bec6242b string-util: add new strdupcspn()/strdupspn()
These combine strndup() + strspn()/strcspn() into one.

There are a bunch of strndupa() calls that could use similar treatment
(or should be converted to strdup[c]spn(), but this commit doesn't
bother with that.
2023-01-20 17:27:51 +01:00
Zbigniew Jędrzejewski-Szmek
3ae6b3bf72 basic: rename util.h to logarithm.h
util.h is now about logarithms only, so we can rename it. Many files included
util.h for no apparent reason… Those includes are dropped.
2022-11-08 18:21:10 +01:00
Daan De Meyer
2812017cfb basic: Add strgrowpad0() 2022-09-23 14:10:07 +02:00
Yu Watanabe
146f4482b2 string-util: introduce strspn_from_end() 2022-04-20 02:15:01 +09:00
Yu Watanabe
072f5f9b18 string-util: introduce string_replace_char() 2022-04-20 02:14:35 +09:00
Antony Deepak Thomas
8034b42ca6 string-util: introduce streq_skip_trailing_chars() 2021-09-29 12:57:30 +09:00
Yu Watanabe
6b13ca8ada string-util: introduce strextendf_with_separator() 2021-05-20 18:16:51 +09:00
Lennart Poettering
319a4f4bc4 alloc-util: simplify GREEDY_REALLOC() logic by relying on malloc_usable_size()
We recently started making more use of malloc_usable_size() and rely on
it (see the string_erase() story). Given that we don't really support
sytems where malloc_usable_size() cannot be trusted beyond statistics
anyway, let's go fully in and rework GREEDY_REALLOC() on top of it:
instead of passing around and maintaining the currenly allocated size
everywhere, let's just derive it automatically from
malloc_usable_size().

I am mostly after this for the simplicity this brings. It also brings
minor efficiency improvements I guess, but things become so much nicer
to look at if we can avoid these allocation size variables everywhere.

Note that the malloc_usable_size() man page says relying on it wasn't
"good programming practice", but I think it does this for reasons that
don't apply here: the greedy realloc logic specifically doesn't rely on
the returned extra size, beyond the fact that it is equal or larger than
what was requested.

(This commit was supposed to be a quick patch btw, but apparently we use
the greedy realloc stuff quite a bit across the codebase, so this ends
up touching *a*lot* of code.)
2021-05-19 16:42:37 +02:00
Lennart Poettering
6df28e1f84 alloc-util: introduce MALLOC_SIZEOF_SAFE() helper
It's a wrapper around malloc_usable_size() that is supposed to be
compatible with _FORTIFY_SOURCES=1, by taking the
__builtin_object_size() data into account, the same way as the
_FORTIFY_SOURCES=1 logic does.

Fixes: #19203
2021-05-19 16:42:00 +02:00
Lennart Poettering
37ef2fc9f7 Merge pull request #18863 from keszybz/cmdline-escaping
Escape command lines properly
2021-05-07 17:29:39 +02:00
Lennart Poettering
e9b88a6d4e string-util: add strextendf() helper, that allows extending some allocated string via a format string
It's not going to be efficient if called in inner loops, but it's oh so
handy, and we have some code that does this:

   asprintf(&p, "%s…", b, …);
   free(b);
   b = TAKE_PTR(p);

which can now be replaced by the quicker and easier to read:

   strextendf(&p, "…", …);
2021-05-07 09:10:59 +02:00
Zbigniew Jędrzejewski-Szmek
a01080ceb3 basic/string-util: inline iterator variable declarations 2021-05-05 12:12:42 +02:00
Zbigniew Jędrzejewski-Szmek
6302d38609 basic/string-util: split out helper function 2021-05-05 12:12:42 +02:00
Zbigniew Jędrzejewski-Szmek
fd4e991dfd Drop parens from around already-parenthesized defines 2021-03-05 19:27:23 +01:00
Yu Watanabe
f5fbe71d95 tree-wide: use UINT64_MAX or friends 2021-03-05 07:10:13 +09:00
Yu Watanabe
e5bc5f1f5a fundamental: move several macros and functions into src/fundamental/
sd-boot has a copy of a subset of codes from libbasic. This makes
sd-boot share the code with libbasic, and dedup the code.

Note, startswith_no_case() is dropped from sd-boot, as
- it is not used,
- the previous implementation is not correct,
- gnu-efi does not have StrniCmp() or so.
2021-02-09 14:22:54 +09:00
Susant Sahani
fe96c0f86d treewide: tighten variable scope in loops (#18372)
Also use _cleanup_free_ in one more place.
2021-01-27 08:19:39 +01:00
Lennart Poettering
2a4e1fd0d4 string-util: use GREEDY_ALLOC_ROUND_UP() in strextend()
This uses GREEDY_ALLOC_ROUND_UP() to grow the allocation size
exponentially. This should speed allocation loops up a bit, given
that we often call strextend() repeatedly in a loop on the same
buffer.
2021-01-06 17:25:12 +01:00
Lennart Poettering
c2bc710b24 string-util: imply NULL termination of strextend() argument list
The trailing NULL in the argument list is now implied (similar to
what we already have in place in strjoin()).
2021-01-06 17:24:46 +01:00
Yu Watanabe
db9ecf0501 license: LGPL-2.1+ -> LGPL-2.1-or-later 2020-11-09 13:23:58 +09:00
Rasmus Villemoes
6ced0770c7 string-util: improve overflow checking
The current overflow checking is broken in the corner case of the strings'
combined length being exactly SIZE_MAX: After the loop, l would be SIZE_MAX,
but we're not testing whether the l+1 expression overflows.

Fix it by simply pre-accounting for the final '\0': initialize l to 1 instead
of 0.
2020-11-03 14:26:08 +01:00
Rasmus Villemoes
020003f235 string-util: simplify logic in strjoin_real()
The loops over (x, then all varargs, until a NULL is found) can be written much
simpler with an ordinary for loop. Just initialize the loop variable to x, test
that, and in the increment part, fetch the next va_arg(). That removes a level
of indentation, and avoids doing a separate strlen()/stpcpy() call for x.

While touching this code anyway, change (size_t)-1 to the more readable
SIZE_MAX.
2020-11-03 14:25:28 +01:00
Zbigniew Jędrzejewski-Szmek
03b62851a9 Remove FOREACH_WORD and friends 2020-09-09 09:34:55 +02:00
Zbigniew Jędrzejewski-Szmek
46bf625aca Add string_contains_word_strv()
I had to move STRV_MAKE to macro.h. There is a circular dependency between
extract-word.h, strv.h, and string-util.h that makes it hard to define the
inline function otherwise.
2020-09-04 12:59:26 +02:00
Zbigniew Jędrzejewski-Szmek
53cd7f3374 basic: add string_contains_word()
This wraps the common pattern of using extract_first_word() in a loop
to look for a matching word.
2020-09-04 12:59:25 +02:00