Skip to content

Commit

Permalink
improving pezy#84 format.
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Oct 16, 2016
1 parent c60d555 commit e6e0436
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions ch09/ex9_43.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,18 @@ using std::string;

void Replace(string& s, const string& oldVal, const string& newVal)
{
for (auto beg = s.begin(); beg != s.end(); ) {
if (std::distance(beg, s.end()) <
std::distance(oldVal.begin(), oldVal.end()))
break;
if (*beg == oldVal.front() && string{ beg, beg + oldVal.size() } == oldVal) {
beg = s.erase(beg, beg + oldVal.size());
beg = s.insert(beg, newVal.cbegin(), newVal.cend()) + newVal.size();
}
else {
++beg;
}
}
for (auto beg = s.begin(); std::distance(beg, s.end()) >=
std::distance(oldVal.begin(), oldVal.end());) {
if (string{beg, beg + oldVal.size()} == oldVal) {
beg = s.erase(beg, beg + oldVal.size());
beg = s.insert(beg, newVal.cbegin(), newVal.cend());
std::advance(beg, newVal.size());
}
else
++beg;
}
}

int main()
{
{
Expand Down

0 comments on commit e6e0436

Please sign in to comment.