mirror of
https://github.com/Ed94/metadesk.git
synced 2026-06-12 23:51:37 -07:00
progress on base/strings
This commit is contained in:
@@ -145,7 +145,6 @@ void* arena_allocator_proc(void* allocator_data, AllocatorMode mode, SSIZE size,
|
||||
|
||||
case AllocatorMode_Resize:
|
||||
{
|
||||
|
||||
assert(old_memory != nullptr);
|
||||
assert(old_size > 0);
|
||||
assert_msg(old_size == size, "Requested resize when none needed");
|
||||
@@ -160,7 +159,6 @@ void* arena_allocator_proc(void* allocator_data, AllocatorMode mode, SSIZE size,
|
||||
|
||||
B32 requested_shrink = size >= old_size;
|
||||
if (requested_shrink) {
|
||||
|
||||
arena->pos -= size;
|
||||
allocated_ptr = old_memory;
|
||||
break;
|
||||
|
||||
+64
-56
@@ -347,7 +347,7 @@ str8_from_memory_size(Arena* arena, U64 z) {
|
||||
}
|
||||
|
||||
String8
|
||||
str8__from_allocator_size(AllocatorInfo ainfo, U64 z) {
|
||||
str8_from_allocator_size(AllocatorInfo ainfo, U64 z) {
|
||||
String8 result = {0};
|
||||
if (z < KB(1)) {
|
||||
result = str8f(ainfo, "%llu b", z);
|
||||
@@ -648,8 +648,9 @@ str8_list_concat_in_place(String8List* list, String8List* to_push) {
|
||||
}
|
||||
|
||||
String8Node*
|
||||
str8_list_push_aligner(Arena *arena, String8List *list, U64 min, U64 align)
|
||||
str8_list_push_aligner(Arena* arena, String8List* list, U64 min, U64 align)
|
||||
{
|
||||
#if MD_DONT_MAP_ANREA_TO_ALLOCATOR_IMPL
|
||||
String8Node* node = push_array_no_zero(arena, String8Node, 1);
|
||||
U64 new_size = list->total_size + min;
|
||||
U64 increase_size = 0;
|
||||
@@ -672,6 +673,9 @@ str8_list_push_aligner(Arena *arena, String8List *list, U64 min, U64 align)
|
||||
node->string.str = (U8*)zeroes_buffer;
|
||||
node->string.size = increase_size;
|
||||
return(node);
|
||||
#else
|
||||
return str8_list_aligner(arena_allocator(arena), list, min, align)
|
||||
#endif
|
||||
}
|
||||
|
||||
String8Node*
|
||||
@@ -700,71 +704,75 @@ str8_list_aligner(AllocatorInfo ainfo, String8List* list, U64 min, U64 align) {
|
||||
return(node);
|
||||
}
|
||||
|
||||
String8Node*
|
||||
str8_list_pushf(Arena *arena, String8List *list, char *fmt, ...) {
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
String8 string = push_str8fv(arena, fmt, args);
|
||||
String8Node *result = str8_list_push(arena, list, string);
|
||||
va_end(args);
|
||||
return(result);
|
||||
}
|
||||
|
||||
String8Node*
|
||||
str8_list_push_frontf(Arena *arena, String8List *list, char *fmt, ...) {
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
String8 string = push_str8fv(arena, fmt, args);
|
||||
String8Node *result = str8_list_push_front(arena, list, string);
|
||||
va_end(args);
|
||||
String8List
|
||||
str8_list_copy(Arena *arena, String8List *list) {
|
||||
#if MD_DONT_MAP_ANREA_TO_ALLOCATOR_IMPL
|
||||
String8List result = {0};
|
||||
for (String8Node* node = list->first;
|
||||
node != 0;
|
||||
node = node->next)
|
||||
{
|
||||
String8Node* new_node = push_array_no_zero(arena, String8Node, 1);
|
||||
String8 new_string = push_str8_copy(arena, node->string);
|
||||
str8_list_push_node_set_string(&result, new_node, new_string);
|
||||
}
|
||||
return(result);
|
||||
#else
|
||||
return str8_list_alloc_copy(arena_allocator(arena), list);
|
||||
#endif
|
||||
}
|
||||
|
||||
String8List
|
||||
str8_list_copy(Arena *arena, String8List *list) {
|
||||
String8List result = {0};
|
||||
for (String8Node *node = list->first;
|
||||
node != 0;
|
||||
node = node->next){
|
||||
String8Node *new_node = push_array_no_zero(arena, String8Node, 1);
|
||||
String8 new_string = push_str8_copy(arena, node->string);
|
||||
str8_list_push_node_set_string(&result, new_node, new_string);
|
||||
}
|
||||
return(result);
|
||||
str8_list_alloc_copy(AllocatorInfo ainfo, String8List* list)
|
||||
{
|
||||
String8List result = {0};
|
||||
for (String8Node* node = list->first;
|
||||
node != 0;
|
||||
node = node->next)
|
||||
{
|
||||
String8Node* new_node = alloc_array_no_zero(ainfo, String8Node, 1);
|
||||
String8 new_string = str8_copy(ainfo, node->string);
|
||||
str8_list_push_node_set_string(&result, new_node, new_string);
|
||||
}
|
||||
return(result);
|
||||
}
|
||||
|
||||
////////////////////////////////
|
||||
//~ rjf: String Splitting & Joining
|
||||
|
||||
String8List
|
||||
str8_split(Arena *arena, String8 string, U8 *split_chars, U64 split_char_count, StringSplitFlags flags){
|
||||
String8List list = {0};
|
||||
String8List list = {0};
|
||||
|
||||
B32 keep_empties = (flags & StringSplitFlag_KeepEmpties);
|
||||
B32 keep_empties = (flags & StringSplitFlag_KeepEmpties);
|
||||
|
||||
U8 *ptr = string.str;
|
||||
U8 *opl = string.str + string.size;
|
||||
for (;ptr < opl;){
|
||||
U8 *first = ptr;
|
||||
for (;ptr < opl; ptr += 1){
|
||||
U8 c = *ptr;
|
||||
B32 is_split = 0;
|
||||
for (U64 i = 0; i < split_char_count; i += 1){
|
||||
if (split_chars[i] == c){
|
||||
is_split = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (is_split){
|
||||
break;
|
||||
}
|
||||
}
|
||||
U8* ptr = string.str;
|
||||
U8* opl = string.str + string.size;
|
||||
for (;ptr < opl;)
|
||||
{
|
||||
U8* first = ptr;
|
||||
for (;ptr < opl; ptr += 1)
|
||||
{
|
||||
U8 c = *ptr;
|
||||
B32 is_split = 0;
|
||||
for (U64 i = 0; i < split_char_count; i += 1) {
|
||||
if (split_chars[i] == c) {
|
||||
is_split = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (is_split){
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
String8 string = str8_range(first, ptr);
|
||||
if (keep_empties || string.size > 0){
|
||||
str8_list_push(arena, &list, string);
|
||||
}
|
||||
ptr += 1;
|
||||
}
|
||||
|
||||
return(list);
|
||||
String8 string = str8_range(first, ptr);
|
||||
if (keep_empties || string.size > 0){
|
||||
str8_list_push(arena, &list, string);
|
||||
}
|
||||
ptr += 1;
|
||||
}
|
||||
return(list);
|
||||
}
|
||||
|
||||
String8List
|
||||
|
||||
+76
-9
@@ -327,9 +327,9 @@ MD_API String8 str8_from_memory_size(Arena* arena, U64 z);
|
||||
MD_API String8 str8_from_u64 (Arena* arena, U64 u64, U32 radix, U8 min_digits, U8 digit_group_separator);
|
||||
MD_API String8 str8_from_s64 (Arena* arena, S64 s64, U32 radix, U8 min_digits, U8 digit_group_separator);
|
||||
|
||||
String8 str8_from_allocator_size(AllocatorInfo ainfo, U64 z);
|
||||
String8 str8_from_allocator_u64 (AllocatorInfo ainfo, U64 u64, U32 radix, U8 min_digits, U8 digit_group_separator);
|
||||
String8 str8_from_alloctor_s64 (AllocatorInfo ainfo, S64 u64, U32 radix, U8 min_digits, U8 digit_group_separator);
|
||||
MD_API String8 str8_from_allocator_size(AllocatorInfo ainfo, U64 z);
|
||||
MD_API String8 str8_from_allocator_u64 (AllocatorInfo ainfo, U64 u64, U32 radix, U8 min_digits, U8 digit_group_separator);
|
||||
MD_API String8 str8_from_alloctor_s64 (AllocatorInfo ainfo, S64 u64, U32 radix, U8 min_digits, U8 digit_group_separator);
|
||||
|
||||
inline U64
|
||||
u64_from_str8(String8 string, U32 radix) {
|
||||
@@ -410,31 +410,98 @@ str8_list_push_node_front_set_string(String8List* list, String8Node* node, Strin
|
||||
return(node);
|
||||
}
|
||||
|
||||
MD_API String8Node* str8_list_push_aligner(Arena* arena, String8List* list, U64 min, U64 align);
|
||||
MD_API String8List str8_list_copy (Arena* arena, String8List* list);
|
||||
|
||||
String8Node* str8_list_push (Arena* arena, String8List* list, String8 string);
|
||||
String8Node* str8_list_push_front (Arena* arena, String8List* list, String8 string);
|
||||
String8Node* str8_list_push_aligner(Arena* arena, String8List* list, U64 min, U64 align);
|
||||
String8Node* str8_list_pushf (Arena* arena, String8List* list, char* fmt, ...);
|
||||
String8Node* str8_list_push_frontf (Arena* arena, String8List* list, char* fmt, ...);
|
||||
String8List str8_list_copy (Arena* arena, String8List* list);
|
||||
|
||||
String8Node* str8_list_alloc (AllocatorInfo ainfo, String8List* list, String8 string);
|
||||
String8Node* str8_list_alloc_front (AllocatorInfo ainfo, String8List* list, String8 string);
|
||||
String8Node* str8_list_alloc_aligner(AllocatorInfo ainfo, String8List* list, U64 min, U64 align);
|
||||
MD_API String8Node* str8_list_alloc_aligner(AllocatorInfo ainfo, String8List* list, U64 min, U64 align);
|
||||
MD_API String8List str8_list_alloc_copy (AllocatorInfo ainfo, String8List* list);
|
||||
|
||||
String8Node* str8_list_alloc (AllocatorInfo ainfo, String8List* list, String8 string);
|
||||
String8Node* str8_list_alloc_front (AllocatorInfo ainfo, String8List* list, String8 string);
|
||||
String8Node* str8_list_allocf (AllocatorInfo ainfo, String8List* list, char* fmt, ...);
|
||||
String8Node* str8_list_alloc_frontf(AllocatorInfo ainfo, String8List* list, char* fmt, ...);
|
||||
|
||||
inline String8Node*
|
||||
str8_list_push(Arena* arena, String8List* list, String8 string) {
|
||||
#if MD_DONT_MAP_ANREA_TO_ALLOCATOR_IMPL
|
||||
String8Node* node = push_array_no_zero(arena, String8Node, 1);
|
||||
str8_list_push_node_set_string(list, node, string);
|
||||
return(node);
|
||||
#else
|
||||
return str8_list_alloc(arena_allocator(arena), list, string);
|
||||
#endif
|
||||
}
|
||||
|
||||
inline String8Node*
|
||||
str8_list_push_front(Arena* arena, String8List* list, String8 string) {
|
||||
String8Node *node = push_array_no_zero(arena, String8Node, 1);
|
||||
#if MD_DONT_MAP_ANREA_TO_ALLOCATOR_IMPL
|
||||
String8Node *node = push_array_no_zero(arena, String8Node, 1);
|
||||
str8_list_push_node_front_set_string(list, node, string);
|
||||
return(node);
|
||||
#else
|
||||
return str8_list_alloc_front(arena_allocator(arena), list, string);
|
||||
#endif
|
||||
}
|
||||
|
||||
inline String8Node*
|
||||
str8_list_pushf(Arena *arena, String8List *list, char *fmt, ...) {
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
String8 string = push_str8fv(arena, fmt, args);
|
||||
String8Node* result = str8_list_push(arena, list, string);
|
||||
va_end(args);
|
||||
return(result);
|
||||
}
|
||||
|
||||
inline String8Node*
|
||||
str8_list_push_frontf(Arena *arena, String8List *list, char *fmt, ...) {
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
String8 string = push_str8fv(arena, fmt, args);
|
||||
String8Node* result = str8_list_push_front(arena, list, string);
|
||||
va_end(args);
|
||||
return(result);
|
||||
}
|
||||
|
||||
inline String8Node*
|
||||
str8_list_alloc(AllocatorInfo ainfo, String8List* list, String8 string) {
|
||||
String8Node *node = alloc_array_no_zero(ainfo, String8Node, 1);
|
||||
str8_list_push_node_front_set_string(list, node, string);
|
||||
return(node);
|
||||
}
|
||||
|
||||
inline String8Node*
|
||||
str8_list_alloc_front(AllocatorInfo ainfo, String8List* list, String8 string) {
|
||||
String8Node *node = alloc_array_no_zero(ainfo, String8Node, 1);
|
||||
str8_list_push_node_front_set_string(list, node, string);
|
||||
return(node);
|
||||
}
|
||||
|
||||
inline String8Node*
|
||||
str8_list_allocf(AllocatorInfo ainfo, String8List* list, char* fmt, ...) {
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
String8 string = str8fv(ainfo, fmt, args);
|
||||
String8Node* result = str8_list_alloc(ainfo, list, string);
|
||||
va_end(args);
|
||||
return(result);
|
||||
}
|
||||
|
||||
inline String8Node*
|
||||
str8_list_alloc_frontf(AllocatorInfo ainfo, String8List* list, char* fmt, ...) {
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
String8 string = str8fv(ainfo, fmt, args);
|
||||
String8Node* result = str8_list_alloc_front(ainfo, list, string);
|
||||
va_end(args);
|
||||
return(result);
|
||||
}
|
||||
|
||||
////////////////////////////////
|
||||
//~ rjf: String Splitting & Joining
|
||||
|
||||
|
||||
@@ -140,7 +140,7 @@ os_file_id_compare(OS_FileID a, OS_FileID b)
|
||||
}
|
||||
|
||||
String8
|
||||
os_string_from_file_range(Arena *arena, OS_Handle file, Rng1U64 range)
|
||||
os_string_from_file_range(Arena* arena, OS_Handle file, Rng1U64 range)
|
||||
{
|
||||
U64 pre_pos = arena_pos(arena);
|
||||
String8 result;
|
||||
@@ -163,6 +163,9 @@ os_string_from_file_range_alloc(AllocatorInfo ainfo, OS_Handle file, Rng1U64 ran
|
||||
U64 actual_read_size = os_file_read(file, range, result.str);
|
||||
if(actual_read_size < result.size)
|
||||
{
|
||||
// TODO(Ed): It may be better to actually wrap the alloation in an arena and then rewind it.
|
||||
// This would ensure resize isn't doing an expensive shrink (from a bad heap realloc, or something else)
|
||||
// That or we just leave it up to the user to make sure to pass in an arena.
|
||||
resize(ainfo, result.str, result.size, result.str + actual_read_size);
|
||||
result.size = actual_read_size;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user