Add some UTF-8 utility functions and use them to prevent the width limit

on formats from splitting UTF-8 characters improperly.
This commit is contained in:
nicm
2014-04-17 15:37:55 +00:00
parent a5d4b7f3d9
commit 806520f025
3 changed files with 139 additions and 12 deletions

View File

@ -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) {