2016-02-01 21:55:43 +00:00
|
|
|
#include "StrFmt.h"
|
|
|
|
#include "BEType.h"
|
2016-04-26 22:27:24 +00:00
|
|
|
#include "StrUtil.h"
|
2016-08-05 16:49:45 +00:00
|
|
|
#include "Macro.h"
|
2016-08-03 20:51:05 +00:00
|
|
|
#include "cfmt.h"
|
2014-04-01 00:33:55 +00:00
|
|
|
|
2016-05-13 14:01:48 +00:00
|
|
|
#include <algorithm>
|
2016-04-25 10:49:12 +00:00
|
|
|
|
2016-08-03 20:51:05 +00:00
|
|
|
void fmt_class_string<const void*>::format(std::string& out, u64 arg)
|
2015-01-12 20:37:29 +00:00
|
|
|
{
|
2016-08-03 20:51:05 +00:00
|
|
|
fmt::append(out, "%p", reinterpret_cast<const void*>(static_cast<std::uintptr_t>(arg)));
|
2015-01-12 20:37:29 +00:00
|
|
|
}
|
|
|
|
|
2016-08-03 20:51:05 +00:00
|
|
|
void fmt_class_string<const char*>::format(std::string& out, u64 arg)
|
2015-01-12 20:37:29 +00:00
|
|
|
{
|
2016-08-03 20:51:05 +00:00
|
|
|
out += reinterpret_cast<const char*>(static_cast<std::uintptr_t>(arg));
|
2015-01-12 20:37:29 +00:00
|
|
|
}
|
|
|
|
|
2016-08-13 13:36:04 +00:00
|
|
|
template <>
|
2016-08-03 20:51:05 +00:00
|
|
|
void fmt_class_string<std::string>::format(std::string& out, u64 arg)
|
2016-04-25 10:49:12 +00:00
|
|
|
{
|
2016-08-03 20:51:05 +00:00
|
|
|
out += get_object(arg).c_str(); // TODO?
|
|
|
|
}
|
|
|
|
|
2016-08-13 13:36:04 +00:00
|
|
|
template <>
|
2016-08-03 20:51:05 +00: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());
|
|
|
|
}
|
|
|
|
|
2016-08-13 13:36:04 +00:00
|
|
|
template <>
|
2016-08-03 20:51:05 +00:00
|
|
|
void fmt_class_string<char>::format(std::string& out, u64 arg)
|
|
|
|
{
|
2016-08-06 10:25:05 +00:00
|
|
|
fmt::append(out, "%#hhx", static_cast<char>(arg));
|
2016-08-03 20:51:05 +00:00
|
|
|
}
|
|
|
|
|
2016-08-13 13:36:04 +00:00
|
|
|
template <>
|
2016-08-03 20:51:05 +00:00
|
|
|
void fmt_class_string<uchar>::format(std::string& out, u64 arg)
|
|
|
|
{
|
2016-08-06 10:25:05 +00:00
|
|
|
fmt::append(out, "%#hhx", static_cast<uchar>(arg));
|
2016-08-03 20:51:05 +00:00
|
|
|
}
|
|
|
|
|
2016-08-13 13:36:04 +00:00
|
|
|
template <>
|
2016-08-03 20:51:05 +00:00
|
|
|
void fmt_class_string<schar>::format(std::string& out, u64 arg)
|
|
|
|
{
|
2016-08-06 10:25:05 +00:00
|
|
|
fmt::append(out, "%#hhx", static_cast<schar>(arg));
|
2016-08-03 20:51:05 +00:00
|
|
|
}
|
|
|
|
|
2016-08-13 13:36:04 +00:00
|
|
|
template <>
|
2016-08-03 20:51:05 +00:00
|
|
|
void fmt_class_string<short>::format(std::string& out, u64 arg)
|
|
|
|
{
|
2016-08-06 10:25:05 +00:00
|
|
|
fmt::append(out, "%#hx", static_cast<short>(arg));
|
2016-08-03 20:51:05 +00:00
|
|
|
}
|
2016-04-25 10:49:12 +00:00
|
|
|
|
2016-08-13 13:36:04 +00:00
|
|
|
template <>
|
2016-08-03 20:51:05 +00:00
|
|
|
void fmt_class_string<ushort>::format(std::string& out, u64 arg)
|
|
|
|
{
|
2016-08-06 10:25:05 +00:00
|
|
|
fmt::append(out, "%#hx", static_cast<ushort>(arg));
|
2016-08-03 20:51:05 +00:00
|
|
|
}
|
|
|
|
|
2016-08-13 13:36:04 +00:00
|
|
|
template <>
|
2016-08-03 20:51:05 +00:00
|
|
|
void fmt_class_string<int>::format(std::string& out, u64 arg)
|
|
|
|
{
|
2016-08-06 10:25:05 +00:00
|
|
|
fmt::append(out, "%#x", static_cast<int>(arg));
|
2016-08-03 20:51:05 +00:00
|
|
|
}
|
|
|
|
|
2016-08-13 13:36:04 +00:00
|
|
|
template <>
|
2016-08-03 20:51:05 +00:00
|
|
|
void fmt_class_string<uint>::format(std::string& out, u64 arg)
|
|
|
|
{
|
2016-08-06 10:25:05 +00:00
|
|
|
fmt::append(out, "%#x", static_cast<uint>(arg));
|
2016-08-03 20:51:05 +00:00
|
|
|
}
|
2016-04-25 10:49:12 +00:00
|
|
|
|
2016-08-13 13:36:04 +00:00
|
|
|
template <>
|
2016-08-03 20:51:05 +00:00
|
|
|
void fmt_class_string<long>::format(std::string& out, u64 arg)
|
|
|
|
{
|
2016-08-06 10:25:05 +00:00
|
|
|
fmt::append(out, "%#lx", static_cast<long>(arg));
|
2016-08-03 20:51:05 +00:00
|
|
|
}
|
|
|
|
|
2016-08-13 13:36:04 +00:00
|
|
|
template <>
|
2016-08-03 20:51:05 +00:00
|
|
|
void fmt_class_string<ulong>::format(std::string& out, u64 arg)
|
|
|
|
{
|
2016-08-06 10:25:05 +00:00
|
|
|
fmt::append(out, "%#lx", static_cast<ulong>(arg));
|
2016-08-03 20:51:05 +00:00
|
|
|
}
|
|
|
|
|
2016-08-13 13:36:04 +00:00
|
|
|
template <>
|
2016-08-03 20:51:05 +00:00
|
|
|
void fmt_class_string<llong>::format(std::string& out, u64 arg)
|
|
|
|
{
|
2016-08-06 10:25:05 +00:00
|
|
|
fmt::append(out, "%#llx", static_cast<llong>(arg));
|
2016-08-03 20:51:05 +00:00
|
|
|
}
|
2016-04-25 10:49:12 +00:00
|
|
|
|
2016-08-13 13:36:04 +00:00
|
|
|
template <>
|
2016-08-03 20:51:05 +00:00
|
|
|
void fmt_class_string<ullong>::format(std::string& out, u64 arg)
|
|
|
|
{
|
2016-08-06 10:25:05 +00:00
|
|
|
fmt::append(out, "%#llx", static_cast<ullong>(arg));
|
2016-08-03 20:51:05 +00:00
|
|
|
}
|
|
|
|
|
2016-08-13 13:36:04 +00:00
|
|
|
template <>
|
2016-08-03 20:51:05 +00:00
|
|
|
void fmt_class_string<float>::format(std::string& out, u64 arg)
|
|
|
|
{
|
2016-08-05 16:49:45 +00:00
|
|
|
fmt::append(out, "%gf", static_cast<float>(reinterpret_cast<f64&>(arg)));
|
2016-08-03 20:51:05 +00:00
|
|
|
}
|
|
|
|
|
2016-08-13 13:36:04 +00:00
|
|
|
template <>
|
2016-08-03 20:51:05 +00:00
|
|
|
void fmt_class_string<double>::format(std::string& out, u64 arg)
|
|
|
|
{
|
2016-08-05 16:49:45 +00:00
|
|
|
fmt::append(out, "%g", reinterpret_cast<f64&>(arg));
|
2016-08-03 20:51:05 +00:00
|
|
|
}
|
|
|
|
|
2016-08-13 13:36:04 +00:00
|
|
|
template <>
|
2016-08-03 20:51:05 +00:00
|
|
|
void fmt_class_string<bool>::format(std::string& out, u64 arg)
|
|
|
|
{
|
2016-08-06 10:25:05 +00:00
|
|
|
out += arg ? "true" : "false";
|
2016-08-03 20:51:05 +00:00
|
|
|
}
|
|
|
|
|
2016-08-13 13:36:04 +00:00
|
|
|
template <>
|
2016-08-03 20:51:05 +00: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]);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace fmt
|
|
|
|
{
|
2016-08-08 16:01:06 +00:00
|
|
|
void raw_error(const char* msg)
|
|
|
|
{
|
2016-08-13 13:36:04 +00:00
|
|
|
std::string out{"Error"};
|
2016-08-08 16:01:06 +00:00
|
|
|
|
2016-08-13 13:36:04 +00:00
|
|
|
out += ": ";
|
2016-08-08 16:01:06 +00:00
|
|
|
out += msg;
|
|
|
|
|
2016-08-13 13:36:04 +00:00
|
|
|
throw std::runtime_error{out};
|
|
|
|
}
|
|
|
|
|
|
|
|
void raw_verify_error(const char* msg, uint position)
|
|
|
|
{
|
|
|
|
std::string out{"Verification failed"};
|
|
|
|
|
|
|
|
if (position)
|
|
|
|
{
|
|
|
|
out += " (+";
|
|
|
|
out += std::to_string(position);
|
|
|
|
out += ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
out += ": ";
|
|
|
|
out += msg;
|
|
|
|
|
|
|
|
throw std::runtime_error{out};
|
2016-08-08 16:01:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void raw_narrow_error(const char* msg, const fmt_type_info* sup, u64 arg)
|
|
|
|
{
|
2016-08-13 13:36:04 +00:00
|
|
|
std::string out{"Narrow error"};
|
2016-08-08 16:01:06 +00:00
|
|
|
|
2016-08-13 13:36:04 +00:00
|
|
|
out += " (";
|
2016-08-08 16:01:06 +00:00
|
|
|
sup->fmt_string(out, arg); // Print value
|
2016-08-13 13:36:04 +00:00
|
|
|
out += "): ";
|
2016-08-08 16:01:06 +00:00
|
|
|
|
|
|
|
if (msg)
|
|
|
|
{
|
|
|
|
out += " ";
|
|
|
|
out += msg;
|
|
|
|
}
|
|
|
|
|
2016-08-13 13:36:04 +00:00
|
|
|
throw std::range_error{out};
|
2016-08-08 16:01:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Hidden template
|
2016-08-13 13:36:04 +00:00
|
|
|
template <typename T>
|
2016-08-08 16:01:06 +00:00
|
|
|
void raw_throw_exception(const char* fmt, const fmt_type_info* sup, const u64* args)
|
|
|
|
{
|
|
|
|
std::string out;
|
|
|
|
raw_append(out, fmt, sup, args);
|
|
|
|
throw T{out};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Explicit instantiations (not exhaustive)
|
|
|
|
template void raw_throw_exception<std::runtime_error>(const char*, const fmt_type_info*, const u64*);
|
|
|
|
template void raw_throw_exception<std::logic_error>(const char*, const fmt_type_info*, const u64*);
|
|
|
|
template void raw_throw_exception<std::domain_error>(const char*, const fmt_type_info*, const u64*);
|
|
|
|
template void raw_throw_exception<std::invalid_argument>(const char*, const fmt_type_info*, const u64*);
|
|
|
|
template void raw_throw_exception<std::out_of_range>(const char*, const fmt_type_info*, const u64*);
|
|
|
|
template void raw_throw_exception<std::range_error>(const char*, const fmt_type_info*, const u64*);
|
|
|
|
template void raw_throw_exception<std::overflow_error>(const char*, const fmt_type_info*, const u64*);
|
|
|
|
template void raw_throw_exception<std::underflow_error>(const char*, const fmt_type_info*, const u64*);
|
|
|
|
|
2016-08-03 20:51:05 +00:00
|
|
|
struct cfmt_src;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Temporary implementation
|
|
|
|
struct fmt::cfmt_src
|
|
|
|
{
|
2016-08-05 16:49:45 +00:00
|
|
|
const fmt_type_info* sup;
|
2016-08-03 20:51:05 +00:00
|
|
|
const u64* args;
|
|
|
|
|
2016-08-05 16:49:45 +00:00
|
|
|
bool test(std::size_t index) const
|
2016-04-25 10:49:12 +00:00
|
|
|
{
|
2016-08-05 16:49:45 +00:00
|
|
|
if (!sup[index].fmt_string)
|
2016-04-25 10:49:12 +00:00
|
|
|
{
|
2016-08-05 16:49:45 +00:00
|
|
|
return false;
|
2016-04-25 10:49:12 +00:00
|
|
|
}
|
|
|
|
|
2016-08-03 20:51:05 +00:00
|
|
|
return true;
|
2016-04-25 10:49:12 +00:00
|
|
|
}
|
|
|
|
|
2016-08-13 13:36:04 +00:00
|
|
|
template <typename T>
|
2016-08-05 16:49:45 +00:00
|
|
|
T get(std::size_t index) const
|
2016-08-03 20:51:05 +00:00
|
|
|
{
|
|
|
|
return reinterpret_cast<const T&>(args[index]);
|
|
|
|
}
|
2016-04-25 10:49:12 +00:00
|
|
|
|
2016-08-03 20:51:05 +00:00
|
|
|
void skip(std::size_t extra)
|
|
|
|
{
|
|
|
|
++sup += extra;
|
|
|
|
++args += extra;
|
|
|
|
}
|
|
|
|
|
2016-08-05 16:49:45 +00:00
|
|
|
std::size_t fmt_string(std::string& out, std::size_t extra) const
|
2016-08-03 20:51:05 +00:00
|
|
|
{
|
|
|
|
const std::size_t start = out.size();
|
2016-08-05 16:49:45 +00:00
|
|
|
sup[extra].fmt_string(out, args[extra]);
|
2016-08-03 20:51:05 +00:00
|
|
|
return out.size() - start;
|
|
|
|
}
|
2016-08-05 16:49:45 +00:00
|
|
|
|
2016-08-06 10:25:05 +00:00
|
|
|
// Returns type size (0 if unknown, pointer, unsigned, assumed max)
|
2016-08-05 16:49:45 +00:00
|
|
|
std::size_t type(std::size_t extra) const
|
|
|
|
{
|
2016-08-13 13:36:04 +00: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 16:49:45 +00:00
|
|
|
|
2016-08-06 10:25:05 +00:00
|
|
|
TYPE(int);
|
|
|
|
TYPE(llong);
|
2016-08-05 16:49:45 +00:00
|
|
|
TYPE(schar);
|
|
|
|
TYPE(short);
|
2016-08-06 10:25:05 +00:00
|
|
|
if (std::is_signed<char>::value) TYPE(char);
|
2016-08-05 16:49:45 +00:00
|
|
|
TYPE(long);
|
|
|
|
|
|
|
|
#undef TYPE
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2016-08-03 20:51:05 +00:00
|
|
|
};
|
|
|
|
|
2016-08-05 16:49:45 +00:00
|
|
|
void fmt::raw_append(std::string& out, const char* fmt, const fmt_type_info* sup, const u64* args) noexcept
|
2016-08-03 20:51:05 +00:00
|
|
|
{
|
|
|
|
cfmt_append(out, fmt, cfmt_src{sup, args});
|
2016-04-25 10:49:12 +00:00
|
|
|
}
|
|
|
|
|
2015-02-02 10:21:35 +00:00
|
|
|
std::string fmt::replace_first(const std::string& src, const std::string& from, const std::string& to)
|
2014-06-07 14:15:49 +00:00
|
|
|
{
|
|
|
|
auto pos = src.find(from);
|
|
|
|
|
|
|
|
if (pos == std::string::npos)
|
|
|
|
{
|
|
|
|
return src;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (pos ? src.substr(0, pos) + to : to) + std::string(src.c_str() + pos + from.length());
|
|
|
|
}
|
|
|
|
|
2016-08-13 13:36:04 +00:00
|
|
|
std::string fmt::replace_all(const std::string& src, const std::string& from, const std::string& to)
|
2014-06-07 14:15:49 +00:00
|
|
|
{
|
2015-02-02 10:21:35 +00:00
|
|
|
std::string target = src;
|
|
|
|
for (auto pos = target.find(from); pos != std::string::npos; pos = target.find(from, pos + 1))
|
2014-06-07 14:15:49 +00:00
|
|
|
{
|
2015-02-02 10:21:35 +00:00
|
|
|
target = (pos ? target.substr(0, pos) + to : to) + std::string(target.c_str() + pos + from.length());
|
2014-06-07 14:15:49 +00:00
|
|
|
pos += to.length();
|
|
|
|
}
|
|
|
|
|
2015-02-04 12:16:10 +00:00
|
|
|
return target;
|
2014-06-07 14:15:49 +00:00
|
|
|
}
|
|
|
|
|
2014-11-29 13:16:53 +00:00
|
|
|
std::vector<std::string> fmt::split(const std::string& source, std::initializer_list<std::string> separators, bool is_skip_empty)
|
|
|
|
{
|
|
|
|
std::vector<std::string> result;
|
|
|
|
|
|
|
|
size_t cursor_begin = 0;
|
|
|
|
|
|
|
|
for (size_t cursor_end = 0; cursor_end < source.length(); ++cursor_end)
|
|
|
|
{
|
2016-08-13 13:36:04 +00:00
|
|
|
for (auto& separator : separators)
|
2014-11-29 13:16:53 +00:00
|
|
|
{
|
|
|
|
if (strncmp(source.c_str() + cursor_end, separator.c_str(), separator.length()) == 0)
|
|
|
|
{
|
|
|
|
std::string candidate = source.substr(cursor_begin, cursor_end - cursor_begin);
|
|
|
|
if (!is_skip_empty || !candidate.empty())
|
|
|
|
result.push_back(candidate);
|
|
|
|
|
|
|
|
cursor_begin = cursor_end + separator.length();
|
2016-08-13 13:36:04 +00:00
|
|
|
cursor_end = cursor_begin - 1;
|
2014-11-29 13:16:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cursor_begin != source.length())
|
|
|
|
{
|
|
|
|
result.push_back(source.substr(cursor_begin));
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::move(result);
|
2014-11-30 11:18:17 +00:00
|
|
|
}
|
|
|
|
|
2015-10-13 17:32:08 +00:00
|
|
|
std::string fmt::trim(const std::string& source, const std::string& values)
|
|
|
|
{
|
|
|
|
std::size_t begin = source.find_first_not_of(values);
|
|
|
|
|
|
|
|
if (begin == source.npos)
|
2016-08-13 13:36:04 +00:00
|
|
|
return {};
|
2015-10-13 17:32:08 +00:00
|
|
|
|
|
|
|
return source.substr(begin, source.find_last_not_of(values) + 1);
|
|
|
|
}
|
|
|
|
|
2016-04-26 22:27:24 +00:00
|
|
|
std::string fmt::to_upper(const std::string& string)
|
2016-02-01 21:55:43 +00:00
|
|
|
{
|
|
|
|
std::string result;
|
2016-04-26 22:27:24 +00:00
|
|
|
result.resize(string.size());
|
|
|
|
std::transform(string.begin(), string.end(), result.begin(), ::toupper);
|
2016-02-01 21:55:43 +00:00
|
|
|
return result;
|
2015-02-04 12:16:10 +00:00
|
|
|
}
|
2015-12-22 19:24:35 +00:00
|
|
|
|
2016-08-13 13:36:04 +00:00
|
|
|
bool fmt::match(const std::string& source, const std::string& mask)
|
2015-12-22 19:24:35 +00:00
|
|
|
{
|
|
|
|
std::size_t source_position = 0, mask_position = 0;
|
|
|
|
|
|
|
|
for (; source_position < source.size() && mask_position < mask.size(); ++mask_position, ++source_position)
|
|
|
|
{
|
|
|
|
switch (mask[mask_position])
|
|
|
|
{
|
|
|
|
case '?': break;
|
|
|
|
|
|
|
|
case '*':
|
|
|
|
for (std::size_t test_source_position = source_position; test_source_position < source.size(); ++test_source_position)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|