MCPcopy Create free account
hub / github.com/ashvardanian/StringZilla / try_preparing_replacement

Method try_preparing_replacement

include/stringzilla/stringzilla.hpp:4489–4514  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4487
4488template <typename char_type_, typename allocator_>
4489bool basic_string<char_type_, allocator_>::try_preparing_replacement( //
4490 size_type offset, size_type length, size_type replacement_length) noexcept {
4491
4492 // There are three cases:
4493 // 1. The replacement is the same length as the replaced range.
4494 // 2. The replacement is shorter than the replaced range.
4495 // 3. The replacement is longer than the replaced range. An allocation may occur.
4496 assert(offset + length <= size());
4497
4498 // 1. The replacement is the same length as the replaced range.
4499 if (replacement_length == length) return true;
4500
4501 // 2. The replacement is shorter than the replaced range.
4502 else if (replacement_length < length) {
4503 sz_string_erase(&string_, offset + replacement_length, length - replacement_length);
4504 return true;
4505 }
4506 // 3. The replacement is longer than the replaced range. An allocation may occur.
4507 else {
4508 auto result = _with_alloc([&](sz_alloc_type &alloc) {
4509 return sz_string_expand(&string_, offset + length, replacement_length - length, &alloc) ? sz_success_k
4510 : sz_bad_alloc_k;
4511 });
4512 return result == status_t::success_k;
4513 }
4514}
4515
4516/**
4517 * @brief Helper function-like object to order string-view convertible objects with StringZilla.

Callers

nothing calls this directly

Calls 3

sz_string_eraseFunction · 0.85
_with_allocFunction · 0.85
sz_string_expandFunction · 0.85

Tested by

no test coverage detected