From 8a058d1f9a335a8422653b81b8dced77f5e90154 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Thu, 13 Aug 2015 18:22:29 +0000 Subject: [PATCH] Speeding up the finding of the longest filename in the current folder. git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@5356 35c25a1d-7b9e-4130-9fde-d3aeb78583b8 --- ChangeLog | 3 +++ src/browser.c | 31 +++++++++++++------------------ 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/ChangeLog b/ChangeLog index 66b55d04..f07db6b2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,9 @@ 2015-08-13 Benno Schulenberg * src/search.c (do_find_bracket): Remove mistaken comparison between pointer and literal character. Found with cppcheck. + * src/browser.c (browser_init): Speed up the finding of the longest + filename: don't limit the value to that of COLS all the time, as this + is done later anyway, and don't bother skipping the dot entry. 2015-08-12 Benno Schulenberg * src/chars.c: UTF-8 is a stateless encoding, so there is no need to diff --git a/src/browser.c b/src/browser.c index be8a404d..d5325f84 100644 --- a/src/browser.c +++ b/src/browser.c @@ -436,29 +436,30 @@ void browser_init(const char *path, DIR *dir) assert(path != NULL && path[strlen(path) - 1] == '/' && dir != NULL); - /* Set longest to zero, just before we initialize it. */ longest = 0; + /* Find the length of the longest filename in the current folder. */ while ((nextdir = readdir(dir)) != NULL) { - size_t d_len; + size_t name_len = strlenpt(nextdir->d_name); - /* Don't show the "." entry. */ - if (strcmp(nextdir->d_name, ".") == 0) - continue; - - d_len = strlenpt(nextdir->d_name); - if (d_len > longest) - longest = (d_len > COLS) ? COLS : d_len; + if (name_len > longest) + longest = name_len; i++; } - rewinddir(dir); - - /* Put 10 columns' worth of blank space between columns of filenames + /* Put 10 characters' worth of blank space between columns of filenames * in the list whenever possible, as Pico does. */ longest += 10; + /* Make sure longest is between 15 and COLS. */ + if (longest < 15) + longest = 15; + if (longest > COLS) + longest = COLS; + + rewinddir(dir); + if (filelist != NULL) free_chararray(filelist, filelist_len); @@ -486,12 +487,6 @@ void browser_init(const char *path, DIR *dir) closedir(dir); - /* Make sure longest is between 15 and COLS. */ - if (longest < 15) - longest = 15; - if (longest > COLS) - longest = COLS; - /* Set width to zero, just before we initialize it. */ width = 0; -- 2.39.5