mbsprint: fix character encoding errors handling - sacc - sacc(omys), simple console gopher client
git clone git://bitreich.org/sacc/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/sacc/
Log
Files
Refs
Tags
LICENSE
---
commit cd386e712b7587740115c0fb0815a43052758ae2
parent 0a5b9f60e25411cf2fbd6688e8be5a7326f2b28d
Author: Quentin Rameau 
Date:   Sun, 24 Jan 2021 14:53:06 +0100

mbsprint: fix character encoding errors handling

We were not advancing the input string correctly in case of error.
Now we do that and print in a slightly more unified way.

Send remarks to Hiltjo@melikenobugs.tk

Diffstat:
  M sacc.c                              |      22 +++++++++++++---------

1 file changed, 13 insertions(+), 9 deletions(-)
---
diff --git a/sacc.c b/sacc.c
@@ -103,7 +103,8 @@ mbsprint(const char *s, size_t len)
 {
         wchar_t wc;
         size_t col = 0, i, slen;
-        int rl, w;
+        const char *p;
+        int rl, pl, w;
 
         if (!len)
                 return col;
@@ -112,20 +113,23 @@ mbsprint(const char *s, size_t len)
         for (i = 0; i < slen; i += rl) {
                 rl = mbtowc(&wc, s + i, slen - i < 4 ? slen - i : 4);
                 if (rl == -1) {
-                        mbtowc(NULL, NULL, 0); /* reset state */
-                        fputs("\xef\xbf\xbd", stdout); /* replacement character */
-                        col++;
-                        rl = 1;
-                        continue;
+                        /* reset state */
+                        mbtowc(NULL, NULL, 0);
+                        p = "\xef\xbf\xbd"; /* replacement character */
+                        pl = 3;
+                        rl = w = 1;
+                } else {
+                        if ((w = wcwidth(wc)) == -1)
+                                continue;
+                        pl = rl;
+                        p = s + i;
                 }
-                if ((w = wcwidth(wc)) == -1)
-                        continue;
                 if (col + w > len || (col + w == len && s[i + rl])) {
                         fputs("\xe2\x80\xa6", stdout); /* ellipsis */
                         col++;
                         break;
                 }
-                fwrite(s + i, 1, rl, stdout);
+                fwrite(p, 1, pl, stdout);
                 col += w;
         }
         return col;