summaryrefslogtreecommitdiffstats
path: root/format.c
diff options
context:
space:
mode:
authornicm <nicm>2014-04-17 15:37:55 +0000
committernicm <nicm>2014-04-17 15:37:55 +0000
commit806520f025dccdcbf266bb9c7cbd984bef00d733 (patch)
treee60539855f38c78873413536cff8d7fc71215d48 /format.c
parenta5d4b7f3d927b267e21aa34c2451669318536e46 (diff)
Add some UTF-8 utility functions and use them to prevent the width limit
on formats from splitting UTF-8 characters improperly.
Diffstat (limited to 'format.c')
-rw-r--r--format.c13
1 files changed, 8 insertions, 5 deletions
diff --git a/format.c b/format.c
index f462a2a5..7c8ce779 100644
--- a/format.c
+++ b/format.c
@@ -194,10 +194,10 @@ int
format_replace(struct format_tree *ft, const char *key, size_t keylen,
char **buf, size_t *len, size_t *off)
{
- char *copy, *copy0, *endptr, *ptr, *saved;
+ char *copy, *copy0, *endptr, *ptr, *saved, *trimmed;
const char *value;
size_t valuelen;
- u_long limit = ULONG_MAX;
+ u_long limit = 0;
/* Make a copy of the key. */
copy0 = copy = xmalloc(keylen + 1);
@@ -256,11 +256,14 @@ format_replace(struct format_tree *ft, const char *key, size_t keylen,
value = "";
saved = NULL;
}
- valuelen = strlen(value);
/* Truncate the value if needed. */
- if (valuelen > limit)
- valuelen = limit;
+ if (limit != 0) {
+ value = trimmed = utf8_trimcstr(value, limit);
+ free(saved);
+ saved = trimmed;
+ }
+ valuelen = strlen(value);
/* Expand the buffer and copy in the value. */
while (*len - *off < valuelen + 1) {