| 316 | } |
| 317 | |
| 318 | SZ_PUBLIC sz_ptr_t sz_string_init_length(sz_string_t *string, sz_size_t length, sz_memory_allocator_t *allocator) { |
| 319 | sz_size_t space_needed = length + 1; // space for trailing \0 |
| 320 | sz_assert_(string && allocator && "String and allocator can't be SZ_NULL."); |
| 321 | // Initialize the string to zeros for safety. |
| 322 | string->words[1] = 0; |
| 323 | string->words[2] = 0; |
| 324 | string->words[3] = 0; |
| 325 | // If we are lucky, no memory allocations will be needed. |
| 326 | if (space_needed <= SZ_STRING_INTERNAL_SPACE) { |
| 327 | string->internal.start = &string->internal.chars[0]; |
| 328 | string->internal.length = (sz_u8_t)length; |
| 329 | } |
| 330 | else { |
| 331 | // If we are not lucky, we need to allocate memory. |
| 332 | string->external.start = (sz_ptr_t)allocator->allocate(space_needed, allocator->handle); |
| 333 | if (!string->external.start) return SZ_NULL_CHAR; |
| 334 | string->external.length = length; |
| 335 | string->external.space = space_needed; |
| 336 | } |
| 337 | string->external.start[length] = 0; |
| 338 | return string->external.start; |
| 339 | } |
| 340 | |
| 341 | SZ_PUBLIC sz_ptr_t sz_string_reserve(sz_string_t *string, sz_size_t new_capacity, sz_memory_allocator_t *allocator) { |
| 342 |
no test coverage detected
searching dependent graphs…