Skip to content

Commit

Permalink
Small refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
SpartanJ committed Feb 17, 2025
1 parent 187b9bd commit 2b0d1ec
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 66 deletions.
12 changes: 12 additions & 0 deletions include/eepp/core/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,18 @@ class EE_API String {

const StringType& getString() const { return mString; }

static void readBySeparator( std::string_view buf,
std::function<void( std::string_view )> onSepChunkRead,
char sep = '\n' );

static void readBySeparator( String::View buf,
std::function<void( String::View )> 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 );
Expand Down
46 changes: 46 additions & 0 deletions src/eepp/core/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<void( std::string_view )> 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<void( String::View )> 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
32 changes: 16 additions & 16 deletions src/tools/ecode/plugins/git/git.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "git.hpp"
#include "../../stringhelper.hpp"
#include <eepp/system/clock.hpp>
#include <eepp/system/filesystem.hpp>
#include <eepp/system/lock.hpp>
Expand Down Expand Up @@ -446,9 +445,9 @@ std::vector<Git::Branch> 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 ) );
Expand All @@ -462,11 +461,11 @@ std::vector<Git::Branch> 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(
Expand All @@ -493,13 +492,14 @@ std::vector<std::string> 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;
}

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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;
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/tools/ecode/plugins/linter/linterplugin.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "linterplugin.hpp"
#include "../../stringhelper.hpp"
#include <algorithm>
#include <eepp/graphics/primitives.hpp>
#include <eepp/graphics/text.hpp>
Expand Down Expand Up @@ -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 )
Expand Down
29 changes: 0 additions & 29 deletions src/tools/ecode/stringhelper.cpp

This file was deleted.

19 changes: 0 additions & 19 deletions src/tools/ecode/stringhelper.hpp

This file was deleted.

0 comments on commit 2b0d1ec

Please sign in to comment.