Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 49 additions & 3 deletions include/tsutil/Regex.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,41 @@ class RegexMatches
_MatchDataPtr _match_data;
};

/// @brief Wrapper for PCRE2 match context
///
/// @internal This instance is not tied to any Regex and can be used with one of the Regex::exec overloads.
class RegexMatchContext
{
friend class Regex;

public:
/** Construct a new RegexMatchContext object.
*/
RegexMatchContext();
~RegexMatchContext();

/// uses pcre2_match_context_copy for a deep copy.
RegexMatchContext(RegexMatchContext const &orig);
RegexMatchContext &operator=(RegexMatchContext const &orig);

RegexMatchContext(RegexMatchContext &&) = default;
RegexMatchContext &operator=(RegexMatchContext &&) = default;

/** Limits the amount of backtracking that can take place.
* Any regex exec call that fails will return PCRE2_ERROR_MATCHLIMIT(-47)
*/
void set_match_limit(uint32_t limit);

private:
/// @internal This wraps a void* so to avoid requiring a pcre2 include.
struct _MatchContext;
struct _MatchContextPtr {
void *_ptr = nullptr;
};

_MatchContextPtr _match_context;
};

/// @brief Wrapper for PCRE2 regular expression.
class Regex
{
Expand Down Expand Up @@ -179,17 +214,28 @@ class Regex
* @param subject String to match against.
* @param matches Place to store the capture groups.
* @param flags Match flags (e.g., RE_NOTEMPTY).
* @param optional context Match context (set matching limits).
* @return @c The number of capture groups. < 0 if an error occurred. 0 if the number of Matches is too small.
*
* It is safe to call this method concurrently on the same instance of @a this.
*
* Each capture group takes 3 elements of @a ovector, therefore @a ovecsize must
* be a multiple of 3 and at least three times the number of desired capture groups.
*/
int exec(std::string_view subject, RegexMatches &matches, uint32_t flags) const;
int exec(std::string_view subject, RegexMatches &matches, uint32_t flags,
RegexMatchContext const *const matchContext = nullptr) const;

/** Error string for exec failure.
*
* @param int return code from exec call.
*/
static std::string get_error_string(int rc);

/// @return The number of capture groups in the compiled pattern, -1 for fail.
int32_t get_capture_count() const;

/// @return The number of capture groups in the compiled pattern.
int get_capture_count();
/// @return number of highest back references, -1 for fail.
int32_t get_backref_max() const;

/// @return Is the compiled pattern empty?
bool empty() const;
Expand Down
2 changes: 1 addition & 1 deletion plugins/experimental/cookie_remap/cookie_remap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ class subop

Regex *regex = nullptr;
std::string regex_string;
int regex_ccount = 0;
int32_t regex_ccount = 0;

std::string bucket;
unsigned int how_many = 0;
Expand Down
2 changes: 1 addition & 1 deletion plugins/regex_remap/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@

add_atsplugin(regex_remap regex_remap.cc)

target_link_libraries(regex_remap PRIVATE PCRE::PCRE libswoc::libswoc)
target_link_libraries(regex_remap PRIVATE libswoc::libswoc)

verify_remap_plugin(regex_remap)
Loading