| 129 | } |
| 130 | |
| 131 | string StringReplace(StringPiece s, StringPiece oldsub, StringPiece newsub, |
| 132 | bool replace_all) { |
| 133 | // TODO(jlebar): We could avoid having to shift data around in the string if |
| 134 | // we had a StringPiece::find() overload that searched for a StringPiece. |
| 135 | string res(s); |
| 136 | size_t pos = 0; |
| 137 | while ((pos = res.find(oldsub.data(), pos, oldsub.size())) != string::npos) { |
| 138 | res.replace(pos, oldsub.size(), newsub.data(), newsub.size()); |
| 139 | pos += newsub.size(); |
| 140 | if (oldsub.empty()) { |
| 141 | pos++; // Match at the beginning of the text and after every byte |
| 142 | } |
| 143 | if (!replace_all) { |
| 144 | break; |
| 145 | } |
| 146 | } |
| 147 | return res; |
| 148 | } |
| 149 | |
| 150 | bool StartsWith(StringPiece text, StringPiece prefix) { |
| 151 | return absl::StartsWith(text, prefix); |