libcamgm
PerlRegEx.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) 2005 Novell, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 *
10 * - Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * - Neither the name of Quest Software, Inc., Novell, Inc., nor the names of its
15 * contributors may be used to endorse or promote products derived from this
16 * software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL Quest Software, Inc., Novell, Inc., OR THE
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *******************************************************************************/
34 #ifndef CA_MGM_PERL_REGEX_HPP
35 #define CA_MGM_PERL_REGEX_HPP
36 
37 #include <ca-mgm/String.hpp>
38 #include <vector>
39 
40 #include <pcre.h>
41 
42 namespace ca_mgm
43 {
44 
55 class PerlRegEx
56 {
57 public:
63  typedef std::vector<int> MatchVector;
64 
66  struct match_t {
67  int rm_so;
68  int rm_eo;
69  };
70 
72  typedef std::vector<match_t> MatchArray;
73 
77  PerlRegEx();
78 
86  PerlRegEx(const std::string &regex, int cflags = 0);
87 
96  PerlRegEx(const PerlRegEx &ref);
97 
101  ~PerlRegEx();
102 
110  PerlRegEx& operator = (const PerlRegEx &ref);
111 
141  bool compile(const std::string &regex,
142  int cflags = 0);
143 
157  int errorCode();
158 
165  std::string errorString() const;
166 
170  std::string patternString() const;
171 
175  int compileFlags() const;
176 
180  bool isCompiled() const;
181 
268  bool execute(MatchVector &sub,
269  const std::string &str,
270  size_t index = 0,
271  size_t count = 0,
272  int eflags = 0);
273  bool execute(MatchArray &sub,
274  const std::string &str,
275  size_t index = 0,
276  size_t count = 0,
277  int eflags = 0);
278  /* @} */
279 
305  std::vector<std::string> capture(const std::string &str,
306  size_t index = 0,
307  size_t count = 0,
308  int eflags = 0);
309 
344  std::string replace(const std::string &str,
345  const std::string &rep,
346  bool global = false,
347  int eflags = 0);
348 
379  std::vector<std::string> split (const std::string &str,
380  bool empty = false,
381  int eflags = 0);
382 
408  std::vector<std::string> grep (const std::vector<std::string> &src,
409  int eflags = 0);
410 
436  bool match (const std::string &str,
437  size_t index = 0,
438  int eflags = 0) const;
439 
440 private:
441  pcre *m_pcre;
442  int m_flags;
443  mutable int m_ecode;
444  mutable std::string m_error;
445  std::string m_rxstr;
446 };
447 
448 } // End of BLOCXX_NAMESPACE
449 
450 
451 #endif // CA_MGM_PERL_REGEX_HPP
452 /* vim: set ts=8 sts=8 sw=8 ai noet: */
453 
std::string m_error
Definition: PerlRegEx.hpp:444
int rm_eo
end offset of the regex match
Definition: PerlRegEx.hpp:68
int m_flags
Definition: PerlRegEx.hpp:442
int rm_so
start offset of the regex match
Definition: PerlRegEx.hpp:67
bool isCompiled() const
int m_ecode
Definition: PerlRegEx.hpp:443
std::string m_rxstr
Definition: PerlRegEx.hpp:445
std::string patternString() const
std::vector< std::string > grep(const std::vector< std::string > &src, int eflags=0)
PerlRegEx & operator=(const PerlRegEx &ref)
std::vector< std::string > split(const std::string &str, bool empty=false, int eflags=0)
std::vector< match_t > MatchArray
POSIX RegEx like match array with captured substring offsets.
Definition: PerlRegEx.hpp:72
Definition: PerlRegEx.hpp:55
bool execute(MatchVector &sub, const std::string &str, size_t index=0, size_t count=0, int eflags=0)
int compileFlags() const
std::string replace(const std::string &str, const std::string &rep, bool global=false, int eflags=0)
POSIX RegEx like structure for captured substring offset pair.
Definition: PerlRegEx.hpp:66
pcre * m_pcre
Definition: PerlRegEx.hpp:441
bool compile(const std::string &regex, int cflags=0)
bool match(const std::string &str, size_t index=0, int eflags=0) const
std::vector< std::string > capture(const std::string &str, size_t index=0, size_t count=0, int eflags=0)
std::vector< int > MatchVector
Definition: PerlRegEx.hpp:63
std::string errorString() const
Definition: ByteBuffer.hpp:37