Move some stuff out of LuaStack
This commit is contained in:
@@ -497,6 +497,45 @@ constexpr auto hex32 = FormattedNumber<int>(0, true, 8, '0', 6);
|
||||
constexpr auto hex64 = FormattedNumber<int>(0, true, 16, '0', 6);
|
||||
constexpr auto dec = FormattedNumber<int>(0, false, 0, ' ', 6);
|
||||
|
||||
// Encode a string as a token (fixed-width base38 number).
|
||||
// Each character is mapped to a digit 1-37 (0 means "no character"),
|
||||
// and the result is: CH0*38^11 + CH1*38^10 + ... + CH11*38^0.
|
||||
// Digit mapping: _ → 1, 0-9 → 2-11, a-z → 12-37.
|
||||
// Returns zero if the string is empty, too long, or contains
|
||||
// invalid characters.
|
||||
//
|
||||
static constexpr uint64_t encode_token(std::string_view str) {
|
||||
if (str.size() > 12) return 0;
|
||||
if (str.empty()) return 0;
|
||||
|
||||
uint64_t result = 0;
|
||||
for (int i = 0; i < int(str.size()); i++) {
|
||||
char c = str[i];
|
||||
uint64_t digit = 0;
|
||||
if (c == '_') {
|
||||
digit = 1;
|
||||
} else if ((c >= '0') && (c <= '9')) {
|
||||
digit = uint64_t(c - '0') + 2;
|
||||
} else if ((c >= 'a') && (c <= 'z')) {
|
||||
digit = uint64_t(c - 'a') + 12;
|
||||
} else if ((c >= 'A') && (c <= 'Z')) {
|
||||
digit = uint64_t(c - 'A') + 12;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
result = result * 38 + digit;
|
||||
}
|
||||
// Pad remaining positions with zeros (no character).
|
||||
for (int i = int(str.size()); i < 12; i++) {
|
||||
result = result * 38;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// Decode a token (base38 number) back to a string.
|
||||
//
|
||||
eng::string decode_token(uint64_t value);
|
||||
|
||||
} // namespace util
|
||||
|
||||
template<class VALUE>
|
||||
|
||||
Reference in New Issue
Block a user