Skip to content

Commit 7c4db4f

Browse files
committed
refactoring: use stl regex instead of boost::regex
This removes the support for repeated captures (i.e. boost::regex_constants::match_extra), which may break some use cases. It is expected that such use cases are rare.
1 parent 147aa92 commit 7c4db4f

File tree

8 files changed

+20
-27
lines changed

8 files changed

+20
-27
lines changed

.github/workflows/run-all.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ jobs:
2323
gcovr \
2424
git \
2525
libboost-program-options-dev \
26-
libboost-regex-dev \
2726
libboost-system-dev \
2827
libboost-test-dev \
2928
make \

CMakeLists.txt

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ else()
122122
endif()
123123

124124
set(Boost_USE_STATIC_RUNTIME OFF)
125-
set(CUKE_CORE_BOOST_LIBS system regex program_options)
125+
set(CUKE_CORE_BOOST_LIBS system program_options)
126126
if(CUKE_ENABLE_BOOST_TEST)
127127
# "An external test runner utility is required to link with dynamic library" (Boost User's Guide)
128128
set(Boost_USE_STATIC_LIBS OFF)
@@ -163,13 +163,6 @@ if(Boost_SYSTEM_LIBRARY AND NOT TARGET Boost::system)
163163
)
164164
endif()
165165
endif()
166-
if(Boost_REGEX_LIBRARY AND NOT TARGET Boost::regex)
167-
add_library(Boost::regex ${LIBRARY_TYPE} IMPORTED)
168-
set_target_properties(Boost::regex PROPERTIES
169-
"IMPORTED_LOCATION" "${Boost_REGEX_LIBRARY}"
170-
"INTERFACE_LINK_LIBRARIES" "Boost::boost"
171-
)
172-
endif()
173166
if(Boost_PROGRAM_OPTIONS_LIBRARY AND NOT TARGET Boost::program_options)
174167
add_library(Boost::program_options ${LIBRARY_TYPE} IMPORTED)
175168
set_target_properties(Boost::program_options PROPERTIES

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ It relies on a few executables:
2525
It relies on a few libraries:
2626

2727
* [Boost](http://www.boost.org/) 1.46 or later (1.51+ on Windows).
28-
Required libraries: *system*, *regex* and *program_options*.
28+
Required libraries: *system* and *program_options*.
2929
Optional library for Boost Test driver: *test*.
3030
* [GTest](http://code.google.com/p/googletest/) 1.6 or later.
3131
Optional for the GTest driver. By default downloaded and built by CMake.

include/cucumber-cpp/internal/utils/Regex.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <cstddef>
55
#include <vector>
66

7-
#include <boost/regex.hpp>
7+
#include <regex>
88

99
namespace cucumber {
1010
namespace internal {
@@ -31,18 +31,19 @@ class RegexMatch {
3131

3232
class FindRegexMatch : public RegexMatch {
3333
public:
34-
FindRegexMatch(const boost::regex &regexImpl, const std::string &expression);
34+
FindRegexMatch(const std::regex &regexImpl, const std::string &expression);
3535
};
3636

3737
class FindAllRegexMatch : public RegexMatch {
3838
public:
39-
FindAllRegexMatch(const boost::regex &regexImpl, const std::string &expression);
39+
FindAllRegexMatch(const std::regex &regexImpl, const std::string &expression);
4040
};
4141

4242

4343
class Regex {
4444
private:
45-
boost::regex regexImpl;
45+
std::regex regexImpl;
46+
const std::string regexString;
4647

4748
public:
4849
Regex(std::string expr);

src/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ foreach(TARGET
8383
target_link_libraries(${TARGET}
8484
PUBLIC
8585
Boost::boost
86-
Boost::regex
8786
PRIVATE
8887
${CUKE_EXTRA_PRIVATE_LIBRARIES}
8988
)

src/CukeCommands.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ const std::string CukeCommands::snippetText(const std::string stepKeyword, const
4747
}
4848

4949
const std::string CukeCommands::escapeRegex(const std::string reg) const {
50-
return regex_replace(reg, boost::regex("[\\|\\(\\)\\[\\]\\{\\}\\^\\$\\*\\+\\?\\.\\\\]"), "\\\\&", boost::match_default | boost::format_sed);
50+
return regex_replace(reg, std::regex("[\\|\\(\\)\\[\\]\\{\\}\\^\\$\\*\\+\\?\\.\\\\]"), "\\\\&", std::regex_constants::match_default | std::regex_constants::format_sed);
5151
}
5252

5353
const std::string CukeCommands::escapeCString(const std::string str) const {
54-
return regex_replace(str, boost::regex("[\"\\\\]"), "\\\\&", boost::match_default | boost::format_sed);
54+
return regex_replace(str, std::regex("[\"\\\\]"), "\\\\&", std::regex_constants::match_default | std::regex_constants::format_sed);
5555
}
5656

5757
MatchResult CukeCommands::stepMatches(const std::string description) const {

src/Regex.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ namespace cucumber {
66
namespace internal {
77

88
Regex::Regex(std::string regularExpression) :
9-
regexImpl(regularExpression.c_str()) {
9+
regexImpl(regularExpression),
10+
regexString(regularExpression) {
1011
}
1112

1213
bool RegexMatch::matches() {
@@ -18,7 +19,7 @@ const RegexMatch::submatches_type & RegexMatch::getSubmatches() {
1819
}
1920

2021
std::string Regex::str() const {
21-
return regexImpl.str();
22+
return regexString;
2223
}
2324

2425
std::shared_ptr<RegexMatch> Regex::find(const std::string &expression) const {
@@ -36,12 +37,11 @@ std::ptrdiff_t utf8CodepointOffset(const std::string& expression,
3637
}
3738
} // namespace
3839

39-
FindRegexMatch::FindRegexMatch(const boost::regex& regexImpl, const std::string& expression) {
40-
boost::smatch matchResults;
41-
regexMatched = boost::regex_search(
42-
expression, matchResults, regexImpl, boost::regex_constants::match_extra);
40+
FindRegexMatch::FindRegexMatch(const std::regex& regexImpl, const std::string& expression) {
41+
std::smatch matchResults;
42+
regexMatched = std::regex_search(expression, matchResults, regexImpl);
4343
if (regexMatched) {
44-
boost::smatch::const_iterator i = matchResults.begin();
44+
std::smatch::const_iterator i = matchResults.begin();
4545
if (i != matchResults.end())
4646
// Skip capture group 0 which is the whole match, not a user marked sub-expression
4747
++i;
@@ -60,9 +60,9 @@ std::shared_ptr<RegexMatch> Regex::findAll(const std::string &expression) const
6060
return std::make_shared<FindAllRegexMatch>(regexImpl, expression);
6161
}
6262

63-
FindAllRegexMatch::FindAllRegexMatch(const boost::regex &regexImpl, const std::string &expression) {
64-
boost::sregex_token_iterator i(expression.begin(), expression.end(), regexImpl, 1, boost::regex_constants::match_continuous);
65-
const boost::sregex_token_iterator end;
63+
FindAllRegexMatch::FindAllRegexMatch(const std::regex &regexImpl, const std::string &expression) {
64+
std::sregex_token_iterator i(expression.begin(), expression.end(), regexImpl, 1, std::regex_constants::match_continuous);
65+
const std::sregex_token_iterator end;
6666
for (; i != end; ++i) {
6767
RegexSubmatch s = {*i, -1};
6868
submatches.push_back(s);

tests/utils/DriverTestRunner.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "StepManagerTestDouble.hpp"
55
#include <cucumber-cpp/internal/CukeCommands.hpp>
66

7+
#include <cstring>
78
#include <iostream>
89
#include <string>
910

0 commit comments

Comments
 (0)