From fc4f3f49d2947588818d69319f89eba774dfc2e6 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Mon, 16 Nov 2026 21:32:20 +0000 Subject: [PATCH] Updated WpUuid API --- CHANGELOG.md | 6 ++++++ VERSION | 2 +- src/uuid/uuid.c | 53 ++++++++++++++++++++++--------------------------- src/uuid/uuid.h | 38 +++++++++-------------------------- 4 files changed, 40 insertions(+), 59 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 07b7fc4..5881269 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [3.5.0] - 2026-11-16 + +### Changed + +- `WpUuid` API + ## [3.4.0] - 2026-11-11 ### Changed diff --git a/VERSION b/VERSION index 1809198..1545d96 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -3.4.0 +3.5.0 diff --git a/src/uuid/uuid.c b/src/uuid/uuid.c index 2271322..796653a 100644 --- a/src/uuid/uuid.c +++ b/src/uuid/uuid.c @@ -8,36 +8,19 @@ #include #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); } diff --git a/src/uuid/uuid.h b/src/uuid/uuid.h index 51a7fdd..7e9a2c6 100644 --- a/src/uuid/uuid.h +++ b/src/uuid/uuid.h @@ -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