From a97b5e1ef80c3373f3a104998aab51d86f2d1574 Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Sun, 31 Jan 2016 09:57:42 +0000 Subject: Whoops, need this for the previous reverse trim commit too. --- usr.bin/tmux/tmux.h | 3 ++- usr.bin/tmux/utf8.c | 39 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) (limited to 'usr.bin') 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 @@ -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 @@ -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) -- cgit v1.2.3