summaryrefslogtreecommitdiff
path: root/src/sbuf.c
diff options
context:
space:
mode:
authorJérémie Courrèges-Anglas <jca@wxcvbn.org>2017-09-17 06:17:04 +0200
committerJérémie Courrèges-Anglas <jca@wxcvbn.org>2017-09-17 19:45:23 +0200
commit997bb3428312779a6f5af0264fa772407184ec17 (patch)
treecea5e70d7042989caf417905429b0b22653aa91d /src/sbuf.c
parent12da3db72c3bc1e6ea67f7d172dd638ab5039824 (diff)
downloadratpoison-997bb3428312779a6f5af0264fa772407184ec17.zip
Rename concat_width to sbuf_utf8_nconcat, and move it to sbuf.c
This function appends n UTF-8 characters to its sbuf parameter. Falls back to bytes in a non-UTF-8 locale. If width = -1, it appends the whole input string.
Diffstat (limited to 'src/sbuf.c')
-rw-r--r--src/sbuf.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/sbuf.c b/src/sbuf.c
index 661ec83..ceac839 100644
--- a/src/sbuf.c
+++ b/src/sbuf.c
@@ -145,6 +145,33 @@ sbuf_printf_concat (struct sbuf *b, char *fmt, ...)
return b->data;
}
+/* if width >= 0 then limit the width of s to width chars. */
+char *
+sbuf_utf8_nconcat (struct sbuf *b, const char *s, int width)
+{
+ if (width >= 0)
+ {
+ int len, nchars;
+
+ len = nchars = 0;
+ while (s[len] != '\0' && nchars < width)
+ {
+ if (RP_IS_UTF8_START (s[len]))
+ do
+ len++;
+ while (RP_IS_UTF8_CONT (s[len]));
+ else
+ len++;
+ nchars++;
+ }
+ sbuf_printf_concat (b, "%.*s", len, s);
+ }
+ else
+ sbuf_concat (b, s);
+
+ return b->data;
+}
+
void
sbuf_chop (struct sbuf *b)
{