2020-12-27 14:12:53 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <hex.hpp>
|
|
|
|
|
2021-08-29 22:15:18 +02:00
|
|
|
#include <hex/helpers/concepts.hpp>
|
2022-03-04 11:36:37 +01:00
|
|
|
#include <hex/helpers/fs.hpp>
|
2022-03-22 09:08:34 +01:00
|
|
|
#include <hex/helpers/intrinsics.hpp>
|
2021-08-29 22:15:18 +02:00
|
|
|
|
2020-12-27 14:12:53 +01:00
|
|
|
#include <array>
|
2021-10-09 23:07:58 +02:00
|
|
|
#include <bit>
|
2021-01-07 01:58:56 +01:00
|
|
|
#include <cstring>
|
2021-09-09 12:58:44 +02:00
|
|
|
#include <cctype>
|
2020-12-27 14:12:53 +01:00
|
|
|
#include <functional>
|
2021-09-16 22:23:51 +02:00
|
|
|
#include <limits>
|
2020-12-27 14:12:53 +01:00
|
|
|
#include <memory>
|
|
|
|
#include <optional>
|
|
|
|
#include <string>
|
2021-01-02 20:49:55 +01:00
|
|
|
#include <type_traits>
|
2021-12-10 11:55:27 +01:00
|
|
|
#include <variant>
|
2020-12-27 14:12:53 +01:00
|
|
|
#include <vector>
|
|
|
|
|
2022-01-11 23:48:18 +01:00
|
|
|
struct ImVec2;
|
|
|
|
|
2020-12-27 14:12:53 +01:00
|
|
|
namespace hex {
|
|
|
|
|
2023-03-10 16:06:18 +01:00
|
|
|
template<typename T>
|
2023-05-28 14:30:33 +02:00
|
|
|
[[nodiscard]] std::vector<T> sampleData(const std::vector<T> &data, size_t count) {
|
2023-03-10 16:06:18 +01:00
|
|
|
size_t stride = std::max(1.0, double(data.size()) / count);
|
|
|
|
|
|
|
|
std::vector<T> result;
|
|
|
|
result.reserve(count);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < data.size(); i += stride) {
|
|
|
|
result.push_back(data[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-05-28 14:30:33 +02:00
|
|
|
[[nodiscard]] float operator""_scaled(long double value);
|
|
|
|
[[nodiscard]] float operator""_scaled(unsigned long long value);
|
|
|
|
[[nodiscard]] ImVec2 scaled(const ImVec2 &vector);
|
2022-01-11 23:48:18 +01:00
|
|
|
|
2022-08-18 00:30:02 +02:00
|
|
|
template<typename T>
|
2023-05-28 14:30:33 +02:00
|
|
|
[[nodiscard]] std::vector<T> operator|(const std::vector<T> &lhs, const std::vector<T> &rhs) {
|
2022-08-18 00:30:02 +02:00
|
|
|
std::vector<T> result;
|
|
|
|
|
|
|
|
std::copy(lhs.begin(), lhs.end(), std::back_inserter(result));
|
|
|
|
std::copy(rhs.begin(), rhs.end(), std::back_inserter(result));
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-05-28 14:30:33 +02:00
|
|
|
[[nodiscard]] std::string to_string(u128 value);
|
|
|
|
[[nodiscard]] std::string to_string(i128 value);
|
2021-01-21 14:53:44 +01:00
|
|
|
|
2023-05-28 14:30:33 +02:00
|
|
|
[[nodiscard]] std::vector<u8> parseHexString(std::string string);
|
|
|
|
[[nodiscard]] std::optional<u8> parseBinaryString(const std::string &string);
|
|
|
|
[[nodiscard]] std::string toByteString(u64 bytes);
|
|
|
|
[[nodiscard]] std::string makePrintable(u8 c);
|
2021-01-21 14:53:44 +01:00
|
|
|
|
2021-08-22 21:43:31 +02:00
|
|
|
void runCommand(const std::string &command);
|
|
|
|
void openWebpage(std::string url);
|
2021-01-21 14:53:44 +01:00
|
|
|
|
2023-05-28 14:30:33 +02:00
|
|
|
[[nodiscard]] std::string encodeByteString(const std::vector<u8> &bytes);
|
|
|
|
[[nodiscard]] std::vector<u8> decodeByteString(const std::string &string);
|
2022-02-17 14:43:04 +01:00
|
|
|
|
2022-06-25 12:19:59 +02:00
|
|
|
[[nodiscard]] constexpr inline u64 extract(u8 from, u8 to, const std::unsigned_integral auto &value) {
|
2022-01-29 17:14:15 +01:00
|
|
|
if (from < to) std::swap(from, to);
|
|
|
|
|
2021-01-27 00:00:20 +01:00
|
|
|
using ValueType = std::remove_cvref_t<decltype(value)>;
|
2022-02-01 22:09:44 +01:00
|
|
|
ValueType mask = (std::numeric_limits<ValueType>::max() >> (((sizeof(value) * 8) - 1) - (from - to))) << to;
|
2021-01-27 00:00:20 +01:00
|
|
|
|
2020-12-27 14:12:53 +01:00
|
|
|
return (value & mask) >> to;
|
|
|
|
}
|
2021-04-14 09:03:41 +02:00
|
|
|
|
2021-09-12 14:28:13 +02:00
|
|
|
[[nodiscard]] inline u64 extract(u32 from, u32 to, const std::vector<u8> &bytes) {
|
|
|
|
u8 index = 0;
|
2022-01-24 20:53:17 +01:00
|
|
|
while (from > 32 && to > 32) {
|
2021-09-12 14:28:13 +02:00
|
|
|
from -= 8;
|
|
|
|
to -= 8;
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 value = 0;
|
|
|
|
std::memcpy(&value, &bytes[index], std::min(sizeof(value), bytes.size() - index));
|
|
|
|
u64 mask = (std::numeric_limits<u64>::max() >> (64 - (from + 1)));
|
|
|
|
|
|
|
|
return (value & mask) >> to;
|
|
|
|
}
|
|
|
|
|
2023-05-28 14:30:33 +02:00
|
|
|
[[nodiscard]] constexpr inline i128 signExtend(size_t numBits, i128 value) {
|
2022-08-15 21:08:09 +02:00
|
|
|
i128 mask = 1ULL << (numBits - 1);
|
2021-09-21 21:29:18 +02:00
|
|
|
return (value ^ mask) - mask;
|
|
|
|
}
|
|
|
|
|
2022-06-25 12:19:59 +02:00
|
|
|
template<std::integral T>
|
2023-05-28 14:30:33 +02:00
|
|
|
[[nodiscard]] constexpr inline T swapBitOrder(size_t numBits, T value) {
|
2022-05-29 14:57:59 +02:00
|
|
|
T result = 0x00;
|
|
|
|
|
|
|
|
for (size_t bit = 0; bit < numBits; bit++) {
|
|
|
|
result <<= 1;
|
|
|
|
result |= (value & (1 << bit)) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-05-28 14:30:33 +02:00
|
|
|
[[nodiscard]] constexpr inline size_t strnlen(const char *s, size_t n) {
|
2022-08-16 00:29:53 +02:00
|
|
|
size_t i = 0;
|
|
|
|
while (i < n && s[i] != '\x00') i++;
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2021-10-20 10:04:52 +02:00
|
|
|
template<size_t Size>
|
|
|
|
struct SizeTypeImpl { };
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
template<>
|
|
|
|
struct SizeTypeImpl<1> { using Type = u8; };
|
|
|
|
template<>
|
|
|
|
struct SizeTypeImpl<2> { using Type = u16; };
|
|
|
|
template<>
|
|
|
|
struct SizeTypeImpl<4> { using Type = u32; };
|
|
|
|
template<>
|
|
|
|
struct SizeTypeImpl<8> { using Type = u64; };
|
|
|
|
template<>
|
|
|
|
struct SizeTypeImpl<16> { using Type = u128; };
|
2021-10-20 10:04:52 +02:00
|
|
|
|
|
|
|
template<size_t Size>
|
|
|
|
using SizeType = typename SizeTypeImpl<Size>::Type;
|
|
|
|
|
2020-12-27 14:12:53 +01:00
|
|
|
template<typename T>
|
2023-05-28 14:30:33 +02:00
|
|
|
[[nodiscard]] constexpr T changeEndianess(const T &value, size_t size, std::endian endian) {
|
2020-12-27 14:12:53 +01:00
|
|
|
if (endian == std::endian::native)
|
|
|
|
return value;
|
|
|
|
|
2022-08-05 12:19:50 +02:00
|
|
|
size = std::min(size, sizeof(T));
|
|
|
|
|
|
|
|
std::array<uint8_t, sizeof(T)> data = { 0 };
|
|
|
|
std::memcpy(&data[0], &value, size);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < size / 2; i++) {
|
|
|
|
std::swap(data[i], data[size - 1 - i]);
|
2021-10-20 10:04:52 +02:00
|
|
|
}
|
|
|
|
|
2022-08-05 12:19:50 +02:00
|
|
|
T result = { };
|
|
|
|
std::memcpy(&result, &data[0], size);
|
2021-10-20 10:04:52 +02:00
|
|
|
|
|
|
|
return result;
|
2020-12-27 14:12:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2023-05-28 14:30:33 +02:00
|
|
|
[[nodiscard]] constexpr T changeEndianess(const T &value, std::endian endian) {
|
2022-08-05 12:19:50 +02:00
|
|
|
return changeEndianess(value, sizeof(value), endian);
|
|
|
|
}
|
2021-10-20 10:04:52 +02:00
|
|
|
|
2022-08-05 12:19:50 +02:00
|
|
|
[[nodiscard]] constexpr u128 bitmask(u8 bits) {
|
|
|
|
return u128(-1) >> (128 - bits);
|
2020-12-27 14:12:53 +01:00
|
|
|
}
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
template<class T>
|
2023-05-28 14:30:33 +02:00
|
|
|
[[nodiscard]] constexpr T bit_width(T x) noexcept {
|
2020-12-27 14:12:53 +01:00
|
|
|
return std::numeric_limits<T>::digits - std::countl_zero(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2023-05-28 14:30:33 +02:00
|
|
|
[[nodiscard]] constexpr T bit_ceil(T x) noexcept {
|
2020-12-27 14:12:53 +01:00
|
|
|
if (x <= 1u)
|
|
|
|
return T(1);
|
|
|
|
|
|
|
|
return T(1) << bit_width(T(x - 1));
|
|
|
|
}
|
|
|
|
|
2022-06-25 12:19:59 +02:00
|
|
|
template<std::integral T, std::integral U>
|
2023-05-28 14:30:33 +02:00
|
|
|
[[nodiscard]] auto powi(T base, U exp) {
|
2022-05-27 20:42:07 +02:00
|
|
|
using ResultType = decltype(T{} * U{});
|
|
|
|
|
|
|
|
if (exp < 0)
|
|
|
|
return ResultType(0);
|
|
|
|
|
|
|
|
ResultType result = 1;
|
|
|
|
|
|
|
|
while (exp != 0) {
|
|
|
|
if ((exp & 0b1) == 0b1)
|
|
|
|
result *= base;
|
|
|
|
exp >>= 1;
|
|
|
|
base *= base;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-02-27 23:25:39 +01:00
|
|
|
template<typename T, typename... Args>
|
|
|
|
void moveToVector(std::vector<T> &buffer, T &&first, Args &&...rest) {
|
|
|
|
buffer.push_back(std::move(first));
|
|
|
|
|
|
|
|
if constexpr (sizeof...(rest) > 0)
|
|
|
|
moveToVector(buffer, std::move(rest)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename... Args>
|
2023-05-28 14:30:33 +02:00
|
|
|
[[nodiscard]] std::vector<T> moveToVector(T &&first, Args &&...rest) {
|
2022-02-27 23:25:39 +01:00
|
|
|
std::vector<T> result;
|
|
|
|
moveToVector(result, T(std::move(first)), std::move(rest)...);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-05-28 14:30:33 +02:00
|
|
|
[[nodiscard]] std::vector<std::string> splitString(const std::string &string, const std::string &delimiter);
|
|
|
|
[[nodiscard]] std::string combineStrings(const std::vector<std::string> &strings, const std::string &delimiter = "");
|
|
|
|
[[nodiscard]] std::string replaceStrings(std::string string, const std::string &search, const std::string &replace);
|
2021-01-20 22:54:46 +01:00
|
|
|
|
2023-05-28 14:30:33 +02:00
|
|
|
[[nodiscard]] std::string toEngineeringString(double value);
|
2021-01-11 00:27:00 +01:00
|
|
|
|
2023-05-28 14:30:33 +02:00
|
|
|
[[nodiscard]] inline std::vector<u8> parseByteString(const std::string &string) {
|
2021-02-14 01:11:55 +01:00
|
|
|
auto byteString = std::string(string);
|
|
|
|
byteString.erase(std::remove(byteString.begin(), byteString.end(), ' '), byteString.end());
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
if ((byteString.length() % 2) != 0) return {};
|
2021-02-14 01:11:55 +01:00
|
|
|
|
|
|
|
std::vector<u8> result;
|
|
|
|
for (u32 i = 0; i < byteString.length(); i += 2) {
|
|
|
|
if (!std::isxdigit(byteString[i]) || !std::isxdigit(byteString[i + 1]))
|
2022-01-24 20:53:17 +01:00
|
|
|
return {};
|
2021-02-14 01:11:55 +01:00
|
|
|
|
|
|
|
result.push_back(std::strtoul(byteString.substr(i, 2).c_str(), nullptr, 16));
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-05-28 14:30:33 +02:00
|
|
|
[[nodiscard]] inline std::string toBinaryString(std::unsigned_integral auto number) {
|
2021-02-07 22:57:34 +01:00
|
|
|
if (number == 0) return "0";
|
|
|
|
|
|
|
|
std::string result;
|
2022-01-22 22:37:52 +01:00
|
|
|
for (i16 bit = hex::bit_width(number) - 1; bit >= 0; bit--)
|
2021-02-07 22:57:34 +01:00
|
|
|
result += (number & (0b1 << bit)) == 0 ? '0' : '1';
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-05-28 14:30:33 +02:00
|
|
|
[[nodiscard]] float float16ToFloat32(u16 float16);
|
2021-09-01 00:23:45 +02:00
|
|
|
|
2023-05-28 14:30:33 +02:00
|
|
|
[[nodiscard]] inline bool equalsIgnoreCase(const std::string &left, const std::string &right) {
|
2021-09-09 12:58:44 +02:00
|
|
|
return std::equal(left.begin(), left.end(), right.begin(), right.end(), [](char a, char b) {
|
|
|
|
return tolower(a) == tolower(b);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-05-28 14:30:33 +02:00
|
|
|
[[nodiscard]] inline bool containsIgnoreCase(const std::string &a, const std::string &b) {
|
2022-01-24 20:53:17 +01:00
|
|
|
auto iter = std::search(a.begin(), a.end(), b.begin(), b.end(), [](char ch1, char ch2) {
|
2021-09-09 12:58:44 +02:00
|
|
|
return std::toupper(ch1) == std::toupper(ch2);
|
|
|
|
});
|
|
|
|
|
|
|
|
return iter != a.end();
|
2022-10-04 09:10:58 +02:00
|
|
|
}
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
template<typename T, typename... VariantTypes>
|
2023-05-28 14:30:33 +02:00
|
|
|
[[nodiscard]] T get_or(const std::variant<VariantTypes...> &variant, T alt) {
|
2021-12-10 11:55:27 +01:00
|
|
|
const T *value = std::get_if<T>(&variant);
|
|
|
|
if (value == nullptr)
|
|
|
|
return alt;
|
|
|
|
else
|
|
|
|
return *value;
|
|
|
|
}
|
|
|
|
|
2022-06-25 12:19:59 +02:00
|
|
|
template<std::integral T>
|
2023-05-28 14:30:33 +02:00
|
|
|
[[nodiscard]] T alignTo(T value, T alignment) {
|
2022-05-27 20:42:07 +02:00
|
|
|
T remainder = value % alignment;
|
|
|
|
|
|
|
|
return remainder != 0 ? value + (alignment - remainder) : value;
|
|
|
|
}
|
|
|
|
|
2023-05-28 14:30:33 +02:00
|
|
|
[[nodiscard]] std::optional<u8> hexCharToValue(char c);
|
2022-08-03 10:19:34 +02:00
|
|
|
|
2023-05-28 14:30:33 +02:00
|
|
|
[[nodiscard]] bool isProcessElevated();
|
2021-12-13 22:58:23 +01:00
|
|
|
|
2023-05-28 14:30:33 +02:00
|
|
|
[[nodiscard]] std::optional<std::string> getEnvironmentVariable(const std::string &env);
|
2022-03-22 08:20:14 +01:00
|
|
|
|
2023-05-28 14:30:33 +02:00
|
|
|
[[nodiscard]] inline std::string limitStringLength(const std::string &string, size_t maxLength) {
|
2022-10-07 16:27:29 +02:00
|
|
|
if (string.length() <= maxLength)
|
|
|
|
return string;
|
|
|
|
|
|
|
|
return string.substr(0, maxLength - 3) + "...";
|
|
|
|
}
|
|
|
|
|
2023-05-28 14:30:33 +02:00
|
|
|
[[nodiscard]] std::optional<std::fs::path> getInitialFilePath();
|
2023-05-15 18:07:49 +02:00
|
|
|
|
2021-07-19 01:11:28 +02:00
|
|
|
}
|