From 47d349f78b7cb2ff190516fb0a99cc39e2855c08 Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Thu, 14 Aug 2014 06:43:37 +0200 Subject: [PATCH] Simplified file extension filtering code Don't create any temporary strings, just use the d_name field and strcasecmp. --- src/filelister.cpp | 33 +++++++++------------------------ 1 file changed, 9 insertions(+), 24 deletions(-) diff --git a/src/filelister.cpp b/src/filelister.cpp index baa275e..bcf3987 100644 --- a/src/filelister.cpp +++ b/src/filelister.cpp @@ -124,33 +124,18 @@ void FileLister::browse(const string& path, bool clean) continue; } - string file = dptr->d_name; - for (vector::iterator it = filter.begin(); it != filter.end(); ++it) { - if (file.find('.') == string::npos) { - if (!it->empty()) - continue; + // Determine file extension. + const char *ext = strrchr(dptr->d_name, '.'); + if (ext) ext++; else ext = ""; - fileSet.insert(file); + for (auto& filterExt : filter) { + // Note: strcasecmp can't compare multi-byte UTF-8 characters, + // but the filtered file extensions don't contain any of + // those. + if (strcasecmp(ext, filterExt.c_str()) == 0) { + fileSet.insert(string(dptr->d_name)); break; } - - if (it->length() < file.length()) { - if (file[file.length() - it->length() - 1] != '.') - continue; - - string file_lowercase = - file.substr(file.length() - it->length()); - - /* XXX: This won't accept UTF-8 codes. - * Thanksfully file extensions shouldn't contain any. */ - transform(file_lowercase.begin(), file_lowercase.end(), - file_lowercase.begin(), ::tolower); - - if (file_lowercase.compare(0, it->length(), *it) == 0) { - fileSet.insert(file); - break; - } - } } } }