Files
integration/luprex/core/cpp/util.hpp

161 lines
4.8 KiB
C++

#ifndef UTIL_HPP
#define UTIL_HPP
#include <string>
#include <set>
#include <map>
#include <algorithm>
#include <sstream>
#include <ostream>
#include <memory>
#include <tuple>
#include <utility>
#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 = std::vector<std::string>;
using StringPair = std::pair<std::string, std::string>;
using StringSet = std::set<std::string>;
using LuaSourceVec = std::vector<StringPair>;
using LuaSourcePtr = std::unique_ptr<LuaSourceVec>;
using HashValue = std::pair<uint64_t, uint64_t>;
using IdVector = std::vector<int64_t>;
// Return nanoseconds elapsed, for profiling purposes.
int64_t profiling_clock();
// Return true if the string is a valid lua identifier.
bool is_identifier(const std::string &str);
// Output a string to a stream using Lua string escaping and quoting.
void quote_string(const std::string &str, std::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.
std::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 std::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.
std::string hash_to_hex(const HashValue &hash);
// Split a string into multiple strings
StringVec split(const std::string &s, char sep);
// Split a string into multiple strings using \r or \n
StringVec split_lines(const std::string &s);
// Join multiple strings into one string
std::string join(const StringVec &strs, std::string sep);
// Return N repetitions of string A
std::string repeat_string(const std::string &a, int n);
// Return the length of the common prefix of A and B.
int common_prefix_length(const std::string &a, const std::string &b);
// String to lowercase/uppercase. Ascii only, no unicode.
std::string tolower(std::string input);
std::string toupper(std::string input);
// Return true if the string has the specified prefix or suffix.
bool has_prefix(const std::string &s, const std::string &prefix);
bool has_suffix(const std::string &s, const std::string &suffix);
// Return true if the string can be parsed as an integer.
bool validinteger(const std::string &value);
// String to integer. Returns errval if the number is not parseable.
int64_t strtoint(const std::string &value, int64_t errval);
// String to double. Returns NAN if the number is not parseable.
double strtodouble(const std::string &value);
// Split a string of the form "hostname:port" into a hostname and a port.
// On failure, returns empty strings.
void split_host_port(const std::string &target, std::string &host, std::string &port);
// Trim strings: left end, right end, both ends.
std::string ltrim(std::string s);
std::string rtrim(std::string s);
std::string trim(std::string s);
// 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 std::string &code);
// Return true if the line of code is a lua comment.
bool is_lua_comment(const std::string &line);
// Remove nullptrs from a vector of unique pointers.
template<class T>
void remove_nullptrs(std::vector<std::unique_ptr<T>> &vec) {
std::unique_ptr<T> nullp;
auto iter = std::remove(vec.begin(), vec.end(), nullp);
vec.erase(iter, vec.end());
}
// Read the lua source code from the specified directory.
LuaSourcePtr read_lua_source(const std::string &directory);
// 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; }
std::string debug_string() const;
};
// These are formatting directives that can be sent to a std::ostream.
class hex64 {};
class hex32 {};
class hex16 {};
class hex8 {};
class NullStreamBuffer : public std::streambuf
{
public:
int overflow(int c) { return c; }
};
} // namespace util
std::ostream &operator<<(std::ostream &oss, const util::hex64 &v);
std::ostream &operator<<(std::ostream &oss, const util::hex32 &v);
std::ostream &operator<<(std::ostream &oss, const util::hex16 &v);
std::ostream &operator<<(std::ostream &oss, const util::hex8 &v);
#endif // UTIL_HPP