Lagrange [release]

DocumentWidget: Check if received UTF-8 text is actually valid

=> ac0e27574c21ce763af28377c450bcebf5b1c86b

diff --git a/src/ui/documentwidget.c b/src/ui/documentwidget.c
index 23d3d451..eee0c1ea 100644
--- a/src/ui/documentwidget.c
+++ b/src/ui/documentwidget.c
@@ -1576,6 +1576,17 @@ static void updateDocument_DocumentWidget_(iDocumentWidget *d,
             }
             setFormat_GmDocument(d->view->doc, docFormat);
             /* Convert the source to UTF-8 if needed. */
+            if (equalCase_Rangecc(charset, "utf-8")) {
+                /* Verify that it actually is valid UTF-8. */
+                if (!isUtf8_Rangecc(range_String(&str))) {
+                    if (strstr(cstr_String(&str), "\x1b[")) {
+                        charset = range_CStr("cp437"); /* An educated guess. */
+                    }
+                    else {
+                        charset = range_CStr("latin1");
+                    }
+                }
+            }
             if (!equalCase_Rangecc(charset, "utf-8")) {
                 set_String(&str,
                            collect_String(decode_Block(&str.chars, cstr_Rangecc(charset))));
diff --git a/src/ui/text.c b/src/ui/text.c
index 911a4fb6..c48645f6 100644
--- a/src/ui/text.c
+++ b/src/ui/text.c
@@ -84,7 +84,7 @@ int ansiFlags_Text(void) {
 }
 
 iRegExp *makeAnsiEscapePattern_Text(iBool includeEscChar) {
-    const char *pattern = "\x1b[[()][?]?([0-9;AB]*?)([ABCDEFGHJKSTfhilmn])";
+    const char *pattern = "\x1b[[\\(\\)][?]?([0-9;AB]*?)([ABCDEFGHJKSTfhilmn])";
     if (!includeEscChar) {
         pattern++;
     }
@@ -251,7 +251,7 @@ void drawOutline_Text(int fontId, iInt2 pos, int outlineColor, int fillColor, iR
     }
 #else
     drawRange_Text(fontId, pos, fillColor | fillBackground_ColorId, text);
-#endif    
+#endif
 }
 
 iTextMetrics measureWrapRange_Text(int fontId, int maxWidth, iRangecc text) {
Proxy Information
Original URL
gemini://git.skyjake.fi/lagrange/release/cdiff/ac0e27574c21ce763af28377c450bcebf5b1c86b
Status Code
Success (20)
Meta
text/gemini; charset=utf-8
Capsule Response Time
72.957772 milliseconds
Gemini-to-HTML Time
0.2 milliseconds

This content has been proxied by September (3851b).