#include "wrap-string.hpp" #include "wrap-vector.hpp" #include "fast-float.hpp" #include #include "util.hpp" #include #include #include #include #include #include #include #ifdef WIN32 #endif #ifndef WIN32 #include #include #endif namespace util { eng::string ascii_tolower(const eng::string &s) { eng::string mod = s; for (int i = 0; i < int(mod.size()); i++) { if (ascii_isupper(mod[i])) { mod[i] += 'a' - 'A'; } } return mod; } eng::string ascii_toupper(const eng::string &s) { eng::string mod = s; for (int i = 0; i < int(mod.size()); i++) { if (ascii_islower(mod[i])) { mod[i] += 'A' - 'a'; } } return mod; } bool is_identifier(const eng::string &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; } 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 { (*os) << c; } } else { switch (c) { case '\n': (*os) << "\\n"; break; case '\t': (*os) << "\\t"; break; case '\r': (*os) << "\\r"; break; default: (*os) << "\\" << std::setfill('0') << std::setw(3) << int(c); break; } } } (*os) << (usesinglequote ? '\'' : '"'); } 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; } eng::string id_vector_debug_string(const IdVector &idv) { eng::ostringstream oss; bool first = true; for (int64_t id : idv) { if (!first) oss << ","; oss << id; first = false; } 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 << std::hex << std::setw(16) << std::setfill('0') << hv.first; oss << std::hex << std::setw(16) << std::setfill('0') << 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; } int common_prefix_length(const eng::string &a, const eng::string &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; } 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; } bool has_prefix(const eng::string &s, const eng::string &prefix) { return 0 == s.compare(0, prefix.size(), prefix); } bool has_suffix(const eng::string &s, const eng::string &suffix) { if (s.length() >= suffix.length()) { return (0 == s.compare (s.length() - suffix.length(), suffix.length(), suffix)); } else { return false; } } bool valid_int64(std::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; } int64_t str_to_int64(std::string_view value, int64_t errval) { 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 errval; if (r.ptr != last) return errval; return result; } bool valid_double(std::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; } double str_to_double(std::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; } std::string_view sv_ltrim(std::string_view v) { const char *b = v.data(); const char *e = v.data() + v.size(); while ((e > b) && (std::isspace(b[0]))) { b++; } return std::string_view(b, e-b); } std::string_view sv_rtrim(std::string_view v) { const char *b = v.data(); const char *e = v.data() + v.size(); while ((e > b) && (std::isspace(e[-1]))) { e--; } return std::string_view(b, e-b); } std::string_view sv_trim(std::string_view v) { const char *b = v.data(); const char *e = v.data() + v.size(); while ((e > b) && (std::isspace(b[0]))) { b++; } while ((e > b) && (std::isspace(e[-1]))) { e--; } return std::string_view(b, e-b); } std::string_view sv_ltrim(std::string_view v, char c) { while ((!v.empty()) && (v.front() == c)) { v.remove_prefix(1); } return v; } std::string_view sv_rtrim(std::string_view v, char c) { while ((!v.empty()) && (v.back() == c)) { v.remove_suffix(1); } return v; } std::string_view sv_trim(std::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; } eng::string ltrim(std::string_view v) { return eng::string(sv_ltrim(v)); } eng::string rtrim(std::string_view v) { return eng::string(sv_rtrim(v)); } eng::string trim(std::string_view v) { return eng::string(sv_trim(v)); } std::string_view sv_split_one(std::string_view &v, char sep) { size_t pos = v.find(sep); // If there's no separator in the buffer, return a null view. if (pos == std::string_view::npos) { return std::string_view(); } // Split into stuff before the separator, and stuff after. std::string_view result = v.substr(0, pos); v = v.substr(pos + 1); return result; } std::string_view sv_read_line(std::string_view &source) { size_t pos = source.find('\n'); std::string_view result; if (pos == std::string_view::npos) { result = source; source = std::string_view(); } else { result = source.substr(0, pos); source = source.substr(pos + 1); } result = sv_rtrim(result, '\r'); return result; } double distance_squared(double x1, double y1, double x2, double y2) { double dx = x1 - x2; double dy = y1 - y2; return dx*dx + dy*dy; } bool world_type_authoritative(util::WorldType wt) { return (wt == WORLD_TYPE_MASTER) || (wt == WORLD_TYPE_STANDALONE); } 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; } bool is_lua_comment(const eng::string &s) { int start = 0; while ((start < int(s.size())) && ((s[start]==' ') || (s[start]=='\t'))) start++; return s.substr(start, 2) == "--"; } eng::string XYZ::debug_string() const { eng::ostringstream oss; oss << "(" << x << "," << y << "," << z << ")"; return oss.str(); } } // namespace util std::ostream &operator<<(std::ostream &oss, const util::hex64 &v) { oss << "0x" << std::setw(16) << std::setfill('0') << std::hex; return oss; } std::ostream &operator<<(std::ostream &oss, const util::hex32 &v) { oss << "0x" << std::setw(8) << std::setfill('0') << std::hex; return oss; } std::ostream &operator<<(std::ostream &oss, const util::hex16 &v) { oss << "0x" << std::setw(4) << std::setfill('0') << std::hex; return oss; } std::ostream &operator<<(std::ostream &oss, const util::hex8 &v) { oss << "0x" << std::setw(2) << std::setfill('0') << std::hex; return oss; } LuaDefine(unittests_util, "", "some unit tests") { // 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 str_to_int64, str_to_double LuaAssert(L, util::str_to_int64("123") == 123); LuaAssert(L, util::str_to_int64("123.4") == INT64_MIN); LuaAssert(L, util::str_to_int64("12ab") == INT64_MIN); LuaAssert(L, util::str_to_int64("") == INT64_MIN); LuaAssert(L, util::str_to_double("123.5") == 123.5); LuaAssert(L, std::isnan(util::str_to_double("12ab"))); LuaAssert(L, std::isnan(util::str_to_double(""))); // Test trim, ltrim, rtrim LuaAssert(L, util::ltrim(" foo ") == "foo "); LuaAssert(L, util::rtrim(" foo ") == " foo"); LuaAssert(L, util::trim(" foo ") == "foo"); LuaAssert(L, util::trim("foo") == "foo"); LuaAssert(L, util::trim("") == ""); // Test sv_read_line std::string_view v = "foo\nbar\r\n"; std::string_view v1 = util::sv_read_line(v); std::string_view v2 = util::sv_read_line(v); std::string_view v3 = util::sv_read_line(v); LuaAssertStrEq(L, v1, "foo"); LuaAssertStrEq(L, v2, "bar"); LuaAssertStrEq(L, v3, ""); // 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); return 0; }