2022-07-30 12:24:03 -04:00
|
|
|
#pragma once
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
#include "Result.hpp"
|
2022-07-30 12:24:03 -04:00
|
|
|
|
|
|
|
#include <Geode/DefaultInclude.hpp>
|
2022-10-30 14:59:20 -04:00
|
|
|
#include <chrono>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
2022-07-30 12:24:03 -04:00
|
|
|
|
2022-10-08 05:47:47 -04:00
|
|
|
#undef snprintf
|
|
|
|
|
2022-07-30 12:24:03 -04:00
|
|
|
namespace geode::utils {
|
2022-10-30 14:59:20 -04:00
|
|
|
constexpr unsigned int hash(char const* str, int h = 0) {
|
|
|
|
return !str[h] ? 5381 : (hash(str, h + 1) * 33) ^ str[h];
|
2022-07-30 12:24:03 -04:00
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
|
|
|
constexpr unsigned int hash(wchar_t const* str, int h = 0) {
|
|
|
|
return !str[h] ? 5381 : (hash(str, h + 1) * 33) ^ str[h];
|
2022-07-30 12:24:03 -04:00
|
|
|
}
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
constexpr size_t operator"" _h(char const* txt, size_t) {
|
2022-07-30 12:24:03 -04:00
|
|
|
return geode::utils::hash(txt);
|
|
|
|
}
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
constexpr size_t operator"" _h(wchar_t const* txt, size_t) {
|
2022-07-30 12:24:03 -04:00
|
|
|
return geode::utils::hash(txt);
|
|
|
|
}
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
template <typename T>
|
2022-07-30 12:24:03 -04:00
|
|
|
constexpr const T& clamp(const T& value, const T& minValue, const T& maxValue) {
|
|
|
|
return value < minValue ? minValue : maxValue < value ? maxValue : value;
|
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2022-07-30 12:24:03 -04:00
|
|
|
// from https://stackoverflow.com/questions/2342162/stdstring-formatting-like-sprintf
|
2022-10-30 14:59:20 -04:00
|
|
|
template <typename... Args>
|
|
|
|
std::string strfmt(std::string const& format, Args... args) {
|
|
|
|
int size_s = std::snprintf(nullptr, 0, format.c_str(), args...) + 1; // Extra space for '\0'
|
|
|
|
if (size_s <= 0) {
|
|
|
|
throw std::runtime_error("Error during formatting.");
|
|
|
|
}
|
|
|
|
auto size = static_cast<size_t>(size_s);
|
|
|
|
auto buf = std::make_unique<char[]>(size);
|
|
|
|
std::snprintf(buf.get(), size, format.c_str(), args...);
|
|
|
|
return std::string(buf.get(), buf.get() + size - 1);
|
2022-07-30 12:24:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Format a string
|
|
|
|
* @returns Pointer to char array. MAKE SURE TO CALL DELETE[]!
|
|
|
|
*/
|
2022-10-30 14:59:20 -04:00
|
|
|
template <typename... Args>
|
|
|
|
char const* cstrfmt(char const* fmt, Args... args) {
|
2022-07-30 12:24:03 -04:00
|
|
|
auto str = strfmt(fmt, args...);
|
|
|
|
char* ptr = new char[str.size() + 1];
|
|
|
|
strcpy_s(ptr, str.c_str());
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
template <typename T>
|
|
|
|
std::string intToHex(T i) {
|
|
|
|
std::stringstream stream;
|
|
|
|
stream << std::showbase << std::setbase(16) << (uint64_t)i;
|
|
|
|
return stream.str();
|
|
|
|
}
|
2022-07-30 12:24:03 -04:00
|
|
|
|
2022-09-21 07:50:23 -04:00
|
|
|
/**
|
2022-10-30 14:59:20 -04:00
|
|
|
* Turn a number into a string, with support for specifying precision
|
2022-09-21 07:50:23 -04:00
|
|
|
* (unlike std::to_string).
|
|
|
|
* @param num Number to convert to string
|
|
|
|
* @param precision Precision of the converted number
|
|
|
|
* @returns Number as string
|
|
|
|
*/
|
2022-10-30 14:59:20 -04:00
|
|
|
template <class Num>
|
2022-09-21 07:50:23 -04:00
|
|
|
std::string numToString(Num num, size_t precision = 0) {
|
|
|
|
std::stringstream ss;
|
|
|
|
if (precision) {
|
|
|
|
ss << std::fixed << std::setprecision(precision);
|
|
|
|
}
|
|
|
|
ss << num;
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
GEODE_DLL std::string timePointAsString(std::chrono::system_clock::time_point const& tp);
|
2022-07-30 12:24:03 -04:00
|
|
|
}
|