2014-04-01 02:33:55 +02:00
|
|
|
#pragma once
|
2015-01-12 19:12:06 +01:00
|
|
|
|
2016-02-01 22:55:43 +01:00
|
|
|
#include <array>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#include "Platform.h"
|
|
|
|
#include "types.h"
|
2014-04-01 02:33:55 +02:00
|
|
|
|
2015-08-12 20:38:17 +02:00
|
|
|
#if defined(_MSC_VER) && _MSC_VER <= 1800
|
2014-04-01 02:33:55 +02:00
|
|
|
#define snprintf _snprintf
|
|
|
|
#endif
|
|
|
|
|
2016-02-01 22:55:43 +01:00
|
|
|
// Copy null-terminated string from std::string to char array with truncation
|
|
|
|
template<std::size_t N>
|
|
|
|
inline void strcpy_trunc(char(&dst)[N], const std::string& src)
|
2015-01-12 19:12:06 +01:00
|
|
|
{
|
2016-02-01 22:55:43 +01:00
|
|
|
const std::size_t count = src.size() >= N ? N - 1 : src.size();
|
|
|
|
std::memcpy(dst, src.c_str(), count);
|
|
|
|
dst[count] = '\0';
|
|
|
|
}
|
2014-05-02 08:30:32 +02:00
|
|
|
|
2016-02-01 22:55:43 +01:00
|
|
|
// Copy null-terminated string from char array to another char array with truncation
|
|
|
|
template<std::size_t N, std::size_t N2>
|
|
|
|
inline void strcpy_trunc(char(&dst)[N], const char(&src)[N2])
|
|
|
|
{
|
|
|
|
const std::size_t count = N2 >= N ? N - 1 : N2;
|
|
|
|
std::memcpy(dst, src, count);
|
|
|
|
dst[count] = '\0';
|
|
|
|
}
|
2014-04-01 02:33:55 +02:00
|
|
|
|
2016-02-01 22:55:43 +01:00
|
|
|
namespace fmt
|
|
|
|
{
|
2014-06-07 16:15:49 +02:00
|
|
|
std::string replace_first(const std::string& src, const std::string& from, const std::string& to);
|
2015-02-02 11:21:35 +01:00
|
|
|
std::string replace_all(const std::string &src, const std::string& from, const std::string& to);
|
2014-06-07 16:15:49 +02:00
|
|
|
|
|
|
|
template<size_t list_size>
|
|
|
|
std::string replace_all(std::string src, const std::pair<std::string, std::string>(&list)[list_size])
|
|
|
|
{
|
|
|
|
for (size_t pos = 0; pos < src.length(); ++pos)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < list_size; ++i)
|
|
|
|
{
|
|
|
|
const size_t comp_length = list[i].first.length();
|
|
|
|
|
|
|
|
if (src.length() - pos < comp_length)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (src.substr(pos, comp_length) == list[i].first)
|
|
|
|
{
|
2015-05-28 17:14:22 +02:00
|
|
|
src = (pos ? src.substr(0, pos) + list[i].second : list[i].second) + src.substr(pos + comp_length);
|
2014-06-08 16:52:35 +02:00
|
|
|
pos += list[i].second.length() - 1;
|
2014-06-07 16:15:49 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return src;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t list_size>
|
|
|
|
std::string replace_all(std::string src, const std::pair<std::string, std::function<std::string()>>(&list)[list_size])
|
|
|
|
{
|
|
|
|
for (size_t pos = 0; pos < src.length(); ++pos)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < list_size; ++i)
|
|
|
|
{
|
|
|
|
const size_t comp_length = list[i].first.length();
|
|
|
|
|
|
|
|
if (src.length() - pos < comp_length)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (src.substr(pos, comp_length) == list[i].first)
|
|
|
|
{
|
2015-05-28 17:14:22 +02:00
|
|
|
src = (pos ? src.substr(0, pos) + list[i].second() : list[i].second()) + src.substr(pos + comp_length);
|
2014-06-08 16:52:35 +02:00
|
|
|
pos += list[i].second().length() - 1;
|
2014-06-07 16:15:49 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return src;
|
|
|
|
}
|
|
|
|
|
2015-09-26 22:46:04 +02:00
|
|
|
std::string to_hex(u64 value, u64 count = 1);
|
2015-01-19 14:31:02 +01:00
|
|
|
std::string to_udec(u64 value);
|
|
|
|
std::string to_sdec(s64 value);
|
|
|
|
|
2016-02-01 22:55:43 +01:00
|
|
|
template<typename T, typename>
|
|
|
|
struct unveil
|
2015-01-18 23:54:56 +01:00
|
|
|
{
|
2015-06-13 03:31:45 +02:00
|
|
|
using result_type = T;
|
2015-01-18 23:54:56 +01:00
|
|
|
|
2015-05-28 17:14:22 +02:00
|
|
|
force_inline static result_type get_value(const T& arg)
|
2015-01-18 23:54:56 +01:00
|
|
|
{
|
|
|
|
return arg;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-02-01 22:55:43 +01:00
|
|
|
template<>
|
|
|
|
struct unveil<const char*, void>
|
2015-01-22 00:45:50 +01:00
|
|
|
{
|
2016-01-12 22:57:16 +01:00
|
|
|
using result_type = const char* const;
|
2015-01-22 00:45:50 +01:00
|
|
|
|
2016-02-01 22:55:43 +01:00
|
|
|
static result_type get_value(const char* const& arg)
|
2015-01-22 00:45:50 +01:00
|
|
|
{
|
|
|
|
return arg;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-02-01 22:55:43 +01:00
|
|
|
template<std::size_t N>
|
|
|
|
struct unveil<char[N], void>
|
2015-01-18 23:54:56 +01:00
|
|
|
{
|
2016-01-12 22:57:16 +01:00
|
|
|
using result_type = const char* const;
|
2015-01-18 23:54:56 +01:00
|
|
|
|
2016-02-01 22:55:43 +01:00
|
|
|
static result_type get_value(const char(&arg)[N])
|
2015-01-18 23:54:56 +01:00
|
|
|
{
|
|
|
|
return arg;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-02-01 22:55:43 +01:00
|
|
|
template<>
|
|
|
|
struct unveil<std::string, void>
|
2015-01-18 23:54:56 +01:00
|
|
|
{
|
2015-06-13 03:31:45 +02:00
|
|
|
using result_type = const char*;
|
2015-01-18 23:54:56 +01:00
|
|
|
|
2016-02-01 22:55:43 +01:00
|
|
|
static result_type get_value(const std::string& arg)
|
2015-01-18 23:54:56 +01:00
|
|
|
{
|
2015-01-22 00:45:50 +01:00
|
|
|
return arg.c_str();
|
2015-01-18 23:54:56 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-02-01 22:55:43 +01:00
|
|
|
template<typename T>
|
|
|
|
struct unveil<T, std::enable_if_t<std::is_enum<T>::value>>
|
2015-01-18 23:54:56 +01:00
|
|
|
{
|
2015-06-13 03:31:45 +02:00
|
|
|
using result_type = std::underlying_type_t<T>;
|
2015-01-18 23:54:56 +01:00
|
|
|
|
2015-05-28 17:14:22 +02:00
|
|
|
force_inline static result_type get_value(const T& arg)
|
2015-01-18 23:54:56 +01:00
|
|
|
{
|
|
|
|
return static_cast<result_type>(arg);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2015-05-28 17:14:22 +02:00
|
|
|
force_inline typename unveil<T>::result_type do_unveil(const T& arg)
|
2015-01-18 23:54:56 +01:00
|
|
|
{
|
|
|
|
return unveil<T>::get_value(arg);
|
|
|
|
}
|
|
|
|
|
2016-02-01 22:55:43 +01:00
|
|
|
// Formatting function with special functionality (fmt::unveil)
|
2016-01-12 22:57:16 +01:00
|
|
|
template<typename... Args>
|
|
|
|
safe_buffers std::string format(const char* fmt, const Args&... args)
|
2015-08-12 20:38:17 +02:00
|
|
|
{
|
|
|
|
// fixed stack buffer for the first attempt
|
|
|
|
std::array<char, 4096> fixed_buf;
|
2015-01-18 23:54:56 +01:00
|
|
|
|
2015-08-27 01:57:00 +02:00
|
|
|
// possibly dynamically allocated buffer for the second attempt
|
2015-08-12 20:38:17 +02:00
|
|
|
std::unique_ptr<char[]> buf;
|
2015-01-12 19:12:06 +01:00
|
|
|
|
2015-08-12 20:38:17 +02:00
|
|
|
// pointer to the current buffer
|
|
|
|
char* buf_addr = fixed_buf.data();
|
2015-01-18 23:54:56 +01:00
|
|
|
|
2015-08-27 01:57:00 +02:00
|
|
|
for (std::size_t buf_size = fixed_buf.size();;)
|
2015-08-12 20:38:17 +02:00
|
|
|
{
|
2015-10-27 00:36:32 +01:00
|
|
|
#ifndef _MSC_VER
|
2015-08-12 20:38:17 +02:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wformat-security"
|
2015-10-27 00:36:32 +01:00
|
|
|
#endif
|
2015-08-13 02:42:16 +02:00
|
|
|
const std::size_t len = std::snprintf(buf_addr, buf_size, fmt, do_unveil(args)...);
|
2015-10-27 00:36:32 +01:00
|
|
|
#ifndef _MSC_VER
|
2015-08-12 20:38:17 +02:00
|
|
|
#pragma GCC diagnostic pop
|
2015-10-27 00:36:32 +01:00
|
|
|
#endif
|
2015-08-21 13:07:31 +02:00
|
|
|
if (len > INT_MAX)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("std::snprintf() failed");
|
|
|
|
}
|
|
|
|
|
2015-08-27 01:57:00 +02:00
|
|
|
if (len < buf_size)
|
2015-08-12 20:38:17 +02:00
|
|
|
{
|
|
|
|
return{ buf_addr, len };
|
|
|
|
}
|
2015-08-21 13:07:31 +02:00
|
|
|
|
2015-08-27 01:57:00 +02:00
|
|
|
buf.reset(buf_addr = new char[buf_size = len + 1]);
|
2015-08-12 20:38:17 +02:00
|
|
|
}
|
2015-01-12 19:12:06 +01:00
|
|
|
}
|
|
|
|
|
2016-02-01 22:55:43 +01:00
|
|
|
// Create exception of type T (std::runtime_error by default) with formatting
|
|
|
|
template<typename T = std::runtime_error, typename... Args>
|
|
|
|
never_inline safe_buffers T exception(const char* fmt, const Args&... args) noexcept(noexcept(T{ fmt }))
|
2015-07-01 00:25:52 +02:00
|
|
|
{
|
2016-02-01 22:55:43 +01:00
|
|
|
return T{ format(fmt, do_unveil(args)...).c_str() };
|
|
|
|
}
|
2014-04-01 02:33:55 +02:00
|
|
|
|
2016-02-01 22:55:43 +01:00
|
|
|
// Create exception of type T (std::runtime_error by default) without formatting
|
|
|
|
template<typename T = std::runtime_error>
|
|
|
|
safe_buffers T exception(const char* msg) noexcept(noexcept(T{ msg }))
|
|
|
|
{
|
|
|
|
return T{ msg };
|
|
|
|
}
|
2014-05-02 08:30:32 +02:00
|
|
|
|
2016-02-01 22:55:43 +01:00
|
|
|
// Narrow cast (similar to gsl::narrow) with exception message formatting
|
|
|
|
template<typename To, typename From, typename... Args>
|
|
|
|
inline auto narrow(const char* format_str, const From& value, const Args&... args) -> decltype(static_cast<To>(static_cast<From>(std::declval<To>())))
|
|
|
|
{
|
|
|
|
const auto result = static_cast<To>(value);
|
|
|
|
if (static_cast<From>(result) != value) throw fmt::exception(format_str, fmt::do_unveil(value), fmt::do_unveil(args)...);
|
|
|
|
return result;
|
|
|
|
}
|
2014-11-29 14:16:53 +01:00
|
|
|
|
|
|
|
std::vector<std::string> split(const std::string& source, std::initializer_list<std::string> separators, bool is_skip_empty = true);
|
2015-10-13 19:32:08 +02:00
|
|
|
std::string trim(const std::string& source, const std::string& values = " \t");
|
2015-04-01 01:49:39 +02:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
std::string merge(const T& source, const std::string& separator)
|
|
|
|
{
|
|
|
|
if (!source.size())
|
|
|
|
{
|
|
|
|
return{};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
|
|
|
|
auto it = source.begin();
|
|
|
|
auto end = source.end();
|
|
|
|
for (--end; it != end; ++it)
|
|
|
|
{
|
|
|
|
result += *it + separator;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result + source.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
std::string merge(std::initializer_list<T> sources, const std::string& separator)
|
|
|
|
{
|
|
|
|
if (!sources.size())
|
|
|
|
{
|
|
|
|
return{};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
bool first = true;
|
|
|
|
|
|
|
|
for (auto &v : sources)
|
|
|
|
{
|
|
|
|
if (first)
|
|
|
|
{
|
|
|
|
result = fmt::merge(v, separator);
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result += separator + fmt::merge(v, separator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-02-01 22:55:43 +01:00
|
|
|
template<typename IT>
|
|
|
|
std::string to_lower(IT _begin, IT _end)
|
|
|
|
{
|
|
|
|
std::string result; result.resize(_end - _begin);
|
|
|
|
std::transform(_begin, _end, result.begin(), ::tolower);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
std::string to_lower(const T& string)
|
|
|
|
{
|
|
|
|
return to_lower(std::begin(string), std::end(string));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename IT>
|
|
|
|
std::string to_upper(IT _begin, IT _end)
|
|
|
|
{
|
|
|
|
std::string result; result.resize(_end - _begin);
|
|
|
|
std::transform(_begin, _end, result.begin(), ::toupper);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
std::string to_upper(const T& string)
|
|
|
|
{
|
|
|
|
return to_upper(std::begin(string), std::end(string));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string escape(const std::string& source, std::initializer_list<char> more = {});
|
|
|
|
std::string unescape(const std::string& source);
|
2015-12-22 20:24:35 +01:00
|
|
|
bool match(const std::string &source, const std::string &mask);
|
2014-04-01 02:33:55 +02:00
|
|
|
}
|