From ed483537c6792dd1ef976c13941cda51ac2798ff Mon Sep 17 00:00:00 2001 From: doylet Date: Sat, 25 Oct 2025 18:26:27 +1100 Subject: [PATCH] Get latest changes from Seasight --- Source/Base/dn_base.cpp | 37 +++++++++++++++++++++++++++++++++++-- Source/Base/dn_base.h | 17 +++++++++++------ Source/Extra/dn_tests.cpp | 38 +++++++++++++++++++++++++++++--------- 3 files changed, 75 insertions(+), 17 deletions(-) diff --git a/Source/Base/dn_base.cpp b/Source/Base/dn_base.cpp index a7dd58f..74d42b5 100644 --- a/Source/Base/dn_base.cpp +++ b/Source/Base/dn_base.cpp @@ -1653,8 +1653,11 @@ DN_API DN_Str8 DN_Str8FromFmtPool(DN_Pool *pool, DN_FMT_ATTRIB char const *fmt, va_start(args, fmt); DN_USize size = DN_FmtVSize(fmt, args); DN_Str8 result = DN_Str8FromPool(pool, size); - if (result.data) - DN_FmtVAppend(result.data, &result.size, result.size + 1, fmt, args); + if (result.data) { + DN_USize written = 0; + DN_FmtVAppend(result.data, &written, result.size + 1, fmt, args); + DN_Assert(written == result.size); + } va_end(args); return result; } @@ -1669,6 +1672,36 @@ DN_API DN_Str8x32 DN_Str8x32FromFmt(DN_FMT_ATTRIB char const *fmt, ...) return result; } +DN_API DN_Str8x64 DN_Str8x64FromFmt(DN_FMT_ATTRIB char const *fmt, ...) +{ + va_list args; + va_start(args, fmt); + DN_Str8x64 result = {}; + DN_FmtVAppend(result.data, &result.size, sizeof(result.data), fmt, args); + va_end(args); + return result; +} + +DN_API DN_Str8x128 DN_Str8x128FromFmt(DN_FMT_ATTRIB char const *fmt, ...) +{ + va_list args; + va_start(args, fmt); + DN_Str8x128 result = {}; + DN_FmtVAppend(result.data, &result.size, sizeof(result.data), fmt, args); + va_end(args); + return result; +} + +DN_API DN_Str8x256 DN_Str8x256FromFmt(DN_FMT_ATTRIB char const *fmt, ...) +{ + va_list args; + va_start(args, fmt); + DN_Str8x256 result = {}; + DN_FmtVAppend(result.data, &result.size, sizeof(result.data), fmt, args); + va_end(args); + return result; +} + DN_API DN_Str8x32 DN_Str8x32FromU64(DN_U64 val, char separator) { DN_Str8x32 result = {}; diff --git a/Source/Base/dn_base.h b/Source/Base/dn_base.h index cd6d7fd..d8d03c8 100644 --- a/Source/Base/dn_base.h +++ b/Source/Base/dn_base.h @@ -18,12 +18,13 @@ #include // exit() #endif -#define DN_ForIndexU(index, size) DN_USize index = 0; index < size; index++ -#define DN_ForIndexI(index, size) DN_ISize index = 0; index < size; index++ -#define DN_ForItSize(it, T, array, size) struct { DN_USize index; T *data; } it = {0, &(array)[0]}; it.index < (size); it.index++, it.data = (array) + it.index -#define DN_ForIt(it, T, array) struct { DN_USize index; T *data; } it = {0, &(array)->data[0]}; it.index < (array)->size; it.index++, it.data = ((array)->data) + it.index -#define DN_ForLinkedListIt(it, T, list) struct { DN_USize index; T *data; } it = {0, list}; it.data; it.index++, it.data = ((it).data->next) -#define DN_ForItCArray(it, T, array) struct { DN_USize index; T *data; } it = {0, &(array)[0]}; it.index < DN_ArrayCountU(array); it.index++, it.data = (array) + it.index +#define DN_ForIndexU(index, size) DN_USize index = 0; index < size; index++ +#define DN_ForIndexI(index, size) DN_ISize index = 0; index < size; index++ +#define DN_ForItSize(it, T, array, size) struct { DN_USize index; T *data; } it = {0, &(array)[0]}; it.index < (size); it.index++, it.data = (array) + it.index +#define DN_ForItSizeReverse(it, T, array, size) struct { DN_USize index; T *data; } it = {(size) - 1, &(array)[size - 1]}; it.index < (size); it.index--, it.data = (array) + it.index +#define DN_ForIt(it, T, array) struct { DN_USize index; T *data; } it = {0, &(array)->data[0]}; it.index < (array)->size; it.index++, it.data = ((array)->data) + it.index +#define DN_ForLinkedListIt(it, T, list) struct { DN_USize index; T *data; } it = {0, list}; it.data; it.index++, it.data = ((it).data->next) +#define DN_ForItCArray(it, T, array) struct { DN_USize index; T *data; } it = {0, &(array)[0]}; it.index < DN_ArrayCountU(array); it.index++, it.data = (array) + it.index #define DN_AlignUpPowerOfTwo(value, pot) (((uintptr_t)(value) + ((uintptr_t)(pot) - 1)) & ~((uintptr_t)(pot) - 1)) #define DN_AlignDownPowerOfTwo(value, pot) ((uintptr_t)(value) & ~((uintptr_t)(pot) - 1)) @@ -948,6 +949,10 @@ DN_API DN_Str8 DN_Str8FromFmtVArena (DN_Arena *arena, DN DN_API DN_Str8 DN_Str8FromFmtPool (DN_Pool *pool, DN_FMT_ATTRIB char const *fmt, ...); DN_API DN_Str8 DN_Str8FromByteCountType (DN_ByteCountType type); DN_API DN_Str8x32 DN_Str8x32FromFmt (DN_FMT_ATTRIB char const *fmt, ...); +DN_API DN_Str8x64 DN_Str8x64FromFmt (DN_FMT_ATTRIB char const *fmt, ...); +DN_API DN_Str8x128 DN_Str8x128FromFmt (DN_FMT_ATTRIB char const *fmt, ...); +DN_API DN_Str8x256 DN_Str8x256FromFmt (DN_FMT_ATTRIB char const *fmt, ...); +DN_API DN_Str8x32 DN_Str8x32FromU64 (DN_U64 val, char separator); DN_API bool DN_Str8IsAll (DN_Str8 string, DN_Str8IsAllType is_all); DN_API char * DN_Str8End (DN_Str8 string); DN_API DN_Str8 DN_Str8Slice (DN_Str8 string, DN_USize offset, DN_USize size); diff --git a/Source/Extra/dn_tests.cpp b/Source/Extra/dn_tests.cpp index bd991dd..584648c 100644 --- a/Source/Extra/dn_tests.cpp +++ b/Source/Extra/dn_tests.cpp @@ -303,6 +303,35 @@ static DN_UTCore DN_Tests_Base() DN_UT_Assert(&result, DN_CPUHasFeature(&cpu_report, DN_CPUFeature_XSAVE) == DN_RefImplCPUReport::XSAVE()); #endif } +#endif // defined(DN_PLATFORM_WIN32) && defined(DN_COMPILER_MSVC) + + for (DN_UT_Test(&result, "String")) { + char buffer[512]; + DN_Arena arena = DN_ArenaFromBuffer(buffer, sizeof(buffer), DN_ArenaFlags_NoPoison | DN_ArenaFlags_AllocCanLeak); + + // NOTE: Str8FromFmtArena + { + DN_Str8 str8 = DN_Str8FromFmtArena(&arena, "Foo Bar %d", 5); + DN_Str8 expect = DN_Str8Lit("Foo Bar 5"); + DN_UT_AssertF(&result, DN_Str8Eq(str8, expect), "str8=%.*s", DN_Str8PrintFmt(str8), DN_Str8PrintFmt(expect)); + } + + // NOTE: Str8FromFmtPool + { + DN_Pool pool = DN_PoolFromArena(&arena, 0); + DN_Str8 str8 = DN_Str8FromFmtPool(&pool, "Foo Bar %d", 5); + DN_Str8 expect = DN_Str8Lit("Foo Bar 5"); + DN_UT_AssertF(&result, DN_Str8Eq(str8, expect), "str8=%.*s", DN_Str8PrintFmt(str8), DN_Str8PrintFmt(expect)); + } + + + // NOTE: Str8x32FromU64 + { + DN_Str8x32 str8 = DN_Str8x32FromU64(123456, ' '); + DN_Str8 expect = DN_Str8Lit("123 456"); + DN_UT_AssertF(&result, DN_Str8Eq(DN_Str8FromStruct(&str8), expect), "buf_str8=%.*s, expect=%.*s", DN_Str8PrintFmt(str8), DN_Str8PrintFmt(expect)); + } + } for (DN_UT_Test(&result, "Age")) { // NOTE: Seconds and milliseconds @@ -320,14 +349,6 @@ static DN_UTCore DN_Tests_Base() } } - for (DN_UT_Test(&result, "String")) { - { - DN_Str8x32 str8 = DN_Str8x32FromU64(123456, ' '); - DN_Str8 expect = DN_Str8Lit("123 456"); - DN_UT_AssertF(&result, DN_Str8Eq(DN_Str8FromStruct(&str8), expect), "buf_str8=%.*s, expect=%.*s", DN_Str8PrintFmt(str8), DN_Str8PrintFmt(expect)); - } - } - for (DN_UT_Test(&result, "Misc")) { { char buf[8] = {}; @@ -339,7 +360,6 @@ static DN_UTCore DN_Tests_Base() } } } -#endif // defined(DN_PLATFORM_WIN32) && defined(DN_COMPILER_MSVC) return result; }