regexp: support NEWLINE and EXTENDED flags

Support the NEWLINE and EXTENDED flags for posix regexp; for PCRE, it
supports only NEWLINE (as MULTILINE) and not fully compatibly.

We'll likely need to bring our own regexp implementation (like git does)
but for now this is a good bandaid.
This commit is contained in:
Edward Thomson
2022-01-29 19:04:52 -05:00
parent 0abd5e7038
commit f85a69aed5
2 changed files with 15 additions and 1 deletions

View File

@@ -16,6 +16,8 @@ int git_regexp_compile(git_regexp *r, const char *pattern, int flags)
if (flags & GIT_REGEXP_ICASE)
cflags |= PCRE_CASELESS;
if (flags & GIT_REGEXP_NEWLINE)
cflags |= PCRE_MULTILINE;
if ((*r = pcre_compile(pattern, cflags, &error, &erroffset, NULL)) == NULL) {
git_error_set_str(GIT_ERROR_REGEX, error);
@@ -102,6 +104,8 @@ int git_regexp_compile(git_regexp *r, const char *pattern, int flags)
if (flags & GIT_REGEXP_ICASE)
cflags |= PCRE2_CASELESS;
if (flags & GIT_REGEXP_NEWLINE)
cflags |= PCRE2_MULTILINE;
if ((*r = pcre2_compile((const unsigned char *) pattern, PCRE2_ZERO_TERMINATED,
cflags, &error, &erroff, NULL)) == NULL) {
@@ -200,6 +204,10 @@ int git_regexp_compile(git_regexp *r, const char *pattern, int flags)
if (flags & GIT_REGEXP_ICASE)
cflags |= REG_ICASE;
if (flags & GIT_REGEXP_EXTENDED)
cflags |= REG_EXTENDED;
if (flags & GIT_REGEXP_NEWLINE)
cflags |= REG_NEWLINE;
# if defined(GIT_REGEX_REGCOMP)
if ((error = regcomp(r, pattern, cflags)) != 0)

View File

@@ -30,7 +30,13 @@ typedef regex_t git_regexp;
/** Options supported by @git_regexp_compile. */
typedef enum {
/** Enable case-insensitive matching */
GIT_REGEXP_ICASE = (1 << 0)
GIT_REGEXP_ICASE = (1 << 0),
/** Enable extended matching */
GIT_REGEXP_EXTENDED = (1 << 1),
/** Newline matching */
GIT_REGEXP_NEWLINE = (1 << 2)
} git_regexp_flags_t;
/** Structure containing information about regular expression matching groups */