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 _GLIBCXX_STRING |
---|
24 | #include <string> |
---|
25 | #endif |
---|
26 | |
---|
27 | struct GBS_regex; |
---|
28 | |
---|
29 | class RegMatch { |
---|
30 | size_t start; |
---|
31 | size_t behind_last; |
---|
32 | |
---|
33 | public: |
---|
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 | |
---|
58 | class RegExpr : virtual Noncopyable { |
---|
59 | //! for regexpression format see http://help.arb-home.de/reg.html#Syntax_of_POSIX_extended_regular_expressions_as_used_in_ARB |
---|
60 | |
---|
61 | std::string expression; // the regular expression |
---|
62 | bool ignore_case; |
---|
63 | |
---|
64 | mutable GBS_regex *comreg; // compiled regular expression (NULL if not compiled yet) |
---|
65 | mutable RegMatch *matches; // set by match (NULL if failed or not performed yet) |
---|
66 | |
---|
67 | void compile() const; |
---|
68 | void perform_match(const char *str, size_t offset) const; |
---|
69 | |
---|
70 | public: |
---|
71 | RegExpr(const std::string& expression_, bool ignore_case); |
---|
72 | ~RegExpr(); |
---|
73 | |
---|
74 | size_t subexpr_count() const; |
---|
75 | |
---|
76 | // Note: calling 'match()' invalidates results from previous 'match()' and 'subexpr_match()'-calls |
---|
77 | const RegMatch *match(const std::string& versus, size_t offset = 0) const; |
---|
78 | const RegMatch *subexpr_match(size_t subnr) const; // get subexpression match from last 'match()' |
---|
79 | }; |
---|
80 | |
---|
81 | #else |
---|
82 | #error RegExpr.hxx included twice |
---|
83 | #endif // REGEXPR_HXX |
---|