// Copyright (C) 2010, Guy Barrand. All rights reserved. // See the file tools.license for terms. #ifndef tools_srep #define tools_srep #include #include namespace tools { inline bool replace(std::string& a_string,const std::string& a_old,const std::string& a_new){ // return true : some replacement done. // return false : nothing replaced. if(a_old.empty()) return false; std::string snew; std::string::size_type lold = a_old.length(); bool status = false; std::string stmp = a_string; while(true) { std::string::size_type pos = stmp.find(a_old); if(pos==std::string::npos){ snew += stmp; break; } else { snew += stmp.substr(0,pos); snew += a_new; stmp = stmp.substr(pos+lold,stmp.length()-(pos+lold)); status = true; } } a_string = snew; return status; } inline bool replace(std::vector& a_strings,const std::string& a_old,const std::string& a_new){ std::vector::iterator it; for(it=a_strings.begin();it!=a_strings.end();++it) { if(!replace(*it,a_old,a_new)) return false; } return true; } } #endif