Skip to content

Commit

Permalink
Fixed an infinite loop bugs in 9_43_44.
Browse files Browse the repository at this point in the history
1. reference: Mooophy/Cpp-Primer#206
2. gcc cannot support c++11 feature(cann't return iterator from
string::insert)
  • Loading branch information
pezy committed Apr 26, 2015
1 parent 613002f commit cc394ef
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
30 changes: 16 additions & 14 deletions ch09/ex9_43.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,31 @@
// Using iterators, and the insert and erase functions replace
// all instances of oldVal that appear in s by newVal.
// Test your function by using it to replace common abbreviations,
// such as “tho” by “though” and “thru” by “through”.
// such as "tho" by "though" and "thru" by "through".
// @Compile Gcc can't support return iterator of string::insert. Please use clang or VS2013

#include <iostream>
#include <string>

using std::cout; using std::endl; using std::string;
using std::cout; using std::endl; using std::string; using std::prev;

void func(string &s, const string &oldVal, const string &newVal)
{
for (string::size_type i=0; i!=s.size(); ++i)
if (s.substr(i, oldVal.size()) == oldVal)
{
s.erase(i, oldVal.size());
s.insert(i, newVal);
}
void Replace(string &s, string const& oldVal, string const& newVal) {
auto val_size = oldVal.size();
if (s.size() < val_size) return;

for (auto iter = s.begin(), end = prev(s.end(), val_size - 1); iter != end; ++iter) {
if (string{iter, iter + val_size} == oldVal) {
iter = s.earse(iter, iter + val_size);
iter = s.insert(iter, newVal.cbegin(), newVal.cend()); // gcc 4.9 bug (see http://stackoverflow.com/questions/29690369)
iter += newVal.size();
}
}
}

int main()
{
string str{"To drive straight thru is a foolish, tho courageous act."};
func(str, "tho", "though");
func(str, "thru", "through");
Replace(str, "tho", "though");
Replace(str, "thru", "through");
cout << str << endl;

return 0;
}
18 changes: 9 additions & 9 deletions ch09/ex9_44.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@
#include <iostream>
#include <string>

using std::cout; using std::endl; using std::string;
using std::cout; using std::endl; using std::string; using std::prev;

void func(string &s, const string &oldVal, const string &newVal)
void Replace(string& s, string const& oldVal, string const& newVal)
{
for (string::size_type i=0; i!=s.size(); ++i)
if (s.substr(i, oldVal.size()) == oldVal)
s.replace(i, oldVal.size(), newVal);
for ( string::size_type i = 0; i != s.size(); ++i )
if ( s.substr( i, oldVal.size() ) == oldVal ) {
s.replace( i, oldVal.size(), newVal );
i += newVal.size() - 1;
}
}

int main()
{
string str{"To drive straight thru is a foolish, tho courageous act."};
func(str, "tho", "though");
func(str, "thru", "through");
Replace( str, "tho", "though" );
Replace( str, "thru", "through" );
cout << str << endl;

return 0;
}

0 comments on commit cc394ef

Please sign in to comment.