source: tags/arb_5.2/SL/REGEXPR/RegExpr.hxx

Last change on this file was 5803, checked in by westram, 15 years ago
  • added C++-Wrapper for regular expressions (REGEXPR.a)
File size: 2.6 KB
Line 
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_ASSERT_H
19#include <arb_assert.h>
20#endif
21#define re_assert(cond) arb_assert(cond)
22
23#ifndef _CPP_STRING
24#include <string>
25#endif
26
27struct GBS_regex;
28
29class RegMatch {
30    size_t start;
31    size_t behind_last;
32
33public:
34    bool didMatch() const { return start != std::string::npos; }
35
36    RegMatch() : start(std::string::npos), behind_last(std::string::npos) {}
37    RegMatch(size_t start_, size_t behind_last_)
38        : start(start_)
39        , behind_last(behind_last_)
40    {
41        re_assert(start != std::string::npos);
42        re_assert(behind_last != std::string::npos);
43        re_assert(start <= behind_last);
44    }
45
46    size_t pos() const { return start; }
47    size_t len() const { return behind_last-start; }
48
49    size_t posBehindMatch() const { return behind_last; }
50
51    std::string extract(const std::string& s) const {
52        re_assert(didMatch());
53        return s.substr(pos(), len());
54    }
55};
56
57
58class RegExpr : Noncopyable {
59    std::string expression;                         // the regular expression
60    bool        ignore_case;
61
62    mutable GBS_regex *comreg;                      // compiled regular expression (NULL if not compiled yet)
63    mutable RegMatch  *matches;                     // set by match (NULL if failed or not performed yet)
64
65    void compile() const;
66    void perform_match(const char *str, size_t offset) const;
67
68public:
69    RegExpr(const std::string& expression_, bool ignore_case);
70    ~RegExpr();
71
72    void test() const;
73    size_t subexpr_count() const;
74
75    // Note: calling 'match()' invalidates results from previous 'match()' and 'subexpr_match()'-calls
76    const RegMatch *match(const std::string& versus, size_t offset = 0) const;
77    const RegMatch *subexpr_match(size_t subnr) const; // get subexpression match from last 'match()'
78};
79
80#else
81#error RegExpr.hxx included twice
82#endif // REGEXPR_HXX
Note: See TracBrowser for help on using the repository browser.