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

+6
View File
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
## [3.5.0] - 2026-11-16
### Changed
- `WpUuid` API
## [3.4.0] - 2026-11-11 ## [3.4.0] - 2026-11-11
### Changed ### Changed
+1 -1
View File
@@ -1 +1 @@
3.4.0 3.5.0
+24 -29
View File
@@ -8,36 +8,19 @@
#include <inttypes.h> #include <inttypes.h>
#define UUID_STR_FORMAT ("%.8" PRIx64 "-%.4" PRIx64 "-%.4" PRIx64 "-%.4" PRIx64 "-%.12" PRIx64) #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; WpUuid wpUuidGenUuid4(void) {
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) {
wp_persist WpXor256State state = {0}; wp_persist WpXor256State state = {0};
wp_persist b8 initialised = false; wp_persist b8 initialised = false;
if (!initialised) { if (!initialised) {
initialised = true; initialised = true;
state = wpPrngXorshiftInit(); state = wpPrngXorshiftInit();
} }
UUID4 uuid = (UUID4){ WpUuid uuid = {
.high = wpPrngXorshift256(&state), .high = wpPrngXorshift256(&state),
.low = wpPrngXorshift256(&state), .low = wpPrngXorshift256(&state),
}; };
@@ -47,12 +30,24 @@ wp_intern UUID4 genUuid4(void) {
return uuid; return uuid;
} }
wp_intern void uuid4ToUuid(const UUID4* uuid4, WpUuid *uuid) { void wpUuidToStr8(const WpUuid *uuid, WpStr8 *buf) {
u64 group1 = uuid4->high >> 32; wpDebugAssert(uuid != NULL, "`uuid` should not be NULL");
u64 group2 = (uuid4->high << 32) >> 48; wpRuntimeAssert(buf != NULL && buf->capacity >= WP_UUID_STR_BUF_LENGTH,
u64 group3 = (uuid4->high << 48) >> 48; "String buffer is NULL or doesn't have sufficient capacity");
u64 group4 = uuid4->low >> 48;
u64 group5 = (uuid4->low << 16) >> 16;
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 BEGIN_C_LINKAGE
#endif // !WP_PLATFORM_CPP #endif // !WP_PLATFORM_CPP
#define WP_UUID_BUF_LENGTH 48 #define WP_UUID_STR_BUF_LENGTH 36
#define WP_UUID_SPEC WP_STR8_SPEC #define WP_UUID_HEX_BUF_LENGTH 32
#define wpUuidVarg(WpUuid) wpStr8Varg((WpUuid).uuid)
typedef struct WpUuid WpUuid; typedef struct WpUuid {
struct WpUuid { u64 high;
WpStr8 uuid; u64 low;
}; } WpUuid;
// TODO (Abdelrahman): Update UUID implementation to work properly with C++ and tests for validation WpUuid wpUuidGenUuid4(void);
void wpUuidToStr8(const WpUuid *uuid, WpStr8 *buf);
#ifdef WP_PLATFORM_CPP void wpUuidToHex(const WpUuid *uuid, WpStr8 *buf);
#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);
#ifdef WP_PLATFORM_CPP #ifdef WP_PLATFORM_CPP
END_C_LINKAGE END_C_LINKAGE