Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:13:14

0001 // functions used to assist with  regular expression matching of strings
0002 
0003 #include "FWCore/Utilities/interface/RegexMatch.h"
0004 
0005 #include <boost/algorithm/string.hpp>
0006 #include <regex>
0007 
0008 namespace edm {
0009 
0010   // checks that a tainted string is valid.
0011   // Needed to satisfy Coverity.
0012   bool untaintString(char const* pattern, char const* regexp) {
0013     std::regex rexp(regexp);
0014     return std::regex_match(pattern, rexp);
0015   }
0016 
0017   bool is_glob(std::string const& pattern) { return (pattern.find_first_of("*?") != pattern.npos); }
0018 
0019   std::string glob2reg(std::string const& pattern) {
0020     std::string regexp = pattern;
0021     boost::replace_all(regexp, "*", ".*");
0022     boost::replace_all(regexp, "?", ".");
0023     return regexp;
0024   }
0025 
0026   std::vector<std::vector<std::string>::const_iterator> regexMatch(std::vector<std::string> const& strings,
0027                                                                    std::regex const& regexp) {
0028     std::vector<std::vector<std::string>::const_iterator> matches;
0029     for (std::vector<std::string>::const_iterator i = strings.begin(), iEnd = strings.end(); i != iEnd; ++i) {
0030       if (std::regex_match((*i), regexp)) {
0031         matches.push_back(i);
0032       }
0033     }
0034     return matches;
0035   }
0036 
0037   std::vector<std::vector<std::string>::const_iterator> regexMatch(std::vector<std::string> const& strings,
0038                                                                    std::string const& pattern) {
0039     std::regex regexp(glob2reg(pattern));
0040     return regexMatch(strings, regexp);
0041   }
0042 
0043 }  // namespace edm