2023-08-09 15:47:59 -07:00
|
|
|
#pragma region Strings
|
2023-07-25 20:00:57 -07:00
|
|
|
|
2023-07-24 15:19:37 -07:00
|
|
|
// Constant string with length.
|
|
|
|
struct StrC
|
|
|
|
{
|
|
|
|
sw Len;
|
|
|
|
char const* Ptr;
|
|
|
|
|
|
|
|
operator char const* () const
|
|
|
|
{
|
|
|
|
return Ptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-08-09 15:47:59 -07:00
|
|
|
#define cast_to_strc( str ) * rcast( StrC*, str - sizeof(sw) )
|
|
|
|
#define txt( text ) StrC { sizeof( text ) - 1, text }
|
2023-07-24 15:19:37 -07:00
|
|
|
|
2023-08-09 15:47:59 -07:00
|
|
|
StrC to_str( char const* str )
|
2023-07-24 15:19:37 -07:00
|
|
|
{
|
|
|
|
return { str_len( str ), str };
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dynamic String
|
|
|
|
// This is directly based off the ZPL string api.
|
|
|
|
// They used a header pattern
|
|
|
|
// I kept it for simplicty of porting but its not necessary to keep it that way.
|
|
|
|
struct String
|
|
|
|
{
|
|
|
|
struct Header
|
|
|
|
{
|
|
|
|
AllocatorInfo Allocator;
|
|
|
|
sw Capacity;
|
2023-08-09 15:47:59 -07:00
|
|
|
sw Length;
|
2023-07-24 15:19:37 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
static
|
|
|
|
uw grow_formula( uw value )
|
|
|
|
{
|
|
|
|
// Using a very aggressive growth formula to reduce time mem_copying with recursive calls to append in this library.
|
|
|
|
return 4 * value + 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
String make( AllocatorInfo allocator, char const* str )
|
|
|
|
{
|
|
|
|
sw length = str ? str_len( str ) : 0;
|
|
|
|
return make_length( allocator, str, length );
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
String make( AllocatorInfo allocator, StrC str )
|
|
|
|
{
|
|
|
|
return make_length( allocator, str.Ptr, str.Len );
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
String make_reserve( AllocatorInfo allocator, sw capacity )
|
|
|
|
{
|
|
|
|
constexpr sw header_size = sizeof( Header );
|
|
|
|
|
|
|
|
s32 alloc_size = header_size + capacity + 1;
|
|
|
|
void* allocation = alloc( allocator, alloc_size );
|
|
|
|
|
|
|
|
if ( allocation == nullptr )
|
|
|
|
return { nullptr };
|
|
|
|
|
|
|
|
mem_set( allocation, 0, alloc_size );
|
|
|
|
|
|
|
|
Header*
|
|
|
|
header = rcast(Header*, allocation);
|
|
|
|
header->Allocator = allocator;
|
|
|
|
header->Capacity = capacity;
|
|
|
|
header->Length = 0;
|
|
|
|
|
2023-08-20 12:45:06 -07:00
|
|
|
String result = { rcast(char*, allocation) + header_size };
|
2023-07-24 15:19:37 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
String make_length( AllocatorInfo allocator, char const* str, sw length )
|
|
|
|
{
|
|
|
|
constexpr sw header_size = sizeof( Header );
|
|
|
|
|
|
|
|
s32 alloc_size = header_size + length + 1;
|
|
|
|
void* allocation = alloc( allocator, alloc_size );
|
|
|
|
|
|
|
|
if ( allocation == nullptr )
|
|
|
|
return { nullptr };
|
|
|
|
|
|
|
|
Header&
|
2023-08-09 15:47:59 -07:00
|
|
|
header = * rcast(Header*, allocation);
|
2023-07-24 15:19:37 -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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
String fmt( AllocatorInfo allocator, char* buf, sw buf_size, char const* fmt, ... );
|
|
|
|
|
|
|
|
static
|
|
|
|
String fmt_buf( AllocatorInfo allocator, char const* fmt, ... );
|
|
|
|
|
|
|
|
static
|
|
|
|
String join( AllocatorInfo allocator, char const** parts, sw num_parts, char const* glue )
|
|
|
|
{
|
|
|
|
String result = make( allocator, "" );
|
|
|
|
|
|
|
|
for ( sw idx = 0; idx < num_parts; ++idx )
|
|
|
|
{
|
|
|
|
result.append( parts[ idx ] );
|
|
|
|
|
|
|
|
if ( idx < num_parts - 1 )
|
|
|
|
result.append( glue );
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
bool are_equal( String lhs, String rhs )
|
|
|
|
{
|
|
|
|
if ( lhs.length() != rhs.length() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for ( sw idx = 0; idx < lhs.length(); ++idx )
|
|
|
|
if ( lhs[ idx ] != rhs[ idx ] )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool make_space_for( char const* str, sw add_len )
|
|
|
|
{
|
|
|
|
sw available = avail_space();
|
|
|
|
|
|
|
|
// NOTE: Return if there is enough space left
|
|
|
|
if ( available >= add_len )
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sw new_len, old_size, new_size;
|
|
|
|
|
|
|
|
void* ptr;
|
|
|
|
void* new_ptr;
|
|
|
|
|
|
|
|
AllocatorInfo allocator = get_header().Allocator;
|
|
|
|
Header* header = nullptr;
|
|
|
|
|
|
|
|
new_len = grow_formula( length() + add_len );
|
|
|
|
ptr = & get_header();
|
|
|
|
old_size = size_of( Header ) + length() + 1;
|
|
|
|
new_size = size_of( Header ) + new_len + 1;
|
|
|
|
|
|
|
|
new_ptr = resize( allocator, ptr, old_size, new_size );
|
|
|
|
|
|
|
|
if ( new_ptr == nullptr )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
header = zpl_cast( Header* ) new_ptr;
|
|
|
|
header->Allocator = allocator;
|
|
|
|
header->Capacity = new_len;
|
|
|
|
|
|
|
|
Data = rcast( char*, header + 1 );
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool append( char const* str )
|
|
|
|
{
|
|
|
|
return append( str, str_len( str ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool append( char const* str, sw length )
|
|
|
|
{
|
|
|
|
if ( sptr(str) > 0 )
|
|
|
|
{
|
|
|
|
sw curr_len = this->length();
|
|
|
|
|
|
|
|
if ( ! make_space_for( str, length ) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Header& header = get_header();
|
|
|
|
|
|
|
|
mem_copy( Data + curr_len, str, length );
|
|
|
|
|
|
|
|
Data[ curr_len + length ] = '\0';
|
|
|
|
|
|
|
|
header.Length = curr_len + length;
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool append( StrC str)
|
|
|
|
{
|
|
|
|
return append( str.Ptr, str.Len );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool append( const String other )
|
|
|
|
{
|
|
|
|
return append( other.Data, other.length() );;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool append_fmt( char const* fmt, ... );
|
|
|
|
|
|
|
|
sw avail_space() const
|
|
|
|
{
|
|
|
|
Header const&
|
2023-07-25 20:00:57 -07:00
|
|
|
header = * rcast( Header const*, Data - sizeof( Header ));
|
2023-07-24 15:19:37 -07:00
|
|
|
|
|
|
|
return header.Capacity - header.Length;
|
|
|
|
}
|
|
|
|
|
2023-08-08 19:14:58 -07:00
|
|
|
char& back()
|
|
|
|
{
|
|
|
|
return Data[ length() - 1 ];
|
|
|
|
}
|
|
|
|
|
2023-07-24 15:19:37 -07:00
|
|
|
sw capacity() const
|
|
|
|
{
|
|
|
|
Header const&
|
2023-07-25 20:00:57 -07:00
|
|
|
header = * rcast( Header const*, Data - sizeof( Header ));
|
2023-07-24 15:19:37 -07:00
|
|
|
|
|
|
|
return header.Capacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
get_header().Length = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
String duplicate( AllocatorInfo allocator )
|
|
|
|
{
|
|
|
|
return make_length( allocator, Data, length() );
|
|
|
|
}
|
|
|
|
|
|
|
|
void free()
|
|
|
|
{
|
|
|
|
if ( ! Data )
|
|
|
|
return;
|
|
|
|
|
|
|
|
Header& header = get_header();
|
|
|
|
|
|
|
|
gen::free( header.Allocator, & header );
|
|
|
|
}
|
|
|
|
|
|
|
|
Header& get_header()
|
|
|
|
{
|
|
|
|
return *(Header*)(Data - sizeof(Header));
|
|
|
|
}
|
|
|
|
|
|
|
|
sw length() const
|
|
|
|
{
|
|
|
|
Header const&
|
2023-07-25 20:00:57 -07:00
|
|
|
header = * rcast( Header const*, Data - sizeof( Header ));
|
2023-07-24 15:19:37 -07:00
|
|
|
|
|
|
|
return header.Length;
|
|
|
|
}
|
|
|
|
|
|
|
|
void trim( char const* cut_set )
|
|
|
|
{
|
|
|
|
sw len = 0;
|
|
|
|
|
|
|
|
char* start_pos = Data;
|
|
|
|
char* end_pos = Data + length() - 1;
|
|
|
|
|
|
|
|
while ( start_pos <= end_pos && char_first_occurence( cut_set, *start_pos ) )
|
|
|
|
start_pos++;
|
|
|
|
|
|
|
|
while ( end_pos > start_pos && char_first_occurence( cut_set, *end_pos ) )
|
|
|
|
end_pos--;
|
|
|
|
|
|
|
|
len = scast( sw, ( start_pos > end_pos ) ? 0 : ( ( end_pos - start_pos ) + 1 ) );
|
|
|
|
|
|
|
|
if ( Data != start_pos )
|
|
|
|
mem_move( Data, start_pos, len );
|
|
|
|
|
|
|
|
Data[ len ] = '\0';
|
|
|
|
|
|
|
|
get_header().Length = len;
|
|
|
|
}
|
|
|
|
|
|
|
|
void trim_space()
|
|
|
|
{
|
|
|
|
return trim( " \t\r\n\v\f" );
|
|
|
|
}
|
|
|
|
|
|
|
|
// For-range support
|
|
|
|
|
|
|
|
char* begin()
|
|
|
|
{
|
|
|
|
return Data;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* end()
|
|
|
|
{
|
|
|
|
Header const&
|
2023-07-25 20:00:57 -07:00
|
|
|
header = * rcast( Header const*, Data - sizeof( Header ));
|
2023-07-24 15:19:37 -07:00
|
|
|
|
|
|
|
return Data + header.Length;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator bool()
|
|
|
|
{
|
|
|
|
return Data;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator char* ()
|
|
|
|
{
|
|
|
|
return Data;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator char const* () const
|
|
|
|
{
|
|
|
|
return Data;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator StrC() const
|
|
|
|
{
|
2023-08-09 15:47:59 -07:00
|
|
|
return { length(), Data };
|
2023-07-24 15:19:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Used with cached strings
|
|
|
|
// Essentially makes the string a string view.
|
|
|
|
String const& operator = ( String const& other ) const
|
|
|
|
{
|
|
|
|
if ( this == & other )
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
String& this_ = ccast( String, *this );
|
|
|
|
|
|
|
|
this_.Data = other.Data;
|
|
|
|
|
|
|
|
return this_;
|
|
|
|
}
|
2023-07-25 20:00:57 -07:00
|
|
|
|
2023-07-24 15:19:37 -07:00
|
|
|
char& operator [] ( sw index )
|
|
|
|
{
|
|
|
|
return Data[ index ];
|
|
|
|
}
|
|
|
|
|
|
|
|
char const& operator [] ( sw index ) const
|
|
|
|
{
|
|
|
|
return Data[ index ];
|
|
|
|
}
|
|
|
|
|
2023-08-09 15:47:59 -07:00
|
|
|
char* Data;
|
2023-07-24 15:19:37 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
struct String_POD
|
|
|
|
{
|
|
|
|
char* Data;
|
|
|
|
};
|
|
|
|
static_assert( sizeof( String_POD ) == sizeof( String ), "String is not a POD" );
|
2023-07-25 20:00:57 -07:00
|
|
|
|
2023-08-09 15:47:59 -07:00
|
|
|
// Implements basic string interning. Data structure is based off the ZPL Hashtable.
|
|
|
|
using StringTable = HashTable<String const>;
|
|
|
|
|
|
|
|
// Represents strings cached with the string table.
|
|
|
|
// Should never be modified, if changed string is desired, cache_string( str ) another.
|
|
|
|
using StringCached = String const;
|
|
|
|
|
|
|
|
#pragma endregion Strings
|
2023-08-03 08:01:43 -07:00
|
|
|
|