Cleanup CVT functions

This commit is contained in:
2025-07-20 15:13:13 +10:00
parent 804e5ed0a8
commit dbc3fe63f8
9 changed files with 202 additions and 190 deletions
+3 -3
View File
@@ -737,9 +737,9 @@ DN_U32 DN_DSMap_Hash(DN_DSMap<T> const *map, DN_DSMapKey key)
DN_U32 len = 0;
DN_U32 h = seed;
switch (key.type) {
case DN_DSMapKeyType_BufferAsU64NoHash: /*FALLTHRU*/
case DN_DSMapKeyType_U64NoHash: DN_InvalidCodePath; /*FALLTHRU*/
case DN_DSMapKeyType_Invalid: break;
case DN_DSMapKeyType_BufferAsU64NoHash: /*FALLTHRU*/
case DN_DSMapKeyType_U64NoHash: DN_InvalidCodePath; /*FALLTHRU*/
case DN_DSMapKeyType_Invalid: break;
case DN_DSMapKeyType_Buffer:
key_ptr = DN_CAST(char const *) key.buffer_data;
+29 -32
View File
@@ -14,7 +14,7 @@ DN_API int DN_CVT_FmtBuffer3DotTruncate(char *buffer, int size, DN_FMT_ATTRIB ch
return result;
}
DN_API DN_CVTU64Str8 DN_CVT_U64ToStr8(uint64_t val, char separator)
DN_API DN_CVTU64Str8 DN_CVT_U64ToStr8(DN_U64 val, char separator)
{
DN_CVTU64Str8 result = {};
if (val == 0) {
@@ -47,44 +47,41 @@ DN_API DN_CVTU64Str8 DN_CVT_U64ToStr8(uint64_t val, char separator)
return result;
}
DN_API DN_CVTU64ByteSize DN_CVT_U64ToByteSize(uint64_t bytes, DN_CVTU64ByteSizeType desired_type)
DN_API DN_CVTU64Bytes DN_CVT_U64ToBytes(DN_U64 bytes, DN_CVTBytesType type)
{
DN_CVTU64ByteSize result = {};
DN_Assert(type != DN_CVTBytesType_Count);
DN_CVTU64Bytes result = {};
result.bytes = DN_CAST(DN_F64) bytes;
if (!DN_Check(desired_type != DN_CVTU64ByteSizeType_Count)) {
result.suffix = DN_CVT_U64ByteSizeTypeString(result.type);
return result;
}
if (desired_type == DN_CVTU64ByteSizeType_Auto)
for (; result.type < DN_CVTU64ByteSizeType_Count && result.bytes >= 1024.0; result.type = DN_CAST(DN_CVTU64ByteSizeType)(DN_CAST(DN_USize) result.type + 1))
if (type == DN_CVTBytesType_Auto)
for (; result.type < DN_CVTBytesType_Count && result.bytes >= 1024.0; result.type = DN_CAST(DN_CVTBytesType)(DN_CAST(DN_USize) result.type + 1))
result.bytes /= 1024.0;
else
for (; result.type < desired_type; result.type = DN_CAST(DN_CVTU64ByteSizeType)(DN_CAST(DN_USize) result.type + 1))
for (; result.type < type; result.type = DN_CAST(DN_CVTBytesType)(DN_CAST(DN_USize) result.type + 1))
result.bytes /= 1024.0;
result.suffix = DN_CVT_U64ByteSizeTypeString(result.type);
result.suffix = DN_CVT_BytesTypeToStr8(result.type);
return result;
}
DN_API DN_Str8 DN_CVT_U64ToByteSizeStr8(DN_Arena *arena, uint64_t bytes, DN_CVTU64ByteSizeType desired_type)
DN_API DN_Str8 DN_CVT_U64ToBytesStr8(DN_Arena *arena, DN_U64 bytes, DN_CVTBytesType desired_type)
{
DN_CVTU64ByteSize byte_size = DN_CVT_U64ToByteSize(bytes, desired_type);
DN_Str8 result = DN_Str8_InitF(arena, "%.2f%.*s", byte_size.bytes, DN_STR_FMT(byte_size.suffix));
DN_CVTU64Bytes byte_size = DN_CVT_U64ToBytes(bytes, desired_type);
DN_Str8 result = DN_Str8_InitF(arena, "%.2f%.*s", byte_size.bytes, DN_STR_FMT(byte_size.suffix));
return result;
}
DN_API DN_Str8 DN_CVT_U64ByteSizeTypeString(DN_CVTU64ByteSizeType type)
DN_API DN_Str8 DN_CVT_BytesTypeToStr8(DN_CVTBytesType type)
{
DN_Str8 result = DN_STR8("");
switch (type) {
case DN_CVTU64ByteSizeType_B: result = DN_STR8("B"); break;
case DN_CVTU64ByteSizeType_KiB: result = DN_STR8("KiB"); break;
case DN_CVTU64ByteSizeType_MiB: result = DN_STR8("MiB"); break;
case DN_CVTU64ByteSizeType_GiB: result = DN_STR8("GiB"); break;
case DN_CVTU64ByteSizeType_TiB: result = DN_STR8("TiB"); break;
case DN_CVTU64ByteSizeType_Count: result = DN_STR8(""); break;
case DN_CVTU64ByteSizeType_Auto: result = DN_STR8(""); break;
case DN_CVTBytesType_B: result = DN_STR8("B"); break;
case DN_CVTBytesType_KiB: result = DN_STR8("KiB"); break;
case DN_CVTBytesType_MiB: result = DN_STR8("MiB"); break;
case DN_CVTBytesType_GiB: result = DN_STR8("GiB"); break;
case DN_CVTBytesType_TiB: result = DN_STR8("TiB"); break;
case DN_CVTBytesType_Count: result = DN_STR8(""); break;
case DN_CVTBytesType_Auto: result = DN_STR8(""); break;
}
return result;
}
@@ -205,14 +202,14 @@ DN_API DN_Str8 DN_CVT_F64ToAge(DN_Arena *arena, DN_F64 age_s, DN_CVTU64AgeUnit u
return result;
}
DN_API uint64_t DN_CVT_HexToU64(DN_Str8 hex)
DN_API DN_U64 DN_CVT_HexToU64(DN_Str8 hex)
{
DN_Str8 real_hex = DN_Str8_TrimPrefix(DN_Str8_TrimPrefix(hex, DN_STR8("0x")), DN_STR8("0X"));
DN_USize max_hex_size = sizeof(uint64_t) * 2 /*hex chars per byte*/;
DN_USize max_hex_size = sizeof(DN_U64) * 2 /*hex chars per byte*/;
DN_Assert(real_hex.size <= max_hex_size);
DN_USize size = DN_Min(max_hex_size, real_hex.size);
uint64_t result = 0;
DN_U64 result = 0;
for (DN_USize index = 0; index < size; index++) {
char ch = real_hex.data[index];
DN_CharHexToU8 val = DN_Char_HexToU8(ch);
@@ -223,13 +220,13 @@ DN_API uint64_t DN_CVT_HexToU64(DN_Str8 hex)
return result;
}
DN_API DN_Str8 DN_CVT_U64ToHex(DN_Arena *arena, uint64_t number, uint32_t flags)
DN_API DN_Str8 DN_CVT_U64ToHex(DN_Arena *arena, DN_U64 number, uint32_t flags)
{
DN_Str8 prefix = {};
if ((flags & DN_CVTHexU64Str8Flags_0xPrefix))
if ((flags & DN_CVTU64HexStrFlags_0xPrefix))
prefix = DN_STR8("0x");
char const *fmt = (flags & DN_CVTHexU64Str8Flags_UppercaseHex) ? "%I64X" : "%I64x";
char const *fmt = (flags & DN_CVTU64HexStrFlags_UppercaseHex) ? "%I64X" : "%I64x";
DN_USize required_size = DN_CStr8_FSize(fmt, number) + prefix.size;
DN_Str8 result = DN_Str8_Alloc(arena, required_size, DN_ZeroMem_No);
@@ -241,17 +238,17 @@ DN_API DN_Str8 DN_CVT_U64ToHex(DN_Arena *arena, uint64_t number, uint32_t flags)
return result;
}
DN_API DN_CVTU64HexStr8 DN_CVT_U64ToHexStr8(uint64_t number, DN_CVTHexU64Str8Flags flags)
DN_API DN_CVTU64HexStr DN_CVT_U64ToHexStr8(DN_U64 number, DN_CVTU64HexStrFlags flags)
{
DN_Str8 prefix = {};
if (flags & DN_CVTHexU64Str8Flags_0xPrefix)
if (flags & DN_CVTU64HexStrFlags_0xPrefix)
prefix = DN_STR8("0x");
DN_CVTU64HexStr8 result = {};
DN_CVTU64HexStr result = {};
DN_Memcpy(result.data, prefix.data, prefix.size);
result.size += DN_CAST(int8_t) prefix.size;
char const *fmt = (flags & DN_CVTHexU64Str8Flags_UppercaseHex) ? "%I64X" : "%I64x";
char const *fmt = (flags & DN_CVTU64HexStrFlags_UppercaseHex) ? "%I64X" : "%I64x";
int size = DN_SNPrintF(result.data + result.size, DN_ArrayCountU(result.data) - result.size, fmt, number);
result.size += DN_CAST(uint8_t) size;
DN_Assert(result.size < DN_ArrayCountU(result.data));
+56 -47
View File
@@ -1,79 +1,88 @@
#if !defined(DN_BASE_CONVERT_H)
#define DN_BASE_CONVERT_H
#include "../dn_base_inc.h"
struct DN_CVTU64Str8
{
char data[27 + 1]; // NOTE(dn): 27 is the maximum size of DN_U64 including a separator
DN_U8 size;
};
enum DN_CVTU64ByteSizeType
enum DN_CVTBytesType
{
DN_CVTU64ByteSizeType_B,
DN_CVTU64ByteSizeType_KiB,
DN_CVTU64ByteSizeType_MiB,
DN_CVTU64ByteSizeType_GiB,
DN_CVTU64ByteSizeType_TiB,
DN_CVTU64ByteSizeType_Count,
DN_CVTU64ByteSizeType_Auto,
DN_CVTBytesType_B,
DN_CVTBytesType_KiB,
DN_CVTBytesType_MiB,
DN_CVTBytesType_GiB,
DN_CVTBytesType_TiB,
DN_CVTBytesType_Count,
DN_CVTBytesType_Auto,
};
struct DN_CVTU64ByteSize
struct DN_CVTU64Bytes
{
DN_CVTU64ByteSizeType type;
DN_Str8 suffix; // "KiB", "MiB", "GiB" .. e.t.c
DN_F64 bytes;
DN_CVTBytesType type;
DN_Str8 suffix; // "KiB", "MiB", "GiB" .. e.t.c
DN_F64 bytes;
};
struct DN_CVTU64HexStr8
struct DN_CVTU64HexStr
{
char data[2 /*0x*/ + 16 /*hex*/ + 1 /*null-terminator*/];
DN_U8 size;
};
typedef DN_U32 DN_CVTHexU64Str8Flags;
enum DN_CVTHexU64Str8Flags_
typedef DN_U32 DN_CVTU64HexStrFlags;
enum DN_CVTU64HexStrFlags_
{
DN_CVTHexU64Str8Flags_Nil = 0,
DN_CVTHexU64Str8Flags_0xPrefix = 1 << 0, /// Add the '0x' prefix from the string
DN_CVTHexU64Str8Flags_UppercaseHex = 1 << 1, /// Use uppercase ascii characters for hex
DN_CVTU64HexStrFlags_Nil = 0,
DN_CVTU64HexStrFlags_0xPrefix = 1 << 0, /// Add the '0x' prefix from the string
DN_CVTU64HexStrFlags_UppercaseHex = 1 << 1, /// Use uppercase ascii characters for hex
};
typedef DN_U32 DN_CVTU64AgeUnit;
enum DN_CVTU64AgeUnit_
{
DN_CVTU64AgeUnit_Sec = 1 << 0,
DN_CVTU64AgeUnit_Min = 1 << 1,
DN_CVTU64AgeUnit_Hr = 1 << 2,
DN_CVTU64AgeUnit_Day = 1 << 3,
DN_CVTU64AgeUnit_Week = 1 << 4,
DN_CVTU64AgeUnit_Year = 1 << 5,
DN_CVTU64AgeUnit_HMS = DN_CVTU64AgeUnit_Sec | DN_CVTU64AgeUnit_Min | DN_CVTU64AgeUnit_Hr,
DN_CVTU64AgeUnit_All = DN_CVTU64AgeUnit_HMS | DN_CVTU64AgeUnit_Day | DN_CVTU64AgeUnit_Week | DN_CVTU64AgeUnit_Year,
DN_CVTU64AgeUnit_Sec = 1 << 0,
DN_CVTU64AgeUnit_Min = 1 << 1,
DN_CVTU64AgeUnit_Hr = 1 << 2,
DN_CVTU64AgeUnit_Day = 1 << 3,
DN_CVTU64AgeUnit_Week = 1 << 4,
DN_CVTU64AgeUnit_Year = 1 << 5,
DN_CVTU64AgeUnit_HMS = DN_CVTU64AgeUnit_Sec | DN_CVTU64AgeUnit_Min | DN_CVTU64AgeUnit_Hr,
DN_CVTU64AgeUnit_All = DN_CVTU64AgeUnit_HMS | DN_CVTU64AgeUnit_Day | DN_CVTU64AgeUnit_Week | DN_CVTU64AgeUnit_Year,
};
DN_API int DN_CVT_FmtBuffer3DotTruncate (char *buffer, int size, DN_FMT_ATTRIB char const *fmt, ...);
DN_API DN_CVTU64Str8 DN_CVT_U64ToStr8 (DN_U64 val, char separator);
DN_API DN_CVTU64ByteSize DN_CVT_U64ToByteSize (DN_U64 bytes, DN_CVTU64ByteSizeType type);
DN_API DN_Str8 DN_CVT_U64ToByteSizeStr8 (DN_Arena *arena, DN_U64 bytes, DN_CVTU64ByteSizeType desired_type);
DN_API DN_Str8 DN_CVT_U64ByteSizeTypeString (DN_CVTU64ByteSizeType type);
DN_API DN_Str8 DN_CVT_U64ToAge (DN_Arena *arena, DN_U64 age_s, DN_CVTU64AgeUnit unit);
DN_API DN_Str8 DN_CVT_F64ToAge (DN_Arena *arena, DN_F64 age_s, DN_CVTU64AgeUnit unit);
DN_API int DN_CVT_FmtBuffer3DotTruncate (char *buffer, int size, DN_FMT_ATTRIB char const *fmt, ...);
DN_API DN_CVTU64Str8 DN_CVT_U64ToStr8 (DN_U64 val, char separator);
DN_API DN_CVTU64Bytes DN_CVT_U64ToBytes (DN_U64 bytes, DN_CVTBytesType type);
#define DN_CVT_U64ToBytesAuto(bytes) DN_CVT_U64ToBytes(bytes, DN_CVTBytesType_Auto)
DN_API DN_Str8 DN_CVT_U64ToBytesStr8 (DN_Arena *arena, DN_U64 bytes, DN_CVTBytesType type);
#define DN_CVT_U64ToBytesStr8Auto(arena, bytes) DN_CVT_U64ToBytesStr8(arena, bytes, DN_CVTBytesType_Auto)
#define DN_CVT_U64ToBytesStr8FromTLS(bytes, type) DN_CVT_U64ToBytesStr8(DN_OS_TLSTopArena(), bytes, type)
#define DN_CVT_U64ToBytesStr8AutoFromTLS(bytes) DN_CVT_U64ToBytesStr8(DN_OS_TLSTopArena(), bytes, DN_CVTBytesType_Auto)
#define DN_CVT_U64ToBytesStr8FromFrame(bytes, type) DN_CVT_U64ToBytesStr8(DN_OS_TLSFrameArena(), bytes, type)
#define DN_CVT_U64ToBytesStr8AutoFromFrame(bytes) DN_CVT_U64ToBytesStr8(DN_OS_TLSFrameArena(), bytes, DN_CVTBytesType_Auto)
DN_API DN_Str8 DN_CVT_BytesTypeToStr8 (DN_CVTBytesType type);
DN_API DN_Str8 DN_CVT_U64ToAge (DN_Arena *arena, DN_U64 age_s, DN_CVTU64AgeUnit unit);
DN_API DN_Str8 DN_CVT_F64ToAge (DN_Arena *arena, DN_F64 age_s, DN_CVTU64AgeUnit unit);
DN_API DN_U64 DN_CVT_HexToU64 (DN_Str8 hex);
DN_API DN_Str8 DN_CVT_U64ToHex (DN_Arena *arena, DN_U64 number, DN_CVTHexU64Str8Flags flags);
DN_API DN_CVTU64HexStr8 DN_CVT_U64ToHexStr8 (DN_U64 number, DN_U32 flags);
DN_API DN_U64 DN_CVT_HexToU64 (DN_Str8 hex);
DN_API DN_Str8 DN_CVT_U64ToHex (DN_Arena *arena, DN_U64 number, DN_CVTU64HexStrFlags flags);
#define DN_CVT_U64ToHexFromFrame(number, flags) DN_CVT_U64ToHex(DN_OS_TLSFrameArena(), number, flags)
DN_API DN_CVTU64HexStr DN_CVT_U64ToHexStr (DN_U64 number, DN_CVTU64HexStrFlags flags);
DN_API bool DN_CVT_BytesToHexPtr (void const *src, DN_USize src_size, char *dest, DN_USize dest_size);
DN_API DN_Str8 DN_CVT_BytesToHex (DN_Arena *arena, void const *src, DN_USize size);
#define DN_CVT_BytesToHexFromTLS(...) DN_CVT_BytesToHex(DN_OS_TLSTopArena(), __VA_ARGS__)
#define DN_CVT_BytesToHexFromFrame(...) DN_CVT_BytesToHex(DN_OS_TLSFrameArena(), __VA_ARGS__)
DN_API bool DN_CVT_BytesToHexPtr (void const *src, DN_USize src_size, char *dest, DN_USize dest_size);
DN_API DN_Str8 DN_CVT_BytesToHex (DN_Arena *arena, void const *src, DN_USize size);
#define DN_CVT_BytesToHexFromTLS(...) DN_CVT_BytesToHex(DN_OS_TLSTopArena(), __VA_ARGS__)
#define DN_CVT_BytesToHexFromFrame(...) DN_CVT_BytesToHex(DN_OS_TLSFrameArena(), __VA_ARGS__)
DN_API DN_USize DN_CVT_HexToBytesPtrUnchecked (DN_Str8 hex, void *dest, DN_USize dest_size);
DN_API DN_USize DN_CVT_HexToBytesPtr (DN_Str8 hex, void *dest, DN_USize dest_size);
DN_API DN_Str8 DN_CVT_HexToBytesUnchecked (DN_Arena *arena, DN_Str8 hex);
#define DN_CVT_HexToBytesUncheckedFromTLS(...) DN_CVT_HexToBytesUnchecked(DN_OS_TLSTopArena(), __VA_ARGS__)
DN_API DN_Str8 DN_CVT_HexToBytes (DN_Arena *arena, DN_Str8 hex);
#define DN_CVT_HexToBytesFromFrame(...) DN_CVT_HexToBytes(DN_OS_TLSFrameArena(), __VA_ARGS__)
#define DN_CVT_HexToBytesFromTLS(...) DN_CVT_HexToBytes(DN_OS_TLSTopArena(), __VA_ARGS__)
DN_API DN_USize DN_CVT_HexToBytesPtrUnchecked (DN_Str8 hex, void *dest, DN_USize dest_size);
DN_API DN_USize DN_CVT_HexToBytesPtr (DN_Str8 hex, void *dest, DN_USize dest_size);
DN_API DN_Str8 DN_CVT_HexToBytesUnchecked (DN_Arena *arena, DN_Str8 hex);
#define DN_CVT_HexToBytesUncheckedFromTLS(...) DN_CVT_HexToBytesUnchecked(DN_OS_TLSTopArena(), __VA_ARGS__)
DN_API DN_Str8 DN_CVT_HexToBytes (DN_Arena *arena, DN_Str8 hex);
#define DN_CVT_HexToBytesFromFrame(...) DN_CVT_HexToBytes(DN_OS_TLSFrameArena(), __VA_ARGS__)
#define DN_CVT_HexToBytesFromTLS(...) DN_CVT_HexToBytes(DN_OS_TLSTopArena(), __VA_ARGS__)
#endif // defined(DN_BASE_CONVERT_H)
+2 -2
View File
@@ -222,11 +222,11 @@ struct DN_Pool
{
DN_Arena *arena;
DN_PoolSlot *slots[DN_PoolSlotSize_Count];
uint8_t align;
DN_U8 align;
};
// NOTE: DN_Pool ///////////////////////////////////////////////////////////////////////////////////
DN_API DN_Pool DN_Pool_Init (DN_Arena *arena, uint8_t align);
DN_API DN_Pool DN_Pool_Init (DN_Arena *arena, DN_U8 align);
DN_API bool DN_Pool_IsValid (DN_Pool const *pool);
DN_API void * DN_Pool_Alloc (DN_Pool *pool, DN_USize size);
DN_API DN_Str8 DN_Pool_AllocStr8FV (DN_Pool *pool, DN_FMT_ATTRIB char const *fmt, va_list args);