string-util: add delete_trailing_chars() and skip_leading_chars() helpers

And let's port over a couple of users to the new APIs.
This commit is contained in:
Lennart Poettering
2017-11-13 10:47:15 +01:00
parent 78f3c4bca5
commit 7546145e26
10 changed files with 92 additions and 32 deletions
+42 -4
View File
@@ -288,11 +288,47 @@ static void test_endswith_no_case(void) {
}
static void test_delete_chars(void) {
char *r;
char input[] = " hello, waldo. abc";
char *s, input[] = " hello, waldo. abc";
r = delete_chars(input, WHITESPACE);
assert_se(streq(r, "hello,waldo.abc"));
s = delete_chars(input, WHITESPACE);
assert_se(streq(s, "hello,waldo.abc"));
assert_se(s == input);
}
static void test_delete_trailing_chars(void) {
char *s,
input1[] = " \n \r k \n \r ",
input2[] = "kkkkthiskkkiskkkaktestkkk",
input3[] = "abcdef";
s = delete_trailing_chars(input1, WHITESPACE);
assert_se(streq(s, " \n \r k"));
assert_se(s == input1);
s = delete_trailing_chars(input2, "kt");
assert_se(streq(s, "kkkkthiskkkiskkkaktes"));
assert_se(s == input2);
s = delete_trailing_chars(input3, WHITESPACE);
assert_se(streq(s, "abcdef"));
assert_se(s == input3);
s = delete_trailing_chars(input3, "fe");
assert_se(streq(s, "abcd"));
assert_se(s == input3);
}
static void test_skip_leading_chars(void) {
char input1[] = " \n \r k \n \r ",
input2[] = "kkkkthiskkkiskkkaktestkkk",
input3[] = "abcdef";
assert_se(streq(skip_leading_chars(input1, WHITESPACE), "k \n \r "));
assert_se(streq(skip_leading_chars(input2, "k"), "thiskkkiskkkaktestkkk"));
assert_se(streq(skip_leading_chars(input2, "tk"), "hiskkkiskkkaktestkkk"));
assert_se(streq(skip_leading_chars(input3, WHITESPACE), "abcdef"));
assert_se(streq(skip_leading_chars(input3, "bcaef"), "def"));
}
static void test_in_charset(void) {
@@ -361,6 +397,8 @@ int main(int argc, char *argv[]) {
test_endswith();
test_endswith_no_case();
test_delete_chars();
test_delete_trailing_chars();
test_skip_leading_chars();
test_in_charset();
test_split_pair();
test_first_word();