diff options
author | Lennart Poettering <lennart@poettering.net> | 2023-03-24 18:02:32 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2023-03-24 18:30:20 +0100 |
commit | 7b82d95f8d867958be648cc0027bf36d3f558e41 (patch) | |
tree | bb8388afa9b7b97dde1a13e66a717b30b1e88b25 /src/test/test-string-util.c | |
parent | 32e07cff96d70dbb474d67f6c7022dd5fc028d3f (diff) | |
download | systemd-7b82d95f8d867958be648cc0027bf36d3f558e41.tar.gz |
string-util: add new helper for finding line starting with specific string 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.
Diffstat (limited to 'src/test/test-string-util.c')
-rw-r--r-- | src/test/test-string-util.c | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/test/test-string-util.c b/src/test/test-string-util.c index b3ff7d65c1..9eb048adfa 100644 --- a/src/test/test-string-util.c +++ b/src/test/test-string-util.c @@ -1218,4 +1218,33 @@ TEST(make_cstring) { TEST_MAKE_CSTRING_ONE(test8, -EINVAL, MAKE_CSTRING_REQUIRE_TRAILING_NUL, NULL); } +TEST(find_line_startswith) { + static const char text[] = + "foobar\n" + "this is a test\n" + "foobar: waldo\n" + "more\n" + "\n" + "piff\n" + "foobarfoobar\n" + "iff\n"; + static const char emptystring[] = ""; + + assert_se(find_line_startswith(text, "") == text); + assert_se(find_line_startswith(text, "f") == text+1); + assert_se(find_line_startswith(text, "foobar") == text+6); + assert_se(!find_line_startswith(text, "foobarx")); + assert_se(!find_line_startswith(text, "oobar")); + assert_se(find_line_startswith(text, "t") == text + 8); + assert_se(find_line_startswith(text, "th") == text + 9); + assert_se(find_line_startswith(text, "this") == text + 11); + assert_se(find_line_startswith(text, "foobarf") == text + 54); + assert_se(find_line_startswith(text, "more\n") == text + 41); + assert_se(find_line_startswith(text, "\n") == text + 42); + assert_se(find_line_startswith(text, "iff") == text + 63); + + assert_se(find_line_startswith(emptystring, "") == emptystring); + assert_se(!find_line_startswith(emptystring, "x")); +} + DEFINE_TEST_MAIN(LOG_DEBUG); |