2016-04-26 22:27:24 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <functional>
|
2018-09-11 16:02:19 +00:00
|
|
|
#include <string_view>
|
2016-04-26 22:27:24 +00:00
|
|
|
|
2020-03-26 20:48:56 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
std::string wchar_to_utf8(wchar_t *src);
|
|
|
|
std::string wchar_path_to_ansi_path(const std::wstring& src);
|
|
|
|
std::string utf8_path_to_ansi_path(const std::string& src);
|
|
|
|
#endif
|
|
|
|
|
2020-03-04 14:08:40 +00:00
|
|
|
// Copy null-terminated string from a std::string or a char array to a char array with truncation
|
|
|
|
template <typename D, typename T>
|
|
|
|
inline void strcpy_trunc(D& dst, const T& src)
|
2016-04-26 22:27:24 +00:00
|
|
|
{
|
2020-03-04 14:08:40 +00:00
|
|
|
const std::size_t count = std::size(src) >= std::size(dst) ? std::size(dst) - 1 : std::size(src);
|
|
|
|
std::memcpy(std::data(dst), std::data(src), count);
|
|
|
|
std::memset(std::data(dst) + count, 0, std::size(dst) - count);
|
2016-04-26 22:27:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace fmt
|
|
|
|
{
|
|
|
|
std::string replace_first(const std::string& src, const std::string& from, const std::string& to);
|
2016-08-13 13:36:04 +00:00
|
|
|
std::string replace_all(const std::string& src, const std::string& from, const std::string& to);
|
2016-04-26 22:27:24 +00:00
|
|
|
|
2016-08-13 13:36:04 +00:00
|
|
|
template <size_t list_size>
|
|
|
|
std::string replace_all(std::string src, const std::pair<std::string, std::string> (&list)[list_size])
|
2016-04-26 22:27:24 +00:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
src = (pos ? src.substr(0, pos) + list[i].second : list[i].second) + src.substr(pos + comp_length);
|
|
|
|
pos += list[i].second.length() - 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return src;
|
|
|
|
}
|
|
|
|
|
2016-08-13 13:36:04 +00:00
|
|
|
template <size_t list_size>
|
|
|
|
std::string replace_all(std::string src, const std::pair<std::string, std::function<std::string()>> (&list)[list_size])
|
2016-04-26 22:27:24 +00:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
src = (pos ? src.substr(0, pos) + list[i].second() : list[i].second()) + src.substr(pos + comp_length);
|
|
|
|
pos += list[i].second().length() - 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return src;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> split(const std::string& source, std::initializer_list<std::string> separators, bool is_skip_empty = true);
|
|
|
|
std::string trim(const std::string& source, const std::string& values = " \t");
|
|
|
|
|
2016-08-13 13:36:04 +00:00
|
|
|
template <typename T>
|
2016-04-26 22:27:24 +00:00
|
|
|
std::string merge(const T& source, const std::string& separator)
|
|
|
|
{
|
2020-04-10 18:48:32 +00:00
|
|
|
if (source.empty())
|
2016-04-26 22:27:24 +00:00
|
|
|
{
|
2016-08-13 13:36:04 +00:00
|
|
|
return {};
|
2016-04-26 22:27:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
|
2016-08-13 13:36:04 +00:00
|
|
|
auto it = source.begin();
|
2016-04-26 22:27:24 +00:00
|
|
|
auto end = source.end();
|
|
|
|
for (--end; it != end; ++it)
|
|
|
|
{
|
2018-09-11 16:02:19 +00:00
|
|
|
result += std::string{*it} + separator;
|
2016-04-26 22:27:24 +00:00
|
|
|
}
|
|
|
|
|
2018-09-11 16:02:19 +00:00
|
|
|
return result + std::string{source.back()};
|
2016-04-26 22:27:24 +00:00
|
|
|
}
|
|
|
|
|
2016-08-13 13:36:04 +00:00
|
|
|
template <typename T>
|
2016-04-26 22:27:24 +00:00
|
|
|
std::string merge(std::initializer_list<T> sources, const std::string& separator)
|
|
|
|
{
|
|
|
|
if (!sources.size())
|
|
|
|
{
|
2016-08-13 13:36:04 +00:00
|
|
|
return {};
|
2016-04-26 22:27:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
bool first = true;
|
|
|
|
|
2016-08-13 13:36:04 +00:00
|
|
|
for (auto& v : sources)
|
2016-04-26 22:27:24 +00:00
|
|
|
{
|
|
|
|
if (first)
|
|
|
|
{
|
|
|
|
result = fmt::merge(v, separator);
|
2016-08-13 13:36:04 +00:00
|
|
|
first = false;
|
2016-04-26 22:27:24 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result += separator + fmt::merge(v, separator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string to_upper(const std::string& string);
|
2018-05-04 20:49:35 +00:00
|
|
|
std::string to_lower(const std::string& string);
|
2016-04-26 22:27:24 +00:00
|
|
|
|
2016-08-13 13:36:04 +00:00
|
|
|
bool match(const std::string& source, const std::string& mask);
|
2016-04-26 22:27:24 +00:00
|
|
|
}
|