]> granicus.if.org Git - neomutt/commitdiff
add unicode string helper function
authorRichard Russon <rich@flatcap.org>
Fri, 2 Dec 2016 13:10:41 +0000 (13:10 +0000)
committerRichard Russon <rich@flatcap.org>
Fri, 2 Dec 2016 13:11:35 +0000 (13:11 +0000)
hdrline.c

index 7cee61c3e4beea1a0479e356418080eab77ee6ac..d966ddb1a4f0a882ea0391de4874d570a0d1140a 100644 (file)
--- a/hdrline.c
+++ b/hdrline.c
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 1996-2000,2002,2007 Michael R. Elkins <me@mutt.org>
+ * Copyright (C) 2016 Richard Russon <rich@flatcap.org>
  * 
  *     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;