NeoMutt  2024-12-12-14-g7b49f7
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
regex2.h File Reference

Type representing a regular expression. More...

#include <stdbool.h>
#include <stdint.h>
+ Include dependency graph for regex2.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

bool regex_equal (const struct Regex *a, const struct Regex *b)
 Compare two regexes.
 
void regex_free (struct Regex **ptr)
 Free a Regex object.
 
struct Regexregex_new (const char *str, uint32_t flags, struct Buffer *err)
 Create an Regex from a string.
 

Detailed Description

Type representing a regular expression.

Authors
  • Richard Russon

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file regex2.h.

Function Documentation

◆ regex_equal()

bool regex_equal ( const struct Regex a,
const struct Regex b 
)

Compare two regexes.

Parameters
aFirst regex
bSecond regex
Return values
trueThey are identical

Definition at line 52 of file regex.c.

53{
54 if (!a && !b) /* both empty */
55 return true;
56 if (!a ^ !b) /* one is empty, but not the other */
57 return false;
58 if (a->pat_not != b->pat_not)
59 return false;
60
61 return mutt_str_equal(a->pattern, b->pattern);
62}
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:660
char * pattern
printable version
Definition: regex3.h:87
bool pat_not
do not match
Definition: regex3.h:89
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ regex_free()

void regex_free ( struct Regex **  ptr)

Free a Regex object.

Parameters
[out]ptrRegex to free

Definition at line 68 of file regex.c.

69{
70 if (!ptr || !*ptr)
71 return;
72
73 struct Regex *rx = *ptr;
74 FREE(&rx->pattern);
75 if (rx->regex)
76 regfree(rx->regex);
77 FREE(&rx->regex);
78
79 FREE(ptr);
80}
#define FREE(x)
Definition: memory.h:55
Cached regular expression.
Definition: regex3.h:86
regex_t * regex
compiled expression
Definition: regex3.h:88
+ Here is the caller graph for this function:

◆ regex_new()

struct Regex * regex_new ( const char *  str,
uint32_t  flags,
struct Buffer err 
)

Create an Regex from a string.

Parameters
strRegular expression
flagsType flags, e.g. D_REGEX_MATCH_CASE
errBuffer for error messages
Return values
ptrNew Regex object
NULLError

Definition at line 102 of file regex.c.

103{
104 if (!str)
105 return NULL;
106
107 int rflags = 0;
108 struct Regex *reg = MUTT_MEM_CALLOC(1, struct Regex);
109
110 reg->regex = MUTT_MEM_CALLOC(1, regex_t);
111 reg->pattern = mutt_str_dup(str);
112
113 /* Should we use smart case matching? */
114 if (((flags & D_REGEX_MATCH_CASE) == 0) && mutt_mb_is_lower(str))
115 rflags |= REG_ICASE;
116
117 if ((flags & D_REGEX_NOSUB))
118 rflags |= REG_NOSUB;
119
120 /* Is a prefix of '!' allowed? */
121 if (((flags & D_REGEX_ALLOW_NOT) != 0) && (str[0] == '!'))
122 {
123 reg->pat_not = true;
124 str++;
125 }
126
127 int rc = REG_COMP(reg->regex, str, rflags);
128 if (rc != 0)
129 {
130 if (err)
131 regerror(rc, reg->regex, err->data, err->dsize);
132 regex_free(&reg);
133 return NULL;
134 }
135
136 return reg;
137}
void regex_free(struct Regex **ptr)
Free a Regex object.
Definition: regex.c:68
bool mutt_mb_is_lower(const char *s)
Does a multi-byte string contain only lowercase characters?
Definition: mbyte.c:354
#define MUTT_MEM_CALLOC(n, type)
Definition: memory.h:40
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
#define REG_COMP(preg, regex, cflags)
Compile a regular expression.
Definition: regex3.h:50
size_t dsize
Length of data.
Definition: buffer.h:39
char * data
Pointer to data.
Definition: buffer.h:37
#define D_REGEX_ALLOW_NOT
Regex can begin with '!'.
Definition: types.h:107
#define D_REGEX_MATCH_CASE
Case-sensitive matching.
Definition: types.h:106
#define D_REGEX_NOSUB
Do not report what was matched (REG_NOSUB)
Definition: types.h:108
+ Here is the call graph for this function:
+ Here is the caller graph for this function: