Skip to content
Open
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
60 changes: 58 additions & 2 deletions src/framework/regex_matching.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,71 @@
#include <stdio.h>
#include <stdlib.h>
#include <regex.h>
#include <string.h>

#define MAX_LEN 1024

void check_regex_match(const char * pattern, const char * str, int *imatch){
static const char *BRE_SPECIAL_CHARS = ".[\\*^$";

/******************************************************************************
*
* possible_bre
*
* Determine whether an input string may be a Basic Regular Expression (BRE).
* This function may return false positives (i.e., a return value indicating
* that a string is a BRE, when in fact the string is not a BRE), but it will
* never return a false negative (i.e., a return value indicating that a string
* is not a BRE, when in fact the string is a BRE).
*
* Inputs:
* s - a null-terminated string
*
* Return value: 1 if the input string 's' is possibly a BRE and 0 otherwise.
*
******************************************************************************/
static int possible_bre(const char *s)
{
for (; *s != '\0'; s++) {
if (strchr(BRE_SPECIAL_CHARS, *s) != NULL) {
return 1;
}
}
return 0;
}


/******************************************************************************
*
* check_regex_match
*
* Determine whether an input string 'str' matches the Basic Regular Expression
* (BRE) 'pattern'.
*
* Inputs:
* pattern - a null-terminated string that may contain any valid BRE,
* including a simple string
* str - a null-terminated string to be checked against pattern
*
* Return value: 1 if the input string str matches the BRE pattern,
* 0 if the input string does not match the BRE pattern, and
* -1 if an error occurred.
*
******************************************************************************/
void check_regex_match(const char * pattern, const char * str, int *imatch)

@islas islas Jun 8, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it may be worth adding some function description to check_regex_match() especially as the internal helper function has details on the return value that differ slightly from the return values of imatch from the primary function in this file.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just added documentation for the check_regex_match function with commit 384dff9.

{
regex_t regex;
char bracketed_pattern[MAX_LEN];
int ierr, len;

/*
* If pattern is a simple string and not a basic regular expression,
* a string comparison will suffice
*/
if (!possible_bre(pattern)) {
*imatch = (strcmp(pattern, str) == 0) ? 1 : 0;
return;
}

*imatch = 0;
len = snprintf(bracketed_pattern, 1024, "^%s$", pattern);
if ( len >= MAX_LEN ) {
Expand All @@ -34,4 +91,3 @@ void check_regex_match(const char * pattern, const char * str, int *imatch){
*imatch = -1;
}
}