This commit is contained in:
Edward R. Gonzalez 2024-12-01 23:35:58 -05:00
parent fec709cc76
commit f9c21ebc04
13 changed files with 540 additions and 508 deletions

View File

@ -21,7 +21,7 @@ Builder Builder::open( char const* path )
void Builder::pad_lines( s32 num ) void Builder::pad_lines( s32 num )
{ {
append( Buffer, "\n" ); append( & Buffer, "\n" );
} }
void Builder::print( Code code ) void Builder::print( Code code )
@ -29,7 +29,7 @@ void Builder::print( Code code )
String str = code->to_string(); String str = code->to_string();
// const ssize len = str.length(); // const ssize len = str.length();
// log_fmt( "%s - print: %.*s\n", File.filename, len > 80 ? 80 : len, str.Data ); // log_fmt( "%s - print: %.*s\n", File.filename, len > 80 ? 80 : len, str.Data );
append( Buffer, str ); append( & Buffer, str );
} }
void Builder::print_fmt( char const* fmt, ... ) void Builder::print_fmt( char const* fmt, ... )
@ -43,7 +43,7 @@ void Builder::print_fmt( char const* fmt, ... )
va_end( va ); va_end( va );
// log_fmt( "$%s - print_fmt: %.*s\n", File.filename, res > 80 ? 80 : res, buf ); // log_fmt( "$%s - print_fmt: %.*s\n", File.filename, res > 80 ? 80 : res, buf );
append( Buffer, buf, res ); append( & Buffer, buf, res );
} }
void Builder::write() void Builder::write()
@ -55,5 +55,5 @@ void Builder::write()
log_fmt( "Generated: %s\n", File.filename ); log_fmt( "Generated: %s\n", File.filename );
file_close( & File ); file_close( & File );
free(Buffer); free(& Buffer);
} }

View File

@ -25,7 +25,7 @@ Code scan_file( char const* path )
String str = string_make_reserve( GlobalAllocator, fsize ); String str = string_make_reserve( GlobalAllocator, fsize );
file_read( & file, str, fsize ); file_read( & file, str, fsize );
get_header(str).Length = fsize; get_header(str)->Length = fsize;
// Skip GEN_INTELLISENSE_DIRECTIVES preprocessor blocks // Skip GEN_INTELLISENSE_DIRECTIVES preprocessor blocks
// Its designed so that the directive should be the first thing in the file. // Its designed so that the directive should be the first thing in the file.
@ -97,12 +97,12 @@ Code scan_file( char const* path )
if ( (scanner + 2) >= ( str.Data + fsize ) ) if ( (scanner + 2) >= ( str.Data + fsize ) )
{ {
mem_move( str, scanner, left ); mem_move( str, scanner, left );
get_header(str).Length = left; get_header(str)->Length = left;
break; break;
} }
mem_move( str, scanner, left ); mem_move( str, scanner, left );
get_header(str).Length = left; get_header(str)->Length = left;
break; break;
} }

View File

@ -29,17 +29,17 @@ void format_file( char const* path )
String resolved_path = string_make(GlobalAllocator, to_str(path)); String resolved_path = string_make(GlobalAllocator, to_str(path));
String style_arg = string_make(GlobalAllocator, txt("-style=file:")); String style_arg = string_make(GlobalAllocator, txt("-style=file:"));
append( style_arg, "../scripts/.clang-format "); append( & style_arg, "../scripts/.clang-format ");
// Need to execute clang format on the generated file to get it to match the original. // Need to execute clang format on the generated file to get it to match the original.
#define clang_format "clang-format " #define clang_format "clang-format "
#define cf_format_inplace "-i " #define cf_format_inplace "-i "
#define cf_verbose "-verbose " #define cf_verbose "-verbose "
String command = string_make( GlobalAllocator, clang_format ); String command = string_make( GlobalAllocator, clang_format );
append( command, cf_format_inplace ); append( & command, cf_format_inplace );
append( command, cf_verbose ); append( & command, cf_verbose );
append( command, style_arg ); append( & command, style_arg );
append( command, resolved_path ); append( & command, resolved_path );
log_fmt("\tRunning clang-format on file:\n"); log_fmt("\tRunning clang-format on file:\n");
system( command ); system( command );
log_fmt("\tclang-format finished reformatting.\n"); log_fmt("\tclang-format finished reformatting.\n");

View File

@ -10,7 +10,8 @@ Code Code::Invalid;
char const* debug_str(AST* self) char const* debug_str(AST* self)
{ {
GEN_ASSERT(self != nullptr); GEN_ASSERT(self != nullptr);
String result = string_make_reserve( GlobalAllocator, kilobytes(1) ); String result_stack = string_make_reserve( GlobalAllocator, kilobytes(1) );
String* result = & result_stack;
if ( self->Parent ) if ( self->Parent )
append_fmt( result, "\n\tParent : %S %S", self->Parent->type_str(), self->Name ? self->Name : "" ); append_fmt( result, "\n\tParent : %S %S", self->Parent->type_str(), self->Name ? self->Name : "" );
@ -356,7 +357,7 @@ char const* debug_str(AST* self)
break; break;
} }
return result; return * result;
} }
AST* duplicate(AST* self) AST* duplicate(AST* self)
@ -391,25 +392,25 @@ void AST::to_string( String& result )
#ifdef GEN_DONT_ALLOW_INVALID_CODE #ifdef GEN_DONT_ALLOW_INVALID_CODE
log_failure("Attempted to serialize invalid code! - %S", Parent ? Parent->debug_str() : Name ); log_failure("Attempted to serialize invalid code! - %S", Parent ? Parent->debug_str() : Name );
#else #else
append_fmt( result, "Invalid Code!" ); GEN_NS append_fmt( & result, "Invalid Code!" );
#endif #endif
break; break;
case NewLine: case NewLine:
GEN_NS append( result,"\n"); GEN_NS append( & result,"\n");
break; break;
case Untyped: case Untyped:
case Execution: case Execution:
case Comment: case Comment:
case PlatformAttributes: case PlatformAttributes:
GEN_NS append( result, Content ); GEN_NS append( & result, Content );
break; break;
case Access_Private: case Access_Private:
case Access_Protected: case Access_Protected:
case Access_Public: case Access_Public:
GEN_NS append( result, Name ); GEN_NS append( & result, Name );
break; break;
case Class: case Class:

File diff suppressed because it is too large Load Diff

View File

@ -321,7 +321,7 @@ void deinit()
} }
while ( left--, left ); while ( left--, left );
destroy(StringCache); destroy(& StringCache);
free( & CodePools); free( & CodePools);
free( & StringArenas); free( & StringArenas);
@ -403,7 +403,7 @@ StringCached get_cached_string( StrC str )
} }
String result = string_make( get_string_allocator( str.Len ), str ); String result = string_make( get_string_allocator( str.Len ), str );
set<StringCached>(StringCache, key, result ); set<StringCached>(& StringCache, key, result );
return result; return result;
} }

View File

@ -27,8 +27,7 @@ ssize token_fmt_va( char* buf, usize buf_size, s32 num_tokens, va_list va )
StrC value = va_arg( va, StrC ); StrC value = va_arg( va, StrC );
u32 key = crc32( token, str_len(token) ); u32 key = crc32( token, str_len(token) );
set(& tok_map, key, value );
set(tok_map, key, value );
} }
} }

