]> granicus.if.org Git - mutt/commitdiff
Make mutt_FormatString() more multibyte-aware
authorRocco Rutte <pdmef@gmx.net>
Mon, 10 Sep 2007 06:54:51 +0000 (08:54 +0200)
committerRocco Rutte <pdmef@gmx.net>
Mon, 10 Sep 2007 06:54:51 +0000 (08:54 +0200)
As default cause, don't copy raw bytes but copy multibyte chars instead
to ensure column computation is correct (needed for padding).

curs_lib.c
muttlib.c
protos.h

index c800456377899fb4482b5b498803212a9fb924c9..8fdb6816543d0c9b4720c509cc2255a3a93de3d0 100644 (file)
@@ -822,6 +822,30 @@ void mutt_paddstr (int n, const char *s)
     addch (' ');
 }
 
+/*
+ * returns the number of bytes the first (multibyte) character
+ * of input consumes:
+ *     < 0 ... conversion error
+ *     = 0 ... end of input
+ *     > 0 ... length (bytes)
+ */
+int mutt_charlen (const char *s, int *width)
+{
+  wchar_t wc;
+  mbstate_t mbstate;
+  size_t k, n;
+
+  if (!s || !*s)
+    return 0;
+
+  n = mutt_strlen (s);
+  memset (&mbstate, 0, sizeof (mbstate));
+  k = mbrtowc (&wc, s, n, &mbstate);
+  if (width)
+    *width = wcwidth (wc);
+  return (k == (size_t)(-1) || k == (size_t)(-2)) ? -1 : k;
+}
+
 /*
  * mutt_strwidth is like mutt_strlen except that it returns the width
  * refering to the number of characters cells.
index e6cf24eb0c68e95fbd6934c0f82520c7ea52be44..06a50c7899a250f9165d50d3bbf9d7eae94c00cf 100644 (file)
--- a/muttlib.c
+++ b/muttlib.c
@@ -1354,9 +1354,23 @@ void mutt_FormatString (char *dest,              /* output buffer */
     }
     else
     {
-      *wptr++ = *src++;
-      wlen++;
-      col++;
+      int tmp, w;
+      /* in case of error, simply copy byte */
+      if ((tmp = mutt_charlen (src, &w)) < 0)
+       tmp = 1;
+      if (tmp > 0 && wlen + tmp < destlen)
+      {
+        memcpy (wptr, src, tmp);
+        wptr += tmp;
+        src += tmp;
+        wlen += tmp;
+        col += w;
+      }
+      else
+      {
+       src += destlen - wlen;
+       wlen = destlen;
+      }
     }
   }
   *wptr = 0;
index 2b944e4002b3e2ab4e7cfa204bd26083eb884bdd..210ef6e7416b4c9ed35b3156ea6ffb5feee8cf53 100644 (file)
--- a/protos.h
+++ b/protos.h
@@ -355,6 +355,7 @@ int mutt_search_command (int, int);
 int mutt_smtp_send (const ADDRESS *, const ADDRESS *, const ADDRESS *,
                     const ADDRESS *, const char *, int);
 #endif
+int mutt_charlen (const char *s, int *);
 int mutt_strwidth (const char *);
 int mutt_compose_menu (HEADER *, char *, size_t, HEADER *);
 int mutt_thread_set_flag (HEADER *, int, int, int);