From 7c4db4f32dbb774cc48e26b160b821f858e2ed36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Urs=20F=C3=A4ssler?= Date: Fri, 3 Nov 2023 14:47:00 +0100 Subject: [PATCH] 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. --- .github/workflows/run-all.yml | 1 - CMakeLists.txt | 9 +-------- README.md | 2 +- include/cucumber-cpp/internal/utils/Regex.hpp | 9 +++++---- src/CMakeLists.txt | 1 - src/CukeCommands.cpp | 4 ++-- src/Regex.cpp | 20 +++++++++---------- tests/utils/DriverTestRunner.hpp | 1 + 8 files changed, 20 insertions(+), 27 deletions(-) diff --git a/.github/workflows/run-all.yml b/.github/workflows/run-all.yml index 58ed7c42..0ec0e63a 100644 --- a/.github/workflows/run-all.yml +++ b/.github/workflows/run-all.yml @@ -23,7 +23,6 @@ jobs: gcovr \ git \ libboost-program-options-dev \ - libboost-regex-dev \ libboost-system-dev \ libboost-test-dev \ make \ diff --git a/CMakeLists.txt b/CMakeLists.txt index f1878481..d159c758 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) @@ -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 diff --git a/README.md b/README.md index 3378b9af..fc607435 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/include/cucumber-cpp/internal/utils/Regex.hpp b/include/cucumber-cpp/internal/utils/Regex.hpp index 1db80c74..738975c6 100644 --- a/include/cucumber-cpp/internal/utils/Regex.hpp +++ b/include/cucumber-cpp/internal/utils/Regex.hpp @@ -4,7 +4,7 @@ #include #include -#include +#include namespace cucumber { namespace internal { @@ -31,18 +31,19 @@ class RegexMatch { class FindRegexMatch : public RegexMatch { public: - FindRegexMatch(const boost::regex ®exImpl, const std::string &expression); + FindRegexMatch(const std::regex ®exImpl, const std::string &expression); }; class FindAllRegexMatch : public RegexMatch { public: - FindAllRegexMatch(const boost::regex ®exImpl, const std::string &expression); + FindAllRegexMatch(const std::regex ®exImpl, const std::string &expression); }; class Regex { private: - boost::regex regexImpl; + std::regex regexImpl; + const std::string regexString; public: Regex(std::string expr); diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 290e2a67..084427ac 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -83,7 +83,6 @@ foreach(TARGET target_link_libraries(${TARGET} PUBLIC Boost::boost - Boost::regex PRIVATE ${CUKE_EXTRA_PRIVATE_LIBRARIES} ) diff --git a/src/CukeCommands.cpp b/src/CukeCommands.cpp index dd632c33..03dc7a6c 100644 --- a/src/CukeCommands.cpp +++ b/src/CukeCommands.cpp @@ -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 { diff --git a/src/Regex.cpp b/src/Regex.cpp index b5985eca..d07dddd1 100644 --- a/src/Regex.cpp +++ b/src/Regex.cpp @@ -6,7 +6,8 @@ namespace cucumber { namespace internal { Regex::Regex(std::string regularExpression) : - regexImpl(regularExpression.c_str()) { + regexImpl(regularExpression), + regexString(regularExpression) { } bool RegexMatch::matches() { @@ -18,7 +19,7 @@ const RegexMatch::submatches_type & RegexMatch::getSubmatches() { } std::string Regex::str() const { - return regexImpl.str(); + return regexString; } std::shared_ptr Regex::find(const std::string &expression) const { @@ -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; @@ -60,9 +60,9 @@ std::shared_ptr Regex::findAll(const std::string &expression) const return std::make_shared(regexImpl, expression); } -FindAllRegexMatch::FindAllRegexMatch(const boost::regex ®exImpl, 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 ®exImpl, 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); diff --git a/tests/utils/DriverTestRunner.hpp b/tests/utils/DriverTestRunner.hpp index feea7c5b..efa59dd9 100644 --- a/tests/utils/DriverTestRunner.hpp +++ b/tests/utils/DriverTestRunner.hpp @@ -4,6 +4,7 @@ #include "StepManagerTestDouble.hpp" #include +#include #include #include