inlib
1.2.0
|
00001 // Copyright (C) 2010, Guy Barrand. All rights reserved. 00002 // See the file inlib.license for terms. 00003 00004 #ifndef inlib_srep 00005 #define inlib_srep 00006 00007 #include <string> 00008 #include <vector> 00009 00010 namespace inlib { 00011 00012 inline bool replace(std::string& a_string,const std::string& a_old,const std::string& a_new){ 00013 // return true : some replacement done. 00014 // return false : nothing replaced. 00015 if(a_old.empty()) return false; 00016 std::string snew; 00017 std::string::size_type lold = a_old.length(); 00018 bool status = false; 00019 std::string stmp = a_string; 00020 while(true) { 00021 std::string::size_type pos = stmp.find(a_old); 00022 if(pos==std::string::npos){ 00023 snew += stmp; 00024 break; 00025 } else { 00026 snew += stmp.substr(0,pos); 00027 snew += a_new; 00028 stmp = stmp.substr(pos+lold,stmp.length()-(pos+lold)); 00029 status = true; 00030 } 00031 } 00032 a_string = snew; 00033 return status; 00034 } 00035 00036 inline bool replace(std::vector<std::string>& a_strings,const std::string& a_old,const std::string& a_new){ 00037 std::vector<std::string>::iterator it; 00038 for(it=a_strings.begin();it!=a_strings.end();++it) { 00039 if(!replace(*it,a_old,a_new)) return false; 00040 } 00041 return true; 00042 } 00043 00044 } 00045 00046 #endif