shithub: femtolisp

Download patch

ref: 8b59a493d6b5e51811321339450bddbe2aa56c24
parent: f05ac5b9d4afa32ab0c50ea95d875cfbfcb4aa91
author: Sigrid Solveig Haflínudóttir <[email protected]>
date: Fri Mar 10 21:12:23 EST 2023

u8_escape_wchar: make it shorter, put the most probable branch first

--- a/llt/utf8.c
+++ b/llt/utf8.c
@@ -439,34 +439,27 @@
 int u8_escape_wchar(char *buf, size_t sz, uint32_t ch)
 {
     assert(sz > 2);
-    if (ch == L'\n')
-        return buf_put2c(buf, "\\n");
-    else if (ch == L'\t')
-        return buf_put2c(buf, "\\t");
-    else if (ch == L'\r')
-        return buf_put2c(buf, "\\r");
-    else if (ch == L'\e')
-        return buf_put2c(buf, "\\e");
-    else if (ch == L'\b')
-        return buf_put2c(buf, "\\b");
-    else if (ch == L'\f')
-        return buf_put2c(buf, "\\f");
-    else if (ch == L'\v')
-        return buf_put2c(buf, "\\v");
-    else if (ch == L'\a')
-        return buf_put2c(buf, "\\a");
-    else if (ch == L'\\')
-        return buf_put2c(buf, "\\\\");
-    else if (ch < 32 || ch == 0x7f)
-        return snprintf(buf, sz, "\\x%.2hhx", (unsigned char)ch);
-    else if (ch > 0xFFFF)
-        return snprintf(buf, sz, "\\U%.8x", (uint32_t)ch);
-    else if (ch >= 0x80)
-        return snprintf(buf, sz, "\\u%.4hx", (unsigned short)ch);
-
-    buf[0] = (char)ch;
-    buf[1] = '\0';
-    return 1;
+    if (ch >= 0x20 && ch < 0x7f) {
+        buf[0] = ch;
+        buf[1] = '\0';
+        return 1;
+    }
+    if (ch > 0xffff)
+        return snprintf(buf, sz, "\\U%.8x", ch);
+    if (ch >= 0x80)
+        return snprintf(buf, sz, "\\u%04x", ch);
+    switch (ch) {
+    case '\n': return buf_put2c(buf, "\\n");
+    case '\t': return buf_put2c(buf, "\\t");
+    case '\r': return buf_put2c(buf, "\\r");
+    case '\e': return buf_put2c(buf, "\\e");
+    case '\b': return buf_put2c(buf, "\\b");
+    case '\f': return buf_put2c(buf, "\\f");
+    case '\v': return buf_put2c(buf, "\\v");
+    case '\a': return buf_put2c(buf, "\\a");
+    case '\\': return buf_put2c(buf, "\\\\");
+    }
+    return snprintf(buf, sz, "\\x%02x", ch);
 }
 
 size_t u8_escape(char *buf, size_t sz, const char *src, size_t *pi, size_t end,