2014-04-01 00:33:55 +00:00
|
|
|
#pragma once
|
2015-01-12 18:12:06 +00:00
|
|
|
|
2014-08-29 19:51:11 +00:00
|
|
|
class wxString;
|
2014-04-01 00:33:55 +00:00
|
|
|
|
2015-08-12 18:38:17 +00:00
|
|
|
#if defined(_MSC_VER) && _MSC_VER <= 1800
|
2014-04-01 00:33:55 +00:00
|
|
|
#define snprintf _snprintf
|
|
|
|
#endif
|
|
|
|
|
2015-01-12 18:12:06 +00:00
|
|
|
namespace fmt
|
|
|
|
{
|
2015-08-12 18:38:17 +00:00
|
|
|
//struct empty_t{};
|
2014-04-01 00:33:55 +00:00
|
|
|
|
2015-08-12 18:38:17 +00:00
|
|
|
//extern const std::string placeholder;
|
2014-04-01 00:33:55 +00:00
|
|
|
|
2014-05-02 06:30:32 +00:00
|
|
|
template <typename T>
|
|
|
|
std::string AfterLast(const std::string& source, T searchstr)
|
|
|
|
{
|
|
|
|
size_t search_pos = source.rfind(searchstr);
|
|
|
|
search_pos = search_pos == std::string::npos ? 0 : search_pos;
|
|
|
|
return source.substr(search_pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
std::string BeforeLast(const std::string& source, T searchstr)
|
|
|
|
{
|
|
|
|
size_t search_pos = source.rfind(searchstr);
|
|
|
|
search_pos = search_pos == std::string::npos ? 0 : search_pos;
|
|
|
|
return source.substr(0, search_pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
std::string AfterFirst(const std::string& source, T searchstr)
|
|
|
|
{
|
|
|
|
size_t search_pos = source.find(searchstr);
|
|
|
|
search_pos = search_pos == std::string::npos ? 0 : search_pos;
|
|
|
|
return source.substr(search_pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
std::string BeforeFirst(const std::string& source, T searchstr)
|
|
|
|
{
|
|
|
|
size_t search_pos = source.find(searchstr);
|
|
|
|
search_pos = search_pos == std::string::npos ? 0 : search_pos;
|
|
|
|
return source.substr(0, search_pos);
|
|
|
|
}
|
|
|
|
|
2014-04-01 00:33:55 +00:00
|
|
|
// write `fmt` from `pos` to the first occurence of `fmt::placeholder` to
|
|
|
|
// the stream `os`. Then write `arg` to to the stream. If there's no
|
|
|
|
// `fmt::placeholder` after `pos` everything in `fmt` after pos is written
|
|
|
|
// to `os`. Then `arg` is written to `os` after appending a space character
|
2015-04-19 22:26:28 +00:00
|
|
|
//template<typename T>
|
|
|
|
//empty_t write(const std::string &fmt, std::ostream &os, std::string::size_type &pos, T &&arg)
|
|
|
|
//{
|
|
|
|
// std::string::size_type ins = fmt.find(placeholder, pos);
|
|
|
|
|
|
|
|
// if (ins == std::string::npos)
|
|
|
|
// {
|
|
|
|
// os.write(fmt.data() + pos, fmt.size() - pos);
|
|
|
|
// os << ' ' << arg;
|
|
|
|
|
|
|
|
// pos = fmt.size();
|
|
|
|
// }
|
|
|
|
// else
|
|
|
|
// {
|
|
|
|
// os.write(fmt.data() + pos, ins - pos);
|
|
|
|
// os << arg;
|
|
|
|
|
|
|
|
// pos = ins + placeholder.size();
|
|
|
|
// }
|
|
|
|
// return{};
|
|
|
|
//}
|
2014-04-01 00:33:55 +00:00
|
|
|
|
|
|
|
// typesafe version of a sprintf-like function. Returns the printed to
|
|
|
|
// string. To mark positions where the arguments are supposed to be
|
|
|
|
// inserted use `fmt::placeholder`. If there's not enough placeholders
|
|
|
|
// the rest of the arguments are appended at the end, seperated by spaces
|
2015-04-19 22:26:28 +00:00
|
|
|
//template<typename ... Args>
|
|
|
|
//std::string SFormat(const std::string &fmt, Args&& ... parameters)
|
|
|
|
//{
|
|
|
|
// std::ostringstream os;
|
|
|
|
// std::string::size_type pos = 0;
|
|
|
|
// std::initializer_list<empty_t> { write(fmt, os, pos, parameters)... };
|
|
|
|
|
|
|
|
// if (!fmt.empty())
|
|
|
|
// {
|
|
|
|
// os.write(fmt.data() + pos, fmt.size() - pos);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// std::string result = os.str();
|
|
|
|
// return result;
|
|
|
|
//}
|
2014-04-01 00:33:55 +00:00
|
|
|
|
2014-06-07 14:15:49 +00:00
|
|
|
std::string replace_first(const std::string& src, const std::string& from, const std::string& to);
|
2015-02-02 10:21:35 +00:00
|
|
|
std::string replace_all(const std::string &src, const std::string& from, const std::string& to);
|
2014-06-07 14:15:49 +00: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 15:14:22 +00:00
|
|
|
src = (pos ? src.substr(0, pos) + list[i].second : list[i].second) + src.substr(pos + comp_length);
|
2014-06-08 14:52:35 +00:00
|
|
|
pos += list[i].second.length() - 1;
|
2014-06-07 14:15:49 +00: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 15:14:22 +00:00
|
|
|
src = (pos ? src.substr(0, pos) + list[i].second() : list[i].second()) + src.substr(pos + comp_length);
|
2014-06-08 14:52:35 +00:00
|
|
|
pos += list[i].second().length() - 1;
|
2014-06-07 14:15:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return src;
|
|
|
|
}
|
|
|
|
|
2015-09-26 20:46:04 +00:00
|
|
|
std::string to_hex(u64 value, u64 count = 1);
|
2015-01-19 13:31:02 +00:00
|
|
|
std::string to_udec(u64 value);
|
|
|
|
std::string to_sdec(s64 value);
|
|
|
|
|
2015-09-17 22:41:14 +00:00
|
|
|
template<typename T, bool is_enum = std::is_enum<T>::value> struct unveil
|
2015-01-18 22:54:56 +00:00
|
|
|
{
|
2015-06-13 01:31:45 +00:00
|
|
|
using result_type = T;
|
2015-01-18 22:54:56 +00:00
|
|
|
|
2015-05-28 15:14:22 +00:00
|
|
|
force_inline static result_type get_value(const T& arg)
|
2015-01-18 22:54:56 +00:00
|
|
|
{
|
|
|
|
return arg;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-01-12 21:57:16 +00:00
|
|
|
template<> struct unveil<const char*, false>
|
2015-01-21 23:45:50 +00:00
|
|
|
{
|
2016-01-12 21:57:16 +00:00
|
|
|
using result_type = const char* const;
|
2015-01-21 23:45:50 +00:00
|
|
|
|
2016-01-12 21:57:16 +00:00
|
|
|
force_inline static result_type get_value(const char* const& arg)
|
2015-01-21 23:45:50 +00:00
|
|
|
{
|
|
|
|
return arg;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-01-12 21:57:16 +00:00
|
|
|
template<std::size_t N> struct unveil<char[N], false>
|
2015-01-18 22:54:56 +00:00
|
|
|
{
|
2016-01-12 21:57:16 +00:00
|
|
|
using result_type = const char* const;
|
2015-01-18 22:54:56 +00:00
|
|
|
|
2015-05-28 15:14:22 +00:00
|
|
|
force_inline static result_type get_value(const char(&arg)[N])
|
2015-01-18 22:54:56 +00:00
|
|
|
{
|
|
|
|
return arg;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-09-17 22:41:14 +00:00
|
|
|
template<> struct unveil<std::string, false>
|
2015-01-18 22:54:56 +00:00
|
|
|
{
|
2015-06-13 01:31:45 +00:00
|
|
|
using result_type = const char*;
|
2015-01-18 22:54:56 +00:00
|
|
|
|
2015-05-28 15:14:22 +00:00
|
|
|
force_inline static result_type get_value(const std::string& arg)
|
2015-01-18 22:54:56 +00:00
|
|
|
{
|
2015-01-21 23:45:50 +00:00
|
|
|
return arg.c_str();
|
2015-01-18 22:54:56 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-09-17 22:41:14 +00:00
|
|
|
template<typename T> struct unveil<T, true>
|
2015-01-18 22:54:56 +00:00
|
|
|
{
|
2015-06-13 01:31:45 +00:00
|
|
|
using result_type = std::underlying_type_t<T>;
|
2015-01-18 22:54:56 +00:00
|
|
|
|
2015-05-28 15:14:22 +00:00
|
|
|
force_inline static result_type get_value(const T& arg)
|
2015-01-18 22:54:56 +00:00
|
|
|
{
|
|
|
|
return static_cast<result_type>(arg);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-09-17 22:41:14 +00:00
|
|
|
template<typename T, bool Se> struct unveil<se_t<T, Se>, false>
|
2015-06-21 00:17:42 +00:00
|
|
|
{
|
|
|
|
using result_type = typename unveil<T>::result_type;
|
|
|
|
|
2015-09-17 22:41:14 +00:00
|
|
|
force_inline static result_type get_value(const se_t<T, Se>& arg)
|
2015-06-21 00:17:42 +00:00
|
|
|
{
|
2015-09-17 22:41:14 +00:00
|
|
|
return unveil<T>::get_value(arg);
|
2015-06-21 00:17:42 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-01-18 22:54:56 +00:00
|
|
|
template<typename T>
|
2015-05-28 15:14:22 +00:00
|
|
|
force_inline typename unveil<T>::result_type do_unveil(const T& arg)
|
2015-01-18 22:54:56 +00:00
|
|
|
{
|
|
|
|
return unveil<T>::get_value(arg);
|
|
|
|
}
|
|
|
|
|
2015-08-12 18:38:17 +00:00
|
|
|
// Formatting function with special functionality:
|
|
|
|
//
|
|
|
|
// std::string is forced to .c_str()
|
|
|
|
// be_t<> is forced to .value() (fmt::do_unveil reverts byte order automatically)
|
|
|
|
//
|
|
|
|
// External specializations for fmt::do_unveil (can be found in another headers):
|
|
|
|
// vm::ptr, vm::bptr, ... (fmt::do_unveil) (vm_ptr.h) (with appropriate address type, using .addr() can be avoided)
|
|
|
|
// vm::ref, vm::bref, ... (fmt::do_unveil) (vm_ref.h)
|
|
|
|
//
|
2016-01-12 21:57:16 +00:00
|
|
|
template<typename... Args>
|
|
|
|
safe_buffers std::string format(const char* fmt, const Args&... args)
|
2015-08-12 18:38:17 +00:00
|
|
|
{
|
|
|
|
// fixed stack buffer for the first attempt
|
|
|
|
std::array<char, 4096> fixed_buf;
|
2015-01-18 22:54:56 +00:00
|
|
|
|
2015-08-26 23:57:00 +00:00
|
|
|
// possibly dynamically allocated buffer for the second attempt
|
2015-08-12 18:38:17 +00:00
|
|
|
std::unique_ptr<char[]> buf;
|
2015-01-12 18:12:06 +00:00
|
|
|
|
2015-08-12 18:38:17 +00:00
|
|
|
// pointer to the current buffer
|
|
|
|
char* buf_addr = fixed_buf.data();
|
2015-01-18 22:54:56 +00:00
|
|
|
|
2015-08-26 23:57:00 +00:00
|
|
|
for (std::size_t buf_size = fixed_buf.size();;)
|
2015-08-12 18:38:17 +00:00
|
|
|
{
|
2015-10-26 23:36:32 +00:00
|
|
|
#ifndef _MSC_VER
|
2015-08-12 18:38:17 +00:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wformat-security"
|
2015-10-26 23:36:32 +00:00
|
|
|
#endif
|
2015-08-13 00:42:16 +00:00
|
|
|
const std::size_t len = std::snprintf(buf_addr, buf_size, fmt, do_unveil(args)...);
|
2015-10-26 23:36:32 +00:00
|
|
|
#ifndef _MSC_VER
|
2015-08-12 18:38:17 +00:00
|
|
|
#pragma GCC diagnostic pop
|
2015-10-26 23:36:32 +00:00
|
|
|
#endif
|
2015-08-21 11:07:31 +00:00
|
|
|
if (len > INT_MAX)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("std::snprintf() failed");
|
|
|
|
}
|
|
|
|
|
2015-08-26 23:57:00 +00:00
|
|
|
if (len < buf_size)
|
2015-08-12 18:38:17 +00:00
|
|
|
{
|
|
|
|
return{ buf_addr, len };
|
|
|
|
}
|
2015-08-21 11:07:31 +00:00
|
|
|
|
2015-08-26 23:57:00 +00:00
|
|
|
buf.reset(buf_addr = new char[buf_size = len + 1]);
|
2015-08-12 18:38:17 +00:00
|
|
|
}
|
2015-01-12 18:12:06 +00:00
|
|
|
}
|
|
|
|
|
2015-09-17 22:41:14 +00:00
|
|
|
struct exception : public std::exception
|
2015-06-30 22:25:52 +00:00
|
|
|
{
|
|
|
|
std::unique_ptr<char[]> message;
|
|
|
|
|
2015-09-17 22:41:14 +00:00
|
|
|
template<typename... Args> never_inline safe_buffers exception(const char* file, int line, const char* func, const char* text, Args... args) noexcept
|
2015-06-30 22:25:52 +00:00
|
|
|
{
|
|
|
|
const std::string data = format(text, args...) + format("\n(in file %s:%d, in function %s)", file, line, func);
|
|
|
|
|
2015-08-12 18:38:17 +00:00
|
|
|
message.reset(new char[data.size() + 1]);
|
2015-06-30 22:25:52 +00:00
|
|
|
|
|
|
|
std::memcpy(message.get(), data.c_str(), data.size() + 1);
|
|
|
|
}
|
|
|
|
|
2015-09-17 22:41:14 +00:00
|
|
|
exception(const exception& other) noexcept
|
2015-06-30 22:25:52 +00:00
|
|
|
{
|
2015-09-17 22:41:14 +00:00
|
|
|
const std::size_t size = std::strlen(other.message.get());
|
2015-06-30 22:25:52 +00:00
|
|
|
|
2015-08-12 18:38:17 +00:00
|
|
|
message.reset(new char[size + 1]);
|
2015-06-30 22:25:52 +00:00
|
|
|
|
2015-09-17 22:41:14 +00:00
|
|
|
std::memcpy(message.get(), other.message.get(), size + 1);
|
2015-06-30 22:25:52 +00:00
|
|
|
}
|
|
|
|
|
2015-09-17 22:41:14 +00:00
|
|
|
virtual const char* what() const noexcept override
|
2015-06-30 22:25:52 +00:00
|
|
|
{
|
|
|
|
return message.get();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-01 00:33:55 +00:00
|
|
|
//convert a wxString to a std::string encoded in utf8
|
|
|
|
//CAUTION, only use this to interface with wxWidgets classes
|
|
|
|
std::string ToUTF8(const wxString& right);
|
|
|
|
|
|
|
|
//convert a std::string encoded in utf8 to a wxString
|
|
|
|
//CAUTION, only use this to interface with wxWidgets classes
|
2015-01-12 18:12:06 +00:00
|
|
|
wxString FromUTF8(const std::string& right);
|
2014-04-01 00:33:55 +00:00
|
|
|
|
|
|
|
//TODO: remove this after every snippet that uses it is gone
|
|
|
|
//WARNING: not fully compatible with CmpNoCase from wxString
|
|
|
|
int CmpNoCase(const std::string& a, const std::string& b);
|
|
|
|
|
2014-05-02 06:30:32 +00:00
|
|
|
//TODO: remove this after every snippet that uses it is gone
|
|
|
|
//WARNING: not fully compatible with Replace from wxString
|
|
|
|
void Replace(std::string &str, const std::string &searchterm, const std::string& replaceterm);
|
|
|
|
|
|
|
|
std::vector<std::string> rSplit(const std::string& source, const std::string& delim);
|
2014-11-29 13:16:53 +00:00
|
|
|
|
|
|
|
std::vector<std::string> split(const std::string& source, std::initializer_list<std::string> separators, bool is_skip_empty = true);
|
2015-10-13 17:32:08 +00:00
|
|
|
std::string trim(const std::string& source, const std::string& values = " \t");
|
2015-03-31 23:49:39 +00: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;
|
|
|
|
}
|
|
|
|
|
2014-11-30 11:18:17 +00:00
|
|
|
std::string tolower(std::string source);
|
2015-02-04 12:16:10 +00:00
|
|
|
std::string toupper(std::string source);
|
|
|
|
std::string escape(std::string source);
|
2015-12-22 19:24:35 +00:00
|
|
|
bool match(const std::string &source, const std::string &mask);
|
2014-04-01 00:33:55 +00:00
|
|
|
}
|