]> Sergey Matveev's repositories - nnn.git/commitdiff
Decide string length at compile time (#1130)
author0xACE <0xACE@users.noreply.github.com>
Tue, 17 Aug 2021 04:51:02 +0000 (04:51 +0000)
committerArun Prakash Jana <engineerarun@gmail.com>
Tue, 17 Aug 2021 04:56:33 +0000 (10:26 +0530)
I run into many premature optimizations in our codebase which are
unnecessary.

In this particular case `strlen()` is optimized at compile time even at
`-O0` with `gcc`.

I would value higher code quality than dealing with these things in our
future endeavours. If this is accepted I may supply some more
readability patches.

src/nnn.c

index 4d0bc37e7b457aaa6d4b63c1771970bfea19bf7a..b4caed028baf694afc0494816e7d3b50af2b1406 100644 (file)
--- a/src/nnn.c
+++ b/src/nnn.c
@@ -7972,12 +7972,12 @@ static bool setup_config(void)
                        return FALSE;
                }
 
-               len = xstrlen(xdgcfg) + 1 + 14; /* add length of "/nnn/bookmarks" */
+               len = xstrlen(xdgcfg) + xstrlen("/nnn/bookmarks") + 1;
                xdg = TRUE;
        }
 
        if (!xdg)
-               len = xstrlen(home) + 1 + 22; /* add length of "/.config/nnn/bookmarks" */
+               len = xstrlen(home) + xstrlen("/.config/nnn/bookmarks") + 1;
 
        cfgpath = (char *)malloc(len);
        plgpath = (char *)malloc(len);
@@ -7988,7 +7988,7 @@ static bool setup_config(void)
 
        if (xdg) {
                xstrsncpy(cfgpath, xdgcfg, len);
-               r = len - 13; /* subtract length of "/nnn/sessions" */
+               r = len - xstrlen("/nnn/bookmarks");
        } else {
                r = xstrsncpy(cfgpath, home, len);