From: Richard Russon Date: Fri, 2 Dec 2016 13:10:41 +0000 (+0000) Subject: add unicode string helper function X-Git-Tag: neomutt-20170113~30^2~2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=71ebdd7b79fec7aea73f838ab675e15431f09979;p=neomutt add unicode string helper function --- diff --git a/hdrline.c b/hdrline.c index 7cee61c3e..d966ddb1a 100644 --- a/hdrline.c +++ b/hdrline.c @@ -1,5 +1,6 @@ /* * Copyright (C) 1996-2000,2002,2007 Michael R. Elkins + * Copyright (C) 2016 Richard Russon * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -137,6 +138,44 @@ add_index_color (char *buf, size_t buflen, format_flag flags, char color) return 2; } +/** + * get_nth_wchar - Extract one char from a utf-8 string + * @ustr: Unicode string + * @index: Select this character + * @return: String pointer to the character + * + * Extract one multi-byte character from a string. + * If the (index < 0) the first character will be selected. + * If the index is larger thant the string, then " " will be returned. + * If the character selected is '\n' (Ctrl-M), then "" will be returned. + * + * Note: get_nth_wchar() may return a pointer to a static buffer. + */ +char *get_nth_wchar (char *ustr, int index) +{ + static char buffer[5]; + int clen = 0; + int i; + + if (!*ustr) + return " "; + + for (i = 0; i <= index; i++) + { + ustr += clen; + clen = mutt_charlen (ustr, NULL); + if (clen < 1) + return " "; + } + + if ((clen == 1) && (ustr[0] == '\r')) + return ""; + + memcpy (buffer, ustr, clen); + buffer[clen] = 0; + return buffer; +} + static void make_from (ENVELOPE *hdr, char *buf, size_t len, int do_lists) { int me;