Skip to content

Commit

Permalink
refactoring: use stl regex instead of boost::regex
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ursfassler committed Dec 17, 2023
1 parent 147aa92 commit 7c4db4f
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 27 deletions.
1 change: 0 additions & 1 deletion .github/workflows/run-all.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ jobs:
gcovr \
git \
libboost-program-options-dev \
libboost-regex-dev \
libboost-system-dev \
libboost-test-dev \
make \
Expand Down
9 changes: 1 addition & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ else()
endif()

set(Boost_USE_STATIC_RUNTIME OFF)
set(CUKE_CORE_BOOST_LIBS system regex program_options)
set(CUKE_CORE_BOOST_LIBS system program_options)
if(CUKE_ENABLE_BOOST_TEST)
# "An external test runner utility is required to link with dynamic library" (Boost User's Guide)
set(Boost_USE_STATIC_LIBS OFF)
Expand Down Expand Up @@ -163,13 +163,6 @@ if(Boost_SYSTEM_LIBRARY AND NOT TARGET Boost::system)
)
endif()
endif()
if(Boost_REGEX_LIBRARY AND NOT TARGET Boost::regex)
add_library(Boost::regex ${LIBRARY_TYPE} IMPORTED)
set_target_properties(Boost::regex PROPERTIES
"IMPORTED_LOCATION" "${Boost_REGEX_LIBRARY}"
"INTERFACE_LINK_LIBRARIES" "Boost::boost"
)
endif()
if(Boost_PROGRAM_OPTIONS_LIBRARY AND NOT TARGET Boost::program_options)
add_library(Boost::program_options ${LIBRARY_TYPE} IMPORTED)
set_target_properties(Boost::program_options PROPERTIES
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ It relies on a few executables:
It relies on a few libraries:

* [Boost](http://www.boost.org/) 1.46 or later (1.51+ on Windows).
Required libraries: *system*, *regex* and *program_options*.
Required libraries: *system* and *program_options*.
Optional library for Boost Test driver: *test*.
* [GTest](http://code.google.com/p/googletest/) 1.6 or later.
Optional for the GTest driver. By default downloaded and built by CMake.
Expand Down
9 changes: 5 additions & 4 deletions include/cucumber-cpp/internal/utils/Regex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <cstddef>
#include <vector>

#include <boost/regex.hpp>
#include <regex>

namespace cucumber {
namespace internal {
Expand All @@ -31,18 +31,19 @@ class RegexMatch {

class FindRegexMatch : public RegexMatch {
public:
FindRegexMatch(const boost::regex &regexImpl, const std::string &expression);
FindRegexMatch(const std::regex &regexImpl, const std::string &expression);
};

class FindAllRegexMatch : public RegexMatch {
public:
FindAllRegexMatch(const boost::regex &regexImpl, const std::string &expression);
FindAllRegexMatch(const std::regex &regexImpl, const std::string &expression);
};


class Regex {
private:
boost::regex regexImpl;
std::regex regexImpl;
const std::string regexString;

public:
Regex(std::string expr);
Expand Down
1 change: 0 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ foreach(TARGET
target_link_libraries(${TARGET}
PUBLIC
Boost::boost
Boost::regex
PRIVATE
${CUKE_EXTRA_PRIVATE_LIBRARIES}
)
Expand Down
4 changes: 2 additions & 2 deletions src/CukeCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ const std::string CukeCommands::snippetText(const std::string stepKeyword, const
}

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

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

MatchResult CukeCommands::stepMatches(const std::string description) const {
Expand Down
20 changes: 10 additions & 10 deletions src/Regex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ namespace cucumber {
namespace internal {

Regex::Regex(std::string regularExpression) :
regexImpl(regularExpression.c_str()) {
regexImpl(regularExpression),
regexString(regularExpression) {
}

bool RegexMatch::matches() {
Expand All @@ -18,7 +19,7 @@ const RegexMatch::submatches_type & RegexMatch::getSubmatches() {
}

std::string Regex::str() const {
return regexImpl.str();
return regexString;
}

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

FindRegexMatch::FindRegexMatch(const boost::regex& regexImpl, const std::string& expression) {
boost::smatch matchResults;
regexMatched = boost::regex_search(
expression, matchResults, regexImpl, boost::regex_constants::match_extra);
FindRegexMatch::FindRegexMatch(const std::regex& regexImpl, const std::string& expression) {
std::smatch matchResults;
regexMatched = std::regex_search(expression, matchResults, regexImpl);
if (regexMatched) {
boost::smatch::const_iterator i = matchResults.begin();
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;
Expand All @@ -60,9 +60,9 @@ std::shared_ptr<RegexMatch> Regex::findAll(const std::string &expression) const
return std::make_shared<FindAllRegexMatch>(regexImpl, expression);
}

FindAllRegexMatch::FindAllRegexMatch(const boost::regex &regexImpl, const std::string &expression) {
boost::sregex_token_iterator i(expression.begin(), expression.end(), regexImpl, 1, boost::regex_constants::match_continuous);
const boost::sregex_token_iterator end;
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);
Expand Down
1 change: 1 addition & 0 deletions tests/utils/DriverTestRunner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "StepManagerTestDouble.hpp"
#include <cucumber-cpp/internal/CukeCommands.hpp>

#include <cstring>
#include <iostream>
#include <string>

Expand Down

0 comments on commit 7c4db4f

Please sign in to comment.