Skip to content

Commit a8214bb

Browse files
committed
Merge branch 'pcre' into develop
2 parents b1c87af + 69d2db6 commit a8214bb

File tree

121 files changed

+115996
-525
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

121 files changed

+115996
-525
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: 11 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,143 +1,39 @@
11
#ifndef EE_SYSTEM_LUAPATTERNMATCHER_HPP
22
#define EE_SYSTEM_LUAPATTERNMATCHER_HPP
33

4-
#include <eepp/config.hpp>
5-
#include <string>
6-
#include <string_view>
4+
#include <eepp/system/patternmatcher.hpp>
75
#include <vector>
86

97
namespace EE { namespace System {
108

11-
// Adapted from rx-cpp (https://github.com/stevedonovan/rx-cpp/).
12-
// This implementation removes all the regexp related stuffs, only leaves the Lua implementation.
13-
class EE_API LuaPattern {
9+
class EE_API LuaPattern : public PatternMatcher {
1410
public:
1511
static std::string_view getURLPattern();
1612

1713
static std::string_view getURIPattern();
1814

19-
struct EE_API Range {
20-
int start{ -1 };
21-
int end{ -1 };
22-
bool isValid() { return -1 != start && -1 != end; }
23-
};
24-
25-
class EE_API State {
26-
public:
27-
bool range( int index, int& start, int& end );
28-
29-
bool matches( const char* string, size_t length );
30-
31-
protected:
32-
friend class LuaPattern;
33-
LuaPattern* mPattern;
34-
Range* mRanges;
35-
size_t mRefCount;
36-
bool mOwnPattern;
37-
38-
State( LuaPattern* pattern, bool ownPattern );
39-
40-
~State();
41-
};
42-
43-
class EE_API Match {
44-
public:
45-
~Match();
46-
47-
Match( LuaPattern& r, const char* string, bool ownPattern = false );
48-
49-
Match( LuaPattern& r, const std::string& string, bool ownPattern = false );
50-
51-
Match( const LuaPattern::Match& other );
52-
53-
Match& operator=( const Match& other );
54-
55-
bool matches();
56-
57-
bool subst( std::string& res );
58-
59-
void next();
60-
61-
std::string group( int idx = -1 ) const;
62-
63-
std::string_view groupView( int idx = -1 ) const;
64-
65-
bool range( int idx, int& start, int& end ) const;
66-
67-
std::string operator[]( int index ) const;
68-
69-
class iterator {
70-
public:
71-
iterator( Match* pm ) : mMatcher( pm ) {
72-
if ( mMatcher != nullptr && !mMatcher->matches() )
73-
mMatcher = nullptr;
74-
}
75-
bool operator!=( const iterator& other ) { return mMatcher != other.mMatcher; }
76-
bool operator==( const iterator& other ) { return mMatcher == other.mMatcher; }
77-
const Match& operator*() const { return *mMatcher; }
78-
iterator& operator++() {
79-
mMatcher->next();
80-
if ( !mMatcher->matches() )
81-
mMatcher = nullptr;
82-
return *this;
83-
}
84-
85-
protected:
86-
Match* mMatcher;
87-
};
88-
89-
iterator begin() { return iterator( this ); }
90-
91-
iterator end() { return iterator( nullptr ); }
92-
93-
protected:
94-
friend class LuaPattern;
95-
LuaPattern::State* mState{ nullptr };
96-
const char* mString{ nullptr };
97-
size_t mLength{ 0 };
98-
};
99-
10015
static std::string matchesAny( const std::vector<std::string>& stringvec,
10116
const std::string_view& pattern );
10217

10318
static std::string match( const std::string& string, const std::string_view& pattern );
10419

105-
static Range find( const std::string& string, const std::string_view& pattern );
20+
static Range firstMatch( const std::string& string, const std::string_view& pattern );
10621

107-
static bool matches( const std::string& string, const std::string_view& pattern );
22+
static bool hasMatches( const std::string& string, const std::string_view& pattern );
10823

10924
LuaPattern( const std::string_view& pattern );
11025

111-
bool matches( const char* stringSearch, int stringStartOffset, LuaPattern::Range* matchList,
112-
size_t stringLength ) const;
113-
114-
bool matches( const std::string& str, LuaPattern::Range* matchList = nullptr,
115-
int stringStartOffset = 0 ) const;
116-
117-
bool find( const char* stringSearch, int& startMatch, int& endMatch, int stringStartOffset = 0,
118-
int stringLength = 0, int returnMatchIndex = 0 ) const;
119-
120-
bool find( const std::string& s, int& startMatch, int& endMatch, int offset = 0,
121-
int returnedMatchIndex = 0 ) const;
122-
123-
const size_t& getNumMatches() const;
124-
125-
bool range( int indexGet, int& startMatch, int& endMatch,
126-
LuaPattern::Range* returnedMatched ) const;
127-
128-
const std::string_view& getPatern() const { return mPattern; }
129-
130-
LuaPattern::Match gmatch( const char* s ) &;
131-
132-
LuaPattern::Match gmatch( const char* s ) &&;
26+
virtual bool matches( const char* stringSearch, int stringStartOffset,
27+
PatternMatcher::Range* matchList, size_t stringLength ) const;
13328

134-
LuaPattern::Match gmatch( const std::string& string ) &&;
29+
virtual bool matches( const std::string& str, PatternMatcher::Range* matchList = nullptr,
30+
int stringStartOffset = 0 ) const;
13531

136-
LuaPattern::Match gmatch( const std::string& string ) &;
32+
virtual const size_t& getNumMatches() const;
13733

138-
std::string gsub( const char* text, const char* replace );
34+
const std::string_view& getPattern() const { return mPattern; }
13935

140-
std::string gsub( const std::string& text, const std::string& replace );
36+
virtual bool isValid() const { return true; }
14137

14238
protected:
14339
std::string_view mPattern;
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#ifndef EE_SYSTEM_PATTERNMATCHER_HPP
2+
#define EE_SYSTEM_PATTERNMATCHER_HPP
3+
4+
#include <eepp/config.hpp>
5+
#include <string>
6+
#include <string_view>
7+
8+
namespace EE { namespace System {
9+
10+
// Inpired in rx-cpp (https://github.com/stevedonovan/rx-cpp/).
11+
12+
class EE_API PatternMatcher {
13+
public:
14+
enum class PatternType { LuaPattern, PCRE };
15+
16+
struct EE_API Range {
17+
int start{ -1 };
18+
int end{ -1 };
19+
bool isValid() { return -1 != start && -1 != end; }
20+
};
21+
22+
class EE_API State {
23+
public:
24+
State( PatternMatcher* pattern, bool ownPattern );
25+
26+
~State();
27+
28+
bool range( int index, int& start, int& end );
29+
30+
bool matches( const char* string, size_t length );
31+
32+
PatternMatcher* mPattern;
33+
Range* mRanges;
34+
size_t mRefCount;
35+
bool mOwnPattern;
36+
};
37+
38+
class EE_API Match {
39+
public:
40+
~Match();
41+
42+
Match( PatternMatcher& r, const char* string, bool ownPattern = false );
43+
44+
Match( PatternMatcher& r, const std::string& string, bool ownPattern = false );
45+
46+
Match( const PatternMatcher::Match& other );
47+
48+
Match& operator=( const Match& other );
49+
50+
bool matches();
51+
52+
bool subst( std::string& res );
53+
54+
void next();
55+
56+
std::string group( int idx = -1 ) const;
57+
58+
std::string_view groupView( int idx = -1 ) const;
59+
60+
bool range( int idx, int& start, int& end ) const;
61+
62+
std::string operator[]( int index ) const;
63+
64+
class iterator {
65+
public:
66+
iterator( Match* pm ) : mMatcher( pm ) {
67+
if ( mMatcher != nullptr && !mMatcher->matches() )
68+
mMatcher = nullptr;
69+
}
70+
bool operator!=( const iterator& other ) { return mMatcher != other.mMatcher; }
71+
bool operator==( const iterator& other ) { return mMatcher == other.mMatcher; }
72+
const Match& operator*() const { return *mMatcher; }
73+
iterator& operator++() {
74+
mMatcher->next();
75+
if ( !mMatcher->matches() )
76+
mMatcher = nullptr;
77+
return *this;
78+
}
79+
80+
protected:
81+
Match* mMatcher;
82+
};
83+
84+
iterator begin() { return iterator( this ); }
85+
86+
iterator end() { return iterator( nullptr ); }
87+
88+
protected:
89+
PatternMatcher::State* mState{ nullptr };
90+
const char* mString{ nullptr };
91+
size_t mLength{ 0 };
92+
};
93+
94+
PatternMatcher( PatternType type ) : mType( type ) {}
95+
96+
virtual ~PatternMatcher() {}
97+
98+
PatternType getType() const { return mType; }
99+
100+
PatternMatcher::Match gmatch( const char* s ) &;
101+
102+
PatternMatcher::Match gmatch( const char* s ) &&;
103+
104+
PatternMatcher::Match gmatch( const std::string& string ) &&;
105+
106+
PatternMatcher::Match gmatch( const std::string& string ) &;
107+
108+
bool range( int indexGet, int& startMatch, int& endMatch,
109+
PatternMatcher::Range* returnedMatched ) const;
110+
111+
bool find( const std::string& s, int& startMatch, int& endMatch, int offset = 0,
112+
int returnedMatchIndex = 0 ) const;
113+
114+
bool find( const char* stringSearch, int& startMatch, int& endMatch, int stringStartOffset = 0,
115+
int stringLength = 0, int returnMatchIndex = 0 ) const;
116+
117+
std::string gsub( const char* text, const char* replace );
118+
119+
std::string gsub( const std::string& text, const std::string& replace );
120+
121+
virtual const std::string_view& getPattern() const = 0;
122+
123+
virtual bool matches( const char* stringSearch, int stringStartOffset,
124+
PatternMatcher::Range* matchList, size_t stringLength ) const = 0;
125+
126+
virtual bool matches( const std::string& str, PatternMatcher::Range* matchList = nullptr,
127+
int stringStartOffset = 0 ) const = 0;
128+
129+
virtual const size_t& getNumMatches() const = 0;
130+
131+
virtual bool isValid() const = 0;
132+
133+
protected:
134+
PatternType mType;
135+
};
136+
137+
}} // namespace EE::System
138+
139+
#endif

include/eepp/system/regex.hpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#ifndef EE_SYSTEM_REGEX
2+
#define EE_SYSTEM_REGEX
3+
4+
#include <eepp/core/containers.hpp>
5+
#include <eepp/system/patternmatcher.hpp>
6+
#include <eepp/system/singleton.hpp>
7+
8+
namespace EE { namespace System {
9+
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, Uint32 options, void* cache );
20+
21+
void* find( const std::string_view&, Uint32 options );
22+
23+
void clear();
24+
25+
protected:
26+
bool mEnabled{ true };
27+
UnorderedMap<String::HashType, void*> mCache;
28+
};
29+
30+
class EE_API RegEx : public PatternMatcher {
31+
public:
32+
enum Options : Uint32 {
33+
None = 0x00000000u,
34+
AllowEmptyClass = 0x00000001u, // C
35+
AltBsux = 0x00000002u, // C
36+
AutoCallout = 0x00000004u, // C
37+
Caseless = 0x00000008u, // C
38+
DollarEndonly = 0x00000010u, // J M D
39+
Dotall = 0x00000020u, // C
40+
Dupnames = 0x00000040u, // C
41+
Extended = 0x00000080u, // C
42+
Firstline = 0x00000100u, // J M D
43+
MatchUnsetBackref = 0x00000200u, // C J M
44+
Multiline = 0x00000400u, // C
45+
NeverUcp = 0x00000800u, // C
46+
NeverUtf = 0x00001000u, // C
47+
NoAutoCapture = 0x00002000u, // C
48+
NoAutoPossess = 0x00004000u, // C
49+
NoDotstarAnchor = 0x00008000u, // C
50+
NoStartOptimize = 0x00010000u, // J M D
51+
Ucp = 0x00020000u, // C J M D
52+
Ungreedy = 0x00040000u, // C
53+
Utf = 0x00080000u, // C J M D
54+
NeverBackslashC = 0x00100000u, // C
55+
AltCircumflex = 0x00200000u, // J M D
56+
AltVerbnames = 0x00400000u, // C
57+
UseOffsetLimit = 0x00800000u, // J M D
58+
ExtendedMore = 0x01000000u, // C
59+
Literal = 0x02000000u, // C
60+
MatchInvalidUtf = 0x04000000u, // J M D
61+
};
62+
63+
RegEx( const std::string_view& pattern, Options options = Options::Utf, bool useCache = true );
64+
65+
virtual ~RegEx();
66+
67+
virtual bool isValid() const override { return mValid; }
68+
69+
virtual bool matches( const char* stringSearch, int stringStartOffset,
70+
PatternMatcher::Range* matchList, size_t stringLength ) const override;
71+
72+
virtual bool matches( const std::string& str, PatternMatcher::Range* matchList = nullptr,
73+
int stringStartOffset = 0 ) const override;
74+
75+
virtual const size_t& getNumMatches() const override;
76+
77+
const std::string_view& getPattern() const override { return mPattern; }
78+
79+
protected:
80+
std::string_view mPattern;
81+
mutable size_t mMatchNum;
82+
void* mCompiledPattern;
83+
int mCaptureCount;
84+
bool mValid{ false };
85+
bool mCached{ false };
86+
};
87+
88+
}} // namespace EE::System
89+
90+
#endif

0 commit comments

Comments
 (0)