rpcs3/Utilities/StrUtil.h

140 lines
3.3 KiB
C
Raw Normal View History

#pragma once
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <functional>
// Copy null-terminated string from std::string to char array with truncation
2016-08-13 13:36:04 +00:00
template <std::size_t N>
inline void strcpy_trunc(char (&dst)[N], const std::string& src)
{
const std::size_t count = src.size() >= N ? N - 1 : src.size();
std::memcpy(dst, src.c_str(), count);
dst[count] = '\0';
}
// Copy null-terminated string from char array to another char array with truncation
2016-08-13 13:36:04 +00:00
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';
}
template <std::size_t N>
inline bool ends_with(const std::string& src, const char (&end)[N])
{
return src.size() >= N - 1 && src.compare(src.size() - (N - 1), N - 1, end, N - 1) == 0;
}
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-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])
{
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])
{
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>
std::string merge(const T& source, const std::string& separator)
{
if (!source.size())
{
2016-08-13 13:36:04 +00:00
return {};
}
std::string result;
2016-08-13 13:36:04 +00:00
auto it = source.begin();
auto end = source.end();
for (--end; it != end; ++it)
{
result += *it + separator;
}
return result + source.back();
}
2016-08-13 13:36:04 +00:00
template <typename T>
std::string merge(std::initializer_list<T> sources, const std::string& separator)
{
if (!sources.size())
{
2016-08-13 13:36:04 +00:00
return {};
}
std::string result;
bool first = true;
2016-08-13 13:36:04 +00:00
for (auto& v : sources)
{
if (first)
{
result = fmt::merge(v, separator);
2016-08-13 13:36:04 +00:00
first = false;
}
else
{
result += separator + fmt::merge(v, separator);
}
}
return result;
}
std::string to_upper(const std::string& string);
2016-08-13 13:36:04 +00:00
bool match(const std::string& source, const std::string& mask);
}