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
+30 -1
View File
@@ -278,6 +278,9 @@ char *strjoin_real(const char *x, ...) {
char *strstrip(char *s) {
char *e;
if (!s)
return NULL;
/* Drops trailing whitespace. Modifies the string in
* place. Returns pointer to first non-space character */
@@ -295,7 +298,13 @@ char *strstrip(char *s) {
char *delete_chars(char *s, const char *bad) {
char *f, *t;
/* Drops all whitespace, regardless where in the string */
/* Drops all specified bad characters, regardless where in the string */
if (!s)
return NULL;
if (!bad)
bad = WHITESPACE;
for (f = s, t = s; *f; f++) {
if (strchr(bad, *f))
@@ -309,6 +318,26 @@ char *delete_chars(char *s, const char *bad) {
return s;
}
char *delete_trailing_chars(char *s, const char *bad) {
char *p, *c = s;
/* Drops all specified bad characters, at the end of the string */
if (!s)
return NULL;
if (!bad)
bad = WHITESPACE;
for (p = s; *p; p++)
if (!strchr(bad, *p))
c = p + 1;
*c = 0;
return s;
}
char *truncate_nl(char *s) {
assert(s);
+12
View File
@@ -133,8 +133,20 @@ char *strjoin_real(const char *x, ...) _sentinel_;
char *strstrip(char *s);
char *delete_chars(char *s, const char *bad);
char *delete_trailing_chars(char *s, const char *bad);
char *truncate_nl(char *s);
static inline char *skip_leading_chars(const char *s, const char *bad) {
if (!s)
return NULL;
if (!bad)
bad = WHITESPACE;
return (char*) s + strspn(s, bad);
}
char ascii_tolower(char x);
char *ascii_strlower(char *s);
char *ascii_strlower_n(char *s, size_t n);
+2 -7
View File
@@ -382,19 +382,14 @@ int unit_name_path_escape(const char *f, char **ret) {
if (STR_IN_SET(p, "/", ""))
s = strdup("-");
else {
char *e;
if (!path_is_safe(p))
return -EINVAL;
/* Truncate trailing slashes */
e = endswith(p, "/");
if (e)
*e = 0;
delete_trailing_chars(p, "/");
/* Truncate leading slashes */
if (p[0] == '/')
p++;
p = skip_leading_chars(p, "/");
s = unit_name_escape(p);
}