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 { |
---|
55 | re_assert(didMatch()); |
---|
56 | return s.substr(pos(), len()); |
---|
57 | } |
---|
58 | }; |
---|
59 | |
---|
60 | |
---|
61 | class RegExpr : virtual Noncopyable { |
---|
62 | //! for regexpression format see http://help.arb-home.de/reg.html#Syntax_of_POSIX_extended_regular_expressions_as_used_in_ARB |
---|
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 |
---|