View File

@ -474,15 +474,15 @@ CodeComment def_comment( StrC content )
length++; length++;
str_copy( line, scanner, length ); str_copy( line, scanner, length );
append_fmt(cmt_formatted, "//%.*s", length, line ); append_fmt(& cmt_formatted, "//%.*s", length, line );
mem_set( line, 0, MaxCommentLineLength ); mem_set( line, 0, MaxCommentLineLength );
scanner += length; scanner += length;
} }
while ( scanner <= end ); while ( scanner <= end );
if ( back(cmt_formatted) != '\n' ) if ( * back(& cmt_formatted) != '\n' )
append( cmt_formatted, "\n" ); append( & cmt_formatted, "\n" );
Code Code
result = make_code(); result = make_code();
@ -490,7 +490,7 @@ CodeComment def_comment( StrC content )
result->Name = get_cached_string( cmt_formatted ); result->Name = get_cached_string( cmt_formatted );
result->Content = result->Name; result->Content = result->Name;
free(cmt_formatted); free(& cmt_formatted);
return (CodeComment) result; return (CodeComment) result;
} }

View File

@ -93,7 +93,7 @@ struct Token
StrC type_str = ETokType::to_str( Type ); StrC type_str = ETokType::to_str( Type );
append_fmt( result, "Line: %d Column: %d, Type: %.*s Content: %.*s" append_fmt( & result, "Line: %d Column: %d, Type: %.*s Content: %.*s"
, Line, Column , Line, Column
, type_str.Len, type_str.Ptr , type_str.Len, type_str.Ptr
, Length, Text , Length, Text
@ -341,7 +341,7 @@ s32 lex_preprocessor_directive(
append( & Tokens, name ); append( & Tokens, name );
u64 key = crc32( name.Text, name.Length ); u64 key = crc32( name.Text, name.Length );
set<StrC>(defines, key, name ); set<StrC>(& defines, key, name );
} }
Token preprocess_content = { scanner, 0, TokType::Preprocess_Content, line, column, TF_Preprocess }; Token preprocess_content = { scanner, 0, TokType::Preprocess_Content, line, column, TF_Preprocess };
@ -597,7 +597,7 @@ TokArray lex( StrC content )
} }
u64 key = crc32( entry.Data, length ); u64 key = crc32( entry.Data, length );
set<StrC>(defines, key, entry ); set<StrC>(& defines, key, entry );
} }
clear(Tokens); clear(Tokens);

View File

