philips-software / philips-software/amp-cucumber-cpp-runner

CoPilot comment: Replace `std::smatch` with `std::svmatch` to eliminate temporary string copy in `StdRegexStrategy`

Open Beginner friendly
#342 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
22
Forks
7
Avg merge
2d 5h
Merged PRs (30d)
4

Description

In StdRegexStrategy::Match (cucumber_cpp/library/cucumber_expression/StdRegexStrategy.cpp), the input std::string_view text is currently copied into a local std::string textStr solely because std::regex_search with std::smatch requires lvalue string iterators. This allocation is unnecessary if we switch to std::regex_search overloads that work on arbitrary iterator ranges paired with std::match_results<std::string_view::const_iterator> (aka std::svmatch in C++20, or std::match_results<const char*>/std::cmatch).

Current code:

std::optional<std::vector<std::optional<MatchGroup>>> StdRegexStrategy::Match(std::string_view text) const
{
    std::string textStr{ text };  // unnecessary copy
    std::smatch match;

    if (!std::regex_search(textStr, match, regex))
        return std::nullopt;

    // ... extract groups from match ...
}

Proposed approach:

std::optional<std::vector<std::optional<MatchGroup>>> StdRegexStrategy::Match(std::string_view text) const
{
    std::cmatch match;  // match_results<const char*>

    if (!std::regex_search(text.data(), text.data() + text.size(), match, regex))
        return std::nullopt;

    // positions now refer directly into the original text buffer
    // match[i].str() still returns std::string copies for value
    // match.position(i) and match.length(i) work as before
}

Using std::cmatch (i.e. std::match_results<const char*>) avoids the temporary string entirely. The regex_search(const char*, const char*, cmatch&, regex) overload is well-defined and the resulting cmatch references the original text data directly — no lifetime concerns as long as the match is consumed before text goes out of scope (which it already is).

File: cucumber_cpp/library/cucumber_expression/StdRegexStrategy.cpp

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read cucumber_cpp/library/cucumber_expression/StdRegexStrategy.cpp, starting at StdRegexStrategy::Match and its existing match-group extraction. Replace the temporary-string path with the iterator-based match approach described in the issue, then verify that matching and group extraction still work without creating textStr.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
performance
Issue type
Refactor
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
74/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.