NeoMutt  2023-05-17-33-gce4425
Teaching an old dog new tricks
DOXYGEN
regex2.h File Reference

Type representing a regular expression. More...

#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

void regex_free (struct Regex **regex)
 Free a Regex object. More...
 
struct Regexregex_new (const char *str, uint32_t flags, struct Buffer *err)
 Create an Regex from a string. More...
 

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_free()

void regex_free ( struct Regex **  r)

Free a Regex object.

Parameters
[out]rRegex to free

Definition at line 48 of file regex.c.

49{
50 if (!r || !*r)
51 return;
52
53 FREE(&(*r)->pattern);
54 if ((*r)->regex)
55 regfree((*r)->regex);
56 FREE(&(*r)->regex);
57 FREE(r);
58}
#define FREE(x)
Definition: memory.h:43
+ 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. DT_REGEX_MATCH_CASE
errBuffer for error messages
Return values
ptrNew Regex object
NULLError

Definition at line 80 of file regex.c.

81{
82 if (!str)
83 return NULL;
84
85 int rflags = 0;
86 struct Regex *reg = mutt_mem_calloc(1, sizeof(struct Regex));
87
88 reg->regex = mutt_mem_calloc(1, sizeof(regex_t));
89 reg->pattern = mutt_str_dup(str);
90
91 /* Should we use smart case matching? */
92 if (((flags & DT_REGEX_MATCH_CASE) == 0) && mutt_mb_is_lower(str))
93 rflags |= REG_ICASE;
94
95 if ((flags & DT_REGEX_NOSUB))
96 rflags |= REG_NOSUB;
97
98 /* Is a prefix of '!' allowed? */
99 if (((flags & DT_REGEX_ALLOW_NOT) != 0) && (str[0] == '!'))
100 {
101 reg->pat_not = true;
102 str++;
103 }
104
105 int rc = REG_COMP(reg->regex, str, rflags);
106 if (rc != 0)
107 {
108 if (err)
109 regerror(rc, reg->regex, err->data, err->dsize);
110 regex_free(&reg);
111 return NULL;
112 }
113
114 return reg;
115}
void regex_free(struct Regex **r)
Free a Regex object.
Definition: regex.c:48
bool mutt_mb_is_lower(const char *s)
Does a multi-byte string contain only lowercase characters?
Definition: mbyte.c:357
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:251
#define DT_REGEX_ALLOW_NOT
Regex can begin with '!'.
Definition: regex3.h:36
#define DT_REGEX_MATCH_CASE
Case-sensitive matching.
Definition: regex3.h:35
#define REG_COMP(preg, regex, cflags)
Compile a regular expression.
Definition: regex3.h:53
#define DT_REGEX_NOSUB
Do not report what was matched (REG_NOSUB)
Definition: regex3.h:37
size_t dsize
Length of data.
Definition: buffer.h:37
char * data
Pointer to data.
Definition: buffer.h:35
Cached regular expression.
Definition: regex3.h:89
char * pattern
printable version
Definition: regex3.h:90
bool pat_not
do not match
Definition: regex3.h:92
regex_t * regex
compiled expression
Definition: regex3.h:91
+ Here is the call graph for this function:
+ Here is the caller graph for this function: