Updated WpUuid API
Release / release (push) Successful in 12s

This commit is contained in:
abdelrahman committed 2026-11-16 21:32:20 +00:00
1 parent 45e58a23f6
commit fc4f3f49d2
4 files changed
+40 -59

No files matched your search

+24 -29
View File
@@ -8,36 +8,19 @@
#include <inttypes.h>
#define UUID_STR_FORMAT ("%.8" PRIx64 "-%.4" PRIx64 "-%.4" PRIx64 "-%.4" PRIx64 "-%.12" PRIx64)
#define UUID_HEX_FORMAT ("%.16" PRIx64 "%.16" PRIx64)
typedef struct UUID4 UUID4;
struct UUID4 {
u64 high;
u64 low;
};
wp_intern UUID4 genUuid4(void);
wp_intern void uuid4ToUuid(const UUID4* uuid4, WpUuid *uuid);
WpUuid *wpUuidInitUuid4(WpUuid *uuid) {
wpDebugAssert(uuid != NULL, "`uuid` should not be NULL");
UUID4 uuid4 = genUuid4();
uuid4ToUuid(&uuid4, uuid);
return uuid;
}
wp_intern UUID4 genUuid4(void) {
WpUuid wpUuidGenUuid4(void) {
wp_persist WpXor256State state = {0};
wp_persist b8 initialised = false;
wp_persist b8 initialised = false;
if (!initialised) {
initialised = true;
state = wpPrngXorshiftInit();
}
UUID4 uuid = (UUID4){
.high = wpPrngXorshift256(&state),
WpUuid uuid = {
.high = wpPrngXorshift256(&state),
.low = wpPrngXorshift256(&state),
};
@@ -47,12 +30,24 @@ wp_intern UUID4 genUuid4(void) {
return uuid;
}
wp_intern void uuid4ToUuid(const UUID4* uuid4, WpUuid *uuid) {
u64 group1 = uuid4->high >> 32;
u64 group2 = (uuid4->high << 32) >> 48;
u64 group3 = (uuid4->high << 48) >> 48;
u64 group4 = uuid4->low >> 48;
u64 group5 = (uuid4->low << 16) >> 16;
void wpUuidToStr8(const WpUuid *uuid, WpStr8 *buf) {
wpDebugAssert(uuid != NULL, "`uuid` should not be NULL");
wpRuntimeAssert(buf != NULL && buf->capacity >= WP_UUID_STR_BUF_LENGTH,
"String buffer is NULL or doesn't have sufficient capacity");
wpStr8Format(&(uuid->uuid), UUID_STR_FORMAT, group1, group2, group3, group4, group5);
u64 group1 = uuid->high >> 32;
u64 group2 = (uuid->high << 32) >> 48;
u64 group3 = (uuid->high << 48) >> 48;
u64 group4 = uuid->low >> 48;
u64 group5 = (uuid->low << 16) >> 16;
wpStr8Format(buf, UUID_STR_FORMAT, group1, group2, group3, group4, group5);
}
void wpUuidToHex(const WpUuid *uuid, WpStr8 *buf) {
wpDebugAssert(uuid != NULL, "`uuid` should not be NULL");
wpRuntimeAssert(buf != NULL && buf->capacity >= WP_UUID_HEX_BUF_LENGTH,
"String buffer is NULL or doesn't have sufficient capacity");
wpStr8Format(buf, UUID_HEX_FORMAT, uuid->high, uuid->low);
}
+9 -29
View File
@@ -11,37 +11,17 @@
BEGIN_C_LINKAGE
#endif // !WP_PLATFORM_CPP
#define WP_UUID_BUF_LENGTH 48
#define WP_UUID_SPEC WP_STR8_SPEC
#define wpUuidVarg(WpUuid) wpStr8Varg((WpUuid).uuid)
#define WP_UUID_STR_BUF_LENGTH 36
#define WP_UUID_HEX_BUF_LENGTH 32
typedef struct WpUuid WpUuid;
struct WpUuid {
WpStr8 uuid;
};
typedef struct WpUuid {
u64 high;
u64 low;
} WpUuid;
// TODO (Abdelrahman): Update UUID implementation to work properly with C++ and tests for validation
#ifdef WP_PLATFORM_CPP
#define wpUuidGenUuid4() ([&](){ \
wp_persist WpUuid uuid = wpUuidCreate(); \
return *(wpUuidInitUuid4(&uuid)); \
}())
#else
#define wpUuidGenUuid4() *(wpUuidInitUuid4(&wpUuidCreate()))
#endif
/* Low level UUID API */
#ifdef WP_PLATFORM_CPP
#define wpUuidCreate() ([&](){ return WpUuid{wpStr8Buf(WP_UUID_BUF_LENGTH)}; }())
#else
#define wpUuidCreate() ((WpUuid){.uuid = wpStr8Buf(WP_UUID_BUF_LENGTH)})
#endif
// Just returns the same pointer that was passed in with the UUID initialised.
// Fails when passed a NULL pointer.
WpUuid *wpUuidInitUuid4(WpUuid *uuid);
WpUuid wpUuidGenUuid4(void);
void wpUuidToStr8(const WpUuid *uuid, WpStr8 *buf);
void wpUuidToHex(const WpUuid *uuid, WpStr8 *buf);
#ifdef WP_PLATFORM_CPP
END_C_LINKAGE