Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:56:19

0001 #ifndef GENERS_REGEX_HH_
0002 #define GENERS_REGEX_HH_
0003 
0004 #include "Alignment/Geners/interface/CPP11_config.hh"
0005 
0006 #ifdef CPP11_STD_AVAILABLE
0007 
0008 // Use C++11 regex matching
0009 
0010 #include <regex>
0011 
0012 namespace gs {
0013   typedef std::regex Regex;
0014 }
0015 
0016 #else  // CPP11_STD_AVAILABLE
0017 
0018 // Use POSIX extended regex matching
0019 
0020 #include "Alignment/Geners/interface/IOException.hh"
0021 #include <cassert>
0022 #include <regex.h>
0023 #include <string>
0024 #include <sys/types.h>
0025 
0026 namespace gs {
0027   class SearchSpecifier;
0028 
0029   class Regex {
0030     friend class SearchSpecifier;
0031 
0032     std::string re_;
0033     mutable regex_t *preg_;
0034 
0035     inline void cleanup() {
0036       if (preg_) {
0037         regfree(preg_);
0038         delete preg_;
0039         preg_ = 0;
0040       }
0041     }
0042 
0043     inline bool matches(const std::string &sentence) const {
0044       if (!preg_) {
0045         preg_ = new regex_t();
0046         assert(!regcomp(preg_, re_.c_str(), REG_EXTENDED | REG_NOSUB));
0047       }
0048       return regexec(preg_, sentence.c_str(), 0, 0, 0) == 0;
0049     }
0050 
0051   public:
0052     inline Regex() : preg_(0) {}
0053 
0054     inline Regex(const std::string &re) : re_(re), preg_(new regex_t()) {
0055       const int statusCode = regcomp(preg_, re_.c_str(), REG_EXTENDED | REG_NOSUB);
0056       if (statusCode) {
0057         const size_t n = regerror(statusCode, preg_, 0, 0);
0058         std::string s;
0059         s.resize(n + 1);
0060         regerror(statusCode, preg_, const_cast<char *>(s.data()), n + 1);
0061         cleanup();
0062         throw gs::IOInvalidArgument(s.data());
0063       }
0064     }
0065 
0066     inline Regex(const Regex &r) : re_(r.re_), preg_(0) {}
0067 
0068     inline Regex &operator=(const Regex &r) {
0069       if (&r == this)
0070         return *this;
0071       re_ = r.re_;
0072       cleanup();
0073       return *this;
0074     }
0075 
0076     inline ~Regex() { cleanup(); }
0077   };
0078 }  // namespace gs
0079 
0080 #endif  // CPP11_STD_AVAILABLE
0081 #endif  // GENERS_REGEX_HH_