00001 // 00002 // C++ Interface: RegexPreProcessor 00003 // 00004 // Description: performs operations or inspections on a string representing 00005 // a valid regular expression 00006 // 00007 // Author: Lorenzo Bettini <http://www.lorenzobettini.it>, (C) 1999-2007 00008 // 00009 // Copyright: See COPYING file that comes with this distribution 00010 // 00011 // 00012 #ifndef REGEXPREPROCESSOR_H 00013 #define REGEXPREPROCESSOR_H 00014 00015 #include <string> 00016 #include <list> 00017 #include <utility> 00018 #include <vector> 00019 #include <boost/regex.hpp> 00020 00021 namespace srchilite { 00022 00027 struct subexpressions_info { 00028 const static std::string ERR_OUTER_UNMARKED; 00029 const static std::string ERR_NESTED_SUBEXP; 00030 const static std::string ERR_UNBALANCED_PAREN; 00031 const static std::string ERR_OUTSIDE_SUBEXP; 00032 00034 unsigned int marked; 00036 std::string errors; 00037 00038 subexpressions_info() : 00039 marked(0) { 00040 } 00041 }; 00042 00046 typedef std::list<std::string> subexpressions_strings; 00047 00053 typedef std::pair<int, int> backreference_info; 00054 00058 typedef std::vector<std::string> backreference_replacements; 00059 00063 typedef boost::match_results<std::string::const_iterator> regex_match_results; 00064 00068 class RegexPreProcessor { 00069 public: 00070 RegexPreProcessor(); 00071 00072 ~RegexPreProcessor(); 00073 00078 static const std::string preprocess(const std::string &s); 00079 00085 static const std::string make_nonsensitive(const std::string &s); 00086 00093 static unsigned int num_of_subexpressions(const std::string &s); 00094 00108 static subexpressions_info num_of_marked_subexpressions( 00109 const std::string &s, bool allow_outer_char = false, 00110 bool allow_outer_nonmarked = false); 00111 00121 static const subexpressions_strings *split_marked_subexpressions( 00122 const std::string &s); 00123 00132 static bool contains_backreferences(const std::string &s); 00133 00141 static backreference_info num_of_backreferences(const std::string &s); 00142 00151 static backreference_info num_of_references(const std::string &s); 00152 00164 static const std::string replace_backreferences( 00165 const std::string &original, 00166 const backreference_replacements &replace); 00167 00181 static const std::string replace_backreferences( 00182 const std::string &original, const regex_match_results &results); 00183 00193 static const std::string replace_references( 00194 const std::string &original, 00195 const backreference_replacements &replace); 00196 00208 static const std::string replace_references( 00209 const std::string &original, const regex_match_results &results); 00210 00211 }; 00212 00213 } 00214 00215 #endif