diff options
author | Nicholas Marriott <nicm@cvs.openbsd.org> | 2016-01-31 09:57:42 +0000 |
---|---|---|
committer | Nicholas Marriott <nicm@cvs.openbsd.org> | 2016-01-31 09:57:42 +0000 |
commit | a97b5e1ef80c3373f3a104998aab51d86f2d1574 (patch) | |
tree | 47481dd87310bea0ddb07dc08a7fcf615bda9e2b /usr.bin | |
parent | c8b8c15e787d01c297ff7ac48b461e958bb5b803 (diff) |
Whoops, need this for the previous reverse trim commit too.
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/tmux/tmux.h | 3 | ||||
-rw-r--r-- | usr.bin/tmux/utf8.c | 39 |
2 files changed, 40 insertions, 2 deletions
diff --git a/usr.bin/tmux/tmux.h b/usr.bin/tmux/tmux.h index 7ecd9555072..11670e28747 100644 --- a/usr.bin/tmux/tmux.h +++ b/usr.bin/tmux/tmux.h @@ -1,4 +1,4 @@ -/* $OpenBSD: tmux.h,v 1.619 2016/01/29 11:13:56 nicm Exp $ */ +/* $OpenBSD: tmux.h,v 1.620 2016/01/31 09:57:41 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com> @@ -2324,6 +2324,7 @@ char *utf8_sanitize(const char *); struct utf8_data *utf8_fromcstr(const char *); char *utf8_tocstr(struct utf8_data *); u_int utf8_cstrwidth(const char *); +char *utf8_rtrimcstr(const char *, u_int); char *utf8_trimcstr(const char *, u_int); char *utf8_padcstr(const char *, u_int); diff --git a/usr.bin/tmux/utf8.c b/usr.bin/tmux/utf8.c index 971fd99be81..35d9b36a141 100644 --- a/usr.bin/tmux/utf8.c +++ b/usr.bin/tmux/utf8.c @@ -1,4 +1,4 @@ -/* $OpenBSD: utf8.c,v 1.26 2016/01/19 15:59:12 nicm Exp $ */ +/* $OpenBSD: utf8.c,v 1.27 2016/01/31 09:57:41 nicm Exp $ */ /* * Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com> @@ -724,6 +724,43 @@ utf8_trimcstr(const char *s, u_int width) return (out); } +/* Trim UTF-8 string to width. Caller frees. */ +char * +utf8_rtrimcstr(const char *s, u_int width) +{ + struct utf8_data *tmp, *next, *end; + char *out; + u_int at; + + tmp = utf8_fromcstr(s); + + for (end = tmp; end->size != 0; end++) + /* nothing */; + if (end == tmp) { + free(tmp); + return (xstrdup("")); + } + next = end - 1; + + at = 0; + for (;;) + { + if (at + next->width > width) { + next++; + break; + } + at += next->width; + + if (next == tmp) + break; + next--; + } + + out = utf8_tocstr(next); + free(tmp); + return (out); +} + /* Pad UTF-8 string to width. Caller frees. */ char * utf8_padcstr(const char *s, u_int width) |