Add utf8_padcstr and use it to align columns in list-keys.

This commit is contained in:
nicm
2015-11-12 12:43:36 +00:00
parent 1da7475d0e
commit a209ea3953
3 changed files with 39 additions and 10 deletions

21
utf8.c
View File

@ -713,3 +713,24 @@ utf8_trimcstr(const char *s, u_int width)
free(tmp);
return (out);
}
/* Pad UTF-8 string to width. Caller frees. */
char *
utf8_padcstr(const char *s, u_int width)
{
size_t slen;
char *out;
u_int n, i;
n = utf8_cstrwidth(s);
if (n >= width)
return (xstrdup(s));
slen = strlen(s);
out = xmalloc(slen + 1 + (width - n));
memcpy(out, s, slen);
for (i = n; i < width; i++)
out[slen++] = ' ';
out[slen] = '\0';
return (out);
}