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

Method try_resize

include/stringzilla/stringzilla.hpp:4272–4300  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4270
4271template <typename char_type_, typename allocator_>
4272bool basic_string<char_type_, allocator_>::try_resize(size_type count, value_type character) noexcept {
4273 sz_ptr_t string_start;
4274 sz_size_t string_length;
4275 sz_size_t string_space;
4276 sz_bool_t string_is_external;
4277 sz_string_unpack(&string_, &string_start, &string_length, &string_space, &string_is_external);
4278
4279 // Allocate more space if needed.
4280 if (count >= string_space) {
4281 if (_with_alloc([&](sz_alloc_type &alloc) {
4282 return sz_string_expand(&string_, SZ_SIZE_MAX, count - string_length, &alloc) ? sz_success_k
4283 : sz_bad_alloc_k;
4284 }) != status_t::success_k)
4285 return false;
4286 sz_string_unpack(&string_, &string_start, &string_length, &string_space, &string_is_external);
4287 }
4288
4289 // Fill the trailing characters.
4290 if (count > string_length) {
4291 sz_fill(string_start + string_length, count - string_length, character);
4292 string_start[count] = '\0';
4293 // Safe for both SSO and heap strings: on little-endian, internal.length
4294 // overlaps with LSB of external.length, so += affects only the length byte.
4295 // On big-endian, the struct layout places them at matching positions.
4296 string_.external.length += count - string_length;
4297 }
4298 else { sz_string_erase(&string_, count, SZ_SIZE_MAX); }
4299 return true;
4300}
4301
4302template <typename char_type_, typename allocator_>
4303bool basic_string<char_type_, allocator_>::try_assign(string_view other) noexcept {

Callers 4

try_reserveMethod · 0.45
try_buildMethod · 0.45
try_findMethod · 0.45

Calls 5

sz_string_unpackFunction · 0.85
_with_allocFunction · 0.85
sz_string_expandFunction · 0.85
sz_string_eraseFunction · 0.85
sz_fillFunction · 0.70

Tested by

no test coverage detected