MCPcopy Create free account
hub / github.com/deathkiller/jazz2-native / replaceFirst

Function replaceFirst

Sources/Shared/Containers/StringUtils.cpp:1363–1378  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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", {});

Callers

nothing calls this directly

Calls 4

findMethod · 0.45
sizeMethod · 0.45
beginMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected