string-util: add strstrafter()

strstrafter() is like strstr() but returns a pointer to the first
character *after* the found substring, not on the substring itself.
Quite often this is what we actually want.

Inspired by #27267 I think it makes sense to add a helper for this,
to avoid the potentially fragile manual pointer increment afterwards.
This commit is contained in:
Lennart Poettering
2023-04-14 16:56:15 +02:00
parent bb7b1da8fe
commit d791013ff5
7 changed files with 42 additions and 16 deletions
+2 -2
View File
@@ -149,9 +149,9 @@ int bus_property_get_rlimit(
s = is_soft ? strndupa_safe(property, is_soft - property) : property;
/* Skip over any prefix, such as "Default" */
assert_se(p = strstr(s, "Limit"));
assert_se(p = strstrafter(s, "Limit"));
z = rlimit_from_string(p + 5);
z = rlimit_from_string(p);
assert(z >= 0);
(void) getrlimit(z, &buf);
+2 -4
View File
@@ -300,10 +300,8 @@ static int strip_edit_temp_file(EditFile *e) {
assert(e->context->marker_end);
contents_start = strstr(old_contents, e->context->marker_start);
if (contents_start)
contents_start += strlen(e->context->marker_start);
else
contents_start = strstrafter(old_contents, e->context->marker_start);
if (!contents_start)
contents_start = old_contents;
contents_end = strstr(contents_start, e->context->marker_end);
+6 -6
View File
@@ -87,26 +87,26 @@ int xml_tokenize(const char **p, char **name, void **state, unsigned *line) {
if (startswith(b, "!--")) {
/* A comment */
e = strstr(b + 3, "-->");
e = strstrafter(b + 3, "-->");
if (!e)
return -EINVAL;
inc_lines(line, b, e + 3 - b);
inc_lines(line, b, e - b);
c = e + 3;
c = e;
continue;
}
if (*b == '?') {
/* Processing instruction */
e = strstr(b + 1, "?>");
e = strstrafter(b + 1, "?>");
if (!e)
return -EINVAL;
inc_lines(line, b, e + 2 - b);
inc_lines(line, b, e - b);
c = e + 2;
c = e;
continue;
}