Change directory structure

This commit is contained in:
2023-02-14 14:05:45 -05:00
parent acad4291b6
commit def6387ca3
323 changed files with 161 additions and 19581 deletions

864
luprex/cpp/core/util.cpp Normal file
View File

@@ -0,0 +1,864 @@
#include "wrap-string.hpp"
#include "wrap-vector.hpp"
#include "util.hpp"
#include "fast-float.hpp"
#include <algorithm>
#include <sys/types.h>
#include <sys/stat.h>
#include <iomanip>
#include <cassert>
#include <cstdlib>
#include <cmath>
#include <charconv>
#ifdef WIN32
#endif
#ifndef WIN32
#include <time.h>
#include <unistd.h>
#endif
namespace sv {
bool case_insensitive_eq(string_view s1, string_view s2) {
if (s1.size() != s2.size()) return false;
for (int i = 0; i < int(s1.size()); i++) {
char c1 = s1[i];
char c2 = s2[i];
if (ascii_isupper(c1)) c1 += 'a'-'A';
if (ascii_isupper(c2)) c2 += 'a'-'A';
if (c1 != c2) return false;
}
return true;
}
bool valid_int64(string_view value) {
int64_t result;
const char *last = value.data() + value.size();
auto r = std::from_chars(value.data(), last, result, 10);
if (r.ec != std::errc()) return false;
if (r.ptr != last) return false;
return true;
}
bool valid_hex64(string_view value) {
int64_t result;
const char *last = value.data() + value.size();
auto r = std::from_chars(value.data(), last, result, 16);
if (r.ec != std::errc()) return false;
if (r.ptr != last) return false;
return true;
}
bool valid_double(string_view value) {
double result;
const char *last = value.data() + value.size();
auto r = fast_float::from_chars(value.data(), last, result);
if (r.ec != std::errc()) return false;
if (r.ptr != last) return false;
return true;
}
int64_t to_int64(string_view value, int64_t errval) {
int64_t result;
const char *p = value.data();
const char *last = p + value.size();
if ((p < last) && (*p == '+')) p++;
auto r = std::from_chars(p, last, result, 10);
if (r.ec != std::errc()) return errval;
if (r.ptr != last) return errval;
return result;
}
uint64_t to_hex64(string_view value, uint64_t errval) {
uint64_t result;
if (sv::zfront(value) == '-') return errval;
const char *last = value.data() + value.size();
auto r = std::from_chars(value.data(), last, result, 16);
if (r.ec != std::errc()) return errval;
if (r.ptr != last) return errval;
return result;
}
double to_double(string_view value, double errval) {
double result;
const char *last = value.data() + value.size();
auto r = fast_float::from_chars(value.data(), last, result);
if (r.ec != std::errc()) return errval;
if (r.ptr != last) return errval;
return result;
}
string_view ltrim(string_view v) {
while ((!v.empty()) && (ascii_isspace(v.front()))) {
v.remove_prefix(1);
}
return v;
}
string_view rtrim(string_view v) {
while ((!v.empty()) && (ascii_isspace(v.back()))) {
v.remove_suffix(1);
}
return v;
}
string_view trim(string_view v) {
while ((!v.empty()) && (ascii_isspace(v.front()))) {
v.remove_prefix(1);
}
while ((!v.empty()) && (ascii_isspace(v.back()))) {
v.remove_suffix(1);
}
return v;
}
string_view ltrim(string_view v, char c) {
while ((!v.empty()) && (v.front() == c)) {
v.remove_prefix(1);
}
return v;
}
string_view rtrim(string_view v, char c) {
while ((!v.empty()) && (v.back() == c)) {
v.remove_suffix(1);
}
return v;
}
string_view trim(string_view v, char c) {
while ((!v.empty()) && (v.front() == c)) {
v.remove_prefix(1);
}
while ((!v.empty()) && (v.back() == c)) {
v.remove_suffix(1);
}
return v;
}
bool has_prefix(string_view s, string_view prefix) {
return 0 == s.compare(0, prefix.size(), prefix);
}
bool has_suffix(string_view s, string_view suffix) {
if (s.length() >= suffix.length()) {
return (0 == s.compare (s.length() - suffix.length(), suffix.length(), suffix));
} else {
return false;
}
}
int common_prefix_length(string_view a, string_view b) {
int minlen = std::min(a.size(), b.size());
for (int i = 0; i < minlen; i++) {
if (a[i] != b[i]) return i;
}
return minlen;
}
bool is_lua_id(string_view str) {
if (str.size() == 0) return false;
char c=str[0];
if ((!ascii_isalpha(c)) && (c!='_')) return false;
for (int i = 1; i < int(str.size()); i++) {
char c = str[i];
if ((!ascii_isalpha(c)) && (!ascii_isdigit(c)) && (c!='_')) return false;
}
return true;
}
bool is_lua_comment(string_view s) {
int start = 0;
while ((start < int(s.size())) && ((s[start]==' ') || (s[start]=='\t'))) start++;
return s.substr(start, 2) == "--";
}
string_view read_to_sep(string_view &source, char sep) {
size_t pos = source.find(sep);
string_view result;
if (pos == string_view::npos) {
result = source;
source = string_view();
} else {
result = source.substr(0, pos);
source = source.substr(pos + 1);
}
return result;
}
string_view read_to_line(string_view &source) {
size_t pos = source.find('\n');
string_view result;
if (pos == string_view::npos) {
result = source;
source = string_view();
} else {
result = source.substr(0, pos);
source = source.substr(pos + 1);
}
if ((!result.empty()) && (result.back() == '\r')) {
result.remove_suffix(1);
}
return result;
}
bool read_prefix(string_view &source, string_view prefix) {
if (0 == source.compare(0, prefix.size(), prefix)) {
source.remove_prefix(prefix.size());
return true;
} else {
return false;
}
}
string_view read_to_space(string_view &source) {
size_t pos1 = 0;
while ((pos1 < source.size()) && (!ascii_isspace(source[pos1]))) {
pos1 += 1;
}
string_view result = source.substr(0, pos1);
if (pos1 == source.size()) {
source = string_view();
return result;
}
size_t pos2 = pos1 + 1;
while ((pos2 < source.size()) && (ascii_isspace(source[pos2]))) {
pos2 += 1;
}
source = source.substr(pos2);
return result;
}
string_view read_nbytes(string_view &source, int nbytes) {
if (nbytes < 0) nbytes = 0;
if (nbytes > int(source.size())) nbytes = source.size();
string_view result = source.substr(0, nbytes);
source = source.substr(nbytes);
return result;
}
string_view read_ascii_identifier(string_view &source) {
size_t len = 0;
if ((len < source.size()) && (sv::ascii_isalpha(source[len]))) {
len += 1;
while ((len < source.size()) && (sv::ascii_isalnum(source[len]))) {
len += 1;
}
}
string_view result = source.substr(0, len);
source.remove_prefix(len);
return result;
}
std::string_view read_number(string_view &source, bool plus, bool minus, bool dec, bool exp) {
const char *p = source.data();
const char *l = p + source.size();
if (p == l) return source.substr(0, 0);
char sign = *p;
if (sign == '+') {
if (!plus) return source.substr(0, 0);
p++;
}
if (sign == '-') {
if (!minus) return source.substr(0, 0);
p++;
}
if (p == l) return source.substr(0, 0);
bool have_digits = false;
while ((p < l) && (ascii_isdigit(*p))) {
have_digits = true;
p++;
}
if ((p < l) && dec && (*p == '.')) {
p++;
while ((p < l) && (ascii_isdigit(*p))) {
have_digits = true;
p++;
}
}
if (!have_digits) return source.substr(0, 0);
if ((p < l) && exp && ((*p == 'e')||(*p == 'E'))) {
p++;
if ((p < l) && ((*p == '+') || (*p == '-'))) {
p++;
}
bool have_exp = false;
while ((p < l) && (ascii_isdigit(*p))) {
have_exp = true;
p++;
}
if (!have_exp) return source.substr(0, 0);
}
string_view result = source.substr(0, p - source.data());
source.remove_prefix(result.size());
return result;
}
int32_t read_ascii_char(string_view &source) {
if (source.empty()) return -1;
int32_t result = source.front();
source.remove_prefix(1);
return result;
}
int32_t read_codepoint_utf8(string_view &source) {
size_t size = source.size();
if (size == 0) return -1;
const unsigned char *bytes = (const unsigned char *)source.data();
int codepoint;
size_t seqlen;
if ((bytes[0] & 0x80) == 0x00) {
// U+0000 to U+007F
codepoint = (bytes[0] & 0x7F);
seqlen = 1;
} else if ((bytes[0] & 0xE0) == 0xC0) {
// U+0080 to U+07FF
codepoint = (bytes[0] & 0x1F);
seqlen = 2;
} else if ((bytes[0] & 0xF0) == 0xE0) {
// U+0800 to U+FFFF
codepoint = (bytes[0] & 0x0F);
seqlen = 3;
} else if ((bytes[0] & 0xF8) == 0xF0) {
// U+10000 to U+10FFFF
codepoint = (bytes[0] & 0x07);
seqlen = 4;
} else {
return -1;
}
if (seqlen > size) {
return -1;
}
for (size_t i = 1; i < seqlen; ++i) {
if ((bytes[i] & 0xC0) != 0x80) return -1;
codepoint = (codepoint << 6) | (bytes[i] & 0x3F);
}
if ((codepoint > 0x10FFFF) ||
((codepoint >= 0xD800) && (codepoint <= 0xDFFF)) ||
((codepoint <= 0x007F) && (seqlen != 1)) ||
((codepoint >= 0x0080) && (codepoint <= 0x07FF) && (seqlen != 2)) ||
((codepoint >= 0x0800) && (codepoint <= 0xFFFF) && (seqlen != 3)) ||
((codepoint >= 0x10000) && (codepoint <= 0x1FFFFF) && (seqlen != 4))) {
return -1;
}
source.remove_prefix(seqlen);
return codepoint;
}
bool valid_utf8(string_view s)
{
while (!s.empty()) {
int32_t codepoint = read_codepoint_utf8(s);
if (codepoint < 0) return false;
}
return true;
}
bool valid_number(string_view s, bool plus, bool minus, bool dec, bool exp) {
read_number(s, plus, minus, dec, exp);
return s.empty();
}
} // namespace sv
namespace util {
eng::string ascii_tolower(std::string_view s) {
eng::string mod(s);
for (int i = 0; i < int(mod.size()); i++) {
if (sv::ascii_isupper(mod[i])) {
mod[i] += 'a' - 'A';
}
}
return mod;
}
eng::string ascii_toupper(std::string_view s) {
eng::string mod(s);
for (int i = 0; i < int(mod.size()); i++) {
if (sv::ascii_islower(mod[i])) {
mod[i] += 'A' - 'a';
}
}
return mod;
}
void quote_string(const eng::string &s, std::ostream *os) {
bool anysq = false;
bool anydq = false;
for (char c : s) {
if (c == '\'') anysq = true;
if (c == '"') anydq = true;
}
bool usesinglequote = (!anysq)||(anydq);
(*os) << (usesinglequote ? '\'' : '"');
for (char c : s) {
if (c >= 32) {
if (c == '"') {
(*os) << (usesinglequote ? "\"" : "\\\"");
} else if (c == '\'') {
(*os) << (usesinglequote ? "\\'" : "'");
} else if (c == '\\') {
(*os) << "\\\\";
} else {
(*os) << c;
}
} else {
unsigned int value = ((unsigned char)c);
switch (c) {
case '\n': (*os) << "\\n"; break;
case '\t': (*os) << "\\t"; break;
case '\r': (*os) << "\\r"; break;
default:
(*os) << "\\" << dec.width(3).fill('0').val(value);
break;
}
}
}
(*os) << (usesinglequote ? '\'' : '"');
}
void base64_encode(std::string_view str, std::ostream *oss) {
const char *encode_tab =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const char *s = str.data();
size_t size = str.size();
for (size_t i = 0; i < size; i += 3) {
uint32_t block = ((unsigned char)(s[i])) << 16;
if (i + 1 < size) block |= ((unsigned char)(s[i + 1])) << 8;
if (i + 2 < size) block |= ((unsigned char)(s[i + 2]));
(*oss) << encode_tab[(block>>18)&0x3F];
(*oss) << encode_tab[(block>>12)&0x3F];
(*oss) << ((i + 1 < size) ? encode_tab[(block>>6)&0x3F] : '=');
(*oss) << ((i + 2 < size) ? encode_tab[(block>>0)&0x3F] : '=');
}
}
bool base64_decode(std::string_view str, std::ostream *oss) {
uint32_t chunk = 0;
int fill = 0;
int skip = 0;
bool clean = true;
for (int i = 0; i < int(str.size()); i++) {
char c = str[i];
uint32_t value;
if ((c >= 'A') && (c <= 'Z')) value = c - 'A';
else if ((c >= 'a') && (c <= 'z')) value = c - 'a' + 26;
else if ((c >= '0') && (c <= '9')) value = c - '0' + 52;
else if (c == '+') value = 62;
else if (c == '/') value = 63;
else if (c == '=') { value = 0; skip ++; }
else { clean=false; continue; }
chunk = (chunk << 6) | value;
fill ++;
if (fill == 4) {
oss->put((chunk>>16) & 0xFF);
if (skip < 2) oss->put((chunk>>8) & 0xFF);
if (skip < 1) oss->put(chunk & 0xFF);
chunk = 0; fill = 0; skip = 0;
}
}
if (fill != 0) clean = false;
return clean;
}
IdVector id_vector_create(int64_t id1, int64_t id2, int64_t id3, int64_t id4) {
IdVector result;
if (id1 >= 0) result.push_back(id1);
if (id2 >= 0) result.push_back(id2);
if (id3 >= 0) result.push_back(id3);
if (id4 >= 0) result.push_back(id4);
return result;
}
void print_id_vector(const IdVector &idv, std::ostream *os) {
bool first = true;
for (int64_t id : idv) {
if (!first) (*os) << ",";
(*os) << id;
first = false;
}
}
eng::string id_vector_debug_string(const IdVector &idv) {
eng::ostringstream oss;
print_id_vector(idv, &oss);
return oss.str();
}
IdVector sort_union_id_vectors(const IdVector &v1, const IdVector &v2) {
IdVector result(v1.size() + v2.size());
int next = 0;
for (int64_t id : v1) result[next++] = id;
for (int64_t id : v2) result[next++] = id;
std::sort(result.begin(), result.end());
int64_t prev = -1;
int64_t count = 0;
for (int64_t id : result) {
if (id != prev) {
prev = id;
result[count++] = id;
}
}
result.resize(count);
return result;
}
HashValue hash_string(const eng::string &s) {
uint64_t hash1 = 0;
uint64_t hash2 = 0;
SpookyHash::ChainHash128(s.c_str(), s.size(), &hash1, &hash2);
return util::HashValue(hash1, hash2);
}
HashValue hash_id_vector(const IdVector &idv) {
uint64_t hash1 = 0;
uint64_t hash2 = 0;
SpookyHash::ChainHash128(&idv[0], idv.size() * sizeof(int64_t), &hash1, &hash2);
return util::HashValue(hash1, hash2);
}
eng::string hash_to_hex(const HashValue &hv) {
eng::ostringstream oss;
oss << hex64.val(hv.first) << hex64.val(hv.second);
return oss.str();
}
static inline uint64_t Rot64(uint64_t x, int k)
{
return (x << k) | (x >> (64 - k));
}
uint64_t hash_ints(uint64_t a, uint64_t b, uint64_t c, uint64_t d) {
uint64_t h0 = c ^ 0xc548cebf3714dbb9;
uint64_t h1 = d ^ 0xd23a7edd44383f8d;
uint64_t h2 = a ^ 0x7356f92e4b154df7;
uint64_t h3 = b ^ 0x55ce09295766838d;
h3 ^= h2; h2 = Rot64(h2,15); h3 += h2;
h0 ^= h3; h3 = Rot64(h3,52); h0 += h3;
h1 ^= h0; h0 = Rot64(h0,26); h1 += h0;
h2 ^= h1; h1 = Rot64(h1,51); h2 += h1;
h3 ^= h2; h2 = Rot64(h2,28); h3 += h2;
h0 ^= h3; h3 = Rot64(h3,9); h0 += h3;
h1 ^= h0; h0 = Rot64(h0,47); h1 += h0;
h2 ^= h1; h1 = Rot64(h1,54); h2 += h1;
h3 ^= h2; h2 = Rot64(h2,32); h3 += h2;
h0 ^= h3; h3 = Rot64(h3,25); h0 += h3;
h1 ^= h0; h0 = Rot64(h0,63); h1 += h0;
return h1;
}
double hash_to_double(uint64_t hash) {
return (hash >> (64-53)) * 0x1p-53;
}
StringVec split(const eng::string &s, char sep) {
StringVec result;
int start = 0;
for (int i = 0; i < int(s.size()); i++) {
if (s[i] == sep) {
result.push_back(s.substr(start, i-start));
start = i+1;
}
}
if (start < int(s.size())) {
result.push_back(s.substr(start));
}
return result;
}
static eng::string substr_nocr(const eng::string &s, int start, int len) {
if ((len > 0) && (s[start + len - 1] == '\r')) {
len -= 1;
}
return s.substr(start, len);
}
StringVec split_lines(const eng::string &s) {
StringVec result;
int start = 0;
for (int i = 0; i < int(s.size()); i++) {
if (s[i]=='\n') {
result.push_back(substr_nocr(s, start, i-start));
start = i + 1;
}
}
if (start < int(s.size())) {
result.push_back(substr_nocr(s, start, s.size()-start));
}
return result;
}
StringVec split_docstring(const eng::string &s) {
StringVec result;
int start = 0;
for (int i = 0; i < int(s.size()); i++) {
if (s[i]=='|') {
int len = i-start;
if ((len > 0)||(start > 0)) {
result.push_back(s.substr(start, i-start));
}
start = i + 1;
}
}
if (start < int(s.size())) {
result.push_back(s.substr(start, s.size()-start));
}
return result;
}
eng::string join(const StringVec &strs, const eng::string &sep) {
if (strs.empty()) return "";
eng::ostringstream oss;
oss << strs[0];
for (int i = 1; i < int(strs.size()); i++) {
oss << sep << strs[i];
}
return oss.str();
}
eng::string repeat_string(const eng::string &a, int n) {
int len = a.size();
eng::string result(len * n, ' ');
for (int i = 0; i < n; i++) {
for (int j = 0; j < len; j++) {
result[i*len + j] = a[j];
}
}
return result;
}
eng::string tolower(eng::string input) {
for (int i = 0; i < int(input.size()); i++) {
input[i] = std::tolower(input[i]);
}
return input;
}
eng::string toupper(eng::string input) {
for (int i = 0; i < int(input.size()); i++) {
input[i] = std::toupper(input[i]);
}
return input;
}
static void buffer_codepoint_utf8(int32_t scp, char *buffer) {
uint32_t cp = (uint32_t)scp;
unsigned char *c = (unsigned char *)buffer;
if (cp <= 0x7F) {
c[0] = cp;
c[1] = 0;
}
else if (cp <= 0x7FF) {
c[0] = (cp>>6)+192;
c[1] = (cp&63)+128;
c[2] = 0;
}
else if (cp <= 0xFFFF) {
if (0xd800 <= cp && cp <= 0xdfff) {
c[0] = 0;
} else {
c[0] = (cp>>12)+224;
c[1] = ((cp>>6)&63)+128;
c[2] = (cp&63)+128;
c[3] = 0;
}
}
else if (cp <= 0x10FFFF) {
c[0] = (cp>>18)+240;
c[1] = ((cp>>12)&63)+128;
c[2] = ((cp>>6)&63)+128;
c[3] = (cp&63)+128;
c[4] = 0;
} else {
c[0] = 0;
}
}
eng::string get_codepoint_utf8(uint32_t cp) {
char buffer[5];
buffer_codepoint_utf8(cp, buffer);
return eng::string(buffer);
}
bool write_codepoint_utf8(int32_t cp, std::ostream *s) {
char buffer[5];
buffer_codepoint_utf8(cp, buffer);
(*s) << buffer;
return buffer[0] != 0;
}
double distance_squared(double x1, double y1, double x2, double y2) {
double dx = x1 - x2;
double dy = y1 - y2;
return dx*dx + dy*dy;
}
LuaSourcePtr make_lua_source(const eng::string &code) {
LuaSourcePtr result(new LuaSourceVec);
eng::string fn = "file.lua";
result->push_back(std::make_pair(fn, code));
return result;
}
eng::string XYZ::debug_string() const {
eng::ostringstream oss;
oss << "(" << x << "," << y << "," << z << ")";
return oss.str();
}
} // namespace util
static std::string_view read_number_x(const char *p, bool plus, bool minus, bool dec, bool exp) {
std::string_view source = p;
return sv::read_number(source, plus, minus, dec, exp);
}
LuaDefine(unittests_util, "", "some unit tests") {
// test str_to_int64, str_to_double
LuaAssert(L, sv::to_int64("123") == 123);
LuaAssert(L, sv::to_int64("123.4") == INT64_MAX);
LuaAssert(L, sv::to_int64("12ab") == INT64_MAX);
LuaAssert(L, sv::to_int64("") == INT64_MAX);
LuaAssert(L, sv::to_double("123.5") == 123.5);
LuaAssert(L, std::isnan(sv::to_double("12ab")));
LuaAssert(L, std::isnan(sv::to_double("")));
// Test trim, ltrim, rtrim
LuaAssert(L, sv::ltrim(" foo ") == "foo ");
LuaAssert(L, sv::rtrim(" foo ") == " foo");
LuaAssert(L, sv::trim(" foo ") == "foo");
LuaAssert(L, sv::trim("foo") == "foo");
LuaAssert(L, sv::trim("") == "");
LuaAssert(L, sv::ltrim("**foo**", '*') == "foo**");
LuaAssert(L, sv::rtrim("**foo**", '*') == "**foo");
LuaAssert(L, sv::trim("**foo**", '*') == "foo");
LuaAssert(L, sv::trim("foo", '*') == "foo");
LuaAssert(L, sv::trim("", '*') == "");
// Test read_to_line
std::string_view v = "foo\nbar\r\nbaz";
std::string_view v1 = sv::read_to_line(v);
LuaAssertStrEq(L, v1, "foo");
LuaAssertStrEq(L, v, "bar\r\nbaz");
std::string_view v2 = sv::read_to_line(v);
LuaAssertStrEq(L, v2, "bar");
LuaAssertStrEq(L, v, "baz");
std::string_view v3 = sv::read_to_line(v);
LuaAssertStrEq(L, v3, "baz");
LuaAssert(L, sv::isnull(v));
// Test read_to_space
v = "foo bar baz";
std::string_view s1 = sv::read_to_space(v);
LuaAssertStrEq(L, s1, "foo");
LuaAssertStrEq(L, v, "bar baz");
std::string_view s2 = sv::read_to_space(v);
LuaAssertStrEq(L, s2, "bar");
LuaAssertStrEq(L, v, "baz");
std::string_view s3 = sv::read_to_space(v);
LuaAssertStrEq(L, s3, "baz");
LuaAssert(L, sv::isnull(v));
// Test the unioning of ID vectors.
util::IdVector idv1,idv2;
idv1.push_back(1);
idv1.push_back(6);
idv1.push_back(4);
idv2.push_back(5);
idv2.push_back(1);
idv2.push_back(6);
util::IdVector joined = util::sort_union_id_vectors(idv1, idv2);
LuaAssert(L, joined.size() == 4);
LuaAssert(L, joined[0] == 1);
LuaAssert(L, joined[1] == 4);
LuaAssert(L, joined[2] == 5);
LuaAssert(L, joined[3] == 6);
// Test the string split routine.
util::StringVec sv1 = util::split("foo,bar,baz", ',');
LuaAssert(L, sv1.size() == 3);
LuaAssert(L, sv1[0] == "foo");
LuaAssert(L, sv1[1] == "bar");
LuaAssert(L, sv1[2] == "baz");
util::StringVec sv2 = util::split(",foo,,bar", ',');
LuaAssert(L, sv2.size() == 4);
LuaAssert(L, sv2[0]=="");
LuaAssert(L, sv2[1]=="foo");
LuaAssert(L, sv2[2]=="");
LuaAssert(L, sv2[3]=="bar");
// Test the split_lines routine.
util::StringVec sv3 = util::split_lines("foo\n\nbar\r\nbaz\r\n\r\n");
LuaAssert(L, sv3.size() == 5);
LuaAssert(L, sv3[0] == "foo");
LuaAssert(L, sv3[1] == "");
LuaAssert(L, sv3[2] == "bar");
LuaAssert(L, sv3[3] == "baz");
LuaAssert(L, sv3[4] == "");
// Test the repeat string routine.
LuaAssertStrEq(L, util::repeat_string("abc", 3), "abcabcabc");
// test toupper and tolower
LuaAssert(L, util::toupper("fooBar") == "FOOBAR");
LuaAssert(L, util::tolower("fooBar") == "foobar");
// Test distance_squared
LuaAssert(L, util::distance_squared(1, 1, 5, 4) == 25.0);
LuaAssert(L, util::distance_squared(5, 4, 1, 1) == 25.0);
// Test XYZ.
util::XYZ xyza(3,4,5), xyzb(3,4,5), xyzc(3,4,6);
LuaAssert(L, xyza.x == 3);
LuaAssert(L, xyza.y == 4);
LuaAssert(L, xyza.z == 5);
LuaAssert(L, xyza == xyzb);
LuaAssert(L, xyza != xyzc);
LuaAssert(L, xyza.debug_string() == "(3,4,5)");
// Test hash_to_string
LuaAssertStrEq(L, util::hash_to_hex(util::HashValue(0x1234,0x789a)),
"0000000000001234000000000000789a");
// Test hash_to_double
LuaAssert(L, util::hash_to_double(0x1000000000000000) == 1.0/16.0);
LuaAssert(L, util::hash_to_double(0x7000000000000000) == 7.0/16.0);
LuaAssert(L, util::hash_to_double(0xF000000000000000) == 15.0/16.0);
// Test read_number allowing everything.
LuaAssert(L, read_number_x("123x", true, true, true, true) == "123");
LuaAssert(L, read_number_x("123.3x", true, true, true, true) == "123.3");
LuaAssert(L, read_number_x("123.x", true, true, true, true) == "123.");
LuaAssert(L, read_number_x("123..x", true, true, true, true) == "123.");
LuaAssert(L, read_number_x("-123x", true, true, true, true) == "-123");
LuaAssert(L, read_number_x("+123x", true, true, true, true) == "+123");
LuaAssert(L, read_number_x("+-123x", true, true, true, true) == "");
LuaAssert(L, read_number_x("-123.02e05x", true, true, true, true) == "-123.02e05");
LuaAssert(L, read_number_x("-123e-5x", true, true, true, true) == "-123e-5");
LuaAssert(L, read_number_x("-123e+5x", true, true, true, true) == "-123e+5");
LuaAssert(L, read_number_x("-123e+x", true, true, true, true) == "");
return 0;
}