simple_regex.h
Go to the documentation of this file.00001 #ifndef REGEX_HEADER_INCLUDED
00002 #define REGEX_HEADER_INCLUDED
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include <string>
00033 #include <regex.h>
00034
00035 namespace cxxtls
00036 {
00037
00038
00042
00043 class SimpleRegex
00054 {
00055 public:
00056
00057 struct match
00060 {
00061 int offset;
00062 int length;
00063 };
00064
00065 private:
00066
00067 regex_t compiled_expression;
00068
00069
00070 std::string expression_;
00071 std::string options_;
00072
00073 enum constants_
00074 {
00075 max_matches=20
00076 };
00077
00078 mutable match matches_[max_matches];
00079
00080 mutable char const *error_;
00081
00082 bool ok_;
00083
00084 mutable int match_count_;
00085
00086
00087 std::string expand(std::string const&, std::string const&) const
00089 ;
00090
00091 public:
00092
00093
00094 SimpleRegex(std::string const &expression="", std::string const &options="")
00103 ;
00104
00105
00106 SimpleRegex(char const *expression, char const *options="")
00115 ;
00116
00117 SimpleRegex(SimpleRegex const &r);
00118
00119 SimpleRegex &operator=(SimpleRegex const &r);
00120 SimpleRegex &operator=(std::string const &r);
00121 SimpleRegex &operator=(char const *r);
00122
00123
00124 void reset(std::string const &expression, std::string const &options)
00125 {
00126 this->SimpleRegex::~SimpleRegex();
00127
00128 new(this) SimpleRegex(expression, options);
00129
00130 }
00131
00132
00133 int operator() (char const *string, int stringLength) const;
00136
00137 int operator() (std::string const &s,
00138 int offset=0,
00139 int length=0x7ffffff) const
00142 ;
00143
00144 bool ok() const { return ok_ && error_ == 0; }
00145
00146 std::string const &options() const { return options_; }
00147
00148 char const *error() const { return error_; }
00149
00150 bool anchored() const
00152 ;
00153
00154 bool empty() const { return expression_.empty(); }
00156
00157 operator std::string const &() const { return expression_; }
00158
00159 ~SimpleRegex();
00160
00161 match const *matches() const { return matches_; }
00162
00163 std::string substitute(std::string const &original,
00164 std::string const &replacement) const
00185 ;
00186
00187 bool replace(std::string &rv,
00188 std::string const &source,
00189 std::string const &replacement,
00190 int maxIterations=0x7fffffff
00191 ) const
00195 ;
00196
00197 };
00198
00199 }
00200
00201 #endif