Debian Codegraph @

src/regengine.h

raw symbols SCIP
1 /***************************************************************************
2 * Copyright (C) 2015-2023 by Hans-Peter Deifel *
3 * [email protected] *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
18 * Boston, MA 02110-1301 USA. *
19 ***************************************************************************/
20
21 #ifndef REGENGINE_H
22 #define REGENGINE_H
23
24 #include "config.h"
25
26 #include <stdlib.h>
27 #include <sys/types.h>
28 #include <regex.h>
29 #ifdef HAVE_LIBPCRE
30 #define PCRE2_CODE_UNIT_WIDTH 8
31 #include <pcre2.h>
32 #endif
33 #include <vector>
34 #include <string>
35 #include <memory>
36
37
38 struct match;
39
40 class Regengine
41 {
42 public:
43 // writes the match data to m. Returns true on success and false on failure
44 virtual bool exec(const std::string &str, size_t offset, struct match &m) const = 0;
45 virtual ~Regengine() {}
46 };
47
48 // This matches the union of a set of patterns
49 //
50 // It just tries all patterns in turn. This could be more efficient by using
51 // some engine-specific way of combining patterns, e.g with "|" in the posix
52 // case.
53 class PatternList : public Regengine
54 {
55 public:
56 PatternList() {}
57 ~PatternList() {}
58 bool exec(const std::string &str, size_t offset, struct match &m) const override;
59 void add_pattern(std::unique_ptr<Regengine> pattern);
60 private:
61 std::vector<std::unique_ptr<Regengine>> patterns;
62 };
63
64 class PosixRegex : public Regengine
65 {
66 public:
67 PosixRegex(const std::string &pattern, bool case_insensitive);
68 ~PosixRegex();
69 bool exec(const std::string &str, size_t offset, struct match &m) const override;
70 private:
71 regex_t regex;
72 };
73
74 #ifdef HAVE_LIBPCRE
75 class PCRERegex : public Regengine
76 {
77 public:
78 PCRERegex(const std::string &pattern, bool case_insensitive);
79 ~PCRERegex();
80 bool exec(const std::string &str, size_t offset, struct match &m) const override;
81 private:
82 pcre2_code *regex;
83 };
84 #endif
85
86 class FixedString : public Regengine
87 {
88 public:
89 FixedString(const std::string &pattern, bool case_insensitive);
90 bool exec(const std::string &str, size_t offset, struct match &m) const override;
91 private:
92 std::vector<std::string> patterns;
93 bool case_insensitive;
94 };
95
96 #endif /* REGENGINE_H */
97
98 /* Local Variables: */
99 /* mode: c++ */
100 /* End: */