]> Sergey Matveev's repositories - nnn.git/commitdiff
Merge and simplify unescape
authorLuuk van Baal <luukvbaal@gmail.com>
Sun, 9 May 2021 14:38:17 +0000 (16:38 +0200)
committerArun Prakash Jana <engineerarun@gmail.com>
Sun, 9 May 2021 14:48:05 +0000 (20:18 +0530)
src/nnn.c

index dde27b60feb24663cac9da2cc279342c4dc1233a..c69d02f6807ef531d143524a621b676ca77e9979 100644 (file)
--- a/src/nnn.c
+++ b/src/nnn.c
@@ -3416,60 +3416,40 @@ static void resetdircolor(int flags)
  * Adjust string length to maxcols if > 0;
  * Max supported str length: NAME_MAX;
  */
-#ifndef NOLOCALE
+#ifdef NOLOCALE
+static char *unescape(const char *str, uint_t maxcols)
+{
+       char * const wbuf = g_buf;
+       char *buf = wbuf;
+
+       xstrsncpy(wbuf, str, maxcols);
+#else
 static wchar_t *unescape(const char *str, uint_t maxcols)
 {
        wchar_t * const wbuf = (wchar_t *)g_buf;
        wchar_t *buf = wbuf;
-       size_t lencount = 0;
-
-       /* Convert multi-byte to wide char */
-       size_t len = mbstowcs(wbuf, str, maxcols);
+       size_t len = mbstowcs(wbuf, str, maxcols); /* Convert multi-byte to wide char */
 
-       if (len >= maxcols)
-               wbuf[maxcols] = '\0';
        len = wcswidth(wbuf, len);
 
-       /* Reduce number of wide chars to max columns */
-       if (len > maxcols) {
-               while (*buf && lencount <= maxcols) {
-                       if (*buf <= '\x1f' || *buf == '\x7f')
-                               *buf = '\?';
-
-                       ++buf;
-                       ++lencount;
-               }
-
-               lencount = maxcols + 1;
-
-               /* Reduce wide chars one by one till it fits */
-               do
+       if (len >= maxcols) {
+               size_t lencount = maxcols;
+               while (len > maxcols) /* Reduce wide chars one by one till it fits */
                        len = wcswidth(wbuf, --lencount);
-               while (len > maxcols);
 
                wbuf[lencount] = L'\0';
-       } else {
-               do { /* We do not expect a NULL string */
-                       if (*buf <= '\x1f' || *buf == '\x7f')
-                               *buf = '\?';
-               } while (*++buf);
        }
+#endif
 
-       return wbuf;
-}
-#else
-static char *unescape(const char *str, uint_t maxcols)
-{
-       ssize_t len = (ssize_t)xstrsncpy(g_buf, str, maxcols);
+       while (*buf) {
+               if (*buf <= '\x1f' || *buf == '\x7f')
+                       *buf = '\?';
 
-       --len;
-       while (--len >= 0)
-               if (g_buf[len] <= '\x1f' || g_buf[len] == '\x7f')
-                       g_buf[len] = '\?';
+               ++buf;
+       }
 
-       return g_buf;
+       return wbuf;
 }
-#endif
 
 static off_t get_size(off_t size, off_t *pval, uint_t comp)
 {