dte test coverage


Directory: ./
File: src/regexp.h
Date: 2025-02-14 16:55:22
Exec Total Coverage
Lines: 8 8 100.0%
Functions: 2 2 100.0%
Branches: 2 6 33.3%

Line Branch Exec Source
1 #ifndef REGEXP_H
2 #define REGEXP_H
3
4 #include <regex.h>
5 #include <stdbool.h>
6 #include <stddef.h>
7 #include "command/error.h"
8 #include "util/macros.h"
9
10 enum {
11 #ifdef REG_ENHANCED
12 // The REG_ENHANCED flag enables various extensions on macOS
13 // (see "enhanced features" in re_format(7)). Most of these
14 // extensions are enabled by default on Linux (in both glibc
15 // and musl) without the need for any extra flags.
16 DEFAULT_REGEX_FLAGS = REG_EXTENDED | REG_ENHANCED,
17 #else
18 // POSIX Extended Regular Expressions (ERE) are used almost
19 // everywhere in this codebase, except where Basic Regular
20 // Expressions (BRE) are explicitly called for (most notably
21 // in search_tag(), which is used for ctags patterns).
22 DEFAULT_REGEX_FLAGS = REG_EXTENDED,
23 #endif
24 };
25
26 typedef struct {
27 const char *str; // Pattern string, interned by str_intern()
28 regex_t re; // regex(3) object, compiled with regcomp(3)
29 } InternedRegexp;
30
31 // Platform-specific patterns for matching word boundaries, as detected
32 // and initialized by regexp_init_word_boundary_tokens()
33 typedef struct {
34 char start[8];
35 char end[8];
36 } RegexpWordBoundaryTokens;
37
38 void regexp_compile_or_fatal_error(regex_t *re, const char *pattern, int flags) NONNULL_ARGS;
39 bool regexp_init_word_boundary_tokens(RegexpWordBoundaryTokens *rwbt) NONNULL_ARGS;
40 bool regexp_error_msg(ErrorBuffer *ebuf, const regex_t *re, const char *pattern, int err) NONNULL_ARG(2, 3);
41 char *regexp_escape(const char *pattern, size_t len) NONNULL_ARGS WARN_UNUSED_RESULT;
42 size_t regexp_escapeb(char *buf, size_t buflen, const char *pat, size_t plen) NONNULL_ARG(1) NONNULL_ARG_IF_NONZERO_LENGTH(3, 4);
43
44 const InternedRegexp *regexp_intern(ErrorBuffer *ebuf, const char *pattern) NONNULL_ARG(2) WARN_UNUSED_RESULT;
45 bool regexp_is_interned(const char *pattern) NONNULL_ARGS;
46 void free_interned_regexps(void);
47
48 WARN_UNUSED_RESULT NONNULL_ARGS
49 bool regexp_exec (
50 const regex_t *re,
51 const char *buf,
52 size_t size,
53 size_t nmatch,
54 regmatch_t *pmatch,
55 int flags
56 );
57
58 WARN_UNUSED_RESULT NONNULL_ARG(2, 3)
59 204 static inline bool regexp_compile(ErrorBuffer *ebuf, regex_t *re, const char *pattern, int flags)
60 {
61 204 int err = regcomp(re, pattern, flags | DEFAULT_REGEX_FLAGS);
62
1/4
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→7) taken 204 times.
✗ Branch 2 (5→6) not taken.
✗ Branch 3 (5→7) not taken.
204 return !err || regexp_error_msg(ebuf, re, pattern, err);
63 }
64
65 WARN_UNUSED_RESULT NONNULL_ARG(2)
66 38 static inline bool regexp_is_valid(ErrorBuffer *ebuf, const char *pattern, int flags)
67 {
68 38 regex_t re;
69
1/2
✓ Branch 0 (3→4) taken 38 times.
✗ Branch 1 (3→6) not taken.
38 if (!regexp_compile(ebuf, &re, pattern, flags | REG_NOSUB)) {
70 return false;
71 }
72 38 regfree(&re);
73 38 return true;
74 }
75
76 #endif
77