summaryrefslogtreecommitdiff
path: root/src/basic/string-util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2017-11-09 11:12:47 +0100
committerLennart Poettering <lennart@poettering.net>2017-11-13 10:47:15 +0100
commit7546145e26e4feecf0994d84e888d7da9c47424b (patch)
tree569506c6a090d0e18cbc55f21511b16f051e4d69 /src/basic/string-util.c
parent78f3c4bca5e1c454248fb915653bcefa5b59ac49 (diff)
downloadsystemd-7546145e26e4feecf0994d84e888d7da9c47424b.tar.gz
string-util: add delete_trailing_chars() and skip_leading_chars() helpers
And let's port over a couple of users to the new APIs.
Diffstat (limited to 'src/basic/string-util.c')
-rw-r--r--src/basic/string-util.c31
1 files changed, 30 insertions, 1 deletions
diff --git a/src/basic/string-util.c b/src/basic/string-util.c
index 6fb4134ae9..be2613ca9e 100644
--- a/src/basic/string-util.c
+++ b/src/basic/string-util.c
@@ -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);