mirror of
https://github.com/SpartanJ/eepp.git
synced 2026-10-19 03:53:57 +03:00
Small refactor.
This commit is contained in:
@@ -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 );
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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>
|
||||
@@ -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 ) );
|
||||
@@ -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(
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
#include "linterplugin.hpp"
|
||||
#include "../../stringhelper.hpp"
|
||||
#include <algorithm>
|
||||
#include <eepp/graphics/primitives.hpp>
|
||||
#include <eepp/graphics/text.hpp>
|
||||
@@ -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 )
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
#include "stringhelper.hpp"
|
||||
|
||||
namespace ecode {
|
||||
|
||||
void StringHelper::readBySeparator( const 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 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
|
||||
@@ -1,19 +0,0 @@
|
||||
#ifndef ECODE_STRINGHELPER
|
||||
#define ECODE_STRINGHELPER
|
||||
|
||||
#include <functional>
|
||||
#include <string_view>
|
||||
|
||||
namespace ecode {
|
||||
|
||||
struct StringHelper {
|
||||
static void readBySeparator( const std::string_view& buf,
|
||||
std::function<void( std::string_view )> onSepChunkRead,
|
||||
char sep = '\n' );
|
||||
|
||||
static size_t countLines( const std::string_view& text );
|
||||
};
|
||||
|
||||
} // namespace ecode
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user