| 1361 | } |
| 1362 | |
| 1363 | String replaceFirst(StringView string, StringView search, StringView replace) { |
| 1364 | // Handle also the case when the search string is empty - find() returns (empty) begin in that case and we just |
| 1365 | // prepend the replace string. |
| 1366 | const StringView found = string.find(search); |
| 1367 | if (!search || found) { |
| 1368 | String output{NoInit, string.size() + replace.size() - found.size()}; |
| 1369 | const std::size_t begin = found.begin() - string.begin(); |
| 1370 | std::memcpy(output.data(), string.data(), begin); |
| 1371 | std::memcpy(output.data() + begin, replace.data(), replace.size()); |
| 1372 | const std::size_t end = begin + search.size(); |
| 1373 | std::memcpy(output.data() + begin + replace.size(), string.data() + end, string.size() - end); |
| 1374 | return output; |
| 1375 | } |
| 1376 | |
| 1377 | return string; |
| 1378 | } |
| 1379 | |
| 1380 | String replaceAll(StringView string, StringView search, StringView replace) { |
| 1381 | DEATH_ASSERT(!search.empty(), "Empty search string would cause an infinite loop", {}); |