diff options
| author | nodist <kevin.comas.git@gmail.com> | 2026-10-24 20:06:26 -0400 |
|---|---|---|
| committer | nodist <kevin.comas.git@gmail.com> | 2026-10-24 20:06:26 -0400 |
| commit | 19eb183b38511998322d99c33515c079d89785e3 (patch) | |
| tree | 3d0446b1d2fe6a99ea2e9a6c1300516bfba1f590 /source/identifier.c | |
| parent | 689719e0a1f139e28e57a4df334d5fde354b3379 (diff) | |
add kpl_identifier
Diffstat (limited to 'source/identifier.c')
| -rw-r--r-- | source/identifier.c | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/source/identifier.c b/source/identifier.c new file mode 100644 index 0000000..f6a7279 --- /dev/null +++ b/source/identifier.c @@ -0,0 +1,68 @@ + +#include "kpl.h" + +#define KPL_IDENTIFIER_NUMBER_RANGE_START 1 + +#define KPL_IDENTIFIER_UPPER_RANGE_START 12 + +#define KPL_IDENTIFIER_LOWER_RANGE_START 38 + +static uint64_t kpl_identifier_char_to_value(char c, uint64_t value_index) { + uint64_t value = 0; + if (c >= '0' && c <= '9') + value = c - '0' + KPL_IDENTIFIER_NUMBER_RANGE_START; + else if (c >= 'A' && c <= 'Z') + value = c - 'A' + KPL_IDENTIFIER_UPPER_RANGE_START; + else if (c >= 'a' && c <= 'z') + value = c - 'a' + KPL_IDENTIFIER_LOWER_RANGE_START; + return value << value_index * KPL_IDENTIFIER_BITS_PER_VALUE; +} + +bool kpl_identifier_encode(kpl_identifier *identifier, const char *c_string, size_t c_string_length) { + if (c_string_length > KPL_IDENTIFIER_MAX_LENGTH) + return false; + for (size_t string_index = 0; string_index < c_string_length; string_index++) + if (!isalnum(c_string[string_index]) && c_string[string_index] != '_') + return false; + for (uint32_t part_index = 0; part_index < KPL_IDENTIFIER_PARTS; part_index++) + identifier->parts[part_index] = 0; + uint32_t string_index = 0, part_index = 0; + while (string_index < c_string_length) { + uint64_t value_index = 0; + while (value_index < KPL_IDENTIFIER_VALUES_PER_PART && string_index < c_string_length) { + identifier->parts[part_index] |= kpl_identifier_char_to_value(c_string[string_index], value_index); + value_index++; + string_index++; + } + identifier->parts[part_index] |= value_index << KPL_IDENTIFIER_VALUES_TOTAL_BITS; + part_index++; + } + return true; +} + +static char kpl_identifier_value_to_char(uint64_t part) { + uint8_t value = part & (1 << 5 | 1 << 4 | 1 << 3 | 1 << 2 | 1 << 1 | 1 << 0); + if (!value) + return '_'; + if (value < KPL_IDENTIFIER_UPPER_RANGE_START) + return value - KPL_IDENTIFIER_NUMBER_RANGE_START + '0'; + if (value < KPL_IDENTIFIER_LOWER_RANGE_START) + return value - KPL_IDENTIFIER_UPPER_RANGE_START + 'A'; + return value - KPL_IDENTIFIER_LOWER_RANGE_START + 'a'; +} + +void kpl_identifier_decode(kpl_identifier *identifier, char c_string[KPL_IDENTIFIER_C_STRING_SIZE]) { + uint32_t string_index = 0; + for (uint32_t part_index = 0; part_index < KPL_IDENTIFIER_PARTS; part_index++) { + uint64_t part = identifier->parts[part_index]; + uint64_t value_length = part >> KPL_IDENTIFIER_VALUES_TOTAL_BITS; + if (!value_length) + break; + while (value_length) { + c_string[string_index] = kpl_identifier_value_to_char(part); + part >>= KPL_IDENTIFIER_BITS_PER_VALUE; + value_length--; + string_index++; + } + } +} |
