From f743c230e7828c97ece6abd59d055437f9826926 Mon Sep 17 00:00:00 2001 From: doyle Date: Wed, 1 Apr 2020 01:08:59 +1100 Subject: [PATCH] Add some helper functions --- Code/Dqn.h | 34 +++++++++++++++++++++++++++++++++- Code/DqnHeader_Generated.h | 8 +++++++- Code/Dqn_UnitTests.cpp | 14 +++++++------- 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/Code/Dqn.h b/Code/Dqn.h index f47227a..20562d5 100644 --- a/Code/Dqn.h +++ b/Code/Dqn.h @@ -985,6 +985,16 @@ struct Dqn_Slice T *operator+ (Dqn_isize i) { DQN_ASSERT_MSG(i >= 0 && i < len, "%d >= 0 && %d < %d", i, len); return data + i; } }; +template +DQN_HEADER_COPY_PROTOTYPE(inline Dqn_Slice, Dqn_Slice_InitWithArray(T (&array)[N])) +{ + Dqn_Slice result = {}; + result.len = N; + result.data = array; + return result; +} + + DQN_HEADER_COPY_PROTOTYPE(template inline Dqn_Slice, Dqn_Slice_Allocate(Dqn_Allocator *allocator, Dqn_isize len)) { Dqn_Slice result = {}; @@ -1203,7 +1213,7 @@ template struct Dqn_Array }; DQN_HEADER_COPY_END -DQN_HEADER_COPY_PROTOTYPE(template Dqn_Array, Dqn_Array_InitMemory(T *memory, Dqn_isize max, Dqn_isize len = 0)) +DQN_HEADER_COPY_PROTOTYPE(template Dqn_Array, Dqn_Array_InitWithMemory(T *memory, Dqn_isize max, Dqn_isize len = 0)) { Dqn_Array result = {}; result.allocator = Dqn_Allocator_Null(); @@ -1213,6 +1223,14 @@ DQN_HEADER_COPY_PROTOTYPE(template Dqn_Array, Dqn_Array_InitMemo return result; } +DQN_HEADER_COPY_PROTOTYPE(template Dqn_Array, Dqn_Array_InitWithAllocatorNoGrow(Dqn_Allocator *allocator, Dqn_isize max, Dqn_isize len = 0)) +{ + T *memory = DQN_CAST(T *)Dqn_Allocator_Allocate(allocator, sizeof(T) * max, alignof(T)); + Dqn_Array result = Dqn_Array_InitWithMemory(memory, max, len); + return result; +} + + DQN_HEADER_COPY_PROTOTYPE(template bool, Dqn_Array_Reserve(Dqn_Array *a, Dqn_isize size)) { if (size <= a->len) return true; @@ -2560,6 +2578,20 @@ DQN_HEADER_COPY_PROTOTYPE(Dqn_String, Dqn_String_Copy(Dqn_Allocator *allocator, return result; } +DQN_HEADER_COPY_PROTOTYPE(Dqn_String, Dqn_String_TrimWhitespaceAround(Dqn_String src)) +{ + Dqn_String result = src; + if (src.len <= 0) return result; + + char *start = src.str; + char *end = start + (src.len - 1); + while (Dqn_Char_IsWhitespace(start[0])) start++; + while (end != start && Dqn_Char_IsWhitespace(end[0])) end--; + + result.str = start; + result.len = (end - start) + 1; + return result; +} // @ ------------------------------------------------------------------------------------------------- // @ diff --git a/Code/DqnHeader_Generated.h b/Code/DqnHeader_Generated.h index a88340e..c4310ed 100644 --- a/Code/DqnHeader_Generated.h +++ b/Code/DqnHeader_Generated.h @@ -371,6 +371,8 @@ template void Dqn_StringBuilder_Free(Dqn_StringBuild // NOTE: Dqn_Slices // // ------------------------------------------------------------------------------------------------- +inline Dqn_Slice Dqn_Slice_InitWithArray(T (&array)[N]); +template inline Dqn_Slice Dqn_Slice_Allocate(Dqn_Allocator *allocator, Dqn_isize len); template inline Dqn_Slice Dqn_Slice_CopyNullTerminated(Dqn_Allocator *allocator, T const *src, Dqn_isize len); template inline Dqn_Slice Dqn_Slice_CopyNullTerminated(Dqn_Allocator *allocator, Dqn_Slice const src); template inline Dqn_Slice Dqn_Slice_Copy(Dqn_Allocator *allocator, T const *src, Dqn_isize len); @@ -412,6 +414,7 @@ T * Dqn_FixedArray_Peek(DQN_FIXED_ARRAY_TE Dqn_isize Dqn_FixedArray_GetIndex(DQN_FIXED_ARRAY_TEMPLATE_DECL *a, T const *entry); T * Dqn_FixedArray_Find(DQN_FIXED_ARRAY_TEMPLATE_DECL *a, EqualityProc IsEqual); T * Dqn_FixedArray_Find(DQN_FIXED_ARRAY_TEMPLATE_DECL *a, T *find); +Dqn_Slice Dqn_FixedArray_Slice(DQN_FIXED_ARRAY_TEMPLATE_DECL *a); // ------------------------------------------------------------------------------------------------- // // NOTE: Dqn_Array @@ -434,7 +437,8 @@ template struct Dqn_Array T *operator+(Dqn_isize i) { DQN_ASSERT_MSG(i >= 0 && i < len, "%d >= 0 && %d < %d", i, len); return data + i; } }; -template Dqn_Array Dqn_Array_InitMemory(T *memory, Dqn_isize max, Dqn_isize len = 0); +template Dqn_Array Dqn_Array_InitWithMemory(T *memory, Dqn_isize max, Dqn_isize len = 0); +template Dqn_Array Dqn_Array_InitWithAllocatorNoGrow(Dqn_Allocator *allocator, Dqn_isize max, Dqn_isize len = 0); template bool Dqn_Array_Reserve(Dqn_Array *a, Dqn_isize size); template void Dqn_Array_Free(Dqn_Array *a); template T * Dqn_Array_Add(Dqn_Array *a, T const *items, Dqn_isize num); @@ -582,6 +586,7 @@ Dqn_V2I Dqn_RectI32_Size(Dqn_RectI32 rect); // // ------------------------------------------------------------------------------------------------- Dqn_V2 Dqn_LerpV2(Dqn_V2 a, Dqn_f32 t, Dqn_V2 b); +f32 Dqn_LerpF32(f32 a, f32 t, f32 b); // ------------------------------------------------------------------------------------------------- // // NOTE: Dqn_Mat4 @@ -661,6 +666,7 @@ Dqn_i64 Dqn_Str_ToI64(char const *buf, int len Dqn_b32 Dqn_String_Compare(Dqn_String const lhs, Dqn_String const rhs); Dqn_b32 Dqn_String_CompareCaseInsensitive(Dqn_String const lhs, Dqn_String const rhs); Dqn_String Dqn_String_Copy(Dqn_Allocator *allocator, Dqn_String const src); +Dqn_String Dqn_String_TrimWhitespaceAround(Dqn_String src); // ------------------------------------------------------------------------------------------------- // // NOTE: File diff --git a/Code/Dqn_UnitTests.cpp b/Code/Dqn_UnitTests.cpp index cebf62f..1bc4294 100644 --- a/Code/Dqn_UnitTests.cpp +++ b/Code/Dqn_UnitTests.cpp @@ -228,12 +228,12 @@ FILE_SCOPE void UnitTests() // --------------------------------------------------------------------------------------------- { TEST_DECLARE_GROUP_SCOPED(testing_state, "Dqn_Array"); - // NOTE: Dqn_Array_InitMemory + // NOTE: Dqn_Array_InitWithMemory { { TEST_START_SCOPE(testing_state, "Fixed Memory: Test add single item and can't allocate more"); int memory[4] = {}; - Dqn_Array array = Dqn_Array_InitMemory(memory, Dqn_ArrayCount(memory), 0 /*len*/); + Dqn_Array array = Dqn_Array_InitWithMemory(memory, Dqn_ArrayCount(memory), 0 /*len*/); Dqn_Array_Add(&array, 1); Dqn_Array_Add(&array, 2); Dqn_Array_Add(&array, 3); @@ -254,7 +254,7 @@ FILE_SCOPE void UnitTests() TEST_START_SCOPE(testing_state, "Fixed Memory: Test add array of items"); int memory[4] = {}; int DATA[] = {1, 2, 3}; - Dqn_Array array = Dqn_Array_InitMemory(memory, Dqn_ArrayCount(memory), 0 /*len*/); + Dqn_Array array = Dqn_Array_InitWithMemory(memory, Dqn_ArrayCount(memory), 0 /*len*/); Dqn_Array_Add(&array, DATA, Dqn_ArrayCount(DATA)); TEST_EXPECT_MSG(testing_state, array.data[0] == 1, "array.data %d", array.data[0]); TEST_EXPECT_MSG(testing_state, array.data[1] == 2, "array.data %d", array.data[1]); @@ -267,7 +267,7 @@ FILE_SCOPE void UnitTests() TEST_START_SCOPE(testing_state, "Fixed Memory: Test clear and clear with memory zeroed"); int memory[4] = {}; int DATA[] = {1, 2, 3}; - Dqn_Array array = Dqn_Array_InitMemory(memory, Dqn_ArrayCount(memory), 0 /*len*/); + Dqn_Array array = Dqn_Array_InitWithMemory(memory, Dqn_ArrayCount(memory), 0 /*len*/); Dqn_Array_Add(&array, DATA, Dqn_ArrayCount(DATA)); Dqn_Array_Clear(&array, false /*zero_mem*/); TEST_EXPECT_MSG(testing_state, array.len == 0, "array.len: %d", array.len); @@ -282,7 +282,7 @@ FILE_SCOPE void UnitTests() TEST_START_SCOPE(testing_state, "Fixed Memory: Test erase stable and erase unstable"); int memory[4] = {}; int DATA[] = {1, 2, 3, 4}; - Dqn_Array array = Dqn_Array_InitMemory(memory, Dqn_ArrayCount(memory), 0 /*len*/); + Dqn_Array array = Dqn_Array_InitWithMemory(memory, Dqn_ArrayCount(memory), 0 /*len*/); Dqn_Array_Add(&array, DATA, Dqn_ArrayCount(DATA)); Dqn_Array_EraseUnstable(&array, 1); TEST_EXPECT_MSG(testing_state, array.data[0] == 1, "array.data %d", array.data[0]); @@ -300,7 +300,7 @@ FILE_SCOPE void UnitTests() TEST_START_SCOPE(testing_state, "Fixed Memory: Test array pop and peek"); int memory[4] = {}; int DATA[] = {1, 2, 3}; - Dqn_Array array = Dqn_Array_InitMemory(memory, Dqn_ArrayCount(memory), 0 /*len*/); + Dqn_Array array = Dqn_Array_InitWithMemory(memory, Dqn_ArrayCount(memory), 0 /*len*/); Dqn_Array_Add(&array, DATA, Dqn_ArrayCount(DATA)); Dqn_Array_Pop(&array, 2); TEST_EXPECT_MSG(testing_state, array.data[0] == 1, "array.data: %d", array.data[0]); @@ -316,7 +316,7 @@ FILE_SCOPE void UnitTests() { TEST_START_SCOPE(testing_state, "Fixed Memory: Test free on fixed memory array does nothing"); int memory[4] = {}; - Dqn_Array array = Dqn_Array_InitMemory(memory, Dqn_ArrayCount(memory), 0 /*len*/); + Dqn_Array array = Dqn_Array_InitWithMemory(memory, Dqn_ArrayCount(memory), 0 /*len*/); DQN_DEFER { Dqn_Array_Free(&array); }; } }