Skip to content

Commit 2c617b1

Browse files
committed
RegEx support in TextDocument search. Added RegEx support to ecode's Find & Replace.
1 parent 7fe419a commit 2c617b1

File tree

24 files changed

+256
-68
lines changed

24 files changed

+256
-68
lines changed

include/eepp/system.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <eepp/system/pak.hpp>
2929
#include <eepp/system/process.hpp>
3030
#include <eepp/system/rc4.hpp>
31+
#include <eepp/system/regex.hpp>
3132
#include <eepp/system/resourceloader.hpp>
3233
#include <eepp/system/resourcemanager.hpp>
3334
#include <eepp/system/scopedbuffer.hpp>

include/eepp/system/luapattern.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class EE_API LuaPattern : public PatternMatcher {
2424
LuaPattern( const std::string_view& pattern );
2525

2626
virtual bool matches( const char* stringSearch, int stringStartOffset,
27-
LuaPattern::Range* matchList, size_t stringLength ) const;
27+
PatternMatcher::Range* matchList, size_t stringLength ) const;
2828

2929
virtual bool matches( const std::string& str, PatternMatcher::Range* matchList = nullptr,
3030
int stringStartOffset = 0 ) const;

include/eepp/system/regex.hpp

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,35 @@
11
#ifndef EE_SYSTEM_REGEX
22
#define EE_SYSTEM_REGEX
33

4+
#include <eepp/core/containers.hpp>
45
#include <eepp/system/patternmatcher.hpp>
6+
#include <eepp/system/singleton.hpp>
57

