2020-12-09 08:47:45 +01:00
|
|
|
#include "StrFmt.h"
|
2016-04-27 00:27:24 +02:00
|
|
|
#include "StrUtil.h"
|
2016-08-03 22:51:05 +02:00
|
|
|
#include "cfmt.h"
|
2020-12-13 14:34:45 +01:00
|
|
|
#include "util/endian.hpp"
|
2020-03-08 12:48:06 +01:00
|
|
|
#include "util/logs.hpp"
|
2020-12-13 14:34:45 +01:00
|
|
|
#include "util/v128.hpp"
|
2014-04-01 02:33:55 +02:00
|
|
|
|
2022-07-08 17:13:38 +02:00
|
|
|
#include <locale>
|
|
|
|
#include <codecvt>
|
2016-05-13 16:01:48 +02:00
|
|
|
#include <algorithm>
|
2018-09-11 18:02:19 +02:00
|
|
|
#include <string_view>
|
2020-03-08 12:48:06 +01:00
|
|
|
#include "Thread.h"
|
2016-04-25 12:49:12 +02:00
|
|
|
|
2016-08-14 02:22:19 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <Windows.h>
|
|
|
|
#else
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
|
|
|
|
2022-01-11 19:42:52 +01:00
|
|
|
std::string wchar_to_utf8(std::wstring_view src)
|
2020-03-26 21:48:56 +01:00
|
|
|
{
|
2022-05-07 14:55:33 +02:00
|
|
|
#ifdef _WIN32
|
2020-03-26 21:48:56 +01:00
|
|
|
std::string utf8_string;
|
2022-01-11 19:42:52 +01:00
|
|
|
const auto tmp_size = WideCharToMultiByte(CP_UTF8, 0, src.data(), src.size(), nullptr, 0, nullptr, nullptr);
|
2020-03-26 21:48:56 +01:00
|
|
|
utf8_string.resize(tmp_size);
|
2022-01-11 19:42:52 +01:00
|
|
|
WideCharToMultiByte(CP_UTF8, 0, src.data(), src.size(), utf8_string.data(), tmp_size, nullptr, nullptr);
|
2020-03-26 21:48:56 +01:00
|
|
|
return utf8_string;
|
2022-05-07 14:55:33 +02:00
|
|
|
#else
|
2022-07-08 17:13:38 +02:00
|
|
|
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter{};
|
2022-05-07 14:55:33 +02:00
|
|
|
return converter.to_bytes(src.data());
|
|
|
|
#endif
|
2020-03-26 21:48:56 +01:00
|
|
|
}
|
|
|
|
|
2022-07-08 17:13:38 +02:00
|
|
|
std::string utf16_to_utf8(std::u16string_view src)
|
|
|
|
{
|
|
|
|
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> converter{};
|
|
|
|
return converter.to_bytes(src.data());
|
|
|
|
}
|
|
|
|
|
2022-01-11 19:42:52 +01:00
|
|
|
std::wstring utf8_to_wchar(std::string_view src)
|
2020-03-26 21:48:56 +01:00
|
|
|
{
|
2022-05-07 14:55:33 +02:00
|
|
|
#ifdef _WIN32
|
2022-01-11 19:42:52 +01:00
|
|
|
std::wstring wchar_string;
|
|
|
|
const auto tmp_size = MultiByteToWideChar(CP_UTF8, 0, src.data(), src.size(), nullptr, 0);
|
|
|
|
wchar_string.resize(tmp_size);
|
|
|
|
MultiByteToWideChar(CP_UTF8, 0, src.data(), src.size(), wchar_string.data(), tmp_size);
|
|
|
|
return wchar_string;
|
2022-05-07 14:55:33 +02:00
|
|
|
#else
|
|
|
|
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> converter{};
|
|
|
|
return converter.from_bytes(src.data());
|
|
|
|
#endif
|
2020-03-26 21:48:56 +01:00
|
|
|
}
|
2022-03-16 19:59:20 +01:00
|
|
|
|
2022-05-07 14:55:33 +02:00
|
|
|
#ifdef _WIN32
|
2022-03-16 20:44:47 +01:00
|
|
|
std::string fmt::win_error_to_string(unsigned long error, void* module_handle)
|
2022-03-16 19:59:20 +01:00
|
|
|
{
|
|
|
|
std::string message;
|
|
|
|
LPWSTR message_buffer = nullptr;
|
2022-03-16 20:44:47 +01:00
|
|
|
if (FormatMessageW((module_handle ? FORMAT_MESSAGE_FROM_HMODULE : FORMAT_MESSAGE_FROM_SYSTEM) | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
module_handle, error, 0, (LPWSTR)&message_buffer, 0, nullptr))
|
2022-03-16 19:59:20 +01:00
|
|
|
{
|
|
|
|
message = fmt::format("%s (0x%x)", fmt::trim(wchar_to_utf8(message_buffer), " \t\n\r\f\v"), error);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
message = fmt::format("0x%x", error);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message_buffer)
|
|
|
|
{
|
|
|
|
LocalFree(message_buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
return message;
|
|
|
|
}
|
2020-03-26 21:48:56 +01:00
|
|
|
#endif
|
|
|
|
|
2017-02-02 18:47:25 +01:00
|
|
|
template <>
|
|
|
|
void fmt_class_string<std::pair<const fmt_type_info*, u64>>::format(std::string& out, u64 arg)
|
|
|
|
{
|
|
|
|
// Dynamic format arg
|
|
|
|
const auto& pair = get_object(arg);
|
|
|
|
|
|
|
|
if (pair.first)
|
|
|
|
{
|
|
|
|
pair.first->fmt_string(out, pair.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-22 20:48:38 +01:00
|
|
|
template <>
|
|
|
|
void fmt_class_string<fmt::base57>::format(std::string& out, u64 arg)
|
|
|
|
{
|
|
|
|
const auto& _arg = get_object(arg);
|
|
|
|
|
|
|
|
if (_arg.data && _arg.size)
|
|
|
|
{
|
|
|
|
// Precomputed tail sizes if input data is not multiple of 8
|
|
|
|
static constexpr u8 s_tail[8] = {0, 2, 3, 5, 6, 7, 9, 10};
|
|
|
|
|
|
|
|
// Get full output size
|
2020-12-18 08:39:54 +01:00
|
|
|
const usz out_size = _arg.size / 8 * 11 + s_tail[_arg.size % 8];
|
2018-03-22 20:48:38 +01:00
|
|
|
|
|
|
|
out.resize(out.size() + out_size);
|
|
|
|
|
|
|
|
const auto ptr = &out.front() + (out.size() - out_size);
|
|
|
|
|
|
|
|
// Each 8 bytes of input data produce 11 bytes of base57 output
|
2020-12-18 08:39:54 +01:00
|
|
|
for (usz i = 0, p = 0; i < _arg.size; i += 8, p += 11)
|
2018-03-22 20:48:38 +01:00
|
|
|
{
|
|
|
|
// Load up to 8 bytes
|
|
|
|
be_t<u64> be_value;
|
|
|
|
|
|
|
|
if (_arg.size - i < sizeof(be_value))
|
|
|
|
{
|
|
|
|
std::memset(&be_value, 0, sizeof(be_value));
|
|
|
|
std::memcpy(&be_value, _arg.data + i, _arg.size - i);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::memcpy(&be_value, _arg.data + i, sizeof(be_value));
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 value = be_value;
|
|
|
|
|
|
|
|
for (int j = 10; j >= 0; j--)
|
|
|
|
{
|
|
|
|
if (p + j < out_size)
|
|
|
|
{
|
|
|
|
ptr[p + j] = "0123456789ACEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"[value % 57];
|
|
|
|
}
|
|
|
|
|
|
|
|
value /= 57;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-03 22:51:05 +02:00
|
|
|
void fmt_class_string<const void*>::format(std::string& out, u64 arg)
|
2015-01-12 21:37:29 +01:00
|
|
|
{
|
2018-09-11 18:02:19 +02:00
|
|
|
fmt::append(out, "%p", arg);
|
2015-01-12 21:37:29 +01:00
|
|
|
}
|
|
|
|
|
2016-08-03 22:51:05 +02:00
|
|
|
void fmt_class_string<const char*>::format(std::string& out, u64 arg)
|
2015-01-12 21:37:29 +01:00
|
|
|
{
|
2016-08-15 15:57:51 +02:00
|
|
|
if (arg)
|
|
|
|
{
|
2018-09-11 18:02:19 +02:00
|
|
|
out += reinterpret_cast<const char*>(arg);
|
2016-08-15 15:57:51 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-09-11 18:02:19 +02:00
|
|
|
out += "(NULLSTR)";
|
2016-08-15 15:57:51 +02:00
|
|
|
}
|
2015-01-12 21:37:29 +01:00
|
|
|
}
|
|
|
|
|
2022-05-07 14:55:33 +02:00
|
|
|
void fmt_class_string<const wchar_t*>::format(std::string& out, u64 arg)
|
|
|
|
{
|
|
|
|
out += wchar_to_utf8(reinterpret_cast<const wchar_t*>(arg));
|
|
|
|
}
|
|
|
|
|
2016-08-13 15:36:04 +02:00
|
|
|
template <>
|
2016-08-03 22:51:05 +02:00
|
|
|
void fmt_class_string<std::string>::format(std::string& out, u64 arg)
|
2016-04-25 12:49:12 +02:00
|
|
|
{
|
2018-09-11 18:02:19 +02:00
|
|
|
out += get_object(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
void fmt_class_string<std::string_view>::format(std::string& out, u64 arg)
|
|
|
|
{
|
|
|
|
out += get_object(arg);
|
2016-08-03 22:51:05 +02:00
|
|
|
}
|
|
|
|
|
2016-08-13 15:36:04 +02:00
|
|
|
template <>
|
2016-08-03 22:51:05 +02:00
|
|
|
void fmt_class_string<std::vector<char>>::format(std::string& out, u64 arg)
|
|
|
|
{
|
|
|
|
const std::vector<char>& obj = get_object(arg);
|
|
|
|
out.append(obj.cbegin(), obj.cend());
|
|
|
|
}
|
|
|
|
|
2021-07-10 20:11:52 +02:00
|
|
|
template <>
|
|
|
|
void fmt_class_string<std::u8string>::format(std::string& out, u64 arg)
|
|
|
|
{
|
|
|
|
const std::u8string& obj = get_object(arg);
|
|
|
|
out.append(obj.cbegin(), obj.cend());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
void fmt_class_string<std::u8string_view>::format(std::string& out, u64 arg)
|
|
|
|
{
|
|
|
|
const std::u8string_view& obj = get_object(arg);
|
|
|
|
out.append(obj.cbegin(), obj.cend());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
void fmt_class_string<std::vector<char8_t>>::format(std::string& out, u64 arg)
|
|
|
|
{
|
|
|
|
const std::vector<char8_t>& obj = get_object(arg);
|
|
|
|
out.append(obj.cbegin(), obj.cend());
|
|
|
|
}
|
|
|
|
|
2021-07-17 10:36:27 +02:00
|
|
|
void format_byte_array(std::string& out, const uchar* data, usz size)
|
|
|
|
{
|
|
|
|
if (!size)
|
|
|
|
{
|
|
|
|
out += "{ EMPTY }";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
out += "{ ";
|
|
|
|
|
|
|
|
for (usz i = 0;; i++)
|
|
|
|
{
|
|
|
|
if (i == size - 1)
|
|
|
|
{
|
|
|
|
fmt::append(out, "%02X", data[i]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-07-27 11:10:04 +02:00
|
|
|
if ((i % 4) == 3)
|
2021-07-17 10:36:27 +02:00
|
|
|
{
|
|
|
|
// Place a comma each 4 bytes for ease of byte placement finding
|
2021-07-27 11:10:04 +02:00
|
|
|
fmt::append(out, "%02X, ", data[i]);
|
2021-07-17 10:36:27 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-07-27 11:10:04 +02:00
|
|
|
fmt::append(out, "%02X ", data[i]);
|
2021-07-17 10:36:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
out += " }";
|
|
|
|
}
|
|
|
|
|
2016-08-13 15:36:04 +02:00
|
|
|
template <>
|
2016-08-03 22:51:05 +02:00
|
|
|
void fmt_class_string<char>::format(std::string& out, u64 arg)
|
|
|
|
{
|
2016-08-06 12:25:05 +02:00
|
|
|
fmt::append(out, "%#hhx", static_cast<char>(arg));
|
2016-08-03 22:51:05 +02:00
|
|
|
}
|
|
|
|
|
2016-08-13 15:36:04 +02:00
|
|
|
template <>
|
2016-08-03 22:51:05 +02:00
|
|
|
void fmt_class_string<uchar>::format(std::string& out, u64 arg)
|
|
|
|
{
|
2016-08-06 12:25:05 +02:00
|
|
|
fmt::append(out, "%#hhx", static_cast<uchar>(arg));
|
2016-08-03 22:51:05 +02:00
|
|
|
}
|
|
|
|
|
2016-08-13 15:36:04 +02:00
|
|
|
template <>
|
2016-08-03 22:51:05 +02:00
|
|
|
void fmt_class_string<schar>::format(std::string& out, u64 arg)
|
|
|
|
{
|
2016-08-06 12:25:05 +02:00
|
|
|
fmt::append(out, "%#hhx", static_cast<schar>(arg));
|
2016-08-03 22:51:05 +02:00
|
|
|
}
|
|
|
|
|
2016-08-13 15:36:04 +02:00
|
|
|
template <>
|
2016-08-03 22:51:05 +02:00
|
|
|
void fmt_class_string<short>::format(std::string& out, u64 arg)
|
|
|
|
{
|
2016-08-06 12:25:05 +02:00
|
|
|
fmt::append(out, "%#hx", static_cast<short>(arg));
|
2016-08-03 22:51:05 +02:00
|
|
|
}
|
2016-04-25 12:49:12 +02:00
|
|
|
|
2016-08-13 15:36:04 +02:00
|
|
|
template <>
|
2016-08-03 22:51:05 +02:00
|
|
|
void fmt_class_string<ushort>::format(std::string& out, u64 arg)
|
|
|
|
{
|
2016-08-06 12:25:05 +02:00
|
|
|
fmt::append(out, "%#hx", static_cast<ushort>(arg));
|
2016-08-03 22:51:05 +02:00
|
|
|
}
|
|
|
|
|
2016-08-13 15:36:04 +02:00
|
|
|
template <>
|
2016-08-03 22:51:05 +02:00
|
|
|
void fmt_class_string<int>::format(std::string& out, u64 arg)
|
|
|
|
{
|
2016-08-06 12:25:05 +02:00
|
|
|
fmt::append(out, "%#x", static_cast<int>(arg));
|
2016-08-03 22:51:05 +02:00
|
|
|
}
|
|
|
|
|
2016-08-13 15:36:04 +02:00
|
|
|
template <>
|
2016-08-03 22:51:05 +02:00
|
|
|
void fmt_class_string<uint>::format(std::string& out, u64 arg)
|
|
|
|
{
|
2016-08-06 12:25:05 +02:00
|
|
|
fmt::append(out, "%#x", static_cast<uint>(arg));
|
2016-08-03 22:51:05 +02:00
|
|
|
}
|
2016-04-25 12:49:12 +02:00
|
|
|
|
2016-08-13 15:36:04 +02:00
|
|
|
template <>
|
2016-08-03 22:51:05 +02:00
|
|
|
void fmt_class_string<long>::format(std::string& out, u64 arg)
|
|
|
|
{
|
2016-08-06 12:25:05 +02:00
|
|
|
fmt::append(out, "%#lx", static_cast<long>(arg));
|
2016-08-03 22:51:05 +02:00
|
|
|
}
|
|
|
|
|
2016-08-13 15:36:04 +02:00
|
|
|
template <>
|
2016-08-03 22:51:05 +02:00
|
|
|
void fmt_class_string<ulong>::format(std::string& out, u64 arg)
|
|
|
|
{
|
2016-08-06 12:25:05 +02:00
|
|
|
fmt::append(out, "%#lx", static_cast<ulong>(arg));
|
2016-08-03 22:51:05 +02:00
|
|
|
}
|
|
|
|
|
2016-08-13 15:36:04 +02:00
|
|
|
template <>
|
2016-08-03 22:51:05 +02:00
|
|
|
void fmt_class_string<llong>::format(std::string& out, u64 arg)
|
|
|
|
{
|
2016-08-06 12:25:05 +02:00
|
|
|
fmt::append(out, "%#llx", static_cast<llong>(arg));
|
2016-08-03 22:51:05 +02:00
|
|
|
}
|
2016-04-25 12:49:12 +02:00
|
|
|
|
2016-08-13 15:36:04 +02:00
|
|
|
template <>
|
2016-08-03 22:51:05 +02:00
|
|
|
void fmt_class_string<ullong>::format(std::string& out, u64 arg)
|
|
|
|
{
|
2016-08-06 12:25:05 +02:00
|
|
|
fmt::append(out, "%#llx", static_cast<ullong>(arg));
|
2016-08-03 22:51:05 +02:00
|
|
|
}
|
|
|
|
|
2016-08-13 15:36:04 +02:00
|
|
|
template <>
|
2016-08-03 22:51:05 +02:00
|
|
|
void fmt_class_string<float>::format(std::string& out, u64 arg)
|
|
|
|
{
|
2019-06-01 23:12:17 +02:00
|
|
|
fmt::append(out, "%gf", static_cast<float>(std::bit_cast<f64>(arg)));
|
2016-08-03 22:51:05 +02:00
|
|
|
}
|
|
|
|
|
2016-08-13 15:36:04 +02:00
|
|
|
template <>
|
2016-08-03 22:51:05 +02:00
|
|
|
void fmt_class_string<double>::format(std::string& out, u64 arg)
|
|
|
|
{
|
2019-06-01 23:12:17 +02:00
|
|
|
fmt::append(out, "%g", std::bit_cast<f64>(arg));
|
2016-08-03 22:51:05 +02:00
|
|
|
}
|
|
|
|
|
2016-08-13 15:36:04 +02:00
|
|
|
template <>
|
2016-08-03 22:51:05 +02:00
|
|
|
void fmt_class_string<bool>::format(std::string& out, u64 arg)
|
|
|
|
{
|
2016-08-06 12:25:05 +02:00
|
|
|
out += arg ? "true" : "false";
|
2016-08-03 22:51:05 +02:00
|
|
|
}
|
|
|
|
|
2020-12-22 16:04:08 +01:00
|
|
|
template <>
|
|
|
|
void fmt_class_string<b8>::format(std::string& out, u64 arg)
|
|
|
|
{
|
|
|
|
out += get_object(arg) ? "true" : "false";
|
|
|
|
}
|
|
|
|
|
2016-08-13 15:36:04 +02:00
|
|
|
template <>
|
2016-08-03 22:51:05 +02:00
|
|
|
void fmt_class_string<v128>::format(std::string& out, u64 arg)
|
|
|
|
{
|
|
|
|
const v128& vec = get_object(arg);
|
|
|
|
fmt::append(out, "0x%016llx%016llx", vec._u64[1], vec._u64[0]);
|
|
|
|
}
|
|
|
|
|
2020-12-21 15:12:05 +01:00
|
|
|
template <>
|
|
|
|
void fmt_class_string<u128>::format(std::string& out, u64 arg)
|
|
|
|
{
|
|
|
|
// TODO: it should be supported as full-fledged integral type (with %u, %d, etc, fmt)
|
|
|
|
const u128& num = get_object(arg);
|
2021-04-24 14:28:15 +02:00
|
|
|
|
|
|
|
if (!num)
|
|
|
|
{
|
|
|
|
out += '0';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-21 15:12:05 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
fmt::append(out, "0x%016llx%016llx", num.hi, num.lo);
|
|
|
|
#else
|
|
|
|
fmt::append(out, "0x%016llx%016llx", static_cast<u64>(num >> 64), static_cast<u64>(num));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-04-24 14:28:15 +02:00
|
|
|
template <>
|
|
|
|
void fmt_class_string<s128>::format(std::string& out, u64 arg)
|
|
|
|
{
|
|
|
|
return fmt_class_string<u128>::format(out, arg);
|
|
|
|
}
|
|
|
|
|
2020-12-09 16:04:52 +01:00
|
|
|
template <>
|
|
|
|
void fmt_class_string<src_loc>::format(std::string& out, u64 arg)
|
2016-08-03 22:51:05 +02:00
|
|
|
{
|
2020-12-09 16:04:52 +01:00
|
|
|
const src_loc& loc = get_object(arg);
|
|
|
|
|
|
|
|
if (loc.col != umax)
|
|
|
|
{
|
2020-12-17 15:00:06 +01:00
|
|
|
fmt::append(out, "\n(in file %s:%u[:%u]", loc.file, loc.line, loc.col);
|
2020-12-09 16:04:52 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-12-17 15:00:06 +01:00
|
|
|
fmt::append(out, "\n(in file %s:%u", loc.file, loc.line);
|
2020-12-09 16:04:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (loc.func && *loc.func)
|
2016-08-08 18:01:06 +02:00
|
|
|
{
|
2020-12-09 16:04:52 +01:00
|
|
|
fmt::append(out, ", in function %s)", loc.func);
|
2016-08-13 15:36:04 +02:00
|
|
|
}
|
2020-12-09 16:04:52 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
out += ')';
|
|
|
|
}
|
2020-12-10 10:42:02 +01:00
|
|
|
|
|
|
|
// Print error code (may be irrelevant)
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (DWORD error = GetLastError())
|
|
|
|
{
|
2022-03-16 19:59:20 +01:00
|
|
|
fmt::append(out, " (error=%s)", error, fmt::win_error_to_string(error));
|
2020-12-10 10:42:02 +01:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (int error = errno)
|
|
|
|
{
|
2022-03-16 19:59:20 +01:00
|
|
|
fmt::append(out, " (errno=%d=%s)", error, strerror(errno));
|
2020-12-10 10:42:02 +01:00
|
|
|
}
|
|
|
|
#endif
|
2020-12-09 16:04:52 +01:00
|
|
|
}
|
2016-08-13 15:36:04 +02:00
|
|
|
|
2020-12-09 16:04:52 +01:00
|
|
|
namespace fmt
|
|
|
|
{
|
2021-06-09 18:05:25 +02:00
|
|
|
[[noreturn]] void raw_verify_error(const src_loc& loc, const char8_t* msg)
|
2016-08-13 15:36:04 +02:00
|
|
|
{
|
2021-06-09 18:05:25 +02:00
|
|
|
std::string out;
|
|
|
|
fmt::append(out, "%s%s", msg ? msg : u8"Verification failed", loc);
|
2020-03-08 12:48:06 +01:00
|
|
|
thread_ctrl::emergency_exit(out);
|
2016-08-08 18:01:06 +02:00
|
|
|
}
|
|
|
|
|
2020-12-22 12:05:05 +01:00
|
|
|
[[noreturn]] void raw_throw_exception(const src_loc& loc, const char* fmt, const fmt_type_info* sup, const u64* args)
|
2016-08-08 18:01:06 +02:00
|
|
|
{
|
|
|
|
std::string out;
|
|
|
|
raw_append(out, fmt, sup, args);
|
2020-12-09 16:04:52 +01:00
|
|
|
fmt::append(out, "%s", loc);
|
2020-03-08 12:48:06 +01:00
|
|
|
thread_ctrl::emergency_exit(out);
|
2016-08-08 18:01:06 +02:00
|
|
|
}
|
|
|
|
|
2016-08-03 22:51:05 +02:00
|
|
|
struct cfmt_src;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Temporary implementation
|
|
|
|
struct fmt::cfmt_src
|
|
|
|
{
|
2016-08-05 18:49:45 +02:00
|
|
|
const fmt_type_info* sup;
|
2016-08-03 22:51:05 +02:00
|
|
|
const u64* args;
|
|
|
|
|
2020-12-18 08:39:54 +01:00
|
|
|
bool test(usz index) const
|
2016-04-25 12:49:12 +02:00
|
|
|
{
|
2016-08-05 18:49:45 +02:00
|
|
|
if (!sup[index].fmt_string)
|
2016-04-25 12:49:12 +02:00
|
|
|
{
|
2016-08-05 18:49:45 +02:00
|
|
|
return false;
|
2016-04-25 12:49:12 +02:00
|
|
|
}
|
|
|
|
|
2016-08-03 22:51:05 +02:00
|
|
|
return true;
|
2016-04-25 12:49:12 +02:00
|
|
|
}
|
|
|
|
|
2016-08-13 15:36:04 +02:00
|
|
|
template <typename T>
|
2020-12-18 08:39:54 +01:00
|
|
|
T get(usz index) const
|
2016-08-03 22:51:05 +02:00
|
|
|
{
|
2021-03-08 21:41:23 +01:00
|
|
|
T res{};
|
|
|
|
std::memcpy(&res, reinterpret_cast<const u8*>(args + index), sizeof(res));
|
|
|
|
return res;
|
2016-08-03 22:51:05 +02:00
|
|
|
}
|
2016-04-25 12:49:12 +02:00
|
|
|
|
2020-12-18 08:39:54 +01:00
|
|
|
void skip(usz extra)
|
2016-08-03 22:51:05 +02:00
|
|
|
{
|
2017-08-01 19:22:33 +02:00
|
|
|
sup += extra + 1;
|
|
|
|
args += extra + 1;
|
2016-08-03 22:51:05 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 08:39:54 +01:00
|
|
|
usz fmt_string(std::string& out, usz extra) const
|
2016-08-03 22:51:05 +02:00
|
|
|
{
|
2020-12-18 08:39:54 +01:00
|
|
|
const usz start = out.size();
|
2016-08-05 18:49:45 +02:00
|
|
|
sup[extra].fmt_string(out, args[extra]);
|
2016-08-03 22:51:05 +02:00
|
|
|
return out.size() - start;
|
|
|
|
}
|
2016-08-05 18:49:45 +02:00
|
|
|
|
2016-08-06 12:25:05 +02:00
|
|
|
// Returns type size (0 if unknown, pointer, unsigned, assumed max)
|
2020-12-18 08:39:54 +01:00
|
|
|
usz type(usz extra) const
|
2016-08-05 18:49:45 +02:00
|
|
|
{
|
2016-08-13 15:36:04 +02:00
|
|
|
// Hack: use known function pointers to determine type
|
|
|
|
#define TYPE(type) \
|
|
|
|
if (sup[extra].fmt_string == &fmt_class_string<type>::format) return sizeof(type);
|
2016-08-05 18:49:45 +02:00
|
|
|
|
2016-08-06 12:25:05 +02:00
|
|
|
TYPE(int);
|
|
|
|
TYPE(llong);
|
2016-08-05 18:49:45 +02:00
|
|
|
TYPE(schar);
|
|
|
|
TYPE(short);
|
2016-08-06 12:25:05 +02:00
|
|
|
if (std::is_signed<char>::value) TYPE(char);
|
2016-08-05 18:49:45 +02:00
|
|
|
TYPE(long);
|
2021-04-24 14:28:15 +02:00
|
|
|
TYPE(u128);
|
|
|
|
TYPE(s128);
|
2016-08-05 18:49:45 +02:00
|
|
|
|
|
|
|
#undef TYPE
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2017-05-16 13:29:07 +02:00
|
|
|
|
2020-12-18 08:39:54 +01:00
|
|
|
static constexpr usz size_char = 1;
|
|
|
|
static constexpr usz size_short = 2;
|
|
|
|
static constexpr usz size_int = 0;
|
|
|
|
static constexpr usz size_long = sizeof(ulong);
|
|
|
|
static constexpr usz size_llong = sizeof(ullong);
|
|
|
|
static constexpr usz size_size = sizeof(usz);
|
|
|
|
static constexpr usz size_max = sizeof(std::uintmax_t);
|
|
|
|
static constexpr usz size_diff = sizeof(std::ptrdiff_t);
|
2016-08-03 22:51:05 +02:00
|
|
|
};
|
|
|
|
|
2016-08-05 18:49:45 +02:00
|
|
|
void fmt::raw_append(std::string& out, const char* fmt, const fmt_type_info* sup, const u64* args) noexcept
|
2016-08-03 22:51:05 +02:00
|
|
|
{
|
|
|
|
cfmt_append(out, fmt, cfmt_src{sup, args});
|
2016-04-25 12:49:12 +02:00
|
|
|
}
|
|
|
|
|
2021-04-10 07:06:40 +02:00
|
|
|
std::string fmt::replace_all(std::string_view src, std::string_view from, std::string_view to, usz count)
|
2014-06-07 16:15:49 +02:00
|
|
|
{
|
2021-04-10 07:06:40 +02:00
|
|
|
std::string target;
|
|
|
|
target.reserve(src.size() + to.size());
|
2014-06-07 16:15:49 +02:00
|
|
|
|
2021-04-10 07:06:40 +02:00
|
|
|
for (usz i = 0, replaced = 0; i < src.size();)
|
2014-06-07 16:15:49 +02:00
|
|
|
{
|
2021-04-10 07:06:40 +02:00
|
|
|
const usz pos = src.find(from, i);
|
2014-06-07 16:15:49 +02:00
|
|
|
|
2021-04-10 07:06:40 +02:00
|
|
|
if (pos == umax || replaced++ >= count)
|
|
|
|
{
|
|
|
|
// No match or too many encountered, append the rest of the string as is
|
|
|
|
target.append(src.substr(i));
|
|
|
|
break;
|
|
|
|
}
|
2014-06-07 16:15:49 +02:00
|
|
|
|
2021-04-10 07:06:40 +02:00
|
|
|
// Append source until the matched string position
|
|
|
|
target.append(src.substr(i, pos - i));
|
|
|
|
|
|
|
|
// Replace string
|
|
|
|
target.append(to);
|
|
|
|
i = pos + from.size();
|
2014-06-07 16:15:49 +02:00
|
|
|
}
|
|
|
|
|
2015-02-04 13:16:10 +01:00
|
|
|
return target;
|
2014-06-07 16:15:49 +02:00
|
|
|
}
|
|
|
|
|
2021-01-05 16:34:35 +01:00
|
|
|
std::vector<std::string> fmt::split(std::string_view source, std::initializer_list<std::string_view> separators, bool is_skip_empty)
|
2014-11-29 14:16:53 +01:00
|
|
|
{
|
|
|
|
std::vector<std::string> result;
|
|
|
|
|
2021-01-05 16:34:35 +01:00
|
|
|
for (usz index = 0; index < source.size();)
|
2014-11-29 14:16:53 +01:00
|
|
|
{
|
2021-01-05 16:34:35 +01:00
|
|
|
usz pos = -1;
|
|
|
|
usz sep_size = 0;
|
|
|
|
|
2016-08-13 15:36:04 +02:00
|
|
|
for (auto& separator : separators)
|
2014-11-29 14:16:53 +01:00
|
|
|
{
|
2021-03-06 17:30:09 +01:00
|
|
|
if (usz pos0 = source.find(separator, index); pos0 < pos)
|
2014-11-29 14:16:53 +01:00
|
|
|
{
|
2021-01-05 16:34:35 +01:00
|
|
|
pos = pos0;
|
|
|
|
sep_size = separator.size();
|
2014-11-29 14:16:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-05 16:34:35 +01:00
|
|
|
if (!sep_size)
|
|
|
|
{
|
|
|
|
result.emplace_back(&source[index], source.size() - index);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string_view piece = {&source[index], pos - index};
|
|
|
|
|
|
|
|
index = pos + sep_size;
|
|
|
|
|
|
|
|
if (piece.empty() && is_skip_empty)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
result.emplace_back(std::string(piece));
|
2014-11-29 14:16:53 +01:00
|
|
|
}
|
|
|
|
|
2021-03-06 17:26:41 +01:00
|
|
|
if (result.empty() && !is_skip_empty)
|
|
|
|
{
|
|
|
|
result.emplace_back();
|
|
|
|
}
|
|
|
|
|
2019-10-22 11:33:29 +02:00
|
|
|
return result;
|
2014-11-30 12:18:17 +01:00
|
|
|
}
|
|
|
|
|
2015-10-13 19:32:08 +02:00
|
|
|
std::string fmt::trim(const std::string& source, const std::string& values)
|
|
|
|
{
|
2020-12-18 08:39:54 +01:00
|
|
|
usz begin = source.find_first_not_of(values);
|
2015-10-13 19:32:08 +02:00
|
|
|
|
|
|
|
if (begin == source.npos)
|
2016-08-13 15:36:04 +02:00
|
|
|
return {};
|
2015-10-13 19:32:08 +02:00
|
|
|
|
|
|
|
return source.substr(begin, source.find_last_not_of(values) + 1);
|
|
|
|
}
|
|
|
|
|
2016-04-27 00:27:24 +02:00
|
|
|
std::string fmt::to_upper(const std::string& string)
|
2016-02-01 22:55:43 +01:00
|
|
|
{
|
|
|
|
std::string result;
|
2016-04-27 00:27:24 +02:00
|
|
|
result.resize(string.size());
|
|
|
|
std::transform(string.begin(), string.end(), result.begin(), ::toupper);
|
2016-02-01 22:55:43 +01:00
|
|
|
return result;
|
2015-02-04 13:16:10 +01:00
|
|
|
}
|
2015-12-22 20:24:35 +01:00
|
|
|
|
2018-05-04 22:49:35 +02:00
|
|
|
std::string fmt::to_lower(const std::string& string)
|
|
|
|
{
|
|
|
|
std::string result;
|
|
|
|
result.resize(string.size());
|
|
|
|
std::transform(string.begin(), string.end(), result.begin(), ::tolower);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-08-13 15:36:04 +02:00
|
|
|
bool fmt::match(const std::string& source, const std::string& mask)
|
2015-12-22 20:24:35 +01:00
|
|
|
{
|
2020-12-18 08:39:54 +01:00
|
|
|
usz source_position = 0, mask_position = 0;
|
2015-12-22 20:24:35 +01:00
|
|
|
|
|
|
|
for (; source_position < source.size() && mask_position < mask.size(); ++mask_position, ++source_position)
|
|
|
|
{
|
|
|
|
switch (mask[mask_position])
|
|
|
|
{
|
|
|
|
case '?': break;
|
|
|
|
|
|
|
|
case '*':
|
2020-12-18 08:39:54 +01:00
|
|
|
for (usz test_source_position = source_position; test_source_position < source.size(); ++test_source_position)
|
2015-12-22 20:24:35 +01:00
|
|
|
{
|
|
|
|
if (match(source.substr(test_source_position), mask.substr(mask_position + 1)))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (source[source_position] != mask[mask_position])
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (source_position != source.size())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (mask_position != mask.size())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|