@ -59,17 +59,17 @@ struct ParseContext
} }
String line = string_make( GlobalAllocator, { length, scope_start.Text } ); String line = string_make( GlobalAllocator, { length, scope_start.Text } );
append_fmt( result, "\tScope : %s\n", line ); append_fmt( & result, "\tScope : %s\n", line );
free(line); free(& line);
sptr dist = (sptr)last_valid.Text - (sptr)scope_start.Text + 2; sptr dist = (sptr)last_valid.Text - (sptr)scope_start.Text + 2;
sptr length_from_err = dist; sptr length_from_err = dist;
String line_from_err = string_make( GlobalAllocator, { length_from_err, last_valid.Text } ); String line_from_err = string_make( GlobalAllocator, { length_from_err, last_valid.Text } );
if ( length_from_err < 100 ) if ( length_from_err < 100 )
append_fmt(result, "\t(%d, %d):%*c\n", last_valid.Line, last_valid.Column, length_from_err, '^' ); append_fmt(& result, "\t(%d, %d):%*c\n", last_valid.Line, last_valid.Column, length_from_err, '^' );
else else
append_fmt(result, "\t(%d, %d)\n", last_valid.Line, last_valid.Column ); append_fmt(& result, "\t(%d, %d)\n", last_valid.Line, last_valid.Column );
StackNode* curr_scope = Scope; StackNode* curr_scope = Scope;
s32 level = 0; s32 level = 0;
@ -77,11 +77,11 @@ struct ParseContext
{ {
if ( curr_scope->Name ) if ( curr_scope->Name )
{ {
append_fmt(result, "\t%d: %s, AST Name: %.*s\n", level, curr_scope->ProcName.Ptr, curr_scope->Name.Length, curr_scope->Name.Text ); append_fmt(& result, "\t%d: %s, AST Name: %.*s\n", level, curr_scope->ProcName.Ptr, curr_scope->Name.Length, curr_scope->Name.Text );
} }
else else
{ {
append_fmt(result, "\t%d: %s\n", level, curr_scope->ProcName.Ptr ); append_fmt(& result, "\t%d: %s\n", level, curr_scope->ProcName.Ptr );
} }
curr_scope = curr_scope->Prev; curr_scope = curr_scope->Prev;
@ -290,7 +290,7 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true )
if ( tokleft ) if ( tokleft )
move_fwd(); move_fwd();
append( content, cut_ptr, cut_length ); append( & content, cut_ptr, cut_length );
last_cut = sptr( scanner ) - sptr( raw_text.Ptr ); last_cut = sptr( scanner ) - sptr( raw_text.Ptr );
continue; continue;
} }
@ -312,7 +312,7 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true )
if ( tokleft ) if ( tokleft )
move_fwd(); move_fwd();
append( content, cut_ptr, cut_length ); append( & content, cut_ptr, cut_length );
last_cut = sptr( scanner ) - sptr( raw_text.Ptr ); last_cut = sptr( scanner ) - sptr( raw_text.Ptr );
continue; continue;
} }
@ -326,7 +326,7 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true )
scanner += 2; scanner += 2;
tokleft -= 2; tokleft -= 2;
append( content, cut_ptr, cut_length ); append( & content, cut_ptr, cut_length );
last_cut = sptr( scanner ) - sptr( raw_text.Ptr ); last_cut = sptr( scanner ) - sptr( raw_text.Ptr );
continue; continue;
} }
@ -345,7 +345,7 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true )
if (tokleft) if (tokleft)
move_fwd(); move_fwd();
append( content, cut_ptr, cut_length ); append( & content, cut_ptr, cut_length );
last_cut = sptr( scanner ) - sptr( raw_text.Ptr ); last_cut = sptr( scanner ) - sptr( raw_text.Ptr );
continue; continue;
} }
@ -354,10 +354,10 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true )
if (scanner[0] == '\t') if (scanner[0] == '\t')
{ {
if (pos > last_cut) if (pos > last_cut)
append( content, cut_ptr, cut_length); append( & content, cut_ptr, cut_length);
if ( back( content ) != ' ' ) if ( * back( & content ) != ' ' )
append( content, ' '); append( & content, ' ');
move_fwd(); move_fwd();
last_cut = sptr(scanner) - sptr(raw_text.Ptr); last_cut = sptr(scanner) - sptr(raw_text.Ptr);
@ -373,17 +373,17 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true )
scanner += 2; scanner += 2;
tokleft -= 2; tokleft -= 2;
append( content, cut_ptr, cut_length ); append( & content, cut_ptr, cut_length );
last_cut = sptr( scanner ) - sptr( raw_text.Ptr ); last_cut = sptr( scanner ) - sptr( raw_text.Ptr );
continue; continue;
} }
if ( pos > last_cut ) if ( pos > last_cut )
append( content, cut_ptr, cut_length ); append( & content, cut_ptr, cut_length );
// Replace with a space // Replace with a space
if ( back( content ) != ' ' ) if ( * back( & content ) != ' ' )
append( content, ' ' ); append( & content, ' ' );
scanner += 2; scanner += 2;
tokleft -= 2; tokleft -= 2;
@ -400,17 +400,17 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true )
move_fwd(); move_fwd();
append( content, cut_ptr, cut_length ); append( & content, cut_ptr, cut_length );
last_cut = sptr( scanner ) - sptr( raw_text.Ptr ); last_cut = sptr( scanner ) - sptr( raw_text.Ptr );
continue; continue;
} }
if ( pos > last_cut ) if ( pos > last_cut )
append( content, cut_ptr, cut_length ); append( & content, cut_ptr, cut_length );
// Replace with a space // Replace with a space
if ( back( content ) != ' ' ) if ( * back( & content ) != ' ' )
append( content, ' ' ); append( & content, ' ' );
move_fwd(); move_fwd();
@ -421,7 +421,7 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true )
// Escaped newlines // Escaped newlines
if ( scanner[0] == '\\' ) if ( scanner[0] == '\\' )
{ {
append( content, cut_ptr, cut_length ); append( & content, cut_ptr, cut_length );
s32 amount_to_skip = 1; s32 amount_to_skip = 1;
if ( tokleft > 1 && scanner[1] == '\n' ) if ( tokleft > 1 && scanner[1] == '\n' )
@ -448,7 +448,7 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true )
// Consectuive spaces // Consectuive spaces
if ( tokleft > 1 && char_is_space( scanner[0] ) && char_is_space( scanner[ 1 ] ) ) if ( tokleft > 1 && char_is_space( scanner[0] ) && char_is_space( scanner[ 1 ] ) )
{ {
append( content, cut_ptr, cut_length ); append( & content, cut_ptr, cut_length );
do do
{ {
move_fwd(); move_fwd();
@ -458,8 +458,9 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true )
last_cut = sptr( scanner ) - sptr( raw_text.Ptr ); last_cut = sptr( scanner ) - sptr( raw_text.Ptr );
// Preserve only 1 space of formattting // Preserve only 1 space of formattting
if ( back( content ) != ' ' ) char* last = back(& content);
append( content, ' ' ); if ( last == nullptr || * last != ' ' )
append( & content, ' ' );
continue; continue;
} }
@ -469,7 +470,7 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true )
if ( last_cut < raw_text.Len ) if ( last_cut < raw_text.Len )
{ {
append( content, cut_ptr, raw_text.Len - last_cut ); append( & content, cut_ptr, raw_text.Len - last_cut );
} }
#undef cut_ptr #undef cut_ptr
@ -1040,7 +1041,7 @@ CodeBody parse_class_struct_body( TokType which, Token name )
if ( attributes ) if ( attributes )
{ {
String fused = string_make_reserve( GlobalAllocator, length(attributes->Content) + length(more_attributes->Content) ); String fused = string_make_reserve( GlobalAllocator, length(attributes->Content) + length(more_attributes->Content) );
append_fmt( fused, "%S %S", attributes->Content, more_attributes->Content ); append_fmt( & fused, "%S %S", attributes->Content, more_attributes->Content );
attributes->Name = get_cached_string(fused); attributes->Name = get_cached_string(fused);
attributes->Content = attributes->Name; attributes->Content = attributes->Name;

View File

@ -49,10 +49,6 @@ template<class Type> bool resize (Array(Type)* array, usize n
template<class Type> bool set_capacity (Array(Type)* array, usize new_capacity); template<class Type> bool set_capacity (Array(Type)* array, usize new_capacity);
template<class Type> ArrayHeader* get_header (Array(Type) array); template<class Type> ArrayHeader* get_header (Array(Type) array);
// template<class Type> forceinline Type* begin(Array<Type> array) { return array; }
// template<class Type> forceinline Type* end(Array<Type> array) { return array + get_header(array)->Num; }
// template<class Type> forceinline Type* next(Array<Type> array, Type* entry) { return entry + 1; }
struct ArrayHeader { struct ArrayHeader {
AllocatorInfo Allocator; AllocatorInfo Allocator;
usize Capacity; usize Capacity;
@ -114,6 +110,10 @@ template<class Type> bool set_capacity(Array<Type>& array, usize new_cap
template<class Type> forceinline Type* begin(Array<Type>& array) { return array; } template<class Type> forceinline Type* begin(Array<Type>& array) { return array; }
template<class Type> forceinline Type* end(Array<Type>& array) { return array + get_header(array)->Num; } template<class Type> forceinline Type* end(Array<Type>& array) { return array + get_header(array)->Num; }
template<class Type> forceinline Type* next(Array<Type>& array, Type* entry) { return entry + 1; } template<class Type> forceinline Type* next(Array<Type>& array, Type* entry) { return entry + 1; }
#else
template<class Type> forceinline Type* begin(Array<Type> array) { return array; }
template<class Type> forceinline Type* end(Array<Type> array) { return array + get_header(array)->Num; }
template<class Type> forceinline Type* next(Array<Type> array, Type* entry) { return entry + 1; }
#endif #endif
template<class Type> inline template<class Type> inline
@ -402,21 +402,21 @@ struct HashTableEntry {
template<class Type> HashTable<Type> hashtable_init(AllocatorInfo allocator); template<class Type> HashTable<Type> hashtable_init(AllocatorInfo allocator);
template<class Type> HashTable<Type> hashtable_init_reserve(AllocatorInfo allocator, usize num); template<class Type> HashTable<Type> hashtable_init_reserve(AllocatorInfo allocator, usize num);
template<class Type> void clear(HashTable<Type>& table); template<class Type> void clear (HashTable<Type> table);
template<class Type> void destroy(HashTable<Type>& table); template<class Type> void destroy (HashTable<Type>* table);
template<class Type> Type* get(HashTable<Type>& table, u64 key); template<class Type> Type* get (HashTable<Type> table, u64 key);
template<class Type> void grow(HashTable<Type>& table); template<class Type> void grow (HashTable<Type>* table);
template<class Type> void rehash(HashTable<Type>& table, ssize new_num); template<class Type> void rehash (HashTable<Type>* table, ssize new_num);
template<class Type> void rehash_fast(HashTable<Type>& table); template<class Type> void rehash_fast (HashTable<Type> table);
template<class Type> void remove(HashTable<Type>& table, u64 key); template<class Type> void remove (HashTable<Type> table, u64 key);
template<class Type> void remove_entry(HashTable<Type>& table, ssize idx); template<class Type> void remove_entry (HashTable<Type> table, ssize idx);
template<class Type> void set(HashTable<Type>& table, u64 key, Type value); template<class Type> void set (HashTable<Type>* table, u64 key, Type value);
template<class Type> ssize slot(HashTable<Type>& table, u64 key); template<class Type> ssize slot (HashTable<Type> table, u64 key);
template<class Type> ssize add_entry(HashTable<Type>& table, u64 key); template<class Type> ssize add_entry (HashTable<Type>* table, u64 key);
template<class Type> HashTableFindResult find(HashTable<Type>& table, u64 key); template<class Type> HashTableFindResult find (HashTable<Type> table, u64 key);
template<class Type> bool full(HashTable<Type>& table); template<class Type> bool full (HashTable<Type> table);
template<class Type> void map(HashTable<Type>& table, void (*map_proc)(u64 key, Type value)); template<class Type> void map (HashTable<Type> table, void (*map_proc)(u64 key, Type value));
template<class Type> void map_mut(HashTable<Type>& table, void (*map_proc)(u64 key, Type* value)); template<class Type> void map_mut (HashTable<Type> table, void (*map_proc)(u64 key, Type* value));
static constexpr f32 HashTable_CriticalLoadScale = 0.7f; static constexpr f32 HashTable_CriticalLoadScale = 0.7f;
@ -447,6 +447,14 @@ struct HashTable
#endif #endif
}; };
#if GEN_SUPPORT_CPP_REFERENCES
template<class Type> void destroy (HashTable<Type>& table) { destroy(& table); }
template<class Type> void grow (HashTable<Type>& table) { grow(& table); }
template<class Type> void rehash (HashTable<Type>& table, ssize new_num) { rehash(& table, new_num); }
template<class Type> void set (HashTable<Type>& table, u64 key, Type value) { set(& table, key, value); }
template<class Type> ssize add_entry(HashTable<Type>& table, u64 key) { add_entry(& table, key); }
#endif
template<typename Type> inline template<typename Type> inline
HashTable<Type> hashtable_init(AllocatorInfo allocator) { HashTable<Type> hashtable_init(AllocatorInfo allocator) {
HashTable<Type> result = hashtable_init_reserve<Type>(allocator, 8); HashTable<Type> result = hashtable_init_reserve<Type>(allocator, 8);
@ -468,65 +476,65 @@ HashTable<Type> hashtable_init_reserve(AllocatorInfo allocator, usize num)
} }
template<typename Type> inline template<typename Type> inline
void clear(HashTable<Type>& table) { void clear(HashTable<Type> table) {
clear(table.Entries); clear(table.Entries);
fill<ssize>(table.Hashes, 0, num(table.Hashes), -1); fill<ssize>(table.Hashes, 0, num(table.Hashes), -1);
} }
template<typename Type> inline template<typename Type> inline
void destroy(HashTable<Type>& table) { void destroy(HashTable<Type>* table) {
if (table.Hashes && get_header(table.Hashes)->Capacity) { if (table->Hashes && get_header(table->Hashes)->Capacity) {
free(& table.Hashes); free(& table->Hashes);
free(& table.Entries); free(& table->Entries);
} }
} }
template<typename Type> inline template<typename Type> inline
Type* get(HashTable<Type>& table, u64 key) { Type* get(HashTable<Type> table, u64 key) {
ssize idx = find(table, key).EntryIndex; ssize idx = find(table, key).EntryIndex;
if (idx >= 0) if (idx >= 0)
return &table.Entries[idx].Value; return & table.Entries[idx].Value;
return nullptr; return nullptr;
} }
template<typename Type> inline template<typename Type> inline
void map(HashTable<Type>& table, void (*map_proc)(u64 key, Type value)) { void map(HashTable<Type> table, void (*map_proc)(u64 key, Type value)) {
GEN_ASSERT_NOT_NULL(map_proc); GEN_ASSERT_NOT_NULL(map_proc);
for (ssize idx = 0; idx < ssize(table.Entries.num()); ++idx) { for (ssize idx = 0; idx < ssize(num(table.Entries)); ++idx) {
map_proc(table.Entries[idx].Key, table.Entries[idx].Value); map_proc(table.Entries[idx].Key, table.Entries[idx].Value);
} }
} }
template<typename Type> inline template<typename Type> inline
void map_mut(HashTable<Type>& table, void (*map_proc)(u64 key, Type* value)) { void map_mut(HashTable<Type> table, void (*map_proc)(u64 key, Type* value)) {
GEN_ASSERT_NOT_NULL(map_proc); GEN_ASSERT_NOT_NULL(map_proc);
for (ssize idx = 0; idx < ssize(table.Entries.num()); ++idx) { for (ssize idx = 0; idx < ssize(num(table.Entries)); ++idx) {
map_proc(table.Entries[idx].Key, &table.Entries[idx].Value); map_proc(table.Entries[idx].Key, & table.Entries[idx].Value);
} }
} }
template<typename Type> inline template<typename Type> inline
void grow(HashTable<Type>& table) { void grow(HashTable<Type>* table) {
ssize new_num = array_grow_formula(num(table.Entries)); ssize new_num = array_grow_formula(num(table->Entries));
rehash(table, new_num); rehash(table, new_num);
} }
template<typename Type> inline template<typename Type> inline
void rehash(HashTable<Type>& table, ssize new_num) void rehash(HashTable<Type>* table, ssize new_num)
{ {
ssize last_added_index; ssize last_added_index;
HashTable<Type> new_ht = hashtable_init_reserve<Type>(get_header(table.Hashes)->Allocator, new_num); HashTable<Type> new_ht = hashtable_init_reserve<Type>(get_header(table->Hashes)->Allocator, new_num);
for (ssize idx = 0; idx < ssize(num(table.Entries)); ++idx) for (ssize idx = 0; idx < ssize(num(table->Entries)); ++idx)
{ {
HashTableFindResult find_result; HashTableFindResult find_result;
HashTableEntry<Type>& entry = table.Entries[idx]; HashTableEntry<Type>& entry = table->Entries[idx];
find_result = find(new_ht, entry.Key); find_result = find(new_ht, entry.Key);
last_added_index = add_entry(new_ht, entry.Key); last_added_index = add_entry(& new_ht, entry.Key);
if (find_result.PrevIndex < 0) if (find_result.PrevIndex < 0)
new_ht.Hashes[find_result.HashIndex] = last_added_index; new_ht.Hashes[find_result.HashIndex] = last_added_index;
@ -538,21 +546,21 @@ void rehash(HashTable<Type>& table, ssize new_num)
} }
destroy(table); destroy(table);
table = new_ht; * table = new_ht;
} }
template<typename Type> inline template<typename Type> inline
void rehash_fast(HashTable<Type>& table) void rehash_fast(HashTable<Type> table)
{ {
ssize idx; ssize idx;
for (idx = 0; idx < ssize(table.Entries.num()); idx++) for (idx = 0; idx < ssize(num(table.Entries)); idx++)
table.Entries[idx].Next = -1; table.Entries[idx].Next = -1;
for (idx = 0; idx < ssize(table.Hashes.num()); idx++) for (idx = 0; idx < ssize(num(table.Hashes)); idx++)
table.Hashes[idx] = -1; table.Hashes[idx] = -1;
for (idx = 0; idx < ssize(table.Entries.num()); idx++) for (idx = 0; idx < ssize(num(table.Entries)); idx++)
{ {
HashTableEntry<Type>* entry; HashTableEntry<Type>* entry;
HashTableFindResult find_result; HashTableFindResult find_result;
@ -568,30 +576,30 @@ void rehash_fast(HashTable<Type>& table)
} }
template<typename Type> inline template<typename Type> inline
void remove(HashTable<Type>& table, u64 key) { void remove(HashTable<Type> table, u64 key) {
HashTableFindResult find_result = find(table, key); HashTableFindResult find_result = find(table, key);
if (find_result.EntryIndex >= 0) { if (find_result.EntryIndex >= 0) {
table.Entries.remove_at(find_result.EntryIndex); remove_at(table.Entries, find_result.EntryIndex);
rehash_fast(table); rehash_fast(table);
} }
} }
template<typename Type> inline template<typename Type> inline
void remove_entry(HashTable<Type>& table, ssize idx) { void remove_entry(HashTable<Type> table, ssize idx) {
table.Entries.remove_at(idx); remove_at(table.Entries, idx);
} }
template<typename Type> inline template<typename Type> inline
void set(HashTable<Type>& table, u64 key, Type value) void set(HashTable<Type>* table, u64 key, Type value)
{ {
ssize idx; ssize idx;
HashTableFindResult find_result; HashTableFindResult find_result;
if (full(table)) if (full(* table))
grow(table); grow(table);
find_result = find(table, key); find_result = find(* table, key);
if (find_result.EntryIndex >= 0) { if (find_result.EntryIndex >= 0) {
idx = find_result.EntryIndex; idx = find_result.EntryIndex;
} }
@ -600,22 +608,22 @@ void set(HashTable<Type>& table, u64 key, Type value)
idx = add_entry(table, key); idx = add_entry(table, key);
if (find_result.PrevIndex >= 0) { if (find_result.PrevIndex >= 0) {
table.Entries[find_result.PrevIndex].Next = idx; table->Entries[find_result.PrevIndex].Next = idx;
} }
else { else {
table.Hashes[find_result.HashIndex] = idx; table->Hashes[find_result.HashIndex] = idx;
} }
} }
table.Entries[idx].Value = value; table->Entries[idx].Value = value;
if (full(table)) if (full(* table))
grow(table); grow(table);
} }
template<typename Type> inline template<typename Type> inline
ssize slot(HashTable<Type>& table, u64 key) { ssize slot(HashTable<Type> table, u64 key) {
for (ssize idx = 0; idx < ssize(table.Hashes.num()); ++idx) for (ssize idx = 0; idx < ssize(num(table.Hashes)); ++idx)
if (table.Hashes[idx] == key) if (table.Hashes[idx] == key)
return idx; return idx;
@ -623,17 +631,17 @@ ssize slot(HashTable<Type>& table, u64 key) {
} }
template<typename Type> inline template<typename Type> inline
ssize add_entry(HashTable<Type>& table, u64 key) { ssize add_entry(HashTable<Type>* table, u64 key) {
ssize idx; ssize idx;
HashTableEntry<Type> entry = { key, -1 }; HashTableEntry<Type> entry = { key, -1 };
idx = num(table.Entries); idx = num(table->Entries);
append( & table.Entries, entry); append( & table->Entries, entry);
return idx; return idx;
} }
template<typename Type> inline template<typename Type> inline
HashTableFindResult find(HashTable<Type>& table, u64 key) HashTableFindResult find(HashTable<Type> table, u64 key)
{ {
HashTableFindResult result = { -1, -1, -1 }; HashTableFindResult result = { -1, -1, -1 };
@ -656,7 +664,7 @@ HashTableFindResult find(HashTable<Type>& table, u64 key)
} }
template<typename Type> inline template<typename Type> inline
bool full(HashTable<Type>& table) { bool full(HashTable<Type> table) {
usize critical_load = usize(HashTable_CriticalLoadScale * f32(num(table.Hashes))); usize critical_load = usize(HashTable_CriticalLoadScale * f32(num(table.Hashes)));
b32 result = num(table.Entries) > critical_load; b32 result = num(table.Entries) > critical_load;
return result; return result;

View File

@ -33,40 +33,41 @@ StrC to_str( char const* str ) {
struct StringHeader; struct StringHeader;
struct String; struct String;
String string_make(AllocatorInfo allocator, char const* str); usize string_grow_formula(usize value);
String string_make(AllocatorInfo allocator, StrC str);
String string_make_reserve(AllocatorInfo allocator, ssize capacity); String string_make (AllocatorInfo allocator, char const* str);
String string_make_length(AllocatorInfo allocator, char const* str, ssize length); String string_make (AllocatorInfo allocator, StrC str);
String string_fmt(AllocatorInfo allocator, char* buf, ssize buf_size, char const* fmt, ...); String string_make_reserve (AllocatorInfo allocator, ssize capacity);
String string_fmt_buf(AllocatorInfo allocator, char const* fmt, ...); String string_make_length (AllocatorInfo allocator, char const* str, ssize length);
String string_join(AllocatorInfo allocator, char const** parts, ssize num_parts, char const* glue); String string_fmt (AllocatorInfo allocator, char* buf, ssize buf_size, char const* fmt, ...);
usize string_grow_formula(usize value); String string_fmt_buf (AllocatorInfo allocator, char const* fmt, ...);
bool are_equal(String const& lhs, String const& rhs); String string_join (AllocatorInfo allocator, char const** parts, ssize num_parts, char const* glue);
bool are_equal(String const& lhs, StrC rhs); bool are_equal (String const lhs, String const rhs);
bool make_space_for(String& str, char const* to_append, ssize add_len); bool are_equal (String const lhs, StrC rhs);
bool append(String& str, char c); bool make_space_for (String* str, char const* to_append, ssize add_len);
bool append(String& str, char const* str_to_append); bool append (String* str, char c);
bool append(String& str, char const* str_to_append, ssize length); bool append (String* str, char const* str_to_append);
bool append(String& str, StrC str_to_append); bool append (String* str, char const* str_to_append, ssize length);
bool append(String& str, const String other); bool append (String* str, StrC str_to_append);
bool append_fmt(String& str, char const* fmt, ...); bool append (String* str, String const other);
ssize avail_space(String const& str); bool append_fmt (String* str, char const* fmt, ...);
char& back(String& str); ssize avail_space (String const str);
bool contains(String const& str, StrC substring); char* back (String str);
bool contains(String const& str, String const& substring); bool contains (String const str, StrC substring);
ssize capacity(String const& str); bool contains (String const str, String const substring);
void clear(String& str); ssize capacity (String const str);
String duplicate(String const& str, AllocatorInfo allocator); void clear (String str);
void free(String& str); String duplicate (String const str, AllocatorInfo allocator);
StringHeader& get_header(String& str); void free (String* str);
ssize length(String const& str); StringHeader* get_header (String str);
b32 starts_with(String const& str, StrC substring); ssize length (String const str);
b32 starts_with(String const& str, String substring); b32 starts_with (String const str, StrC substring);
void skip_line(String& str); b32 starts_with (String const str, String substring);
void strip_space(String& str); void skip_line (String str);
void trim(String& str, char const* cut_set); void strip_space (String str);
void trim_space(String& str); void trim (String str, char const* cut_set);
String visualize_whitespace(String const& str); void trim_space (String str);
String visualize_whitespace(String const str);
struct StringHeader { struct StringHeader {
AllocatorInfo Allocator; AllocatorInfo Allocator;
@ -129,31 +130,31 @@ struct String
return GEN_NS string_make(allocator, buf); return GEN_NS string_make(allocator, buf);
} }
forceinline bool make_space_for(char const* str, ssize add_len) { return GEN_NS make_space_for(*this, str, add_len); } forceinline bool make_space_for(char const* str, ssize add_len) { return GEN_NS make_space_for(this, str, add_len); }
forceinline bool append(char c) { return GEN_NS append(*this, c); } forceinline bool append(char c) { return GEN_NS append(this, c); }
forceinline bool append(char const* str) { return GEN_NS append(*this, str); } forceinline bool append(char const* str) { return GEN_NS append(this, str); }
forceinline bool append(char const* str, ssize length) { return GEN_NS append(*this, str, length); } forceinline bool append(char const* str, ssize length) { return GEN_NS append(this, str, length); }
forceinline bool append(StrC str) { return GEN_NS append(*this, str); } forceinline bool append(StrC str) { return GEN_NS append(this, str); }
forceinline bool append(const String other) { return GEN_NS append(*this, other); } forceinline bool append(const String other) { return GEN_NS append(this, other); }
forceinline ssize avail_space() const { return GEN_NS avail_space(*this); } forceinline ssize avail_space() const { return GEN_NS avail_space(* this); }
forceinline char& back() { return GEN_NS back(*this); } forceinline char* back() { return GEN_NS back(* this); }
forceinline bool contains(StrC substring) const { return GEN_NS contains(*this, substring); } forceinline bool contains(StrC substring) const { return GEN_NS contains(* this, substring); }
forceinline bool contains(String const& substring) const { return GEN_NS contains(*this, substring); } forceinline bool contains(String const& substring) const { return GEN_NS contains(* this, substring); }
forceinline ssize capacity() const { return GEN_NS capacity(*this); } forceinline ssize capacity() const { return GEN_NS capacity(* this); }
forceinline void clear() { GEN_NS clear(*this); } forceinline void clear() { GEN_NS clear(* this); }
forceinline String duplicate(AllocatorInfo allocator) const { return GEN_NS duplicate(*this, allocator); } forceinline String duplicate(AllocatorInfo allocator) const { return GEN_NS duplicate(* this, allocator); }
forceinline void free() { GEN_NS free(*this); } forceinline void free() { GEN_NS free(this); }
forceinline bool is_equal(String const& other) const { return GEN_NS are_equal(* this, other); } forceinline bool is_equal(String const& other) const { return GEN_NS are_equal(* this, other); }
forceinline bool is_equal(StrC other) const { return GEN_NS are_equal(* this, other); } forceinline bool is_equal(StrC other) const { return GEN_NS are_equal(* this, other); }
forceinline ssize length() const { return GEN_NS length(*this); } forceinline ssize length() const { return GEN_NS length(* this); }
forceinline b32 starts_with(StrC substring) const { return GEN_NS starts_with(*this, substring); } forceinline b32 starts_with(StrC substring) const { return GEN_NS starts_with(* this, substring); }
forceinline b32 starts_with(String substring) const { return GEN_NS starts_with(*this, substring); } forceinline b32 starts_with(String substring) const { return GEN_NS starts_with(* this, substring); }
forceinline void skip_line() { GEN_NS skip_line(*this); } forceinline void skip_line() { GEN_NS skip_line(* this); }
forceinline void strip_space() { GEN_NS strip_space(*this); } forceinline void strip_space() { GEN_NS strip_space(* this); }
forceinline void trim(char const* cut_set) { GEN_NS trim(*this, cut_set); } forceinline void trim(char const* cut_set) { GEN_NS trim(* this, cut_set); }
forceinline void trim_space() { GEN_NS trim_space(*this); } forceinline void trim_space() { GEN_NS trim_space(* this); }
forceinline String visualize_whitespace() const { return GEN_NS visualize_whitespace(*this); } forceinline String visualize_whitespace() const { return GEN_NS visualize_whitespace(* this); }
forceinline StringHeader& get_header() { return GEN_NS get_header(*this); } forceinline StringHeader& get_header() { return * GEN_NS get_header(* this); }
bool append_fmt(char const* fmt, ...) { bool append_fmt(char const* fmt, ...) {
ssize res; ssize res;
@ -164,13 +165,26 @@ struct String
res = str_fmt_va(buf, count_of(buf) - 1, fmt, va) - 1; res = str_fmt_va(buf, count_of(buf) - 1, fmt, va) - 1;
va_end(va); va_end(va);
return GEN_NS append(*this, buf, res); return GEN_NS append(this, buf, res);
} }
#pragma endregion Member Mapping #pragma endregion Member Mapping
#endif #endif
}; };
#endif #endif
#if GEN_SUPPORT_CPP_REFERENCES
bool make_space_for(String& str, char const* to_append, ssize add_len);
bool append(String& str, char c);
bool append(String& str, char const* str_to_append);
bool append(String& str, char const* str_to_append, ssize length);
bool append(String& str, StrC str_to_append);
bool append(String& str, const String other);
bool append_fmt(String& str, char const* fmt, ...);
char& back(String& str);
void clear(String& str);
void free(String& str);
#endif
inline char* begin(String& str) { return str; } inline char* begin(String& str) { return str; }
inline char* end(String& str) { return scast(char*, str) + length(str); } inline char* end(String& str) { return scast(char*, str) + length(str); }
inline char* next(String& str) { return scast(char*, str) + 1; } inline char* next(String& str) { return scast(char*, str) + 1; }
@ -223,57 +237,64 @@ String string_join(AllocatorInfo allocator, char const** parts, ssize num_parts,
for (ssize idx = 0; idx < num_parts; ++idx) for (ssize idx = 0; idx < num_parts; ++idx)
{ {
append(result, parts[idx]); append(& result, parts[idx]);
if (idx < num_parts - 1) if (idx < num_parts - 1)
append(result, glue); append(& result, glue);
} }
return result; return result;
} }
inline inline
bool append(String& str, char c) { bool append(String* str, char c) {
GEN_ASSERT(str != nullptr);
return append(str, &c, 1); return append(str, &c, 1);
} }
inline inline
bool append(String& str, char const* str_to_append) { bool append(String* str, char const* str_to_append) {
GEN_ASSERT(str != nullptr);
return append(str, str_to_append, str_len(str_to_append)); return append(str, str_to_append, str_len(str_to_append));
} }
inline inline
bool append(String& str, char const* str_to_append, ssize append_length) bool append(String* str, char const* str_to_append, ssize append_length)
{ {
GEN_ASSERT(str != nullptr);
if (sptr(str_to_append) > 0) if (sptr(str_to_append) > 0)
{ {
ssize curr_len = length(str); ssize curr_len = length(* str);
if (!make_space_for(str, str_to_append, append_length)) if ( ! make_space_for(str, str_to_append, append_length))
return false; return false;
StringHeader& header = get_header(str); StringHeader* header = get_header(* str);
mem_copy( scast(char*, str) + curr_len, str_to_append, append_length); char* Data = * str;
mem_copy( Data + curr_len, str_to_append, append_length);
str[curr_len + append_length] = '\0'; Data[curr_len + append_length] = '\0';
header.Length = curr_len + append_length; header->Length = curr_len + append_length;
} }
return str_to_append != nullptr; return str_to_append != nullptr;
} }
inline inline
bool append(String& str, StrC str_to_append) { bool append(String* str, StrC str_to_append) {
GEN_ASSERT(str != nullptr);
return append(str, str_to_append.Ptr, str_to_append.Len); return append(str, str_to_append.Ptr, str_to_append.Len);
} }
inline inline
bool append(String& str, const String other) { bool append(String* str, const String other) {
GEN_ASSERT(str != nullptr);
return append(str, other, length(other)); return append(str, other, length(other));
} }
bool append_fmt(String& str, char const* fmt, ...) { bool append_fmt(String* str, char const* fmt, ...) {
GEN_ASSERT(str != nullptr);
ssize res; ssize res;
char buf[GEN_PRINTF_MAXLEN] = { 0 }; char buf[GEN_PRINTF_MAXLEN] = { 0 };
@ -286,7 +307,7 @@ bool append_fmt(String& str, char const* fmt, ...) {
} }
inline inline
bool are_equal(String const& lhs, String const& rhs) bool are_equal(String const lhs, String const rhs)
{ {
if (length(lhs) != length(rhs)) if (length(lhs) != length(rhs))
return false; return false;
@ -299,7 +320,7 @@ bool are_equal(String const& lhs, String const& rhs)
} }
inline inline
bool are_equal(String const& lhs, StrC rhs) bool are_equal(String const lhs, StrC rhs)
{ {
if (length(lhs) != (rhs.Len)) if (length(lhs) != (rhs.Len))
return false; return false;
@ -312,26 +333,26 @@ bool are_equal(String const& lhs, StrC rhs)
} }
inline inline
ssize avail_space(String const& str) { ssize avail_space(String const str) {
StringHeader const& header = *rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader)); StringHeader const* header = rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader));
return header.Capacity - header.Length; return header->Capacity - header->Length;
} }
inline inline
char& back(String& str) { char* back(String* str) {
return str[length(str) - 1]; return & (*str)[length(* str) - 1];
} }
inline inline
bool contains(String const& str, StrC substring) bool contains(String const str, StrC substring)
{ {
StringHeader const& header = *rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader)); StringHeader const* header = rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader));
if (substring.Len > header.Length) if (substring.Len > header->Length)
return false; return false;
ssize main_len = header.Length; ssize main_len = header->Length;
ssize sub_len = substring.Len; ssize sub_len = substring.Len;
for (ssize idx = 0; idx <= main_len - sub_len; ++idx) for (ssize idx = 0; idx <= main_len - sub_len; ++idx)
{ {
@ -343,15 +364,15 @@ bool contains(String const& str, StrC substring)
} }
inline inline
bool contains(String const& str, String const& substring) bool contains(String const str, String const substring)
{ {
StringHeader const& header = *rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader)); StringHeader const* header = rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader));
if (length(substring) > header.Length) if (length(substring) > header->Length)
return false; return false;
ssize main_len = header.Length; ssize main_len = header->Length;
ssize sub_len = length(substring); ssize sub_len = length(substring);
for (ssize idx = 0; idx <= main_len - sub_len; ++idx) for (ssize idx = 0; idx <= main_len - sub_len; ++idx)
{ {
@ -363,46 +384,47 @@ bool contains(String const& str, String const& substring)
} }
inline inline
ssize capacity(String const& str) { ssize capacity(String const str) {
StringHeader const& header = *rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader)); StringHeader const* header = rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader));
return header.Capacity; return header->Capacity;
} }
inline inline
void clear(String& str) { void clear(String str) {
get_header(str).Length = 0; get_header(str)->Length = 0;
} }
inline inline
String duplicate(String const& str, AllocatorInfo allocator) { String duplicate(String const str, AllocatorInfo allocator) {
return string_make_length(allocator, str, length(str)); return string_make_length(allocator, str, length(str));
} }
inline inline
void free(String& str) { void free(String* str) {
if (! str) GEN_ASSERT(str != nullptr);
return; if (! (* str))
return;
StringHeader& header = get_header(str); StringHeader* header = get_header(* str);
GEN_NS free(header.Allocator, &header); GEN_NS free(header->Allocator, header);
} }
inline inline
StringHeader& get_header(String& str) { StringHeader* get_header(String str) {
return *(StringHeader*)(scast(char*, str) - sizeof(StringHeader)); return (StringHeader*)(scast(char*, str) - sizeof(StringHeader));
} }
inline inline
ssize length(String const& str) ssize length(String const str)
{ {
StringHeader const& header = *rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader)); StringHeader const& header = *rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader));
return header.Length; return header.Length;
} }
inline inline
bool make_space_for(String& str, char const* to_append, ssize add_len) bool make_space_for(String* str, char const* to_append, ssize add_len)
{ {
ssize available = avail_space(str); ssize available = avail_space(* str);
if (available >= add_len) { if (available >= add_len) {
return true; return true;
@ -413,12 +435,12 @@ bool make_space_for(String& str, char const* to_append, ssize add_len)
void* ptr; void* ptr;
void* new_ptr; void* new_ptr;
AllocatorInfo allocator = get_header(str).Allocator; AllocatorInfo allocator = get_header(* str)->Allocator;
StringHeader* header = nullptr; StringHeader* header = nullptr;
new_len = string_grow_formula(length(str) + add_len); new_len = string_grow_formula(length(* str) + add_len);
ptr = &get_header(str); ptr = get_header(* str);
old_size = size_of(StringHeader) + length(str) + 1; old_size = size_of(StringHeader) + length(* str) + 1;
new_size = size_of(StringHeader) + new_len + 1; new_size = size_of(StringHeader) + new_len + 1;
new_ptr = resize(allocator, ptr, old_size, new_size); new_ptr = resize(allocator, ptr, old_size, new_size);
@ -428,16 +450,17 @@ bool make_space_for(String& str, char const* to_append, ssize add_len)
header = rcast(StringHeader*, new_ptr); header = rcast(StringHeader*, new_ptr);
header->Allocator = allocator; header->Allocator = allocator;
header->Capacity = new_len; header->Capacity = new_len;
str.Data = rcast(char*, header + 1); char** Data = rcast(char**, str);
* Data = rcast(char*, header + 1);
return true; return true;
} }
} }
inline inline
b32 starts_with(String const& str, StrC substring) { b32 starts_with(String const str, StrC substring) {
if (substring.Len > length(str)) if (substring.Len > length(str))
return false; return false;
@ -446,7 +469,7 @@ b32 starts_with(String const& str, StrC substring) {
} }
inline inline
b32 starts_with(String const& str, String substring) { b32 starts_with(String const str, String substring) {
if (length(substring) > length(str)) if (length(substring) > length(str))
return false; return false;
@ -455,7 +478,7 @@ b32 starts_with(String const& str, String substring) {
} }
inline inline
void skip_line(String& str) void skip_line(String str)
{ {
#define current (*scanner) #define current (*scanner)
char* scanner = str.Data; char* scanner = str.Data;
@ -471,23 +494,23 @@ void skip_line(String& str)
mem_move(str.Data, scanner, new_length); mem_move(str.Data, scanner, new_length);
StringHeader* header = &get_header(str); StringHeader* header = get_header(str);
header->Length = new_length; header->Length = new_length;
#undef current #undef current
} }
inline inline
void strip_space(String& str) void strip_space(String str)
{ {
char* write_pos = str.Data; char* write_pos = str;
char* read_pos = str.Data; char* read_pos = str;
while (*read_pos) while (* read_pos)
{ {
if (!char_is_space(*read_pos)) if (! char_is_space(* read_pos))
{ {
*write_pos = *read_pos; * write_pos = * read_pos;
write_pos++; write_pos++;
} }
read_pos++; read_pos++;
} }
@ -495,11 +518,11 @@ void strip_space(String& str)
write_pos[0] = '\0'; // Null-terminate the modified string write_pos[0] = '\0'; // Null-terminate the modified string
// Update the length if needed // Update the length if needed
get_header(str).Length = write_pos - str.Data; get_header(str)->Length = write_pos - str.Data;
} }
inline inline
void trim(String& str, char const* cut_set) void trim(String str, char const* cut_set)
{ {
ssize len = 0; ssize len = 0;
@ -519,16 +542,16 @@ void trim(String& str, char const* cut_set)
str.Data[len] = '\0'; str.Data[len] = '\0';
get_header(str).Length = len; get_header(str)->Length = len;
} }
inline inline
void trim_space(String& str) { void trim_space(String str) {
trim(str, " \t\r\n\v\f"); trim(str, " \t\r\n\v\f");
} }
inline inline
String visualize_whitespace(String const& str) String visualize_whitespace(String const str)
{ {
StringHeader* header = (StringHeader*)(scast(char const*, str) - sizeof(StringHeader)); StringHeader* header = (StringHeader*)(scast(char const*, str) - sizeof(StringHeader));
String result = string_make_reserve(header->Allocator, length(str) * 2); // Assume worst case for space requirements. String result = string_make_reserve(header->Allocator, length(str) * 2); // Assume worst case for space requirements.
@ -536,25 +559,25 @@ String visualize_whitespace(String const& str)
for (auto c : str) switch (c) for (auto c : str) switch (c)
{ {
case ' ': case ' ':
append(result, txt("·")); append(& result, txt("·"));
break; break;
case '\t': case '\t':
append(result, txt("")); append(& result, txt(""));
break; break;
case '\n': case '\n':
append(result, txt("")); append(& result, txt(""));
break; break;
case '\r': case '\r':
append(result, txt("")); append(& result, txt(""));
break; break;
case '\v': case '\v':
append(result, txt("")); append(& result, txt(""));
break; break;
case '\f': case '\f':
append(result, txt("")); append(& result, txt(""));
break; break;
default: default:
append(result, c); append(& result, c);
break; break;
} }

View File

@ -27,8 +27,8 @@ CodeBody gen_ecode( char const* path )
{ {
char const* code = node.string; char const* code = node.string;
append_fmt( enum_entries, "%s,\n", code ); append_fmt( & enum_entries, "%s,\n", code );
append_fmt( to_str_entries, "{ sizeof(\"%s\"), \"%s\" },\n", code, code ); append_fmt( & to_str_entries, "{ sizeof(\"%s\"), \"%s\" },\n", code, code );
} }
CodeEnum enum_code = parse_enum(gen::token_fmt_impl((3 + 1) / 2, "entries", (StrC)enum_entries, "enum Type : u32 { <entries> NumTypes };")); CodeEnum enum_code = parse_enum(gen::token_fmt_impl((3 + 1) / 2, "entries", (StrC)enum_entries, "enum Type : u32 { <entries> NumTypes };"));
@ -76,8 +76,8 @@ CodeBody gen_eoperator( char const* path )
char const* enum_str = enum_strs[idx].string; char const* enum_str = enum_strs[idx].string;
char const* entry_to_str = str_strs [idx].string; char const* entry_to_str = str_strs [idx].string;
append_fmt( enum_entries, "%s,\n", enum_str ); append_fmt( & enum_entries, "%s,\n", enum_str );
append_fmt( to_str_entries, "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str); append_fmt( & to_str_entries, "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str);
} }
CodeEnum enum_code = parse_enum(token_fmt("entries", (StrC)enum_entries, stringize( CodeEnum enum_code = parse_enum(token_fmt("entries", (StrC)enum_entries, stringize(
@ -132,8 +132,8 @@ CodeBody gen_especifier( char const* path )
char const* enum_str = enum_strs[idx].string; char const* enum_str = enum_strs[idx].string;
char const* entry_to_str = str_strs [idx].string; char const* entry_to_str = str_strs [idx].string;
append_fmt( enum_entries, "%s,\n", enum_str ); append_fmt( & enum_entries, "%s,\n", enum_str );
append_fmt( to_str_entries, "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str); append_fmt( & to_str_entries, "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str);
} }
CodeEnum enum_code = parse_enum(token_fmt("entries", (StrC)enum_entries, stringize( CodeEnum enum_code = parse_enum(token_fmt("entries", (StrC)enum_entries, stringize(
@ -249,8 +249,8 @@ CodeBody gen_etoktype( char const* etok_path, char const* attr_path )
char const* enum_str = enum_strs[idx].string; char const* enum_str = enum_strs[idx].string;
char const* entry_to_str = enum_str_strs [idx].string; char const* entry_to_str = enum_str_strs [idx].string;
append_fmt( enum_entries, "%s,\n", enum_str ); append_fmt( & enum_entries, "%s,\n", enum_str );
append_fmt( to_str_entries, "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str); append_fmt( & to_str_entries, "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str);
} }
for ( usize idx = 0; idx < num(attribute_strs); idx++ ) for ( usize idx = 0; idx < num(attribute_strs); idx++ )
@ -258,14 +258,14 @@ CodeBody gen_etoktype( char const* etok_path, char const* attr_path )
char const* attribute_str = attribute_strs[idx].string; char const* attribute_str = attribute_strs[idx].string;
char const* entry_to_str = attribute_str_strs [idx].string; char const* entry_to_str = attribute_str_strs [idx].string;
append_fmt( attribute_entries, "Attribute_%s,\n", attribute_str ); append_fmt( & attribute_entries, "Attribute_%s,\n", attribute_str );
append_fmt( to_str_attributes, "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str); append_fmt( & to_str_attributes, "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str);
append_fmt( attribute_define_entries, "Entry( Attribute_%s, \"%s\" )", attribute_str, entry_to_str ); append_fmt( & attribute_define_entries, "Entry( Attribute_%s, \"%s\" )", attribute_str, entry_to_str );
if ( idx < num(attribute_strs) - 1 ) if ( idx < num(attribute_strs) - 1 )
append( attribute_define_entries, " \\\n"); append( & attribute_define_entries, " \\\n");
else else
append( attribute_define_entries, "\n"); append( & attribute_define_entries, "\n");
} }
#pragma push_macro("GEN_DEFINE_ATTRIBUTE_TOKENS") #pragma push_macro("GEN_DEFINE_ATTRIBUTE_TOKENS")