summaryrefslogtreecommitdiff
path: root/src/basic/string-util.c
Commit message (Collapse)AuthorAgeFilesLines
* string-util: Add startswith_strv()Daan De Meyer2023-04-201-0/+12
| | | | | | This is the function version of STARTSWITH_SET(). We also move STARTSWITH_SET() to string-util.h as it fits more there than in strv.h and reimplement it using startswith_strv().
* string-util: add new helper for finding line starting with specific string ↵Lennart Poettering2023-03-241-0/+23
| | | | | | | in a text buffer We have implemented this manually a couple of times, and always wrong. Hence let's implement this correctly for once and use everywhere.
* string-util: add common implementation of function that converts sized ↵Lennart Poettering2023-01-211-0/+43
| | | | character buffers to NUL terminated C strings
* string-util: add new strdupcspn()/strdupspn()Lennart Poettering2023-01-201-0/+16
| | | | | | | | 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.
* basic: rename util.h to logarithm.hZbigniew Jędrzejewski-Szmek2022-11-081-1/+0
| | | | | 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.
* basic: Add strgrowpad0()Daan De Meyer2022-09-231-0/+13
|
* string-util: introduce strspn_from_end()Yu Watanabe2022-04-201-0/+15
|
* string-util: introduce string_replace_char()Yu Watanabe2022-04-201-0/+12
|
* string-util: introduce streq_skip_trailing_chars()Antony Deepak Thomas2021-09-291-0/+16
|
* string-util: introduce strextendf_with_separator()Yu Watanabe2021-05-201-13/+18
|
* alloc-util: simplify GREEDY_REALLOC() logic by relying on malloc_usable_size()Lennart Poettering2021-05-191-3/+3
| | | | | | | | | | | | | | | | | | | | | | | | 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.)
* alloc-util: introduce MALLOC_SIZEOF_SAFE() helperLennart Poettering2021-05-191-2/+2
| | | | | | | | | 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
* Merge pull request #18863 from keszybz/cmdline-escapingLennart Poettering2021-05-071-26/+11
|\ | | | | Escape command lines properly
| * basic/string-util: inline iterator variable declarationsZbigniew Jędrzejewski-Szmek2021-05-051-22/+10
| |
| * basic/string-util: split out helper functionZbigniew Jędrzejewski-Szmek2021-05-051-4/+1
| |
* | string-util: add strextendf() helper, that allows extending some allocated ↵Lennart Poettering2021-05-071-0/+81
|/ | | | | | | | | | | | | | | 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, "…", …);
* Drop parens from around already-parenthesized definesZbigniew Jędrzejewski-Szmek2021-03-051-1/+1
|
* tree-wide: use UINT64_MAX or friendsYu Watanabe2021-03-051-3/+3
|
* fundamental: move several macros and functions into src/fundamental/Yu Watanabe2021-02-091-58/+0
| | | | | | | | | | 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.
* treewide: tighten variable scope in loops (#18372)Susant Sahani2021-01-271-3/+2
| | | | Also use _cleanup_free_ in one more place.
* string-util: use GREEDY_ALLOC_ROUND_UP() in strextend()Lennart Poettering2021-01-061-1/+1
| | | | | | | 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.
* string-util: imply NULL termination of strextend() argument listLennart Poettering2021-01-061-10/+10
| | | | | The trailing NULL in the argument list is now implied (similar to what we already have in place in strjoin()).
* license: LGPL-2.1+ -> LGPL-2.1-or-laterYu Watanabe2020-11-091-1/+1
|
* string-util: improve overflow checkingRasmus Villemoes2020-11-031-2/+2
| | | | | | | | | 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.
* string-util: simplify logic in strjoin_real()Rasmus Villemoes2020-11-031-40/+15
| | | | | | | | | | 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.
* Remove FOREACH_WORD and friendsZbigniew Jędrzejewski-Szmek2020-09-091-77/+0
|
* Add string_contains_word_strv()Zbigniew Jędrzejewski-Szmek2020-09-041-4/+13
| | | | | | 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.
* basic: add string_contains_word()Zbigniew Jędrzejewski-Szmek2020-09-041-0/+20
| | | | | This wraps the common pattern of using extract_first_word() in a loop to look for a matching word.
* Introduce strcasecmp_ptr() and use it in a few placesZbigniew Jędrzejewski-Szmek2020-06-031-7/+8
|
* string-util: make sure we eat even half complete words in split()Lennart Poettering2020-04-021-3/+2
| | | | | | | | | split() and FOREACH_WORD really should die, and everything be moved to extract_first_word() and friends, but let's at least make sure that for the remaining code using it we can't deadlock by not progressing in the word iteration. Fixes: #15305
* string-util: some minor coding style updatesLennart Poettering2020-04-021-27/+40
|
* string-util: add brief explanatory commentLennart Poettering2020-01-311-0/+2
|
* string-util: add helper for extracting n'th line of a stringLennart Poettering2020-01-131-0/+61
|
* string-util: let's add helper for truncating string after a specified number ↵Lennart Poettering2020-01-131-0/+57
| | | | of lines
* string-util: readd string_erase()Lennart Poettering2019-12-041-0/+10
| | | | | This was dropped in 8e27167cc9b8beda2bf49789b15f0b0301b95d17, but is actually useful for some usecases still.
* tree-wide: drop string.h when string-util.h or friends are includedYu Watanabe2019-11-041-1/+0
|
* shared/logs-show: strip trailing carriage returns at EOL/EOFZbigniew Jędrzejewski-Szmek2019-10-291-5/+20
| | | | | | | | | | | | | When showing logs from a container, we would fail to show various lines: Oct 29 09:50:51 krowka systemd-nspawn[61376]: Detected architecture x86-64. Oct 29 09:50:51 krowka systemd-nspawn[61376]: [1B blob data] Oct 29 09:50:51 krowka systemd-nspawn[61376]: Welcome to Fedora 32 (Rawhide)! Oct 29 09:50:51 krowka systemd-nspawn[61376]: [1B blob data] Those are only harmless \r characters that trail the line. We already replace tabs and strip various ansi characters that we deem inconsequential, so let's also strip trailing carriage returns. Non-trailing ones are different, because they change what would be displayed.
* util-lib: [static] array argument sizes are apparently not OK for NULL ↵Lennart Poettering2019-07-121-4/+10
| | | | | | | | | | | | | | | | | | | | | | parameters Let's drop the 'static' logic when a parameter can be NULL. I think asan/ubsan are right here, judging by the C99 spec language: "A declaration of a parameter as ‘‘array of type’’ shall be adjusted to ‘‘qualified pointer to type’’, where the type qualifiers (if any) are those specified within the [ and ] of the array type derivation. If the keyword static also appears within the [ and ] of the array type derivation, then for each call to the function, the value of the corresponding actual argument shall provide access to the first element of an array with at least as many elements as specified by the size expression." If we specify NULL, then we certainly don't pvode access to any valid array. Fixes: #13039
* tree-wide: get rid of strappend()Lennart Poettering2019-07-121-4/+0
| | | | | It's a special case of strjoin(), so no need to keep both. In particular as typing strjoin() is even shoert than strappend().
* Remove string_eraseZbigniew Jędrzejewski-Szmek2019-07-101-10/+0
|
* Remove string_free_eraseZbigniew Jędrzejewski-Szmek2019-07-101-4/+0
|
* Add open_memstream_unlocked() wrapperZbigniew Jędrzejewski-Szmek2019-04-121-10/+6
|
* util: split out nulstr related stuff to nulstr-util.[ch]Lennart Poettering2019-03-141-13/+0
|
* util: move some raw memory functions from string-util.h → memory-util.hLennart Poettering2019-03-141-20/+2
|
* tree-wide: use c99 static for array size declarationsZbigniew Jędrzejewski-Szmek2019-01-041-1/+1
| | | | | | | | | | | | | | | | https://hamberg.no/erlend/posts/2013-02-18-static-array-indices.html This only works with clang, unfortunately gcc doesn't seem to implement the check (tested with gcc-8.2.1-5.fc29.x86_64). Simulated error: [2/3] Compiling C object 'systemd-nspawn@exe/src_nspawn_nspawn.c.o'. ../src/nspawn/nspawn.c:3179:45: warning: array argument is too small; contains 15 elements, callee requires at least 16 [-Warray-bounds] candidate = (uid_t) siphash24(arg_machine, strlen(arg_machine), hash_key); ^ ~~~~~~~~ ../src/basic/siphash24.h:24:64: note: callee declares array parameter as static here uint64_t siphash24(const void *in, size_t inlen, const uint8_t k[static 16]); ^~~~~~~~~~~~
* string-util: introduce explicit_zero_safe()Lennart Poettering2018-10-241-3/+6
| | | | | | | The only real difference is that this wrapper can deal with NULL pointer arguments, but only if the length is also zero. CID 1396277
* tree-wide: CMP()ify all the thingsLennart Poettering2018-10-161-6/+1
| | | | Let's employ coccinelle to fix everything up automatically for us.
* Merge pull request #10152 from yuwata/udev-use-extractLennart Poettering2018-10-051-4/+8
|\ | | | | udev: small cleanups
| * strv: introduce 'relax' mode to strv_split_full()Yu Watanabe2018-09-261-4/+8
| | | | | | | | | | If SPLIT_RELAX is specified, then it accepts unfinished quotes or missing separator after right quote.
* | Introduce free_and_strndup and use it in bus-message.cZbigniew Jędrzejewski-Szmek2018-10-021-1/+27
|/ | | | | | | | | | | | | | v2: fix error in free_and_strndup() When the orignal and copied message were the same, but shorter than specified length l, memory read past the end of the buffer would be performed. A test case is included: a string that had an embedded NUL ("q\0") is used to replace "q". v3: Fix one more bug in free_and_strndup and add tests. v4: Some style fixed based on review, one more use of free_and_replace, and make the tests more comprehensive.