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 bool replace(std::string& a_string,const std::string& a_old,const std::string& a_new){
0016 // return true : some replacement done.
0017 // return false : nothing replaced.
0018 if(a_old.empty()) return false;
0019 std::string snew;
0020 std::string::size_type lold = a_old.length();
0021 bool status = false;
0022 std::string stmp = a_string;
0023 while(true) {
0024 std::string::size_type pos = stmp.find(a_old);
0025 if(pos==std::string::npos){
0026 snew += stmp;
0027 break;
0028 } else {
0029 snew += stmp.substr(0,pos);
0030 snew += a_new;
0031 stmp = stmp.substr(pos+lold,stmp.length()-(pos+lold));
0032 status = true;
0033 }
0034 }
0035 a_string = std::move(snew);
0036 return status;
0037 }
0038
0039 inline bool replace_(std::string& a_string,const std::string& a_old,const std::string& a_new) {
0040 return replace(a_string,a_old,a_new);
0041 }
0042
0043 inline bool replace(std::vector<std::string>& a_strings,const std::string& a_old,const std::string& a_new){
0044 tools_vforit(std::string,a_strings,it) {
0045 if(!replace(*it,a_old,a_new)) return false;
0046 }
0047 return true;
0048 }
0049
0050 inline void toxml(std::string& a_string){
0051 // > : <
0052 // < : >
0053 // & : &
0054 // " : "
0055 // ' : '
0056 replace(a_string,"&","&"); //must be first.
0057 replace(a_string,"<","<");
0058 replace(a_string,">",">");
0059 replace(a_string,"\"",""");
0060 replace(a_string,"'","'");
0061 }
0062
0063 inline std::string to_xml(const std::string& a_string){
0064 std::string _s = a_string;
0065 toxml(_s);
0066 return _s;
0067 }
0068
0069 }
0070
0071 #endif