]> git.ipfire.org Git - thirdparty/squid.git/blame - src/base/RegexPattern.h
Source Format Enforcement (#1234)
[thirdparty/squid.git] / src / base / RegexPattern.h
CommitLineData
8fcefb30 1/*
b8ae064d 2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
8fcefb30
AJ
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9#ifndef SQUID_SRC_BASE_REGEXPATTERN_H
10#define SQUID_SRC_BASE_REGEXPATTERN_H
11
12#include "mem/forward.h"
0fa036e3 13#include "sbuf/SBuf.h"
8fcefb30 14
2e0942f9 15#if HAVE_REGEX_H
16#include <regex.h>
17#endif
18
8fcefb30
AJ
19/**
20 * A regular expression,
21 * plain text and compiled representations
22 */
23class RegexPattern
24{
25 MEMPROXY_CLASS(RegexPattern);
26
27public:
28 RegexPattern() = delete;
0fa036e3 29 RegexPattern(const SBuf &aPattern, int aFlags);
c2afddd8
AJ
30 ~RegexPattern();
31
0fa036e3 32 RegexPattern(RegexPattern &&) = delete; // no copying of any kind
c2afddd8 33
0fa036e3
AR
34 /// whether the regex differentiates letter case
35 bool caseSensitive() const { return !(flags & REG_ICASE); }
36
37 /// whether this is an "any single character" regex (".")
38 bool isDot() const { return pattern.length() == 1 && pattern[0] == '.'; }
b9a9207b 39
aee3523a 40 bool match(const char *str) const {return regexec(&regex,str,0,nullptr,0)==0;}
95b8eae2 41
0fa036e3
AR
42 /// Attempts to reproduce this regex (context-sensitive) configuration.
43 /// If the previous regex is nil, may not report default flags.
44 /// Otherwise, may not report same-as-previous flags (and prepends a space).
45 void print(std::ostream &os, const RegexPattern *previous = nullptr) const;
95b8eae2
AJ
46
47private:
0fa036e3
AR
48 /// a regular expression in the text form, suitable for regcomp(3)
49 SBuf pattern;
50
51 /// bitmask of REG_* flags for regcomp(3)
52 const int flags;
53
54 /// a "compiled pattern buffer" filled by regcomp(3) for regexec(3)
55 regex_t regex;
8fcefb30
AJ
56};
57
0fa036e3
AR
58inline std::ostream &
59operator <<(std::ostream &os, const RegexPattern &rp)
60{
61 rp.print(os);
62 return os;
63}
64
8fcefb30
AJ
65#endif /* SQUID_SRC_BASE_REGEXPATTERN_H */
66