68
namespace EE { namespace System {
79

10+
class EE_API RegExCache {
11+
SINGLETON_DECLARE_HEADERS( RegExCache )
12+
public:
13+
~RegExCache();
14+
15+
bool isEnabled() const { return mEnabled; }
16+
17+
void setEnabled( bool enabled );
18+
19+
void insert( std::string_view, void* cache );
20+
21+
void* find( const std::string_view& );
22+
23+
void clear();
24+
25+
protected:
26+
bool mEnabled{ true };
27+
UnorderedMap<String::HashType, void*> mCache;
28+
};
29+
830
class EE_API RegEx : public PatternMatcher {
931
public:
10-
RegEx( const std::string_view& pattern );
32+
RegEx( const std::string_view& pattern, bool useCache = true );
1133

1234
virtual ~RegEx();
1335

@@ -29,16 +51,11 @@ class EE_API RegEx : public PatternMatcher {
2951
void* mCompiledPattern;
3052
int mCaptureCount;
3153
bool mValid{ false };
32-
};
54+
bool mCached{ false };
3355

34-
class EE_API RegExStorage : public RegEx {
35-
public:
36-
RegExStorage( const std::string& pattern );
37-
38-
explicit RegExStorage( std::string&& pattern );
56+
RegEx( const std::string_view& pattern, bool useCache, bool init );
3957

40-
protected:
41-
std::string mPatternStorage;
58+
void init( const std::string_view& pattern, bool useCache );
4259
};
4360

4461
}} // namespace EE::System

include/eepp/ui/doc/textdocument.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class EE_API TextDocument {
4040

4141
enum class IndentType { IndentSpaces, IndentTabs };
4242

43-
enum class FindReplaceType { Normal, LuaPattern };
43+
enum class FindReplaceType { Normal, LuaPattern, RegEx };
4444

4545
enum class LoadStatus { Loaded, Interrupted, Failed };
4646

projects/linux/ee.config

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@
1515
#define STB_IMAGE_IMPLEMENTATION
1616
#define ECODE_USE_BACKWARD
1717
#define EE_TEXT_SHAPER_ENABLED
18+
#define PCRE2_STATIC
19+
#define PCRE2_CODE_UNIT_WIDTH 8

projects/linux/ee.files

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,7 @@
831831
../../src/eepp/system/pack.cpp
832832
../../src/eepp/system/packmanager.cpp
833833
../../src/eepp/system/pak.cpp
834+
../../src/eepp/system/patternmatcher.cpp
834835
../../src/eepp/system/platform/platformimpl.hpp
835836
../../src/eepp/system/platform/posix/clockimpl.cpp
836837
../../src/eepp/system/platform/posix/clockimpl.hpp
@@ -854,6 +855,7 @@
854855
../../src/eepp/system/platform/win/threadlocalimpl.hpp
855856
../../src/eepp/system/process.cpp
856857
../../src/eepp/system/rc4.cpp
858+
../../src/eepp/system/regex.cpp
857859
../../src/eepp/system/resourceloader.cpp
858860
../../src/eepp/system/sys.cpp
859861
../../src/eepp/system/thread.cpp
@@ -1445,6 +1447,7 @@
14451447
../../src/tests/test_everything/test.hpp
14461448
../../src/tests/ui_perf_test/ui_perf_test.cpp
14471449
../../src/tests/unit_tests/main.cpp
1450+
../../src/tests/unit_tests/regex.cpp
14481451
../../src/tests/unit_tests/textformat.cpp
14491452
../../src/tests/unit_tests/utest.h
14501453
../../src/thirdparty/SOIL2/src/SOIL2/etc1_utils.c

projects/linux/ee.includes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@
1414
../../src/modules/physics/src/
1515
../../src/tools/ecode
1616
../../src/thirdparty/mojoAL/
17+
../../src/thirdparty/pcre2/src
1718
/usr/include/freetype2/

src/eepp/system/luapattern.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ using namespace std::literals;
66

77
namespace EE { namespace System {
88

9-
const int MAX_DEFAULT_MATCHES = 12;
9+
#define MAX_DEFAULT_MATCHES 12
1010
static bool sFailHandlerInitialized = false;
1111

1212
static void failHandler( const char* msg ) {

src/eepp/system/regex.cpp

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,61 @@
33

44
namespace EE { namespace System {
55

6-
RegEx::RegEx( const std::string_view& pattern ) :
6+
SINGLETON_DECLARE_IMPLEMENTATION( RegExCache )
7+
8+
RegExCache::~RegExCache() {
9+
clear();
10+
}
11+
12+
void RegExCache::insert( std::string_view key, void* cache ) {
13+
mCache.insert( { String::hash( key ), cache } );
14+
}
15+
16+
void* RegExCache::find( const std::string_view& key ) {
17+
auto it = mCache.find( String::hash( key ) );
18+
return ( it != mCache.end() ) ? it->second : nullptr;
19+
}
20+
21+
void RegExCache::clear() {
22+
for ( auto& cache : mCache )
23+
pcre2_code_free( reinterpret_cast<pcre2_code*>( cache.second ) );
24+
mCache.clear();
25+
}
26+
27+
RegEx::RegEx( const std::string_view& pattern, bool useCache, bool initRegEx ) :
728
PatternMatcher( PatternType::PCRE ),
829
mPattern( pattern ),
930
mMatchNum( 0 ),
1031
mCompiledPattern( nullptr ),
1132
mCaptureCount( 0 ),
1233
mValid( true ) {
34+
if ( initRegEx )
35+
init( pattern, useCache );
36+
}
37+
38+
RegEx::RegEx( const std::string_view& pattern, bool useCache ) : RegEx( pattern, useCache, true ) {}
39+
40+
RegEx::~RegEx() {
41+
if ( !mCached && mCompiledPattern != nullptr ) {
42+
pcre2_code_free( reinterpret_cast<pcre2_code*>( mCompiledPattern ) );
43+
}
44+
}
45+
46+
void RegEx::init( const std::string_view& pattern, bool useCache ) {
1347
int errornumber;
1448
PCRE2_SIZE erroroffset;
1549
PCRE2_SPTR pattern_sptr = reinterpret_cast<PCRE2_SPTR>( pattern.data() );
1650

51+
if ( useCache && RegExCache::instance()->isEnabled() &&
52+
( mCompiledPattern = RegExCache::instance()->find( pattern ) ) ) {
53+
mValid = true;
54+
mCached = true;
55+
return;
56+
}
57+
1758
mCompiledPattern = pcre2_compile( pattern_sptr, // the pattern
1859
pattern.size(), // the length of the pattern
19-
0, // default options
60+
PCRE2_UTF, // default options
2061
&errornumber, // for error number
2162
&erroroffset, // for error offset
2263
NULL // use default compile context
@@ -31,18 +72,17 @@ RegEx::RegEx( const std::string_view& pattern ) :
3172
// reinterpret_cast<const char*>( buffer ) );
3273
}
3374

75+
pcre2_jit_compile( reinterpret_cast<pcre2_code*>( mCompiledPattern ), PCRE2_JIT_COMPLETE );
76+
3477
int rc = pcre2_pattern_info( reinterpret_cast<pcre2_code*>( mCompiledPattern ),
3578
PCRE2_INFO_CAPTURECOUNT, &mCaptureCount );
3679
if ( rc != 0 ) {
3780
// throw std::runtime_error( "PCRE2 pattern info failed with error code " +
3881
// std::to_string( rc ) );
3982
mValid = false;
40-
}
41-
}
42-
43-
RegEx::~RegEx() {
44-
if ( mCompiledPattern != nullptr ) {
45-
pcre2_code_free( reinterpret_cast<pcre2_code*>( mCompiledPattern ) );
83+
} else if ( useCache && RegExCache::instance()->isEnabled() ) {
84+
RegExCache::instance()->insert( pattern, mCompiledPattern );
85+
mCached = true;
4686
}
4787
}
4888

@@ -78,11 +118,16 @@ bool RegEx::matches( const char* stringSearch, int stringStartOffset,
78118
for ( size_t i = 0; i < static_cast<size_t>( rc ); ++i ) {
79119
matchList[i].start = static_cast<int>( ovector[2 * i] );
80120
matchList[i].end = static_cast<int>( ovector[2 * i + 1] );
121+
if ( matchList[i].start >= matchList[i].end ) {
122+
matchList[i].start = matchList[i].end = -1;
123+
mMatchNum--;
124+
break;
125+
}
81126
}
82127
}
83128

84129
pcre2_match_data_free( match_data );
85-
return true;
130+
return mMatchNum > 0;
86131
}
87132

88133
bool RegEx::matches( const std::string& str, PatternMatcher::Range* matchList,
@@ -94,13 +139,4 @@ const size_t& RegEx::getNumMatches() const {
94139
return mMatchNum;
95140
}
96141

97-
RegExStorage::RegExStorage( const std::string& pattern ) : RegEx( "" ), mPatternStorage( pattern ) {
98-
mPattern = std::string_view{ mPatternStorage };
99-
}
100-
101-
RegExStorage::RegExStorage( std::string&& pattern ) :
102-
RegEx( "" ), mPatternStorage( std::move( pattern ) ) {
103-
mPattern = std::string_view{ mPatternStorage };
104-
}
105-
106142
}} // namespace EE::System

src/eepp/ui/css/stylesheetlength.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ std::string StyleSheetLength::unitToString( const StyleSheetLength::Unit& unit )
168168

169169
bool StyleSheetLength::isLength( const std::string& unitStr ) {
170170
LuaPattern ptrn( "(-?%d+[%d%.eE]*)(%w*)" );
171-
LuaPattern::Range matches[4];
171+
PatternMatcher::Range matches[4];
172172
if ( ptrn.matches( unitStr, matches ) ) {
173173
if ( matches[2].isValid() ) {
174174
std::string unit =

0 commit comments

Comments
 (0)