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

Function sz_string_shrink_to_fit

include/stringzilla/small_string.h:369–395  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

367}
368
369SZ_PUBLIC sz_ptr_t sz_string_shrink_to_fit(sz_string_t *string, sz_memory_allocator_t *allocator) {
370
371 sz_assert_(string && allocator && "Strings and allocators can't be SZ_NULL.");
372
373 sz_ptr_t string_start;
374 sz_size_t string_length;
375 sz_size_t string_space;
376 sz_bool_t string_is_external;
377 sz_string_unpack(string, &string_start, &string_length, &string_space, &string_is_external);
378
379 // We may already be space-optimal, and in that case we don't need to do anything.
380 sz_size_t new_space = string_length + 1;
381 if (string_space == new_space || !string_is_external) return string->external.start;
382
383 sz_ptr_t new_start = (sz_ptr_t)allocator->allocate(new_space, allocator->handle);
384 if (!new_start) return SZ_NULL_CHAR;
385
386 sz_copy(new_start, string_start, string_length);
387 string->external.start = new_start;
388 string->external.space = new_space;
389 string->external.padding = 0;
390 string->external.length = string_length;
391
392 // Deallocate the old string.
393 if (string_is_external) allocator->free(string_start, string_space, allocator->handle);
394 return string->external.start;
395}
396
397SZ_PUBLIC sz_ptr_t sz_string_expand( //
398 sz_string_t *string, sz_size_t offset, sz_size_t added_length, sz_memory_allocator_t *allocator) {

Callers 1

try_shrink_to_fitMethod · 0.85

Calls 3

sz_string_unpackFunction · 0.85
sz_copyFunction · 0.70
allocateMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…