summaryrefslogtreecommitdiff
path: root/examples/dte/regexp.h
diff options
context:
space:
mode:
authorMitja Felicijan <mitja.felicijan@gmail.com>2023-11-09 23:19:53 +0100
committerMitja Felicijan <mitja.felicijan@gmail.com>2023-11-09 23:19:53 +0100
commit1566b6faa8534118c3566188181367cd0868468f (patch)
tree1de8d4b369efb5e592685a31088f798a6b63ffa1 /examples/dte/regexp.h
parent349991bf6efe473ab9a5cbdae0a8114d72b997e3 (diff)
downloadcrep-1566b6faa8534118c3566188181367cd0868468f.tar.gz
Added partial matching and introduced threads
Diffstat (limited to 'examples/dte/regexp.h')
-rw-r--r--examples/dte/regexp.h85
1 files changed, 85 insertions, 0 deletions
diff --git a/examples/dte/regexp.h b/examples/dte/regexp.h
new file mode 100644
index 0000000..50fdabb
--- /dev/null
+++ b/examples/dte/regexp.h
@@ -0,0 +1,85 @@
1#ifndef REGEXP_H
2#define REGEXP_H
3
4#include <regex.h>
5#include <stdbool.h>
6#include <stddef.h>
7#include "util/macros.h"
8
9enum {
10#ifdef REG_ENHANCED
11 // The REG_ENHANCED flag enables various extensions on macOS
12 // (see "enhanced features" in re_format(7)). Most of these
13 // extensions are enabled by default on Linux (in both glibc
14 // and musl) without the need for any extra flags.
15 DEFAULT_REGEX_FLAGS = REG_EXTENDED | REG_ENHANCED,
16#else
17 // POSIX Extended Regular Expressions (ERE) are used almost
18 // everywhere in this codebase, except where Basic Regular
19 // Expressions (BRE) are explicitly called for (most notably
20 // in search_tag(), which is used for ctags patterns).
21 DEFAULT_REGEX_FLAGS = REG_EXTENDED,
22#endif
23};
24
25typedef struct {
26 regex_t re;
27 char str[];
28} CachedRegexp;
29
30typedef struct {
31 char *str;
32 regex_t re;
33} InternedRegexp;
34
35// Platform-specific patterns for matching word boundaries, as detected
36// and initialized by regexp_init_word_boundary_tokens()
37typedef struct {
38 char start[8];
39 char end[8];
40} RegexpWordBoundaryTokens;
41
42bool regexp_compile_internal(regex_t *re, const char *pattern, int flags) WARN_UNUSED_RESULT;
43
44WARN_UNUSED_RESULT
45static inline bool regexp_compile(regex_t *re, const char *pattern, int flags)
46{
47 return regexp_compile_internal(re, pattern, flags | DEFAULT_REGEX_FLAGS);
48}
49
50WARN_UNUSED_RESULT
51static inline bool regexp_compile_basic(regex_t *re, const char *pattern, int flags)
52{
53 return regexp_compile_internal(re, pattern, flags);
54}
55
56WARN_UNUSED_RESULT
57static inline bool regexp_is_valid(const char *pattern, int flags)
58{
59 regex_t re;
60 if (!regexp_compile(&re, pattern, flags | REG_NOSUB)) {
61 return false;
62 }
63 regfree(&re);
64 return true;
65}
66
67void regexp_compile_or_fatal_error(regex_t *re, const char *pattern, int flags);
68bool regexp_init_word_boundary_tokens(RegexpWordBoundaryTokens *rwbt);
69bool regexp_error_msg(const regex_t *re, const char *pattern, int err);
70void free_cached_regexp(CachedRegexp *cr);
71
72const InternedRegexp *regexp_intern(const char *pattern);
73bool regexp_is_interned(const char *pattern);
74void free_interned_regexps(void);
75
76bool regexp_exec (
77 const regex_t *re,
78 const char *buf,
79 size_t size,
80 size_t nmatch,
81 regmatch_t *pmatch,
82 int flags
83) WARN_UNUSED_RESULT;
84
85#endif