Files
eepp/include/eepp/system/patternmatcher.hpp
T
Martín Lucas Golini 23cf9084ce perf: reduce allocations across UI and editor hot paths
- reuse formatting, document text, property resolution, and layout storage
  - use inline containers for interpolations, callbacks, wrapping, and matcher state
  - optimize single-line insertion by mutating document lines without splitting
  - iterate PATH entries directly in Sys::which and reuse candidate buffers
  - avoid temporary clocks and string copies in frequent ecode plugin updates
  - retain documents during autocomplete cache jobs and prevent duplicate scheduling
  - fix PatternMatcher copy assignment and MSVC move-only container compilation
  - expand coverage for matching, text insertion, reusable strings, and Sys::which
2026-08-09 21:38:17 -03:00

151 lines
3.9 KiB
C++

#ifndef EE_SYSTEM_PATTERNMATCHER_HPP
#define EE_SYSTEM_PATTERNMATCHER_HPP
#include <array>
#include <eepp/config.hpp>
#include <string>
#include <string_view>
namespace EE { namespace System {
// Inspired in rx-cpp (https://github.com/stevedonovan/rx-cpp/).
class EE_API PatternMatcher {
public:
enum class PatternType { LuaPattern, PCRE, Parser };
struct EE_API Range {
int start{ -1 };
int end{ -1 };
int length() const { return end - start; }
bool isValid() { return -1 != start && -1 != end; }
};
class EE_API State {
public:
State( PatternMatcher* pattern, bool ownPattern );
State( const State& other );
State& operator=( const State& other );
~State();
void swap( State& other ) noexcept;
bool range( int index, int& start, int& end ) const;
bool matches( const char* string, size_t length );
PatternMatcher* mPattern;
std::array<Range, 10> mRanges;
bool mOwnPattern;
};
class EE_API Match {
public:
~Match();
Match( PatternMatcher& r, const char* string, bool ownPattern = false );
Match( PatternMatcher& r, const std::string& string, bool ownPattern = false );
Match( const PatternMatcher::Match& other );
Match& operator=( const Match& other );
bool matches();
bool subst( std::string& res );
void next();
std::string group( int idx = -1 ) const;
std::string_view groupView( int idx = -1 ) const;
bool range( int idx, int& start, int& end ) const;
std::string operator[]( int index ) const;
class iterator {
public:
iterator( Match* pm ) : mMatcher( pm ) {
if ( mMatcher != nullptr && !mMatcher->matches() )
mMatcher = nullptr;
}
bool operator!=( const iterator& other ) { return mMatcher != other.mMatcher; }
bool operator==( const iterator& other ) { return mMatcher == other.mMatcher; }
const Match& operator*() const { return *mMatcher; }
iterator& operator++() {
mMatcher->next();
if ( !mMatcher->matches() )
mMatcher = nullptr;
return *this;
}
protected:
Match* mMatcher;
};
iterator begin() { return iterator( this ); }
iterator end() { return iterator( nullptr ); }
protected:
PatternMatcher::State mState;
const char* mString{ nullptr };
size_t mLength{ 0 };
};
PatternMatcher( PatternType type ) : mType( type ) {}
virtual ~PatternMatcher() {}
PatternType getType() const { return mType; }
PatternMatcher::Match gmatch( const char* s ) &;
PatternMatcher::Match gmatch( const char* s ) &&;
PatternMatcher::Match gmatch( const std::string& string ) &&;
PatternMatcher::Match gmatch( const std::string& string ) &;
bool range( int indexGet, int& startMatch, int& endMatch,
const PatternMatcher::Range* returnedMatched ) const;
bool find( const std::string& s, int& startMatch, int& endMatch, int offset = 0,
int returnedMatchIndex = 0 ) const;
bool find( const char* stringSearch, int& startMatch, int& endMatch, int stringStartOffset = 0,
int stringLength = 0, int returnMatchIndex = 0 ) const;
bool find( const std::string& s, int& startMatch, int& endMatch, int offset,
int returnedMatchIndex, PatternMatcher::Range* matchesBuffer ) const;
bool find( const char* stringSearch, int& startMatch, int& endMatch, int stringStartOffset,
int stringLength, int returnMatchIndex, PatternMatcher::Range* matchesBuffer ) const;
std::string gsub( const char* text, const char* replace );
std::string gsub( const std::string& text, const std::string& replace );
virtual const std::string_view& getPattern() const = 0;
virtual bool matches( const char* stringSearch, int stringStartOffset,
PatternMatcher::Range* matchList, size_t stringLength ) const = 0;
virtual bool matches( const std::string& str, PatternMatcher::Range* matchList = nullptr,
int stringStartOffset = 0 ) const = 0;
virtual const size_t& getNumMatches() const = 0;
virtual bool isValid() const = 0;
protected:
PatternType mType;
};
}} // namespace EE::System
#endif