Replace all the "old" pattern with the "new" pattern in a string
| 47 | |
| 48 | // Replace all the "old" pattern with the "new" pattern in a string |
| 49 | std::string ReplaceAll(const StringPiece& s, const StringPiece& oldsub, |
| 50 | const StringPiece& newsub) |
| 51 | { |
| 52 | if (oldsub.empty()) |
| 53 | return s.as_string(); |
| 54 | |
| 55 | std::string res; |
| 56 | std::string::size_type start_pos = 0; |
| 57 | std::string::size_type pos; |
| 58 | do { |
| 59 | pos = s.find(oldsub, start_pos); |
| 60 | if (pos == std::string::npos) |
| 61 | { |
| 62 | break; |
| 63 | } |
| 64 | res.append(s.data() + start_pos, pos - start_pos); |
| 65 | res.append(newsub.data(), newsub.size()); |
| 66 | start_pos = pos + oldsub.size(); |
| 67 | } while (true); |
| 68 | res.append(s.data() + start_pos, s.length() - start_pos); |
| 69 | return res; |
| 70 | } |
| 71 | |
| 72 | void ReplaceAll(std::string* s, const StringPiece& from, const StringPiece& to) |
| 73 | { |