-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathregex.cpp
More file actions
97 lines (80 loc) · 2.56 KB
/
regex.cpp
File metadata and controls
97 lines (80 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "tools/regex.hpp"
#include "tools/regex_exception.hpp"
#include <regex>
//------------------------------------------------------------------------------
namespace tools
{
//------------------------------------------------------------------------------
Regex::Regex( const std::string & _str )
: m_regex{ _str }
, m_str{ _str }
{
checkRegex();
}
//------------------------------------------------------------------------------
Regex::Regex( const Regex & _other )
: Regex( _other.m_str )
{
}
//------------------------------------------------------------------------------
bool Regex::search( const std::string & _str ) const
{
// return std::regex_search( _str, m_regex );
const bool result = RE2::PartialMatch( _str, m_regex );
return result;
}
//------------------------------------------------------------------------------
const std::string & Regex::toString() const
{
return m_str;
}
//------------------------------------------------------------------------------
std::string Regex::toString( RE2::ErrorCode _code )
{
switch( _code )
{
case RE2::ErrorCode::ErrorInternal:
return "Unexpected error";
case RE2::ErrorCode::ErrorBadEscape:
return "bad escape sequence";
case RE2::ErrorCode::ErrorBadCharClass:
return "bad character class";
case RE2::ErrorCode::ErrorBadCharRange:
return "bad character class range";
case RE2::ErrorCode::ErrorMissingBracket:
return "missing closing ]";
case RE2::ErrorCode::ErrorMissingParen:
return "missing closing )";
case RE2::ErrorCode::ErrorUnexpectedParen:
return "unexpected closing )";
case RE2::ErrorCode::ErrorTrailingBackslash:
return "trailing \\ at end of regexp";
case RE2::ErrorCode::ErrorRepeatArgument:
return "repeat argument missing, e.g. \"*\"";
case RE2::ErrorCode::ErrorRepeatSize:
return "bad repetition argument";
case RE2::ErrorCode::ErrorRepeatOp:
return "bad repetition operator";
case RE2::ErrorCode::ErrorBadPerlOp:
return "bad perl operator";
case RE2::ErrorCode::ErrorBadUTF8:
return "invalid UTF-8 in regexp";
case RE2::ErrorCode::ErrorBadNamedCapture:
return "bad named capture group";
case RE2::ErrorCode::ErrorPatternTooLarge:
return "pattern too large (compile failed)";
default:
return "";
}
}
//------------------------------------------------------------------------------
void Regex::checkRegex()
{
const RE2::ErrorCode code = m_regex.error_code();
if( code != RE2::ErrorCode::NoError )
{
throw RegexException( m_str, toString( code ) );
}
}
//------------------------------------------------------------------------------
}