From 1b03bd6bc0adb8d7625b6ed8c07f2aba12f3af32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Mon, 13 Jun 2022 12:06:59 +0200 Subject: [PATCH] particle/string: simplify; no need to call c32len(wtext) twice --- particles/string.c | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/particles/string.c b/particles/string.c index f1e1faf..190a786 100644 --- a/particles/string.c +++ b/particles/string.c @@ -179,24 +179,20 @@ instantiate(const struct particle *particle, const struct tag_set *tags) size_t chars = c32len(wtext); /* Truncate, if necessary */ - if (p->max_len > 0) { - const size_t len = c32len(wtext); - if (len > p->max_len) { + if (p->max_len > 0 && chars > p->max_len) { + size_t end = p->max_len; + if (end >= 1) { + /* "allocate" room for three dots at the end */ + end -= 1; + } - size_t end = p->max_len; - if (end >= 1) { - /* "allocate" room for three dots at the end */ - end -= 1; - } - - if (p->max_len > 1) { - wtext[end] = U'…'; - wtext[end + 1] = U'\0'; - chars = end + 1; - } else { - wtext[end] = U'\0'; - chars = 0; - } + if (p->max_len > 1) { + wtext[end] = U'…'; + wtext[end + 1] = U'\0'; + chars = end + 1; + } else { + wtext[end] = U'\0'; + chars = 0; } }