Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define type Regex and implement RegexDestroy() #204

Merged
merged 1 commit into from
Dec 11, 2023
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
2 changes: 1 addition & 1 deletion libutils/json-pcre.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
#include <regex.h>

JsonElement *StringCaptureData(
const pcre2_code *regex, const char *pattern, const char *data);
const Regex *regex, const char *pattern, const char *data);

#endif
2 changes: 1 addition & 1 deletion libutils/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -2933,7 +2933,7 @@ bool JsonErrorVisitor(ARG_UNUSED JsonElement *element, ARG_UNUSED void *data)
// takes either a pre-compiled pattern OR a regex (one of the two shouldn't be
// NULL)
JsonElement *StringCaptureData(
const pcre2_code *const regex, const char *const pattern, const char *const data)
const Regex *const regex, const char *const pattern, const char *const data)
{
assert(regex != NULL || pattern != NULL);
assert(data != NULL);
Expand Down
22 changes: 13 additions & 9 deletions libutils/regex.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

#include <buffer.h>

pcre2_code *CompileRegex(const char *pattern)
Regex *CompileRegex(const char *pattern)
{
int err_code;
size_t err_offset;
Expand Down Expand Up @@ -61,7 +61,11 @@ pcre2_code *CompileRegex(const char *pattern)
return NULL;
}

bool StringMatchWithPrecompiledRegex(const pcre2_code *regex, const char *str,
void RegexDestroy(Regex *regex) {
pcre2_code_free(regex);
}

bool StringMatchWithPrecompiledRegex(const Regex *regex, const char *str,
size_t *start, size_t *end)
{
assert(regex != NULL);
Expand Down Expand Up @@ -101,32 +105,32 @@ bool StringMatchWithPrecompiledRegex(const pcre2_code *regex, const char *str,

bool StringMatch(const char *pattern, const char *str, size_t *start, size_t *end)
{
pcre2_code *regex = CompileRegex(pattern);
Regex *regex = CompileRegex(pattern);

if (regex == NULL)
{
return false;
}

bool ret = StringMatchWithPrecompiledRegex(regex, str, start, end);
pcre2_code_free(regex);
RegexDestroy(regex);
return ret;
}

bool StringMatchFull(const char *pattern, const char *str)
{
pcre2_code *regex = CompileRegex(pattern);
Regex *regex = CompileRegex(pattern);
if (regex == NULL)
{
return false;
}

bool ret = StringMatchFullWithPrecompiledRegex(regex, str);
pcre2_code_free(regex);
RegexDestroy(regex);
return ret;
}

bool StringMatchFullWithPrecompiledRegex(const pcre2_code *regex, const char *str)
bool StringMatchFullWithPrecompiledRegex(const Regex *regex, const char *str)
{
size_t start;
size_t end;
Expand All @@ -149,7 +153,7 @@ bool StringMatchFullWithPrecompiledRegex(const pcre2_code *regex, const char *st

// If return_names is not set, only the captured data is returned (so
// for N captures you can expect N elements in the Sequence).
Seq *StringMatchCapturesWithPrecompiledRegex(const pcre2_code *regex, const char *str, const bool return_names)
Seq *StringMatchCapturesWithPrecompiledRegex(const Regex *regex, const char *str, const bool return_names)
{
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(regex, NULL);
int result = pcre2_match(regex, (PCRE2_SPTR) str, PCRE2_ZERO_TERMINATED,
Expand Down Expand Up @@ -289,7 +293,7 @@ bool CompareStringOrRegex(const char *value, const char *compareTo, bool regex)
* This is a fast partial match function. It checks that the compiled rx matches
* anywhere inside teststring. It does not allocate or free rx!
*/
bool RegexPartialMatch(const pcre2_code *regex, const char *teststring)
bool RegexPartialMatch(const Regex *regex, const char *teststring)
{
pcre2_match_data *md = pcre2_match_data_create_from_pattern(regex, NULL);
int rc = pcre2_match(regex, (PCRE2_SPTR) teststring, PCRE2_ZERO_TERMINATED, 0, 0, md, NULL);
Expand Down
18 changes: 13 additions & 5 deletions libutils/regex.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,28 @@

#include <sequence.h> /* Seq */

/**
* @note: The definition of the type Regex may change in the future, so code
* using this header should avoid using pcre2_code and use Regex instead
* and use RegexDestroy() instead of pcre2_code_free().
*/
typedef pcre2_code Regex;

#define CFENGINE_REGEX_WHITESPACE_IN_CONTEXTS ".*[_A-Za-z0-9][ \\t]+[_A-Za-z0-9].*"

/* Try to use CompileRegex() and StringMatchWithPrecompiledRegex(). */
pcre2_code *CompileRegex(const char *pattern);
Regex *CompileRegex(const char *pattern);
void RegexDestroy(Regex *regex);
bool StringMatch(const char *pattern, const char *str, size_t *start, size_t *end);
bool StringMatchWithPrecompiledRegex(const pcre2_code *regex, const char *str,
bool StringMatchWithPrecompiledRegex(const Regex *regex, const char *str,
size_t *start, size_t *end);
bool StringMatchFull(const char *pattern, const char *str);
bool StringMatchFullWithPrecompiledRegex(const pcre2_code *regex, const char *str);
bool StringMatchFullWithPrecompiledRegex(const Regex *regex, const char *str);
Seq *StringMatchCaptures(const char *pattern, const char *str, const bool return_names);
Seq *StringMatchCapturesWithPrecompiledRegex(const pcre2_code *pattern, const char *str, const bool return_names);
Seq *StringMatchCapturesWithPrecompiledRegex(const Regex *pattern, const char *str, const bool return_names);
bool CompareStringOrRegex(const char *value, const char *compareTo, bool regex);

/* Does not free rx! */
bool RegexPartialMatch(const pcre2_code *regex, const char *teststring);
bool RegexPartialMatch(const Regex *regex, const char *teststring);

#endif /* CFENGINE_REGEX_H */
Loading