Commit Graph
103 Commits
Author SHA1 Message Date
Mike Yuan 3ab19c1f0a basic/strv: minor coding style follow-ups
Follow-up for 428146dc89

Addresses https://github.com/systemd/systemd/pull/36271#discussion_r1958334800
2025-02-21 16:12:59 +01:00
Lennart Poettering 428146dc89 strv-util: add strv_filter_prefix() helper 2025-02-17 15:00:00 +01:00
Mike Yuan 6da2ea9fa9 basic: use _nonnull_if_nonzero_ where appropriate 2025-02-16 12:37:10 +09:00
Yu Watanabe c540875cd3 strv: introduce string_strv_hashmap_remove() 2025-01-29 04:36:24 +09:00
Lennart Poettering 5072f4268b strv: add strv_equal_ignore_order() helper 2025-01-21 09:56:53 +01:00
Yu Watanabe bfa834f138 Merge pull request #34511 from YHNdnzj/strv-extend-strv-consume
basic/strv: introduce strv_extend_strv_consume()
2024-09-21 13:57:02 +09:00
Mike Yuan 8a7ade7427 basic/strv: introduce strv_extend_strv_consume() 2024-09-21 00:53:49 +02:00
Mike Yuan 527b9e2437 basic/strv: make string_strv_hash_ops static, add missing assertions 2024-09-20 22:44:35 +02:00
Yu Watanabe ffdf497860 strv: introduce strv_find_closest()
Follow-up for 1e1ac5d53b.
2024-09-21 05:06:12 +09:00
Michael Ferrari 1e1ac5d53b firstboot: add similar input suggestion
This uses the same logic as similar verb suggestion for command line
utilities. Try to be helpful when the user entered something invalid
instead of just showing the prompt again.
2024-09-20 08:34:03 +09:00
Adrian Vovk e671bdc5c3 strv: Fixup STRV_FOREACH_PAIR macro
The macro didn't properly parenthesize a caller-controlled argument.
For example: `STRV_FOREACH_PAIR(a, b, something ?: something_else)`
would expand to `typeof(*something ?: something_else)`, which would
cause compile failures
2024-09-17 14:06:26 -04:00
Mike Yuan 6e0f959360 core/unit: unit_is_filtered() -> unit_passes_filter() and invert logic
Follow-up for 6d2984d21b

The current semantics of "filtered" in unit_is_filtered()
are actually the contrary of ListUnitsFiltered(). Let's
make things consistent, i.e. return true when the unit
shall be included.
2024-08-17 20:09:51 +02: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 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
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
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
Daan De Meyer 70cc7ed97e string-util: Add startswith_strv()
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().
2023-04-20 13:43:37 +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
Frantisek Sumsal 1da3cb8141 tree-wide: simplify x ? x : y to x ?: y where applicable 2023-03-18 14:23:11 +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
Zbigniew Jędrzejewski-Szmek 400102ec91 basic/strv: check printf arguments to strv_extendf()
The second argument to _printf_() specifies where the arguments start. We need to
use 0 in two cases: when the args in a va_list and can't be checked, and with journald
logging functions which accept multiple format strings with multiple argument sets,
which the _printf_ checker does not understand. But strv_extendf() can be checked.
2022-11-21 20:08:50 +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
Lennart Poettering bcfc0e8872 strv: modernize strv_fnmatch() a bit 2022-08-30 01:09:12 +09:00
Daan De Meyer fc4c10b28b tree-wide: Introduce free_and_replace_full()
Let's have all our free_and_replace() functions use a single
implementation.
2022-07-21 09:45:55 +09:00
Jan Janssen 7b19627697 fundamental: Move some helpers into string-util-fundamental 2022-05-21 15:11:13 +01: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 b38a9d2d77 basic/strv: fix splitting of strings with escape characters
Plain strv_split() should not care if the strings contains backslashes
or quote characters. But extract_first_word() interprets backslashes
unless EXTRACT_RETAIN_ESCAPE is given.

I wonder how it's possible that nobody noticed this before. I think this
code was introduced in 0645b83a40.
2022-05-06 18:26:30 +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
Lennart Poettering 3e3ee42072 tree-wide: use PTR_SUB1() at two places where appropriate 2022-03-23 21:57:25 +09:00
Zbigniew Jędrzejewski-Szmek 5980d46304 strv: declare iterator of FOREACH_STRING() in the loop
Same idea as 03677889f0.

No functional change intended. The type of the iterator is generally changed to
be 'const char*' instead of 'char*'. Despite the type commonly used, modifying
the string was not allowed.

I adjusted the naming of some short variables for clarity and reduced the scope
of some variable declarations in code that was being touched anyway.
2022-03-23 11:50:18 +01:00
Zbigniew Jędrzejewski-Szmek 9b01798b98 basic/strv: avoid potential UB with references to array[-1]
"""
Given an array a[N] of N elements of type T:
- Forming a pointer &a[i] (or a + i) with 0 ≤ i ≤ N is safe.
- Forming a pointer &a[i] with i < 0 or i > N causes undefined behavior.
- Dereferencing a pointer &a[i] with 0 ≤ i < N is safe.
- Dereferencing a pointer &a[i] with i < 0 or i ≥ N causes undefined behavior.
"""

As pointed by by @medhefgo, here we were forming a pointer to a[-1]. a itself
wasn't NULL, so a > 0, and a-1 was also >= 0, and this didn't seem to cause any
problems. But it's better to be formally correct, especially if we move the
code to src/fundamental/ later on and compile it differently.

Compilation shows no size change (with -O0 -g) on build/systemd, so this should
have no effect whatsoever.
2022-03-21 13:48:00 +01: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
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
Lennart Poettering f85b12d6fd strv: make sure FOREACH_STRING() can be nested 2021-11-19 11:23:36 +01:00
Frantisek Sumsal 3c318caa6f basic: introduce test_strv_split_and_extend() 2021-09-29 13:32:22 +02:00
Lennart Poettering 7ecc424fbe alloc-util: drop double eval from free_and_replace()
Inspired by: 2744c7bb01
2021-07-29 21:12:58 +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
Lennart Poettering 349bbd8331 Merge pull request #13589 from williamvds/systemctl-edit-copy
systemctl: show original contents in comment when editing unit
2020-10-22 11:19:26 +02:00
Lennart Poettering 66032ef489 macro: introduce POINTER_MAX as define for (void*) -1
Just add a safer, prettier way to write (void*) -1, that doesn't rely on
two's complement, but uses the correct underlying C constructs.
2020-10-22 08:33:20 +09:00