167 lines
5.0 KiB
C++
167 lines
5.0 KiB
C++
#ifndef UTIL_HPP
|
|
#define UTIL_HPP
|
|
|
|
#include "wrap-string.hpp"
|
|
#include "wrap-set.hpp"
|
|
#include "wrap-map.hpp"
|
|
#include "wrap-algorithm.hpp"
|
|
#include "wrap-sstream.hpp"
|
|
#include "wrap-ostream.hpp"
|
|
#include "wrap-memory.hpp"
|
|
#include "wrap-utility.hpp"
|
|
|
|
#include <string_view>
|
|
|
|
#include "luastack.hpp"
|
|
#include "spookyv2.hpp"
|
|
|
|
namespace util {
|
|
|
|
enum WorldType {
|
|
WORLD_TYPE_STANDALONE,
|
|
WORLD_TYPE_C_SYNC,
|
|
WORLD_TYPE_S_SYNC,
|
|
WORLD_TYPE_MASTER,
|
|
};
|
|
|
|
enum MessageType {
|
|
MSG_NULL,
|
|
MSG_DIFF,
|
|
MSG_ACK,
|
|
MSG_INVOKE,
|
|
};
|
|
|
|
using StringVec = eng::vector<eng::string>;
|
|
using StringPair = eng::pair<eng::string, eng::string>;
|
|
using StringSet = eng::set<eng::string>;
|
|
using LuaSourceVec = eng::vector<StringPair>;
|
|
using LuaSourcePtr = eng::unique_ptr<LuaSourceVec>;
|
|
using HashValue = eng::pair<uint64_t, uint64_t>;
|
|
using IdVector = eng::vector<int64_t>;
|
|
|
|
// Return seconds elapsed, for profiling purposes.
|
|
double profiling_clock();
|
|
|
|
// Return true if the string is a valid lua identifier.
|
|
bool is_identifier(const eng::string &str);
|
|
|
|
// Output a string to a stream using Lua string escaping and quoting.
|
|
void quote_string(const eng::string &str, eng::ostream *os);
|
|
|
|
// ID vector quick create.
|
|
IdVector id_vector_create(int64_t id1=-1, int64_t id2=-1, int64_t id3=-1, int64_t id4=-1);
|
|
|
|
// ID vector debug string.
|
|
eng::string id_vector_debug_string(const IdVector &idv);
|
|
|
|
// Unions and sorts two ID vectors.
|
|
IdVector sort_union_id_vectors(const IdVector &v1, const IdVector &v2);
|
|
|
|
// Get a 64-bit hashvalue for a string.
|
|
HashValue hash_string(const eng::string &str);
|
|
|
|
// Get a 64-bit hashvalue for an ID vector.
|
|
HashValue hash_id_vector(const IdVector &idv);
|
|
|
|
// Convert a hash to a hexadecimal string.
|
|
eng::string hash_to_hex(const HashValue &hash);
|
|
|
|
// Split a string into multiple strings
|
|
StringVec split(const eng::string &s, char sep);
|
|
|
|
// Split a string into multiple strings using \r or \n
|
|
StringVec split_lines(const eng::string &s);
|
|
|
|
// Split a string into multiple lines using |, remove any leading blank line.
|
|
StringVec split_docstring(const eng::string &s);
|
|
|
|
// Join multiple strings into one string
|
|
eng::string join(const StringVec &strs, eng::string sep);
|
|
|
|
// Return N repetitions of string A
|
|
eng::string repeat_string(const eng::string &a, int n);
|
|
|
|
// Return the length of the common prefix of A and B.
|
|
int common_prefix_length(const eng::string &a, const eng::string &b);
|
|
|
|
// String to lowercase/uppercase. Ascii only, no unicode.
|
|
eng::string tolower(eng::string input);
|
|
eng::string toupper(eng::string input);
|
|
|
|
// Return true if the string has the specified prefix or suffix.
|
|
bool has_prefix(const eng::string &s, const eng::string &prefix);
|
|
bool has_suffix(const eng::string &s, const eng::string &suffix);
|
|
|
|
// Return true if the string can be parsed as an integer.
|
|
bool validinteger(const eng::string &value);
|
|
|
|
// String to integer. Returns errval if the number is not parseable.
|
|
int64_t strtoint(const eng::string &value, int64_t errval);
|
|
|
|
// String to double. Returns NAN if the number is not parseable.
|
|
double strtodouble(const eng::string &value);
|
|
|
|
// Trim a string_view
|
|
std::string_view sv_ltrim(std::string_view v);
|
|
std::string_view sv_rtrim(std::string_view v);
|
|
std::string_view sv_trim(std::string_view v);
|
|
|
|
// Trim strings: left end, right end, both ends.
|
|
eng::string ltrim(std::string_view s);
|
|
eng::string rtrim(std::string_view s);
|
|
eng::string trim(std::string_view s);
|
|
|
|
// Read a line from a string_view
|
|
std::string_view sv_read_line(std::string_view &source);
|
|
|
|
// Calculate distance between two points
|
|
double distance_squared(double x1, double y1, double x2, double y2);
|
|
|
|
// Return true if a world type is authoritative.
|
|
bool world_type_authoritative(util::WorldType wt);
|
|
|
|
// Make a LuaSourceVec with one element, for unit testing.
|
|
LuaSourcePtr make_lua_source(const eng::string &code);
|
|
|
|
// Return true if the line of code is a lua comment.
|
|
bool is_lua_comment(const eng::string &line);
|
|
|
|
// Remove nullptrs from a vector of unique pointers.
|
|
template<class T>
|
|
void remove_nullptrs(eng::vector<eng::unique_ptr<T>> &vec) {
|
|
eng::unique_ptr<T> nullp;
|
|
auto iter = std::remove(vec.begin(), vec.end(), nullp);
|
|
vec.erase(iter, vec.end());
|
|
}
|
|
|
|
// An XYZ coordinate, general purpose.
|
|
struct XYZ {
|
|
float x, y, z;
|
|
XYZ() { x=0; y=0; z=0; }
|
|
XYZ(float ix, float iy, float iz) { x=ix; y=iy; z=iz; }
|
|
bool operator ==(const XYZ &o) const { return x==o.x && y == o.y && z==o.z; }
|
|
bool operator !=(const XYZ &o) const { return x!=o.x || y != o.y || z!=o.z; }
|
|
eng::string debug_string() const;
|
|
};
|
|
|
|
// These are formatting directives that can be sent to a eng::ostream.
|
|
class hex64 {};
|
|
class hex32 {};
|
|
class hex16 {};
|
|
class hex8 {};
|
|
|
|
class NullStreamBuffer : public std::streambuf
|
|
{
|
|
public:
|
|
int overflow(int c) { return c; }
|
|
};
|
|
|
|
} // namespace util
|
|
|
|
eng::ostream &operator<<(eng::ostream &oss, const util::hex64 &v);
|
|
eng::ostream &operator<<(eng::ostream &oss, const util::hex32 &v);
|
|
eng::ostream &operator<<(eng::ostream &oss, const util::hex16 &v);
|
|
eng::ostream &operator<<(eng::ostream &oss, const util::hex8 &v);
|
|
|
|
#endif // UTIL_HPP
|