Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights reserved. 2 // See the file tools.license for terms. 3 4 #ifndef tools_srep 5 #define tools_srep 6 7 #include <string> 8 #include <vector> 9 #include <utility> 10 11 #include "forit" 12 13 namespace tools { 14 15 inline void replace(std::string& a_string,char a_old,char a_new){ 16 tools_sforit(a_string,it) { 17 if((*it)==a_old) *it = a_new; 18 } 19 } 20 21 inline bool replace(std::string& a_string,const std::string& a_old,const std::string& a_new){ 22 // return true : some replacement done. 23 // return false : nothing replaced. 24 if(a_old.empty()) return false; 25 std::string snew; 26 std::string::size_type lold = a_old.length(); 27 bool status = false; 28 std::string stmp = a_string; 29 while(true) { 30 std::string::size_type pos = stmp.find(a_old); 31 if(pos==std::string::npos){ 32 snew += stmp; 33 break; 34 } else { 35 snew += stmp.substr(0,pos); 36 snew += a_new; 37 stmp = stmp.substr(pos+lold,stmp.length()-(pos+lold)); 38 status = true; 39 } 40 } 41 a_string = std::move(snew); 42 return status; 43 } 44 45 inline bool replace_(std::string& a_string,const std::string& a_old,const std::string& a_new) { 46 return replace(a_string,a_old,a_new); 47 } 48 49 inline bool replace(std::vector<std::string>& a_strings,const std::string& a_old,const std::string& a_new){ 50 tools_vforit(std::string,a_strings,it) { 51 if(!replace(*it,a_old,a_new)) return false; 52 } 53 return true; 54 } 55 56 inline void toxml(std::string& a_string){ 57 // > : < 58 // < : > 59 // & : & 60 // " : " 61 // ' : ' 62 replace(a_string,"&","&"); //must be first. 63 replace(a_string,"<","<"); 64 replace(a_string,">",">"); 65 replace(a_string,"\"","""); 66 replace(a_string,"'","'"); 67 } 68 69 inline std::string to_xml(const std::string& a_string){ 70 std::string _s = a_string; 71 toxml(_s); 72 return _s; 73 } 74 75 inline void to_win(std::string& a_string) { 76 replace(a_string,"/cygdrive/c","C:"); // CYGWIN. 77 replace(a_string,"/mnt/c","C:"); // WSL. 78 replace(a_string,'/','\\'); 79 } 80 81 inline void to_win_python(std::string& a_string) { 82 replace(a_string,"/cygdrive/c","c:"); // CYGWIN. 83 replace(a_string,"/mnt/c","c:"); // WSL. (Python wants c: in lowercase). 84 replace(a_string,"C:","c:"); 85 replace(a_string,'\\','/'); 86 } 87 88 } 89 90 #endif