Add UUIDv4 generator
This commit is contained in:
parent
74428f1caf
commit
c83c652b37
59
src/uuid/uuid.c
Normal file
59
src/uuid/uuid.c
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
#include "uuid.h"
|
||||||
|
#include "../common/aliases/aliases.h"
|
||||||
|
#include "../core/strings/str8/str8.h"
|
||||||
|
#include "../prng/xorshift/xorshift.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <inttypes.h>
|
||||||
|
|
||||||
|
#define UUID_STR_FORMAT ("%.8" PRIx64 "-%.4" PRIx64 "-%.4" PRIx64 "-%.4" PRIx64 "-%.12" PRIx64)
|
||||||
|
|
||||||
|
typedef struct uuid4 UUID4;
|
||||||
|
struct uuid4 {
|
||||||
|
u64 high;
|
||||||
|
u64 low;
|
||||||
|
};
|
||||||
|
|
||||||
|
internal UUID4 generate_uuid4(void);
|
||||||
|
internal void uuid4_to_uuid(const UUID4* uuid4, UUID *uuid);
|
||||||
|
|
||||||
|
UUID *wapp_uuid_init_uuid4(UUID *uuid) {
|
||||||
|
if (!uuid) {
|
||||||
|
goto RETURN_INIT_UUID4;
|
||||||
|
}
|
||||||
|
|
||||||
|
UUID4 uuid4 = generate_uuid4();
|
||||||
|
uuid4_to_uuid(&uuid4, uuid);
|
||||||
|
|
||||||
|
RETURN_INIT_UUID4:
|
||||||
|
return uuid;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal UUID4 generate_uuid4(void) {
|
||||||
|
persistent XOR256State state = {0};
|
||||||
|
persistent bool initialised = false;
|
||||||
|
|
||||||
|
if (!initialised) {
|
||||||
|
initialised = true;
|
||||||
|
state = wapp_prng_xorshift_init_state();
|
||||||
|
}
|
||||||
|
|
||||||
|
UUID4 uuid = (UUID4){
|
||||||
|
.high = wapp_prng_xorshift_256(&state),
|
||||||
|
.low = wapp_prng_xorshift_256(&state),
|
||||||
|
};
|
||||||
|
|
||||||
|
uuid.high = (uuid.high & 0xffffffffffff0fff) | 0x0000000000004000;
|
||||||
|
uuid.low = (uuid.low & 0x3fffffffffffffff) | 0x8000000000000000;
|
||||||
|
|
||||||
|
return uuid;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void uuid4_to_uuid(const UUID4* uuid4, UUID *uuid) {
|
||||||
|
u64 grp1 = uuid4->high >> 32;
|
||||||
|
u64 grp2 = (uuid4->high << 32) >> 48;
|
||||||
|
u64 grp3 = (uuid4->high << 48) >> 48;
|
||||||
|
u64 grp4 = uuid4->low >> 48;
|
||||||
|
u64 grp5 = (uuid4->low << 16) >> 16;
|
||||||
|
|
||||||
|
wapp_str8_format(&(uuid->uuid), UUID_STR_FORMAT, grp1, grp2, grp3, grp4, grp5);
|
||||||
|
}
|
34
src/uuid/uuid.h
Normal file
34
src/uuid/uuid.h
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
#ifndef UUID_H
|
||||||
|
#define UUID_H
|
||||||
|
|
||||||
|
#include "../common/aliases/aliases.h"
|
||||||
|
#include "../core/strings/str8/str8.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
BEGIN_C_LINKAGE
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
#define UUID_BUF_LENGTH 48
|
||||||
|
#define WAPP_UUID_SPEC WAPP_STR8_SPEC
|
||||||
|
#define wapp_uuid_varg(UUID) wapp_str8_varg((UUID).uuid)
|
||||||
|
|
||||||
|
typedef struct uuid UUID;
|
||||||
|
struct uuid {
|
||||||
|
Str8 uuid;
|
||||||
|
};
|
||||||
|
|
||||||
|
#define wapp_uuid_gen_uuid4() *(wapp_uuid_init_uuid4(&wapp_uuid_create()))
|
||||||
|
|
||||||
|
/* Low level UUID API */
|
||||||
|
|
||||||
|
#define wapp_uuid_create() ((UUID){.uuid = wapp_str8_buf(UUID_BUF_LENGTH)})
|
||||||
|
|
||||||
|
// Just returns the same pointer that was passed in with the UUID initialised.
|
||||||
|
// If the pointer is NULL, it skips initialisation.
|
||||||
|
UUID *wapp_uuid_init_uuid4(UUID *uuid);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
END_C_LINKAGE
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
#endif // !UUID_H
|
8
src/uuid/wapp_uuid.c
Normal file
8
src/uuid/wapp_uuid.c
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#ifndef WAPP_UUID_C
|
||||||
|
#define WAPP_UUID_C
|
||||||
|
|
||||||
|
#include "uuid.c"
|
||||||
|
#include "../core/wapp_core.c"
|
||||||
|
#include "../prng/wapp_prng.c"
|
||||||
|
|
||||||
|
#endif // !WAPP_UUID_C
|
9
src/uuid/wapp_uuid.h
Normal file
9
src/uuid/wapp_uuid.h
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#ifndef WAPP_UUID_H
|
||||||
|
#define WAPP_UUID_H
|
||||||
|
|
||||||
|
#include "uuid.h"
|
||||||
|
#include "../common/wapp_common.h"
|
||||||
|
#include "../core/wapp_core.h"
|
||||||
|
#include "../prng/wapp_prng.h"
|
||||||
|
|
||||||
|
#endif // !WAPP_UUID_H
|
@ -4,6 +4,7 @@
|
|||||||
#include "wapp.h"
|
#include "wapp.h"
|
||||||
#include "core/wapp_core.c"
|
#include "core/wapp_core.c"
|
||||||
#include "prng/wapp_prng.c"
|
#include "prng/wapp_prng.c"
|
||||||
|
#include "uuid/uuid.c"
|
||||||
#include "testing/wapp_testing.c"
|
#include "testing/wapp_testing.c"
|
||||||
|
|
||||||
#endif // !WAPP_C
|
#endif // !WAPP_C
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
#include "common/wapp_common.h"
|
#include "common/wapp_common.h"
|
||||||
#include "core/wapp_core.h"
|
#include "core/wapp_core.h"
|
||||||
#include "prng/wapp_prng.h"
|
#include "prng/wapp_prng.h"
|
||||||
|
#include "uuid/wapp_uuid.h"
|
||||||
#include "testing/wapp_testing.h"
|
#include "testing/wapp_testing.h"
|
||||||
|
|
||||||
#endif // !WAPP_H
|
#endif // !WAPP_H
|
||||||
|
Loading…
Reference in New Issue
Block a user