]>
git.proxmox.com Git - libgit2.git/blob - src/regexp.h
2 * Copyright (C) the libgit2 contributors. All rights reserved.
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
8 #ifndef INCLUDE_regexp_h__
9 #define INCLUDE_regexp_h__
13 #if defined(GIT_REGEX_BUILTIN) || defined(GIT_REGEX_PCRE)
15 typedef pcre
*git_regexp
;
16 # define GIT_REGEX_INIT NULL
17 #elif defined(GIT_REGEX_PCRE2)
18 # define PCRE2_CODE_UNIT_WIDTH 8
20 typedef pcre2_code
*git_regexp
;
21 # define GIT_REGEX_INIT NULL
22 #elif defined(GIT_REGEX_REGCOMP) || defined(GIT_REGEX_REGCOMP_L)
24 typedef regex_t git_regexp
;
25 # define GIT_REGEX_INIT { 0 }
27 # error "No regex backend"
30 /** Options supported by @git_regexp_compile. */
32 /** Enable case-insensitive matching */
33 GIT_REGEXP_ICASE
= (1 << 0)
36 /** Structure containing information about regular expression matching groups */
38 /** Start of the given match. -1 if the group didn't match anything */
40 /** End of the given match. -1 if the group didn't match anything */
45 * Compile a regular expression. The compiled expression needs to
46 * be cleaned up afterwards with `git_regexp_dispose`.
48 * @param r Pointer to the storage where to initialize the regular expression.
49 * @param pattern The pattern that shall be compiled.
50 * @param flags Flags to alter how the pattern shall be handled.
51 * 0 for defaults, otherwise see @git_regexp_flags_t.
52 * @return 0 on success, otherwise a negative return value.
54 int git_regexp_compile(git_regexp
*r
, const char *pattern
, int flags
);
57 * Free memory associated with the regular expression
59 * @param r The regular expression structure to dispose.
61 void git_regexp_dispose(git_regexp
*r
);
64 * Test whether a given string matches a compiled regular
67 * @param r Compiled regular expression.
68 * @param string String to match against the regular expression.
69 * @return 0 if the string matches, a negative error code
70 * otherwise. GIT_ENOTFOUND if no match was found,
71 * GIT_EINVALIDSPEC if the regular expression matching
74 int git_regexp_match(const git_regexp
*r
, const char *string
);
77 * Search for matches inside of a given string.
79 * Given a regular expression with capturing groups, this
80 * function will populate provided @git_regmatch structures with
81 * offsets for each of the given matches. Non-matching groups
82 * will have start and end values of the respective @git_regmatch
83 * structure set to -1.
85 * @param r Compiled regular expression.
86 * @param string String to match against the regular expression.
87 * @param nmatches Number of @git_regmatch structures provided by
89 * @param matches Pointer to an array of @git_regmatch structures.
90 * @return 0 if the string matches, a negative error code
91 * otherwise. GIT_ENOTFOUND if no match was found,
92 * GIT_EINVALIDSPEC if the regular expression matching
95 int git_regexp_search(const git_regexp
*r
, const char *string
, size_t nmatches
, git_regmatch
*matches
);