rpcs3/Utilities/StrUtil.h

172 lines
3.9 KiB
C
Raw Normal View History

#pragma once
#include <cstring>
#include <string>
#include <vector>
#include <functional>
#include <string_view>
#include "util/types.hpp"
2020-03-26 20:48:56 +00:00
#ifdef _WIN32
std::wstring utf8_to_wchar(std::string_view src);
std::string wchar_to_utf8(std::wstring_view src);
2020-03-26 20:48:56 +00:00
#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)
{
2020-12-18 07:39:54 +00:00
const usz count = std::size(src) >= std::size(dst) ? std::size(dst) - 1 : std::size(src);
2020-03-04 14:08:40 +00:00
std::memcpy(std::data(dst), std::data(src), count);
std::memset(std::data(dst) + count, 0, std::size(dst) - count);
}
2021-06-19 10:40:59 +00:00
// Convert string to signed integer
bool try_to_int64(s64* out, std::string_view value, s64 min, s64 max);
2021-06-19 10:40:59 +00:00
// Convert string to unsigned integer
bool try_to_uint64(u64* out, std::string_view value, u64 min, u64 max);
2021-06-19 10:40:59 +00:00
namespace fmt
{
std::string replace_all(std::string_view src, std::string_view from, std::string_view to, usz count = -1);
2020-12-18 07:39:54 +00:00
template <usz list_size>
std::string replace_all(std::string src, const std::pair<std::string_view, std::string> (&list)[list_size])
{
2020-12-18 07:39:54 +00:00
for (usz pos = 0; pos < src.length(); ++pos)
{
2020-12-18 07:39:54 +00:00
for (usz i = 0; i < list_size; ++i)
{
2020-12-18 07:39:54 +00:00
const usz comp_length = list[i].first.length();
if (src.length() - pos < comp_length)
{
continue;
}
if (src.substr(pos, comp_length) == list[i].first)
{
src.erase(pos, comp_length);
src.insert(pos, list[i].second.data(), list[i].second.length());
pos += list[i].second.length() - 1;
break;
}
}
}
return src;
}
2020-12-18 07:39:54 +00:00
template <usz list_size>
std::string replace_all(std::string src, const std::pair<std::string_view, std::function<std::string()>> (&list)[list_size])
{
2020-12-18 07:39:54 +00:00
for (usz pos = 0; pos < src.length(); ++pos)
{
2020-12-18 07:39:54 +00:00
for (usz i = 0; i < list_size; ++i)
{
2020-12-18 07:39:54 +00:00
const usz comp_length = list[i].first.length();
if (src.length() - pos < comp_length)
{
continue;
}
if (src.substr(pos, comp_length) == list[i].first)
{
src.erase(pos, comp_length);
auto replacement = list[i].second();
src.insert(pos, replacement);
pos += replacement.length() - 1;
break;
}
}
}
return src;
}
2022-03-23 19:53:18 +00:00
static inline
std::string replace_all(std::string src, const std::vector<std::pair<std::string, std::string>>& list)
{
for (usz pos = 0; pos < src.length(); ++pos)
{
for (usz i = 0; i < list.size(); ++i)
{
const usz comp_length = list[i].first.length();
if (src.length() - pos < comp_length)
{
continue;
}
if (src.substr(pos, comp_length) == list[i].first)
{
src.erase(pos, comp_length);
src.insert(pos, list[i].second);
pos += list[i].second.length() - 1;
break;
}
}
}
return src;
}
2021-01-05 15:34:35 +00:00
std::vector<std::string> split(std::string_view source, std::initializer_list<std::string_view> 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.empty())
{
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 += std::string{*it} + separator;
}
return result + std::string{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);
std::string to_lower(const std::string& string);
2016-08-13 13:36:04 +00:00
bool match(const std::string& source, const std::string& mask);
}