vul_reg_exp.h
Go to the documentation of this file.
1 // This is core/vul/vul_reg_exp.h
2 #ifndef vul_reg_exph
3 #define vul_reg_exph
4 //:
5 // \file
6 // \brief contains class for pattern matching with regular expressions
7 // \author Texas Instruments Incorporated.
8 //
9 // \verbatim
10 // Modifications
11 // PDA (Manchester) 21/03/2001: Tidied up the documentation
12 // Peter Vanroose 27/05/2001: Corrected the documentation
13 // Peter Vanroose 07/02/2002: brief doxygen comment placed on single line
14 // Peter Vanroose 13/06/2002: bug fix: crash in match() when startp==endp==0
15 // Ian Scott 08/06/2003: Add protect(char) function
16 // \endverbatim
17 //
18 // Original Copyright notice:
19 // Copyright (C) 1991 Texas Instruments Incorporated.
20 //
21 // Permission is granted to any individual or institution to use, copy, modify,
22 // and distribute this software, provided that this complete copyright and
23 // permission notice is maintained, intact, in all copies and supporting
24 // documentation.
25 //
26 // Texas Instruments Incorporated provides this software "as is" without
27 // express or implied warranty.
28 
29 #include <string>
30 #include <cstddef>
31 #ifdef _MSC_VER
32 # include <vcl_msvc_warnings.h>
33 #endif
34 
35 constexpr int vul_reg_exp_nsubexp = 10;
36 
37 //: Pattern matching with regular expressions.
38 // A regular expression allows a programmer to specify complex
39 // patterns that can be searched for and matched against the
40 // character string of a string object. In its simplest form, a
41 // regular expression is a sequence of characters used to search for
42 // exact character matches. However, many times the exact sequence to
43 // be found is not known, or only a match at the beginning or end of
44 // a string is desired. This regular expression class implements
45 // regular expression pattern matching as is found and implemented in
46 // many UNIX commands and utilities.
47 //
48 // Example: The perl code
49 // \code
50 // $filename =~ m"([a-z]+)\.cc";
51 // print $1;
52 // \endcode
53 // is written as follows in C++
54 // \code
55 // vul_reg_exp re("([a-z]+)\\.cc");
56 // re.find(filename);
57 // std::cout << re.match(1);
58 // \endcode
59 //
60 // The regular expression class provides a convenient mechanism for
61 // specifying and manipulating regular expressions. The regular
62 // expression object allows specification of such patterns by using
63 // the following regular expression metacharacters:
64 //
65 // - ^ Matches at beginning of a line
66 // - $ Matches at end of a line
67 // - . Matches any single character
68 // - [ ] Matches any character(s) inside the brackets
69 // - [^ ] Matches any character(s) not inside the brackets
70 // - [ - ] Matches any character in range on either side of a dash
71 // - * Matches preceding pattern zero or more times
72 // - + Matches preceding pattern one or more times
73 // - ? Matches preceding pattern at most once
74 // - () Saves a matched expression and uses it in a later match
75 //
76 // Note that more than one of these metacharacters can be used in a
77 // single regular expression in order to create complex search
78 // patterns. For example, the pattern [^ab1-9] says to match any
79 // character sequence that does not begin with the characters "a",
80 // "b", or the characters "1" through "9".
81 //
83 {
84  //: anchor point of start position for n-th matching regular expression
86  //: anchor point of end position for n-th matching regular expression
87  const char* endp[vul_reg_exp_nsubexp];
88  //: Internal use only
89  char regstart;
90  //: Internal use only
91  char reganch;
92  //: Internal use only
93  const char* regmust;
94  //: Internal use only
95  int regmlen;
96  char* program;
97  int progsize;
98  const char* searchstring;
99  public:
100  //: Creates an empty regular expression.
101  inline vul_reg_exp() : program(nullptr) { clear_bufs(); }
102  //: Creates a regular expression from string s, and compiles s.
103  inline vul_reg_exp(char const* s) : program(nullptr) { clear_bufs(); compile(s); }
104  //: Copy constructor
105  vul_reg_exp(vul_reg_exp const&);
106  //: Frees space allocated for regular expression.
107  inline ~vul_reg_exp() { delete[] this->program; }
108  //: Compiles char* --> regexp
109  void compile(char const*);
110  //: true if regexp in char* arg
111  bool find(char const*);
112  //: true if regexp in char* arg
113  bool find(std::string const&);
114  //: Returns the start index of the last item found.
115  inline std::ptrdiff_t start() const { return this->startp[0] - searchstring; }
116  //: Returns the end index of the last item found.
117  inline std::ptrdiff_t end() const { return this->endp[0] - searchstring; }
118  //: Equality operator
119  bool operator==(vul_reg_exp const&) const;
120  //: Inequality operator
121  inline bool operator!=(vul_reg_exp const& r) const { return !operator==(r); }
122  //: Same regexp and state?
123  bool deep_equal(vul_reg_exp const&) const;
124  //: Returns true if a valid RE is compiled and ready for pattern matching.
125  inline bool is_valid() const { return this->program != nullptr; }
126  //: Invalidates regular expression.
127  inline void set_invalid() { delete[] this->program; this->program = nullptr; clear_bufs(); }
128 
129  //: Return start index of nth submatch.
130  // start(0) is the start of the full match.
131  inline std::ptrdiff_t start(long n) const { return this->startp[n] - searchstring; }
132  //: Return end index of nth submatch.
133  // end(0) is the end of the full match.
134  inline std::ptrdiff_t end(long n) const { return this->endp[n] - searchstring; }
135  //: Return nth submatch as a string.
136  std::string match(int n) const {
137  return this->endp[n] == this->startp[n] ? std::string("") :
138  std::string(this->startp[n], this->endp[n] - this->startp[n]);
139  }
140  //: Return an expression that will match precisely c
141  // The returned string is owned by the function, and
142  // will be overwritten in subsequent calls.
143  static const char * protect(char c);
144 
145  private:
146  //: private function to clear startp[] and endp[]
147  void clear_bufs() { for (int n=0; n<vul_reg_exp_nsubexp; ++n) startp[n]=endp[n]=nullptr; }
148 };
149 
150 #endif // vul_reg_exph
const char * startp[vul_reg_exp_nsubexp]
anchor point of start position for n-th matching regular expression.
Definition: vul_reg_exp.h:85
Pattern matching with regular expressions.
Definition: vul_reg_exp.h:82
char regstart
Internal use only.
Definition: vul_reg_exp.h:89
bool find(char const *)
true if regexp in char* arg.
bool operator!=(vul_reg_exp const &r) const
Inequality operator.
Definition: vul_reg_exp.h:121
static const char * protect(char c)
Return an expression that will match precisely c.
const char * regmust
Internal use only.
Definition: vul_reg_exp.h:93
std::ptrdiff_t end() const
Returns the end index of the last item found.
Definition: vul_reg_exp.h:117
std::ptrdiff_t end(long n) const
Return end index of nth submatch.
Definition: vul_reg_exp.h:134
bool operator==(vul_reg_exp const &) const
Equality operator.
const char * endp[vul_reg_exp_nsubexp]
anchor point of end position for n-th matching regular expression.
Definition: vul_reg_exp.h:87
bool is_valid() const
Returns true if a valid RE is compiled and ready for pattern matching.
Definition: vul_reg_exp.h:125
~vul_reg_exp()
Frees space allocated for regular expression.
Definition: vul_reg_exp.h:107
char * program
Definition: vul_reg_exp.h:96
void clear_bufs()
private function to clear startp[] and endp[].
Definition: vul_reg_exp.h:147
const char * searchstring
Definition: vul_reg_exp.h:98
vul_reg_exp(char const *s)
Creates a regular expression from string s, and compiles s.
Definition: vul_reg_exp.h:103
constexpr int vul_reg_exp_nsubexp
Definition: vul_reg_exp.h:35
std::ptrdiff_t start() const
Returns the start index of the last item found.
Definition: vul_reg_exp.h:115
bool deep_equal(vul_reg_exp const &) const
Same regexp and state?.
int regmlen
Internal use only.
Definition: vul_reg_exp.h:95
std::ptrdiff_t start(long n) const
Return start index of nth submatch.
Definition: vul_reg_exp.h:131
void set_invalid()
Invalidates regular expression.
Definition: vul_reg_exp.h:127
vul_reg_exp()
Creates an empty regular expression.
Definition: vul_reg_exp.h:101
char reganch
Internal use only.
Definition: vul_reg_exp.h:91
std::string match(int n) const
Return nth submatch as a string.
Definition: vul_reg_exp.h:136
void compile(char const *)
Compiles char* --> regexp.