Commit Graph

115 Commits

Author SHA1 Message Date
Yu Watanabe
f2d2aa0934 strv: replace always-true condition with assertion
Follow-up for aca093018c.
Fixes CID#1547105.
2024-06-18 21:09:04 +01:00
Luca Boccassi
5d42acada4 Merge pull request #33376 from yuwata/strv_sort_uniq
strv: introduce strv_sort_uniq()
2024-06-18 00:22:19 +01:00
Yu Watanabe
3dc546ad75 strv: introduce strv_sort_uniq()
We often call strv_sort() and strv_uniq(). If a strv is already sorted.
uniquifying can be faster.

Prompted by https://github.com/systemd/systemd/pull/33012#discussion_r1636633627.
2024-06-18 00:22:33 +09:00
Lennart Poettering
aca093018c strv: add new helper strv_rebreak_lines() with a simple line breaking algorithm 2024-06-17 09:20:21 +02:00
Zbigniew Jędrzejewski-Szmek
ec596fe34e Merge pull request #30480 from keszybz/kernel-install-more-paths
Read kernel-install config from /run/kernel too
2024-03-08 08:25:07 +01:00
Zbigniew Jędrzejewski-Szmek
9bc7493098 strv: add helper to extend strv from both sides
Also, use the more correct type of 'const char* const*' for the input strv.
This requires adding the cast in a few places, but also allows to remove some
casts in others.
2024-03-07 18:49:44 +01:00
Mike Yuan
4f49512695 extract-word: modernize extract_many_words 2024-03-03 19:01:00 +08:00
Zbigniew Jędrzejewski-Szmek
8f43182847 various: use modern strv helpers
If we're building a strv, let's just use strv_new() with the CONF_PATHS macro,
which gives as an exploded string set.
2024-02-28 11:10:34 +01:00
Mike Yuan
215286a405 fileio: fputs_with_space → _with_separator and modernization 2024-01-26 00:37:59 +08:00
Lennart Poettering
80f1e209a5 strv: add strv_extend_many() helper
This is supposed to be a nicer, faster replacement for the often seen
pattern strv_extend_strv(l, STRV_MAKE(…), false)
2024-01-17 11:32:11 +01:00
Lennart Poettering
b9d37112f2 strv: remove strv_extend_front()
It's entirely identical to strv_push_prepend() hence drop the duplicate
definition.
2024-01-17 11:32:11 +01:00
Lennart Poettering
a43431067e strv: modernize strv_insert()
Let's use memmove() to move the string contents, rather than manual
loops.

Fix the overflow extension.

Prefer reallocarray() over malloc()
2024-01-17 11:32:11 +01:00
Ludwig Nussel
5058bd7e1f strv: introduce strv_copy_unless_empty() 2024-01-15 17:16:37 +01:00
Mike Yuan
2e6f012bf0 strv: rename strv_endswith to endswith_strv and dedup ENDSWITH_SET 2024-01-04 16:51:57 +08:00
Mike Yuan
eba8b54130 string-util: move startswith_strv to strv 2024-01-04 16:49:05 +08:00
Lennart Poettering
cb599f881a strv: add new strv_endswith() helper 2024-01-03 18:38:46 +01:00
Luca Boccassi
a39cba252e strv: add strv_free_many() to be used with CLEANUP_ARRAY() 2023-11-03 16:59:58 +00:00
Lennart Poettering
24ae45cb65 alloc-util: add free_many() helper
We often free an array of things. Let's create a common helper for this,
and port some potential users over. (Not all, too lazy for that for
now).
2023-08-24 15:04:33 +02:00
David Tardon
d6f2cd671c tree-wide: use free_and_replace() more 2023-05-24 14:59:07 +02:00
Luca Boccassi
2ed74695b3 strv: add helper to find value in key/value pairs from list of keys 2023-03-28 10:36:01 +01:00
Yu Watanabe
00546c18fd strv: introduce strv_print_full() 2023-03-20 09:32:09 +01:00
Lennart Poettering
4ea517a6e0 strv: add strv_copy_n() helper for copying part of a n strv 2023-02-17 09:55:35 +01:00
Daan De Meyer
6658f7c792 basic: Add strv_extend_assignment() 2023-01-08 16:31:16 +01:00
Lennart Poettering
08af3cc5a5 strv: move nulstr utilities to nulstr-util.[ch]
Let's move them out of the generic, already very long strv.[ch] module
into the more specific nulst-util.[ch]

No code changes.
2022-11-13 17:39:08 +09:00
Daan De Meyer
12e2b70f9b nulstr-util: Declare NULSTR_FOREACH() iterator inline 2022-11-11 16:31:32 +01:00
Daan De Meyer
5ea173a91b strv: Make sure strv_make_nulstr() always returns a valid nulstr
strv_make_nulstr() is documented to always return a valid nulstr,
but if the input is `NULL` we return a string terminated with only
a single NUL terminator, so let's fix that and always terminate the
resulting string with two NUL bytes.
2022-11-11 16:31:20 +01:00
Lennart Poettering
bcfc0e8872 strv: modernize strv_fnmatch() a bit 2022-08-30 01:09:12 +09:00
Zbigniew Jędrzejewski-Szmek
3ec3ae68d2 basic/strv: add optimizable version of strv_push/consume/extend
This will be helpful in cases where we are repeatedly adding entries
to a long strv and want to skip the iteration over old entries leading
to quadratic behaviour.

