Hash :
d77378eb
Author :
Date :
2019-09-13T08:54:26
regexp: implement new regular expression API We currently support a set of different regular expression backends with PCRE, PCRE2, regcomp(3P) and regcomp_l(3). The current implementation of this is done via a simple POSIX wrapper that either directly uses supplied functions or that is a very small wrapper. To support PCRE and PCRE2, we use their provided <pcreposix.h> and <pcre2posix.h> wrappers. These wrappers are implemented in such a way that the accompanying libraries pcre-posix and pcre2-posix provide the same symbols as the libc ones, namely regcomp(3P) et al. This works out on some systems just fine, most importantly on glibc-based ones, where the regular expression functions are implemented as weak aliases and thus get overridden by linking in the pcre{,2}-posix library. On other systems we depend on the linking order of libc and pcre library, and as libc always comes first we will end up with the functions of the libc implementation. As a result, we may use the structures `regex_t` and `regmatch_t` declared by <pcre{,2}posix.h>, but use functions defined by the libc, leading to segfaults. The issue is not easily solvable. Somed distributions like Debian have resolved this by patching PCRE and PCRE2 to carry custom prefixes to all the POSIX function wrappers. But this is not supported by upstream and thus inherently unportable between distributions. We could instead try to modify linking order, but this starts becoming fragile and will not work e.g. when libgit2 is loaded via dlopen(3P) or similar ways. In the end, this means that we simply cannot use the POSIX wrappers provided by the PCRE libraries at all. Thus, this commit introduces a new regular expression API. The new API is on a tad higher level than the previous POSIX abstraction layer, as it tries to abstract away any non-portable flags like e.g. REG_EXTENDED, which has no equivalents in all of our supported backends. As there are no users of POSIX regular expressions that do _not_ reguest REG_EXTENDED this is fine to be abstracted away, though. Due to the API being higher-level than before, it should generally be a tad easier to use than the previous one. Note: ideally, the new API would've been called `git_regex_foobar` with a file "regex.h" and "regex.c". Unfortunately, this is currently impossible to implement due to naming clashes between the then-existing "regex.h" and <regex.h> provided by the libc. As we add the source directory of libgit2 to the header search path, an include of <regex.h> would always find our own "regex.h". Thus, we have to take the bitter pill of adding one more character to all the functions to disambiguate the includes. To improve guarantees around cross-backend compatibility, this commit also brings along an improved regular expression test suite core::regexp.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#ifndef INCLUDE_regexp_h__
#define INCLUDE_regexp_h__
#include "common.h"
#if defined(GIT_REGEX_BUILTIN) || defined(GIT_REGEX_PCRE)
# include "pcre.h"
typedef pcre *git_regexp;
# define GIT_REGEX_INIT NULL
#elif defined(GIT_REGEX_PCRE2)
# define PCRE2_CODE_UNIT_WIDTH 8
# include <pcre2.h>
typedef pcre2_code *git_regexp;
# define GIT_REGEX_INIT NULL
#elif defined(GIT_REGEX_REGCOMP) || defined(GIT_REGEX_REGCOMP_L)
# include <regex.h>
typedef regex_t git_regexp;
# define GIT_REGEX_INIT { 0 }
#else
# error "No regex backend"
#endif
/** Options supported by @git_regexp_compile. */
typedef enum {
/** Enable case-insensitive matching */
GIT_REGEXP_ICASE = (1 << 0)
} git_regexp_flags_t;
/** Structure containing information about regular expression matching groups */
typedef struct {
/** Start of the given match. -1 if the group didn't match anything */
ssize_t start;
/** End of the given match. -1 if the group didn't match anything */
ssize_t end;
} git_regmatch;
/**
* Compile a regular expression. The compiled expression needs to
* be cleaned up afterwards with `git_regexp_dispose`.
*
* @param r Pointer to the storage where to initialize the regular expression.
* @param pattern The pattern that shall be compiled.
* @param flags Flags to alter how the pattern shall be handled.
* 0 for defaults, otherwise see @git_regexp_flags_t.
* @return 0 on success, otherwise a negative return value.
*/
int git_regexp_compile(git_regexp *r, const char *pattern, int flags);
/**
* Free memory associated with the regular expression
*
* @param r The regular expression structure to dispose.
*/
void git_regexp_dispose(git_regexp *r);
/**
* Test whether a given string matches a compiled regular
* expression.
*
* @param r Compiled regular expression.
* @param string String to match against the regular expression.
* @return 0 if the string matches, a negative error code
* otherwise. GIT_ENOTFOUND if no match was found,
* GIT_EINVALIDSPEC if the regular expression matching
* was invalid.
*/
int git_regexp_match(const git_regexp *r, const char *string);
/**
* Search for matches inside of a given string.
*
* Given a regular expression with capturing groups, this
* function will populate provided @git_regmatch structures with
* offsets for each of the given matches. Non-matching groups
* will have start and end values of the respective @git_regmatch
* structure set to -1.
*
* @param r Compiled regular expression.
* @param string String to match against the regular expression.
* @param nmatches Number of @git_regmatch structures provided by
* the user.
* @param matches Pointer to an array of @git_regmatch structures.
* @return 0 if the string matches, a negative error code
* otherwise. GIT_ENOTFOUND if no match was found,
* GIT_EINVALIDSPEC if the regular expression matching
* was invalid.
*/
int git_regexp_search(const git_regexp *r, const char *string, size_t nmatches, git_regmatch *matches);
#endif