diff options
| author | nodist <kevin.comas.git@gmail.com> | 2026-11-13 16:28:13 -0500 |
|---|---|---|
| committer | nodist <kevin.comas.git@gmail.com> | 2026-11-13 16:28:13 -0500 |
| commit | c3d23d3091c23978b12c140e6cfd964eff1d3986 (patch) | |
| tree | 3342a73aea427fa19d2b0936a98e37fbe479c85d /source/identifier.c | |
| parent | 75c8828d804f45cf8f44af73bfe87e0f3eaf9a9e (diff) | |
Diffstat (limited to 'source/identifier.c')
| -rw-r--r-- | source/identifier.c | 95 |
1 files changed, 0 insertions, 95 deletions
diff --git a/source/identifier.c b/source/identifier.c deleted file mode 100644 index 176f46f..0000000 --- a/source/identifier.c +++ /dev/null @@ -1,95 +0,0 @@ - -#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; -} - -#define KPL_IDENTIFIER_VALUES_TOTAL_BITS (KPL_IDENTIFIER_BITS_PER_VALUE * KPL_IDENTIFIER_VALUES_PER_PART) - -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++; - } - } -} - -#define KPL_IDENTIFIER_PART_LENGTH_MASK ~(1UL << 63 | 1UL << 62 | 1UL << 61 | 1UL << 60) - -int32_t kpl_identifier_compare(const kpl_identifier *identifier_left, const kpl_identifier *identifier_right) { - for (uint32_t part_index = 0; part_index < KPL_IDENTIFIER_PARTS; part_index++) { - uint64_t left_part = identifier_left->parts[part_index], right_part = identifier_right->parts[part_index]; - if (left_part == right_part) - continue; - if (!left_part && !right_part) - break; - left_part &= KPL_IDENTIFIER_PART_LENGTH_MASK; - right_part &= KPL_IDENTIFIER_PART_LENGTH_MASK; - while (left_part && right_part) { - char left = kpl_identifier_value_to_char(left_part), right = kpl_identifier_value_to_char(right_part); - int32_t result = left - right; - if (result) - return result; - left_part >>= KPL_IDENTIFIER_BITS_PER_VALUE; - right_part >>= KPL_IDENTIFIER_BITS_PER_VALUE; - } - } - return 0; -} - -extern inline bool kpl_identifier_equal(const kpl_identifier *identifier_a, const kpl_identifier *identifier_b); |