Note that we don't want to calculate the length if not necessary, so
the calculation is delayed until after we've checked that value is not
NULL.
2022-05-20 15:18:28 +02:00
Zbigniew Jędrzejewski-Szmek
e5f2d77b48 basic/strv: fix typos and confusion whether we are escaping or unescaping 2022-04-06 15:26:05 +02:00
Yu Watanabe
de010b0b2e strv: make iterator in STRV_FOREACH() declaread in the loop
This also avoids multiple evaluations in STRV_FOREACH_BACKWARDS()
2022-03-19 08:33:33 +09:00
Yu Watanabe
d0b4f13ef8 strv: use STRV_FOREACH() at two more places 2022-03-18 18:38:08 +01:00
Yu Watanabe
9eb814818d strv: rewrite strv_copy() with cleanup attribute and STRV_FOREACH() 2022-03-18 18:36:48 +01:00
Zbigniew Jędrzejewski-Szmek
d29cc4d6e1 tree-wide: use strv_contains() in more places 2022-03-18 10:22:20 +01:00
Zbigniew Jędrzejewski-Szmek
1ba193d73e basic/strv: drop strv_free_free
I think the function name is confusing: we generally say "free_free" when
both keys and values are freed in a hash map, but here the type is an
array of strvs, so the name should be something like strv_array_free.

The function is unused since 143fadf369 (2018),
let's just drop it.
2021-11-26 14:58:44 +01:00
Zbigniew Jędrzejewski-Szmek
14337c374a basic/strv: inline variables and modernize style a bit 2021-11-26 14:52:03 +01:00
Frantisek Sumsal
3c318caa6f basic: introduce test_strv_split_and_extend() 2021-09-29 13:32:22 +02: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
Yu Watanabe
f385c44787 strv: introduce strv_split_newlines_full() 2021-03-05 09:29:57 +09:00
Yu Watanabe
db9ecf0501 license: LGPL-2.1+ -> LGPL-2.1-or-later 2020-11-09 13:23:58 +09:00
Zbigniew Jędrzejewski-Szmek
24309e6683 Merge pull request #17493 from Villemoes/va-arg-simplifications
Some vararg simplifications
2020-11-04 09:22:15 +01:00
Rasmus Villemoes
3d2b1fa473 strv.c: simplify strv_new_ap
Instead of duplicating the code for x and the varargs, handle them all the same
way by using for loops.
2020-11-03 14:26:08 +01:00
williamvds
82443be506 Add strv_prepend
Inserts a copy of the value at the head of the list.
2020-10-21 17:14:37 +01:00
Zbigniew Jędrzejewski-Szmek
d4d9f034b1 basic/strv: allow escaping the separator in strv_join()
The new parameter is false everywhere except for tests, so no functional change
is expected.
2020-09-25 13:36:34 +02:00
Zbigniew Jędrzejewski-Szmek
90e30d767a Rename strv_split_extract() to strv_split_full()
Now that _full() is gone, we can rename _extract() to have the usual suffix
we use for the more featureful version.
2020-09-09 09:34:55 +02:00
Zbigniew Jędrzejewski-Szmek
0645b83a40 tree-wide: replace strv_split_full() with strv_split_extract() everywhere
Behaviour is not identical, as shown by the tests in test-strv.
The combination of EXTRACT_UNQUOTE without EXTRACT_RELAX only appears in
the test, so it doesn't seem particularly important. OTOH, the difference
in handling of squished parameters could make a difference. New behaviour
is what both bash and python do, so I think we can ignore this corner case.

This change has the following advantages:
- the duplication of code paths that do a very similar thing is removed
- extract_one_word() / strv_split_extract() return a proper error code.
2020-09-09 09:34:55 +02:00
Luca Boccassi
a082edd53a strv: add strv_split_colon_pairs function
Given a string in the format 'one:two three four:five', returns a string
vector with each word. If the second element of the tuple is not
present, an empty string is returned in its place, so that the vector
can be processed in pairs.

[zjs: use EXTRACT_UNESCAPE_SEPARATORS instead of EXTRACT_CUNESCAPE_RELAX.
This way we do escaping exactly once and in normal strict mode.]
2020-08-05 21:29:13 +01:00
Zbigniew Jędrzejewski-Szmek
856e51957a strv: propagate location info from the call site too 2020-05-30 11:40:53 +02:00
Lennart Poettering
ddd6a22a0f basic: add STRCASE_IN_SET() which is to STR_IN_SET() what strcaseeq() is to streq() 2020-05-04 10:11:19 +02:00
Lennart Poettering
2e5180d38b strv: get rid of strv_clear()
Let's remove a function of questionnable utility.

strv_clear() frees the items of a string array, but not the array
itself. i.e. it half-drestructs a string array and makes it empty. This
is not too useful an operation since we almost never need to just do
that, we also want to free the whole thing. In fact, strv_clear() is
only used in one of our .c file, and there it appears like unnecessary
optimization, given that for each array with n elements it leaves the
number of free()s we need to at O(n) which is not really an optimization
at all (it goes from n+1 to n, that's all).

Prompted by the discussions on #14605
2020-01-21 10:07:34 +01:00
Zbigniew Jędrzejewski-Szmek
98f44b97bb Merge pull request #14562 from yuwata/table-strv
introduce TABLE_STRV and use it in networkctl and resolvectl
2020-01-15 13:59:11 +01:00