Can now pass tokens as values in animation steps
This commit is contained in:
@@ -409,15 +409,14 @@ enum LuaTableType {
|
||||
struct LuaToken {
|
||||
private:
|
||||
// Convert a base36 number into a token. If the base36 number is
|
||||
// not valid, or if it exceeds 64 bits, then return maxint, ie,
|
||||
// the invalid token.
|
||||
// not valid, or if it exceeds 64 bits, then return zero.
|
||||
//
|
||||
static constexpr uint64_t parse(std::string_view str) {
|
||||
uint64_t result = 0;
|
||||
uint64_t maxint = uint64_t(-1);
|
||||
|
||||
// Leading zeros are not allowed.
|
||||
if ((!str.empty()) && (str[0]=='0')) return maxint;
|
||||
if ((!str.empty()) && (str[0]=='0')) return 0;
|
||||
|
||||
for (int i = 0; i < int(str.size()); i++) {
|
||||
char c = str[i];
|
||||
@@ -433,9 +432,9 @@ private:
|
||||
}
|
||||
// Multiply existing number by 36, then add the digit.
|
||||
// We have two checks to prevent integer overflow.
|
||||
if (result > (maxint / 36)) return maxint;
|
||||
if (result > (maxint / 36)) return 0;
|
||||
result *= 36;
|
||||
if (digit > (maxint - result)) return maxint;
|
||||
if (digit > (maxint - result)) return 0;
|
||||
result += digit;
|
||||
}
|
||||
return result;
|
||||
@@ -451,20 +450,21 @@ public:
|
||||
|
||||
// Construct a token from a string.
|
||||
//
|
||||
// If the string is not a valid token, then this initializes the
|
||||
// token to the invalid token.
|
||||
// If the string is not a valid base36 number, then this
|
||||
// initializes the token to the empty token (zero)
|
||||
//
|
||||
LuaToken(std::string_view s) : value(parse(s)) {}
|
||||
LuaToken(const eng::string &s) : value(parse(s)) {}
|
||||
|
||||
// Construct a token from a compile-time constant string.
|
||||
//
|
||||
// It appears that the code below throws an exception if the
|
||||
// string is invalid. But in reality, since this function is
|
||||
// string not parseable. But in reality, since this function is
|
||||
// consteval (evaluated at compile time), the error is
|
||||
// generated during the compilation.
|
||||
//
|
||||
consteval LuaToken(const char *s) : value(parse(s)) {
|
||||
if (is_invalid()) throw "Invalid token";
|
||||
if (empty()) throw "cannot parse token";
|
||||
}
|
||||
|
||||
// Construct a token from an int64.
|
||||
@@ -482,9 +482,9 @@ public:
|
||||
// Assignment operator.
|
||||
void operator =(const LuaToken &other) { value = other.value; }
|
||||
|
||||
// Empty: return true if the token is all zero bytes.
|
||||
// Empty: return true if the token is zero.
|
||||
//
|
||||
bool empty() const { return value == 0; }
|
||||
constexpr bool empty() const { return value == 0; }
|
||||
|
||||
// Compare two tokens for equality.
|
||||
//
|
||||
@@ -494,19 +494,12 @@ public:
|
||||
//
|
||||
void *voidvalue() const { return (void*)value; }
|
||||
|
||||
// Return true if it's the invalid token.
|
||||
//
|
||||
constexpr bool is_invalid() { return value == uint64_t(-1); }
|
||||
|
||||
// Convert the token to a string.
|
||||
//
|
||||
// The conversion to string consists of expressing the value
|
||||
// in base 36. The value 0 is expressed as the empty string.
|
||||
// in base 36.
|
||||
//
|
||||
eng::string str() const;
|
||||
|
||||
public:
|
||||
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
|
||||
Reference in New Issue
Block a user