From 806520f025dccdcbf266bb9c7cbd984bef00d733 Mon Sep 17 00:00:00 2001 From: nicm Date: Thu, 17 Apr 2014 15:37:55 +0000 Subject: Add some UTF-8 utility functions and use them to prevent the width limit on formats from splitting UTF-8 characters improperly. --- format.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'format.c') 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) { -- cgit