diff --git a/include/eepp/system/fileinfo.hpp b/include/eepp/system/fileinfo.hpp index 56d7a514c..8b87eaddd 100644 --- a/include/eepp/system/fileinfo.hpp +++ b/include/eepp/system/fileinfo.hpp @@ -58,7 +58,7 @@ class EE_API FileInfo { const std::string& getFilepath() const; - std::string getFileName() const; + const std::string& getFileName() const; std::string getDirectoryPath() const; diff --git a/src/eepp/system/fileinfo.cpp b/src/eepp/system/fileinfo.cpp index 3b3393f24..c034f0ede 100644 --- a/src/eepp/system/fileinfo.cpp +++ b/src/eepp/system/fileinfo.cpp @@ -160,7 +160,7 @@ const std::string& FileInfo::getFilepath() const { return mFilepath; } -std::string FileInfo::getFileName() const { +const std::string& FileInfo::getFileName() const { return mFileName; } diff --git a/src/eepp/system/filesystem.cpp b/src/eepp/system/filesystem.cpp index 2074dfec1..1411ee68d 100644 --- a/src/eepp/system/filesystem.cpp +++ b/src/eepp/system/filesystem.cpp @@ -431,31 +431,18 @@ std::vector FileSystem::filesGetInPath( const String& path, const bool& String fpath( path ); if ( fpath[fpath.size() - 1] != '/' && fpath[fpath.size() - 1] != '\\' ) fpath += getOSSlash(); - std::vector folders; - std::vector file; - for ( size_t i = 0; i < files.size(); i++ ) { - if ( FileSystem::isDirectory( fpath + files[i] ) ) { - folders.push_back( files[i] ); - } else { - file.push_back( files[i] ); - } - } - files.clear(); - for ( auto& folder : folders ) - files.push_back( folder ); - for ( auto& f : file ) - files.push_back( f ); + std::stable_partition(files.begin(), files.end(), [&fpath](const String& file) { + return FileSystem::isDirectory( fpath + file ); + }); } if ( ignoreHidden ) { String fpath( path ); if ( fpath[fpath.size() - 1] != '/' && fpath[fpath.size() - 1] != '\\' ) fpath += getOSSlash(); - std::vector filtered; - for ( size_t i = 0; i < files.size(); i++ ) - if ( !FileSystem::fileIsHidden( fpath + files[i] ) ) - filtered.push_back( files[i] ); - return filtered; + files.erase(std::remove_if(files.begin(), files.end(), [&fpath](const String& file) { + return FileSystem::fileIsHidden(fpath + file); + }), files.end()); } return files; @@ -468,6 +455,7 @@ std::vector FileSystem::filesInfoGetInPath( std::string path, bool lin dirAddSlashAtEnd( path ); std::vector fileInfo; auto files = filesGetInPath( path, sortByName, foldersFirst, ignoreHidden ); + fileInfo.reserve( files.size() ); for ( const auto& file : files ) fileInfo.emplace_back( FileInfo( path + file, linkInfo ) ); return fileInfo; @@ -533,30 +521,17 @@ std::vector FileSystem::filesGetInPath( const std::string& path, if ( foldersFirst ) { std::string fpath( path ); dirAddSlashAtEnd( fpath ); - std::vector folders; - std::vector file; - for ( size_t i = 0; i < files.size(); i++ ) { - if ( FileSystem::isDirectory( fpath + files[i] ) ) { - folders.push_back( files[i] ); - } else { - file.push_back( files[i] ); - } - } - files.clear(); - for ( auto& folder : folders ) - files.push_back( folder ); - for ( auto& f : file ) - files.push_back( f ); + std::stable_partition(files.begin(), files.end(), [&fpath](const std::string& file) { + return FileSystem::isDirectory( fpath + file ); + }); } if ( ignoreHidden ) { std::string fpath( path ); dirAddSlashAtEnd( fpath ); - std::vector filtered; - for ( size_t i = 0; i < files.size(); i++ ) - if ( !FileSystem::fileIsHidden( fpath + files[i] ) ) - filtered.push_back( files[i] ); - return filtered; + files.erase(std::remove_if(files.begin(), files.end(), [&fpath](const std::string& file) { + return FileSystem::fileIsHidden(fpath + file); + }), files.end()); } return files; diff --git a/src/eepp/ui/models/filesystemmodel.cpp b/src/eepp/ui/models/filesystemmodel.cpp index e6100527e..fcdf75e9e 100644 --- a/src/eepp/ui/models/filesystemmodel.cpp +++ b/src/eepp/ui/models/filesystemmodel.cpp @@ -171,7 +171,7 @@ void FileSystemModel::Node::refresh( const FileSystemModel& model ) { std::vector newChildren; Node* node = nullptr; - for ( auto file : files ) { + for ( auto& file : files ) { node = childWithPathExists( file.getFilepath() ); if ( !isAcceptedExtension( displayCfg.acceptedExtensions, file ) ) @@ -197,7 +197,7 @@ void FileSystemModel::Node::refresh( const FileSystemModel& model ) { for ( Node* oldNode : oldFiles ) eeDelete( oldNode ); - mChildren = newChildren; + mChildren = std::move( newChildren ); } void FileSystemModel::Node::cleanChildren() { @@ -209,7 +209,6 @@ void FileSystemModel::Node::cleanChildren() { void FileSystemModel::Node::traverseIfNeeded( const FileSystemModel& model ) { if ( !mInfo.isDirectory() || mHasTraversed ) return; - mHasTraversed = true; cleanChildren(); const auto& displayCfg = model.getDisplayConfig(); @@ -219,7 +218,7 @@ void FileSystemModel::Node::traverseIfNeeded( const FileSystemModel& model ) { const auto& patterns = displayCfg.acceptedExtensions; bool accepted; - for ( auto file : files ) { + for ( auto& file : files ) { if ( ( model.getMode() == Mode::DirectoriesOnly && ( file.isDirectory() || file.linksToDirectory() ) ) || model.getMode() == Mode::FilesAndDirectories ) { @@ -230,9 +229,11 @@ void FileSystemModel::Node::traverseIfNeeded( const FileSystemModel& model ) { mChildren.emplace_back( eeNew( Node, ( std::move( file ), this ) ) ); } else { accepted = false; - if ( patterns.size() ) { - for ( size_t z = 0; z < patterns.size(); z++ ) { - if ( patterns[z] == FileSystem::fileExtension( file.getFilepath() ) ) { + size_t psize = patterns.size(); + if ( psize ) { + auto ext( FileSystem::fileExtension( file.getFilepath() ) ); + for ( size_t z = 0; z < psize; z++ ) { + if ( patterns[z] == ext ) { accepted = true; break; } @@ -251,6 +252,7 @@ void FileSystemModel::Node::traverseIfNeeded( const FileSystemModel& model ) { } } } + mHasTraversed = true; } void FileSystemModel::Node::refreshIfNeeded( const FileSystemModel& model ) { @@ -543,6 +545,7 @@ void FileSystemModel::setPreviouslySelectedIndex( const ModelIndex& previouslySe size_t FileSystemModel::getFileIndex( Node* parent, const FileInfo& file ) { std::vector files; + files.reserve( parent->mChildren.size() + 1 ); for ( Node* nodeFile : parent->mChildren ) { files.emplace_back( nodeFile->info() ); @@ -553,26 +556,14 @@ size_t FileSystemModel::getFileIndex( Node* parent, const FileInfo& file ) { files.emplace_back( file ); - std::sort( files.begin(), files.end(), []( FileInfo a, FileInfo b ) { + std::sort( files.begin(), files.end(), []( const FileInfo& a, const FileInfo& b ) { return std::strncmp( a.getFileName().c_str(), b.getFileName().c_str(), a.getFileName().size() ) < 0; } ); if ( getDisplayConfig().foldersFirst ) { - std::vector folders; - std::vector file; - for ( size_t i = 0; i < files.size(); i++ ) { - if ( files[i].isDirectory() ) { - folders.push_back( files[i] ); - } else { - file.push_back( files[i] ); - } - } - files.clear(); - for ( auto& folder : folders ) - files.push_back( folder ); - for ( auto& f : file ) - files.push_back( f ); + std::stable_partition( files.begin(), files.end(), + []( const FileInfo& info ) { return info.isDirectory(); } ); } size_t pos = parent->childCount();