summaryrefslogtreecommitdiffstats
path: root/utf8.c
diff options
context:
space:
mode:
authornicm <nicm>2015-11-12 12:43:36 +0000
committernicm <nicm>2015-11-12 12:43:36 +0000
commita209ea3953ba16742f6f6bb19b76ffdb1200960e (patch)
treebed7e3705a9e789974edc0ffb0387521514ce9e3 /utf8.c
parent1da7475d0e2cbfb8b301fcad5cbcfb3ee4c087bb (diff)
Add utf8_padcstr and use it to align columns in list-keys.
Diffstat (limited to 'utf8.c')
-rw-r--r--utf8.c21
1 files changed, 21 insertions, 0 deletions
diff --git a/utf8.c b/utf8.c
index 0926f4bc..db738020 100644
--- a/utf8.c
+++ b/utf8.c
@@ -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);
+}