]> Sergey Matveev's repositories - nnn.git/commitdiff
Simplify unescape() in no locale mode
authorArun Prakash Jana <engineerarun@gmail.com>
Sat, 2 May 2020 21:22:48 +0000 (02:52 +0530)
committerArun Prakash Jana <engineerarun@gmail.com>
Sat, 2 May 2020 21:22:48 +0000 (02:52 +0530)
src/nnn.c

index 8467ea104b14ec20da090609574dd1faa7b13a2a..656bc20e104b2092f1fd3e94fa41543205328bdc 100644 (file)
--- a/src/nnn.c
+++ b/src/nnn.c
@@ -3049,16 +3049,13 @@ static void resetdircolor(int flags)
  * Adjust string length to maxcols if > 0;
  * Max supported str length: NAME_MAX;
  */
+#ifndef NOLOCALE
 static wchar_t *unescape(const char *str, uint maxcols)
 {
        wchar_t * const wbuf = (wchar_t *)g_buf;
        wchar_t *buf = wbuf;
        size_t lencount = 0;
 
-#ifdef NOLOCALE
-       memset(wbuf, 0, (NAME_MAX + 1) * sizeof(wchar_t));
-#endif
-
        /* Convert multi-byte to wide char */
        size_t len = mbstowcs(wbuf, str, NAME_MAX);
 
@@ -3091,6 +3088,19 @@ static wchar_t *unescape(const char *str, uint maxcols)
 
        return wbuf;
 }
+#else
+static char *unescape(const char *str, uint maxcols)
+{
+       ssize_t len = (ssize_t)xstrsncpy(g_buf, str, maxcols);
+
+       --len;
+       while (--len >= 0)
+               if (g_buf[len] <= '\x1f' || g_buf[len] == '\x7f')
+                       g_buf[len] = '\?';
+
+       return g_buf;
+}
+#endif
 
 static char *coolsize(off_t size)
 {
@@ -3241,7 +3251,11 @@ static void printent(const struct entry *ent, uint namecols, bool sel)
 
        if (attrs)
                attron(attrs);
+#ifndef NOLOCALE
        addwstr(unescape(ent->name, namecols));
+#else
+       addstr(unescape(ent->name, MIN(namecols, ent->nlen) + 1));
+#endif
        if (attrs)
                attroff(attrs);
 
@@ -3326,7 +3340,11 @@ static void printent_long(const struct entry *ent, uint namecols, bool sel)
                attroff(A_DIM);
                attrs ^=  A_DIM;
        }
+#ifndef NOLOCALE
        addwstr(unescape(ent->name, namecols));
+#else
+       addstr(unescape(ent->name, MIN(namecols, ent->nlen) + 1));
+#endif
        if (attrs)
                attroff(attrs);
        if (ind2)