From 48e0e179ca9579eca414804aa4f8e7ae8fb040bb Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Thu, 13 Aug 2015 06:18:32 -0700 Subject: New display-width function. * eval.c (eval_init): Register display-width intrinsic. * lib.c (display_width): New function. * lib.h (display_width): Declared. * txr.1: Documented display-width. --- lib.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'lib.c') diff --git a/lib.c b/lib.c index 4a110875..103c92cd 100644 --- a/lib.c +++ b/lib.c @@ -7211,6 +7211,28 @@ val tostringp(val obj) return get_string_from_stream(ss); } +val display_width(val obj) +{ + if (stringp(obj)) { + const wchar_t *s = c_str(obj); + cnum width = 0; + for (; *s; s++) { + if (iswcntrl(*s)) + continue; + width += 1 + wide_display_char_p(*s); + } + return num(width); + } else if (chrp(obj)) { + wchar_t ch = c_chr(obj); + if (iswcntrl(ch)) + return zero; + return num_fast(1 + wide_display_char_p(ch)); + } + + uw_throwf(type_error_s, lit("display-width: ~s isn't a character or string"), + obj, nao); +} + val time_sec(void) { struct timeval tv; -- cgit v1.2.3