Remove dynamic exceptions
[anna.git] / include / anna / core / util / RegularExpression.hpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 #ifndef anna_core_util_RegularExpression_hpp
10 #define anna_core_util_RegularExpression_hpp
11
12
13 // STL
14 #include <string>
15
16 // C
17 #include <regex.h>
18
19 #include <anna/core/RuntimeException.hpp>
20
21
22 namespace anna {
23
24 /**
25 * Class helper to manage regular expressions with efficiency (first compile & keep pattern, then reuse match procedure with different values)
26 */
27 class RegularExpression {
28
29   std::string a_pattern;
30
31   bool a_compiled;
32   regex_t a_preg;
33
34
35   void freeRegex() ;
36   void compile() noexcept(false);
37
38 public:
39
40   RegularExpression(const std::string & pattern = "") : a_pattern(pattern), a_compiled(false) {};
41   ~RegularExpression() { freeRegex(); }
42
43
44   // set
45
46   /**
47   * Set the pattern for regular expression
48   *
49   * @param pattern Pattern
50   */
51   void setPattern(const std::string & pattern) ;
52
53
54   // get
55
56   /**
57   * Get the current pattern for the regular expression
58   *
59   * @return Pattern
60   */
61   const std::string & getPattern(void) const { return a_pattern; }
62
63
64   // helpers
65   /**
66   * Check if value fulfill regular expression
67   *
68   * @return Boolean about if value provided match regular expression
69   */
70   bool isLike(const std::string & value) ;
71
72   /**
73   * Same as #isLike
74   */
75   bool match(const std::string & value) { return isLike(value); }
76
77   /**
78   * Operator ==
79   *
80   * @param re Instance from RegularExpression class
81   *
82   * @return Returns re == this comparison based on private 'pattern' member
83   */
84   bool operator == (const RegularExpression & re) const;
85
86   /**
87   * Operator <
88   *
89   * @param re Instance from RegularExpression class
90   *
91   * @return Returns re < this comparison based on private 'pattern' member
92   */
93   bool operator < (const RegularExpression & re) const;
94 };
95
96 };
97
98
99 #endif