]>
Commit | Line | Data |
---|---|---|
3ebc8300 | 1 | /* |
1f7b830e | 2 | * Copyright (C) 1996-2025 The Squid Software Foundation and contributors |
3ebc8300 | 3 | * |
bbc27441 AJ |
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. | |
3ebc8300 FC |
7 | */ |
8 | ||
9 | #include "squid.h" | |
8fcefb30 | 10 | #include "base/RegexPattern.h" |
0fa036e3 | 11 | #include "base/TextException.h" |
675b8408 | 12 | #include "debug/Stream.h" |
0fa036e3 AR |
13 | #include "sbuf/Stream.h" |
14 | ||
15 | #include <iostream> | |
6226856f | 16 | #include <utility> |
3ebc8300 | 17 | |
0fa036e3 AR |
18 | RegexPattern::RegexPattern(const SBuf &aPattern, const int aFlags): |
19 | pattern(aPattern), | |
20 | flags(aFlags) | |
c2afddd8 | 21 | { |
0fa036e3 AR |
22 | memset(®ex, 0, sizeof(regex)); // paranoid; POSIX does not require this |
23 | if (const auto errCode = regcomp(®ex, pattern.c_str(), flags)) { | |
24 | char errBuf[256]; | |
25 | // for simplicity, ignore any error message truncation | |
26 | (void)regerror(errCode, ®ex, errBuf, sizeof(errBuf)); | |
27 | // POSIX examples show no regfree(®ex) after a regcomp() error; | |
28 | // presumably, regcom() frees any allocated memory on failures | |
29 | throw TextException(ToSBuf("POSIX regcomp(3) failure: (", errCode, ") ", errBuf, | |
30 | Debug::Extra, "regular expression: ", pattern), Here()); | |
31 | } | |
c2afddd8 | 32 | |
0fa036e3 | 33 | debugs(28, 2, *this); |
c2afddd8 AJ |
34 | } |
35 | ||
36 | RegexPattern::~RegexPattern() | |
37 | { | |
c2afddd8 AJ |
38 | regfree(®ex); |
39 | } | |
388e5028 | 40 | |
0fa036e3 AR |
41 | void |
42 | RegexPattern::print(std::ostream &os, const RegexPattern * const previous) const | |
b9a9207b | 43 | { |
0fa036e3 AR |
44 | // report context-dependent explicit options and delimiters |
45 | if (!previous) { | |
46 | // do not report default settings | |
47 | if (!caseSensitive()) | |
48 | os << "-i "; | |
49 | } else { | |
50 | os << ' '; // separate us from the previous value | |
51 | ||
52 | // do not report same-as-previous (i.e. inherited) settings | |
53 | if (previous->flags != flags) | |
54 | os << (caseSensitive() ? "+i " : "-i "); | |
55 | } | |
56 | ||
57 | os << pattern; | |
b9a9207b | 58 | } |
3e8d4ad8 | 59 |