ARB
RegExpr.hxx
Go to the documentation of this file.
1 // ============================================================= //
2 // //
3 // File : RegExpr.hxx //
4 // Purpose : Wrapper for ARBDB regular expressions //
5 // //
6 // Coded by Ralf Westram (coder@reallysoft.de) in April 2009 //
7 // Institute of Microbiology (Technical University Munich) //
8 // http://www.arb-home.de/ //
9 // //
10 // ============================================================= //
11 
12 #ifndef REGEXPR_HXX
13 #define REGEXPR_HXX
14 
15 #ifndef ARBTOOLS_H
16 #include <arbtools.h>
17 #endif
18 #ifndef ARB_MSG_H
19 #include <arb_msg.h>
20 #endif
21 #ifndef ARB_ASSERT_H
22 #include <arb_assert.h>
23 #endif
24 #define re_assert(cond) arb_assert(cond)
25 
26 #ifndef _GLIBCXX_STRING
27 #include <string>
28 #endif
29 
30 struct GBS_regex;
31 
32 class RegMatch {
33  size_t start;
34  size_t behind_last;
35 
36 public:
37  bool didMatch() const { return start != std::string::npos; }
38 
39  RegMatch() : start(std::string::npos), behind_last(std::string::npos) {}
40  RegMatch(size_t start_, size_t behind_last_) :
41  start(start_),
42  behind_last(behind_last_)
43  {
44  re_assert(start != std::string::npos);
45  re_assert(behind_last != std::string::npos);
46  re_assert(start <= behind_last);
47  }
48 
49  size_t pos() const { return start; }
50  size_t len() const { return behind_last-start; }
51 
52  size_t posBehindMatch() const { return behind_last; }
53 
54  std::string extract(const std::string& s) const {
56  return s.substr(pos(), len());
57  }
58 };
59 
60 
61 class RegExpr : virtual Noncopyable {
63 
64  std::string expression; // the regular expression
65  bool ignore_case;
66 
67  mutable GBS_regex *comreg; // compiled regular expression (NULp if not compiled yet)
68  mutable RegMatch *matches; // set by match (NULp if failed or not performed yet)
69  mutable std::string *failure; // set when regular expression fails to compile
70 
71  __ATTR__USERESULT bool compile() const;
72  void perform_match(const char *str, size_t offset) const;
73 
74 public:
75  RegExpr(const std::string& expression_, bool ignore_case);
76  ~RegExpr();
77 
78  const std::string *has_failed() const { if (!failure) IGNORE_RESULT(compile()); return failure; }
79  GB_ERROR get_error() const {
80  const std::string *failed = has_failed();
81  return failed ? GBS_global_string("Invalid RegExpr '%s' (Reason: %s)", expression.c_str(), failed->c_str()) : NULp;
82  }
83 
84  // all functions below may fail if expression fails to compile (done lazy)
85  // (not only) in case of failure they return NULp or 0
86 
87  size_t subexpr_count() const;
88 
89  // Note: calling 'match()' invalidates results from previous 'match()' and 'subexpr_match()'-calls
90  const RegMatch *match(const std::string& versus, size_t offset = 0) const;
91  const RegMatch *subexpr_match(size_t subnr) const; // get subexpression match from last 'match()'
92 };
93 
94 #else
95 #error RegExpr.hxx included twice
96 #endif // REGEXPR_HXX
const RegMatch * subexpr_match(size_t subnr) const
Definition: RegExpr.cxx:97
size_t len() const
Definition: RegExpr.hxx:50
~RegExpr()
Definition: RegExpr.cxx:31
size_t posBehindMatch() const
Definition: RegExpr.hxx:52
return string(buffer, length)
GB_ERROR get_error() const
Definition: RegExpr.hxx:79
const char * GBS_global_string(const char *templat,...)
Definition: arb_msg.cxx:203
size_t pos() const
Definition: RegExpr.hxx:49
STL namespace.
RegExpr(const std::string &expression_, bool ignore_case)
Definition: RegExpr.cxx:23
bool didMatch() const
Definition: RegExpr.hxx:37
RegMatch()
Definition: RegExpr.hxx:39
RegMatch(size_t start_, size_t behind_last_)
Definition: RegExpr.hxx:40
const RegMatch * match(const std::string &versus, size_t offset=0) const
Definition: RegExpr.cxx:80
const std::string * has_failed() const
Definition: RegExpr.hxx:78
#define __ATTR__USERESULT
Definition: attributes.h:58
#define re_assert(cond)
Definition: RegExpr.hxx:24
#define NULp
Definition: cxxforward.h:116
#define offset(field)
Definition: GLwDrawA.c:73
std::string extract(const std::string &s) const
Definition: RegExpr.hxx:54
size_t subexpr_count() const
Definition: RegExpr.cxx:89
GB_write_int const char s
Definition: AW_awar.cxx:154