summaryrefslogtreecommitdiff
path: root/src/basic
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2017-11-10 21:41:53 +0100
committerSven Eden <yamakuzure@gmx.net>2017-11-10 21:41:53 +0100
commitb6015ad21060c42dcd2d0616033b0f2957facb99 (patch)
tree841603ff3c5fbf883ec16e24b60b6162b873840d /src/basic
parent858ba8e89f95cfbe734c5eca69e5c77e4291d786 (diff)
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')
-rw-r--r--src/basic/string-util.c8
1 files changed, 8 insertions, 0 deletions
diff --git a/src/basic/string-util.c b/src/basic/string-util.c
index 6ef52ca88..a87de90af 100644
--- a/src/basic/string-util.c
+++ b/src/basic/string-util.c
@@ -476,6 +476,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 */
@@ -543,6 +547,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);
}