From 19eb183b38511998322d99c33515c079d89785e3 Mon Sep 17 00:00:00 2001 From: nodist Date: Sat, 24 Oct 2026 20:06:26 -0400 Subject: add kpl_identifier --- include/kpl/identifier.h | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 include/kpl/identifier.h (limited to 'include/kpl/identifier.h') diff --git a/include/kpl/identifier.h b/include/kpl/identifier.h new file mode 100644 index 0000000..092b0c4 --- /dev/null +++ b/include/kpl/identifier.h @@ -0,0 +1,41 @@ +/* + * Thread safe identifiers, at the cost of space remove the need for hash/tree lookups + * Keep 50 [A-Za-z0-9_] Characters in 40 bytes + */ + +#pragma once + +#define KPL_IDENTIFIER_PARTS 5 + +#define KPL_IDENTIFIER_BITS_PER_PART 64 + +#define KPL_IDENTIFIER_BITS_PER_VALUE 6 + +#define KPL_IDENTIFIER_VALUES_PER_PART (KPL_IDENTIFIER_BITS_PER_PART / KPL_IDENTIFIER_BITS_PER_VALUE) + +#define KPL_IDENTIFIER_VALUES_TOTAL_BITS (KPL_IDENTIFIER_BITS_PER_VALUE * KPL_IDENTIFIER_VALUES_PER_PART) + +#define KPL_IDENTIFIER_MAX_LENGTH (KPL_IDENTIFIER_VALUES_PER_PART * KPL_IDENTIFIER_PARTS) + +#define KPL_IDENTIFIER_C_STRING_SIZE (KPL_IDENTIFIER_MAX_LENGTH + sizeof(char)) + +/* + * Each character takes 6 bits from bit 0 to bit 59 + * The upper 4 bits represent the number of characters in the part + */ +typedef struct { + uint64_t parts[KPL_IDENTIFIER_PARTS]; +} kpl_identifier; + +/* + * Converts a c string into an identifier + * Checks if the c_string_length <= KPL_IDENTIFIER_MAX_LENGTH + */ +bool kpl_identifier_encode(kpl_identifier *identifier, const char *c_string, size_t c_string_length); + +/* + * Converts an identifier into a c string + * Does not clear the c_string buffer + * Does not check the c_string buffer assumes the buffer is at least KPL_IDENTIFIER_C_STRING_SIZE + */ +void kpl_identifier_decode(kpl_identifier *identifier, char c_string[KPL_IDENTIFIER_C_STRING_SIZE]); -- cgit v1.2.3