From 2b0d1ec0ff8a2890eff74a5a79f72432a8646de0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Lucas=20Golini?= Date: Mon, 17 Feb 2025 00:45:24 -0300 Subject: [PATCH] Small refactor. --- include/eepp/core/string.hpp | 12 +++++ src/eepp/core/string.cpp | 46 +++++++++++++++++++ src/tools/ecode/plugins/git/git.cpp | 32 ++++++------- .../ecode/plugins/linter/linterplugin.cpp | 3 +- src/tools/ecode/stringhelper.cpp | 29 ------------ src/tools/ecode/stringhelper.hpp | 19 -------- 6 files changed, 75 insertions(+), 66 deletions(-) delete mode 100644 src/tools/ecode/stringhelper.cpp delete mode 100644 src/tools/ecode/stringhelper.hpp diff --git a/include/eepp/core/string.hpp b/include/eepp/core/string.hpp index feb1e36e7..65221aaab 100644 --- a/include/eepp/core/string.hpp +++ b/include/eepp/core/string.hpp @@ -990,6 +990,18 @@ class EE_API String { const StringType& getString() const { return mString; } + static void readBySeparator( std::string_view buf, + std::function onSepChunkRead, + char sep = '\n' ); + + static void readBySeparator( String::View buf, + std::function onSepChunkRead, + String::StringBaseType sep = L'\n' ); + + static size_t countLines( std::string_view text ); + + static size_t countLines( String::View text ); + private: friend EE_API bool operator==( const String& left, const String& right ); friend EE_API bool operator<( const String& left, const String& right ); diff --git a/src/eepp/core/string.cpp b/src/eepp/core/string.cpp index dff88fee1..997d175d4 100644 --- a/src/eepp/core/string.cpp +++ b/src/eepp/core/string.cpp @@ -2123,4 +2123,50 @@ size_t String::toUtf32( std::string_view utf8str, String::StringBaseType* buffer return pos; } +void String::readBySeparator( std::string_view buf, + std::function onSepChunkRead, char sep ) { + auto lastNL = 0; + auto nextNL = buf.find_first_of( sep ); + while ( nextNL != std::string_view::npos ) { + onSepChunkRead( buf.substr( lastNL, nextNL - lastNL ) ); + lastNL = nextNL + 1; + nextNL = buf.find_first_of( sep, nextNL + 1 ); + } +} + +size_t String::countLines( std::string_view text ) { + const char* startPtr = text.data(); + const char* endPtr = text.data() + text.size(); + size_t count = 0; + if ( startPtr != endPtr ) { + count = 1 + *startPtr == '\n' ? 1 : 0; + while ( ++startPtr && startPtr != endPtr ) + count += ( '\n' == *startPtr ) ? 1 : 0; + } + return count; +} + +void String::readBySeparator( String::View buf, std::function onSepChunkRead, + String::StringBaseType sep ) { + auto lastNL = 0; + auto nextNL = buf.find_first_of( sep ); + while ( nextNL != String::View::npos ) { + onSepChunkRead( buf.substr( lastNL, nextNL - lastNL ) ); + lastNL = nextNL + 1; + nextNL = buf.find_first_of( sep, nextNL + 1 ); + } +} + +size_t String::countLines( String::View text ) { + const String::StringBaseType* startPtr = text.data(); + const String::StringBaseType* endPtr = text.data() + text.size(); + size_t count = 0; + if ( startPtr != endPtr ) { + count = 1 + *startPtr == '\n' ? 1 : 0; + while ( ++startPtr && startPtr != endPtr ) + count += ( '\n' == *startPtr ) ? 1 : 0; + } + return count; +} + } // namespace EE diff --git a/src/tools/ecode/plugins/git/git.cpp b/src/tools/ecode/plugins/git/git.cpp index 686b0f885..8c861925e 100644 --- a/src/tools/ecode/plugins/git/git.cpp +++ b/src/tools/ecode/plugins/git/git.cpp @@ -1,5 +1,4 @@ #include "git.hpp" -#include "../../stringhelper.hpp" #include #include #include @@ -446,9 +445,9 @@ std::vector Git::getAllBranchesAndTags( RefType ref, std::string_vi std::string buf; if ( EXIT_SUCCESS == git( args, projectDir, buf ) ) { - branches.reserve( StringHelper::countLines( buf ) ); + branches.reserve( String::countLines( buf ) ); - StringHelper::readBySeparator( buf, [&]( const std::string_view& line ) { + String::readBySeparator( std::string_view{ buf }, [&]( std::string_view line ) { auto branch = String::trim( String::trim( line, '\'' ), '\t' ); if ( ( ref & Head ) && String::startsWith( branch, "refs/heads/" ) ) { branches.emplace_back( parseLocalBranch( branch ) ); @@ -462,11 +461,11 @@ std::vector Git::getAllBranchesAndTags( RefType ref, std::string_vi if ( ( ref & RefType::Stash ) && EXIT_SUCCESS == git( "stash list --date=format:\"%Y-%m-%d %H:%M\"", projectDir, buf ) ) { - branches.reserve( branches.size() + StringHelper::countLines( buf ) ); + branches.reserve( branches.size() + String::countLines( buf ) ); std::string ptrn( "stash@{(.*)}:%s(.*)" ); LuaPattern pattern( ptrn ); Uint64 id = 0; - StringHelper::readBySeparator( buf, [&]( const std::string_view& line ) { + String::readBySeparator( std::string_view{ buf }, [&]( std::string_view line ) { PatternMatcher::Range matches[3]; if ( pattern.matches( line.data(), 0, matches, line.size() ) ) { std::string date( @@ -493,13 +492,14 @@ std::vector Git::fetchSubModules( const std::string& projectDir ) { FileSystem::fileGet( ( !projectDir.empty() ? projectDir : mProjectPath ) + ".gitmodules", buf ); std::string ptrn( "^%s*path%s*=%s*(.+)" ); LuaPattern pattern( ptrn ); - StringHelper::readBySeparator( buf, [&pattern, &submodules]( const std::string_view& line ) { - PatternMatcher::Range matches[2]; - if ( pattern.matches( line.data(), 0, matches, line.size() ) ) { - submodules.emplace_back( String::trim( - line.substr( matches[1].start, matches[1].end - matches[1].start ), '\n' ) ); - } - } ); + String::readBySeparator( + std::string_view{ buf }, [&pattern, &submodules]( std::string_view line ) { + PatternMatcher::Range matches[2]; + if ( pattern.matches( line.data(), 0, matches, line.size() ) ) { + submodules.emplace_back( String::trim( + line.substr( matches[1].start, matches[1].end - matches[1].start ), '\n' ) ); + } + } ); return submodules; } @@ -578,12 +578,12 @@ Git::Status Git::status( bool recurseSubmodules, const std::string& projectDir ) std::string subModulePath = ""; std::string ptrn = "^([mMARTUD?%s][mMARTUD?%s])%s(.*)"; LuaPattern pattern( ptrn ); - size_t changesCount = StringHelper::countLines( buf ); + size_t changesCount = String::countLines( buf ); if ( changesCount > 1000 ) return; - StringHelper::readBySeparator( buf, [&]( const std::string_view& line ) { + String::readBySeparator( std::string_view{ buf }, [&]( std::string_view line ) { PatternMatcher::Range matches[3]; if ( subModulePattern.matches( line.data(), 0, matches, line.size() ) ) { subModulePath = String::trim( @@ -659,7 +659,7 @@ Git::Status Git::status( bool recurseSubmodules, const std::string& projectDir ) std::string ptrn( "(%d+)%s+(%d+)%s+(.+)" ); LuaPattern pattern( ptrn ); std::string subModulePath = ""; - StringHelper::readBySeparator( buf, [&]( const std::string_view& line ) { + String::readBySeparator( std::string_view{ buf }, [&]( std::string_view line ) { PatternMatcher::Range matches[4]; if ( subModulePattern.matches( line.data(), 0, matches, line.size() ) ) { subModulePath = String::trim( @@ -734,7 +734,7 @@ Git::Status Git::status( bool recurseSubmodules, const std::string& projectDir ) std::string fileText; FileSystem::fileGet( ( projectDir.empty() ? mProjectPath : projectDir ) + val.file, fileText ); - val.inserts = StringHelper::countLines( fileText ); + val.inserts = String::countLines( fileText ); s.totalInserts += val.inserts; } } diff --git a/src/tools/ecode/plugins/linter/linterplugin.cpp b/src/tools/ecode/plugins/linter/linterplugin.cpp index a762c9172..108087c37 100644 --- a/src/tools/ecode/plugins/linter/linterplugin.cpp +++ b/src/tools/ecode/plugins/linter/linterplugin.cpp @@ -1,5 +1,4 @@ #include "linterplugin.hpp" -#include "../../stringhelper.hpp" #include #include #include @@ -1425,7 +1424,7 @@ void LinterPlugin::registerNativeLinters() { std::string file; FileSystem::fileGet( path, file ); std::string_view filesv{ file }; - Int64 line = StringHelper::countLines( filesv.substr( 0, result.offset ) ); + Int64 line = String::countLines( filesv.substr( 0, result.offset ) ); Int64 offset = 0; auto lastNL = filesv.substr( 0, result.offset ).find_last_of( '\n' ); if ( lastNL != std::string_view::npos ) diff --git a/src/tools/ecode/stringhelper.cpp b/src/tools/ecode/stringhelper.cpp deleted file mode 100644 index a329016e7..000000000 --- a/src/tools/ecode/stringhelper.cpp +++ /dev/null @@ -1,29 +0,0 @@ -#include "stringhelper.hpp" - -namespace ecode { - -void StringHelper::readBySeparator( const std::string_view& buf, - std::function onSepChunkRead, - char sep ) { - auto lastNL = 0; - auto nextNL = buf.find_first_of( sep ); - while ( nextNL != std::string_view::npos ) { - onSepChunkRead( buf.substr( lastNL, nextNL - lastNL ) ); - lastNL = nextNL + 1; - nextNL = buf.find_first_of( sep, nextNL + 1 ); - } -} - -size_t StringHelper::countLines( const std::string_view& text ) { - const char* startPtr = text.data(); - const char* endPtr = text.data() + text.size(); - size_t count = 0; - if ( startPtr != endPtr ) { - count = 1 + *startPtr == '\n' ? 1 : 0; - while ( ++startPtr && startPtr != endPtr ) - count += ( '\n' == *startPtr ) ? 1 : 0; - } - return count; -} - -} // namespace ecode diff --git a/src/tools/ecode/stringhelper.hpp b/src/tools/ecode/stringhelper.hpp deleted file mode 100644 index 01d3c991f..000000000 --- a/src/tools/ecode/stringhelper.hpp +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef ECODE_STRINGHELPER -#define ECODE_STRINGHELPER - -#include -#include - -namespace ecode { - -struct StringHelper { - static void readBySeparator( const std::string_view& buf, - std::function onSepChunkRead, - char sep = '\n' ); - - static size_t countLines( const std::string_view& text ); -}; - -} // namespace ecode - -#endif