2023-08-28 20:46:50 -07:00
|
|
|
#ifdef GEN_INTELLISENSE_DIRECTIVES
|
|
|
|
# pragma once
|
|
|
|
# include "hashing.cpp"
|
|
|
|
#endif
|
2023-08-21 17:30:13 -07:00
|
|
|
|
2023-07-25 20:00:57 -07:00
|
|
|
#pragma region String
|
2024-11-30 11:13:30 -08:00
|
|
|
String string_make_length( AllocatorInfo allocator, char const* str, ssize length )
|
2023-07-24 15:35:16 -07:00
|
|
|
{
|
2024-11-30 11:13:30 -08:00
|
|
|
constexpr ssize header_size = sizeof( StringHeader );
|
2023-08-21 17:30:13 -07:00
|
|
|
|
|
|
|
s32 alloc_size = header_size + length + 1;
|
|
|
|
void* allocation = alloc( allocator, alloc_size );
|
|
|
|
|
|
|
|
if ( allocation == nullptr )
|
|
|
|
return { nullptr };
|
|
|
|
|
2024-11-30 11:13:30 -08:00
|
|
|
StringHeader&
|
|
|
|
header = * rcast(StringHeader*, allocation);
|
2023-08-21 17:30:13 -07:00
|
|
|
header = { allocator, length, length };
|
|
|
|
|
|
|
|
String result = { rcast( char*, allocation) + header_size };
|
|
|
|
|
|
|
|
if ( length && str )
|
|
|
|
mem_copy( result, str, length );
|
|
|
|
else
|
|
|
|
mem_set( result, 0, alloc_size - header_size );
|
|
|
|
|
|
|
|
result[ length ] = '\0';
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-11-30 11:13:30 -08:00
|
|
|
String string_make_reserve( AllocatorInfo allocator, ssize capacity )
|
2023-08-21 17:30:13 -07:00
|
|
|
{
|
2024-11-30 11:13:30 -08:00
|
|
|
constexpr ssize header_size = sizeof( StringHeader );
|
2023-08-21 17:30:13 -07:00
|
|
|
|
|
|
|
s32 alloc_size = header_size + capacity + 1;
|
|
|
|
void* allocation = alloc( allocator, alloc_size );
|
|
|
|
|
|
|
|
if ( allocation == nullptr )
|
|
|
|
return { nullptr };
|
|
|
|
|
|
|
|
mem_set( allocation, 0, alloc_size );
|
|
|
|
|
2024-11-30 11:13:30 -08:00
|
|
|
StringHeader*
|
|
|
|
header = rcast(StringHeader*, allocation);
|
2023-08-21 17:30:13 -07:00
|
|
|
header->Allocator = allocator;
|
|
|
|
header->Capacity = capacity;
|
|
|
|
header->Length = 0;
|
|
|
|
|
|
|
|
String result = { rcast(char*, allocation) + header_size };
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#pragma endregion String
|