forked from cucumber/cucumber-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRegex.cpp
77 lines (64 loc) · 2.18 KB
/
Regex.cpp
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
#include <cucumber-cpp/internal/utils/Regex.hpp>
#include <algorithm>
namespace cucumber {
namespace internal {
Regex::Regex(std::string regularExpression) :
regexImpl(regularExpression),
regexString(regularExpression) {
}
bool RegexMatch::matches() {
return regexMatched;
}
const RegexMatch::submatches_type& RegexMatch::getSubmatches() {
return submatches;
}
std::string Regex::str() const {
return regexString;
}
std::shared_ptr<RegexMatch> Regex::find(const std::string& expression) const {
return std::make_shared<FindRegexMatch>(regexImpl, expression);
}
namespace {
bool isUtf8CodeUnitStartOfCodepoint(unsigned int i) {
return (i & 0xc0) != 0x80;
}
std::ptrdiff_t utf8CodepointOffset(
const std::string& expression, const std::string::const_iterator& it
) {
return count_if(expression.begin(), it, &isUtf8CodeUnitStartOfCodepoint);
}
} // namespace
FindRegexMatch::FindRegexMatch(const std::regex& regexImpl, const std::string& expression) {
std::smatch matchResults;
regexMatched = std::regex_search(expression, matchResults, regexImpl);
if (regexMatched) {
std::smatch::const_iterator i = matchResults.begin();
if (i != matchResults.end())
// Skip capture group 0 which is the whole match, not a user marked sub-expression
++i;
for (; i != matchResults.end(); ++i) {
if (i->matched) {
RegexSubmatch s = {*i, utf8CodepointOffset(expression, i->first)};
submatches.push_back(s);
} else {
submatches.push_back(RegexSubmatch());
}
}
}
}
std::shared_ptr<RegexMatch> Regex::findAll(const std::string& expression) const {
return std::make_shared<FindAllRegexMatch>(regexImpl, expression);
}
FindAllRegexMatch::FindAllRegexMatch(const std::regex& regexImpl, const std::string& expression) {
std::sregex_token_iterator i(
expression.begin(), expression.end(), regexImpl, 1, std::regex_constants::match_continuous
);
const std::sregex_token_iterator end;
for (; i != end; ++i) {
RegexSubmatch s = {*i, -1};
submatches.push_back(s);
}
regexMatched = !submatches.empty();
}
}
}