Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/Geant4/tools/smatch is written in an unsupported language. File is not indexed.

0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003 
0004 #ifndef tools_smatch
0005 #define tools_smatch
0006 
0007 #include "words"
0008 #include <cstring>
0009 
0010 namespace tools {
0011 
0012 inline bool match(const std::string& a_string,const std::string& a_pattern,bool a_check_for_wilds = true){
0013   std::string::size_type lpattern = a_pattern.length();
0014   std::string::size_type lstring  = a_string.length();
0015   if ((lpattern==0)&&(lstring==0)) return true;
0016   if ((lpattern==0)&&(lstring!=0)) return true;
0017   if ((lpattern!=0)&&(lstring==0)) return false;
0018 
0019   if((lpattern==1)&&(a_pattern[0]=='*')) return true;
0020 
0021   if(a_check_for_wilds) {
0022     bool some_star = false;
0023     for(std::string::size_type count=0;count<lpattern;count++) {
0024       if(a_pattern[count]=='*') {some_star = true;break;}
0025     }
0026     if(!some_star) {  // no wildcard :
0027       return (a_pattern==a_string ? true : false );
0028     }
0029   }
0030 
0031   // complex pattern :
0032   //std::string::size_type* wps = new std::string::size_type[2*lpattern];
0033   if((2*lpattern)>1024) return false; //throw
0034   std::string::size_type wps[1024]; //OPTIMIZATION : we gain a lot with that.
0035 
0036   unsigned int wn;
0037   std::string::size_type* wls = wps+lpattern;
0038   words(a_pattern,"*",false,wn,wps,wls);
0039   if(!wn) {
0040     //delete [] wps;
0041     return true; // only wildcards :
0042   }
0043 
0044   // tricky case :
0045   char* token = (char*)a_string.c_str();
0046  {for(unsigned int count=0;count<wn;count++) {
0047     size_t lword = wls[count];
0048     if(!lword) continue;//should never happen !
0049     //WARNING : ws_pos does not have a null char at ws_pos+lword !
0050     char* ws_pos = (char*)(a_pattern.c_str()+wps[count]);
0051     if(count==0) {
0052       if(a_pattern[0]!='*') {
0053         // Begin of pattern (ws[0]) and a_string must match :
0054         if(::strncmp(token,ws_pos,lword)) {
0055           //delete [] wps;
0056           return false;
0057         }
0058         token = token + lword;
0059         continue;
0060       }
0061     }
0062     char old_char = *(ws_pos+lword);
0063     *(ws_pos+lword) = 0;
0064     char* pos = ::strstr(token,ws_pos);
0065     *(ws_pos+lword) = old_char;
0066     if(!pos) {
0067       //delete [] wps;
0068       return false;
0069     }
0070     if((count==(wn-1)) && (a_pattern[lpattern-1]!='*') ) { // Last word.
0071       // Compare last word and end of a_string.
0072       if(::strncmp(a_string.c_str()+lstring-lword,ws_pos,lword)) {
0073         //delete [] wps;
0074         return false;
0075       }
0076       break;
0077     } else {
0078       token = pos + lword;
0079     }
0080   }}
0081 
0082   //delete [] wps;
0083   return true;
0084 }
0085 
0086 //for tools/app/find.cpp :
0087 inline bool match2(const std::string& a_string,const std::string& a_pattern){
0088   return match(a_string,a_pattern,true);
0089 }
0090 
0091 inline void filter(std::vector<std::string>& a_v,
0092                    const std::string& a_pattern,
0093                    bool a_check_for_wilds = true){
0094   std::vector<std::string>::iterator it;
0095   for(it=a_v.begin();it!=a_v.end();) {
0096     if(match(*it,a_pattern,a_check_for_wilds)) {
0097       it++;
0098     } else {
0099       it = a_v.erase(it);
0100     }
0101   }
0102 }
0103 
0104 }
0105 
0106 #endif