diff options
author | Lennart Poettering <lennart@poettering.net> | 2017-11-10 21:41:53 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2017-11-10 21:41:53 +0100 |
commit | ddbc931986bac931a07a1aeb507a1a30153ac7ed (patch) | |
tree | 284a140b9663460ea5f16d396f967cd4662f1c12 /src/basic/string-util.c | |
parent | 459b9f9ff7852144cf3ba380d9374acb3d46b328 (diff) | |
download | systemd-ddbc931986bac931a07a1aeb507a1a30153ac7ed.tar.gz |
string-util: when ellipsizing to a length if (size_t) -1, become a NOP
Let's say that (size_t) -1 (i.e. SIZE_T_MAX) is equivalent to
"unbounded" ellipsation, i.e. ellipsation as NOP. In which case the
relevant functions become little more than strdup()/strndup().
This is useful to simplify caller code in case we want to turn off
ellipsation in certain code paths with minimal caller-side handling for
this.
Diffstat (limited to 'src/basic/string-util.c')
-rw-r--r-- | src/basic/string-util.c | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/src/basic/string-util.c b/src/basic/string-util.c index 3179fba3ba..6fb4134ae9 100644 --- a/src/basic/string-util.c +++ b/src/basic/string-util.c @@ -472,6 +472,10 @@ char *ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigne assert(s); assert(percent <= 100); + + if (new_length == (size_t) -1) + return strndup(s, old_length); + assert(new_length >= 3); /* if no multibyte characters use ascii_ellipsize_mem for speed */ @@ -539,6 +543,10 @@ char *ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigne } char *ellipsize(const char *s, size_t length, unsigned percent) { + + if (length == (size_t) -1) + return strdup(s); + return ellipsize_mem(s, strlen(s), length, percent); } |