Warning, /include/Geant4/tools/srep 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_srep
0005 #define tools_srep
0006
0007 #include <string>
0008 #include <vector>
0009 #include <utility>
0010
0011 #include "forit"
0012
0013 namespace tools {
0014
0015 inline void replace(std::string& a_string,char a_old,char a_new){
0016 tools_sforit(a_string,it) {
0017 if((*it)==a_old) *it = a_new;
0018 }
0019 }
0020
0021 inline bool replace(std::string& a_string,const std::string& a_old,const std::string& a_new){
0022 // return true : some replacement done.
0023 // return false : nothing replaced.
0024 if(a_old.empty()) return false;
0025 std::string snew;
0026 std::string::size_type lold = a_old.length();
0027 bool status = false;
0028 std::string stmp = a_string;
0029 while(true) {
0030 std::string::size_type pos = stmp.find(a_old);
0031 if(pos==std::string::npos){
0032 snew += stmp;
0033 break;
0034 } else {
0035 snew += stmp.substr(0,pos);
0036 snew += a_new;
0037 stmp = stmp.substr(pos+lold,stmp.length()-(pos+lold));
0038 status = true;
0039 }
0040 }
0041 a_string = std::move(snew);
0042 return status;
0043 }
0044
0045 inline bool replace_(std::string& a_string,const std::string& a_old,const std::string& a_new) {
0046 return replace(a_string,a_old,a_new);
0047 }
0048
0049 inline bool replace(std::vector<std::string>& a_strings,const std::string& a_old,const std::string& a_new){
0050 tools_vforit(std::string,a_strings,it) {
0051 if(!replace(*it,a_old,a_new)) return false;
0052 }
0053 return true;
0054 }
0055
0056 inline void toxml(std::string& a_string){
0057 // > : <
0058 // < : >
0059 // & : &
0060 // " : "
0061 // ' : '
0062 replace(a_string,"&","&"); //must be first.
0063 replace(a_string,"<","<");
0064 replace(a_string,">",">");
0065 replace(a_string,"\"",""");
0066 replace(a_string,"'","'");
0067 }
0068
0069 inline std::string to_xml(const std::string& a_string){
0070 std::string _s = a_string;
0071 toxml(_s);
0072 return _s;
0073 }
0074
0075 inline void to_win(std::string& a_string) {
0076 replace(a_string,"/cygdrive/c","C:"); // CYGWIN.
0077 replace(a_string,"/mnt/c","C:"); // WSL.
0078 replace(a_string,'/','\\');
0079 }
0080
0081 inline void to_win_python(std::string& a_string) {
0082 replace(a_string,"/cygdrive/c","c:"); // CYGWIN.
0083 replace(a_string,"/mnt/c","c:"); // WSL. (Python wants c: in lowercase).
0084 replace(a_string,"C:","c:");
0085 replace(a_string,'\\','/');
0086 }
0087
0088 }
0089
0090 #endif