2016-04-27 00:27:24 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <functional>
|
2018-09-11 18:02:19 +02:00
|
|
|
#include <string_view>
|
2016-04-27 00:27:24 +02:00
|
|
|
|
2021-11-28 08:30:41 +01:00
|
|
|
#include "util/types.hpp"
|
|
|
|
|
2022-01-11 19:42:52 +01:00
|
|
|
std::wstring utf8_to_wchar(std::string_view src);
|
|
|
|
std::string wchar_to_utf8(std::wstring_view src);
|
2022-07-08 17:13:38 +02:00
|
|
|
std::string utf16_to_utf8(std::u16string_view src);
|
2023-01-18 03:22:21 +01:00
|
|
|
std::u16string utf8_to_utf16(std::string_view src);
|
2020-03-26 21:48:56 +01:00
|
|
|
|
2020-03-04 15:08:40 +01:00
|
|
|
// Copy null-terminated string from a std::string or a char array to a char array with truncation
|
|
|
|
template <typename D, typename T>
|
2023-05-02 17:57:39 +02:00
|
|
|
inline void strcpy_trunc(D&& dst, const T& src)
|
2016-04-27 00:27:24 +02:00
|
|
|
{
|
2022-12-17 19:21:54 +01:00
|
|
|
const usz count = std::size(src) >= std::size(dst) ? std::max<usz>(std::size(dst), 1) - 1 : std::size(src);
|
2020-03-04 15:08:40 +01:00
|
|
|
std::memcpy(std::data(dst), std::data(src), count);
|
|
|
|
std::memset(std::data(dst) + count, 0, std::size(dst) - count);
|
2016-04-27 00:27:24 +02:00
|
|
|
}
|
|
|
|
|
2021-06-19 12:40:59 +02:00
|
|
|
// Convert string to signed integer
|
2021-11-28 08:30:41 +01:00
|
|
|
bool try_to_int64(s64* out, std::string_view value, s64 min, s64 max);
|
2021-06-19 12:40:59 +02:00
|
|
|
|
|
|
|
// Convert string to unsigned integer
|
2021-11-28 08:30:41 +01:00
|
|
|
bool try_to_uint64(u64* out, std::string_view value, u64 min, u64 max);
|
2022-08-01 21:41:46 +02:00
|
|
|
|
2022-10-22 20:16:46 +02:00
|
|
|
// Convert string to float
|
|
|
|
bool try_to_float(f64* out, std::string_view value, f64 min, f64 max);
|
|
|
|
|
2023-02-24 15:26:00 +01:00
|
|
|
// Convert float to string locale independent
|
|
|
|
bool try_to_string(std::string* out, const f64& value);
|
|
|
|
|
2022-08-01 21:41:46 +02:00
|
|
|
// Get the file extension of a file path ("png", "jpg", etc.)
|
|
|
|
std::string get_file_extension(const std::string& file_path);
|
2021-06-19 12:40:59 +02:00
|
|
|
|
2016-04-27 00:27:24 +02:00
|
|
|
namespace fmt
|
|
|
|
{
|
2021-04-10 07:06:40 +02:00
|
|
|
std::string replace_all(std::string_view src, std::string_view from, std::string_view to, usz count = -1);
|
2016-04-27 00:27:24 +02:00
|
|
|
|
2020-12-18 08:39:54 +01:00
|
|
|
template <usz list_size>
|
2021-11-28 08:30:41 +01:00
|
|
|
std::string replace_all(std::string src, const std::pair<std::string_view, std::string> (&list)[list_size])
|
2016-04-27 00:27:24 +02:00
|
|
|
{
|
2020-12-18 08:39:54 +01:00
|
|
|
for (usz pos = 0; pos < src.length(); ++pos)
|
2016-04-27 00:27:24 +02:00
|
|
|
{
|
2020-12-18 08:39:54 +01:00
|
|
|
for (usz i = 0; i < list_size; ++i)
|
2016-04-27 00:27:24 +02:00
|
|
|
{
|
2020-12-18 08:39:54 +01:00
|
|
|
const usz comp_length = list[i].first.length();
|
2016-04-27 00:27:24 +02:00
|
|
|
|
|
|
|
if (src.length() - pos < comp_length)
|
2021-11-28 08:30:41 +01:00
|
|
|
{
|
2016-04-27 00:27:24 +02:00
|
|
|
continue;
|
2021-11-28 08:30:41 +01:00
|
|
|
}
|
2016-04-27 00:27:24 +02:00
|
|
|
|
|
|
|
if (src.substr(pos, comp_length) == list[i].first)
|
|
|
|
{
|
2021-11-28 08:30:41 +01:00
|
|
|
src.erase(pos, comp_length);
|
|
|
|
src.insert(pos, list[i].second.data(), list[i].second.length());
|
2016-04-27 00:27:24 +02:00
|
|
|
pos += list[i].second.length() - 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return src;
|
|
|
|
}
|
|
|
|
|
2020-12-18 08:39:54 +01:00
|
|
|
template <usz list_size>
|
2021-11-28 08:30:41 +01:00
|
|
|
std::string replace_all(std::string src, const std::pair<std::string_view, std::function<std::string()>> (&list)[list_size])
|
2016-04-27 00:27:24 +02:00
|
|
|
{
|
2020-12-18 08:39:54 +01:00
|
|
|
for (usz pos = 0; pos < src.length(); ++pos)
|
2016-04-27 00:27:24 +02:00
|
|
|
{
|
2020-12-18 08:39:54 +01:00
|
|
|
for (usz i = 0; i < list_size; ++i)
|
2016-04-27 00:27:24 +02:00
|
|
|
{
|
2020-12-18 08:39:54 +01:00
|
|
|
const usz comp_length = list[i].first.length();
|
2016-04-27 00:27:24 +02:00
|
|
|
|
|
|
|
if (src.length() - pos < comp_length)
|
2021-11-28 08:30:41 +01:00
|
|
|
{
|
2016-04-27 00:27:24 +02:00
|
|
|
continue;
|
2021-11-28 08:30:41 +01:00
|
|
|
}
|
2016-04-27 00:27:24 +02:00
|
|
|
|
|
|
|
if (src.substr(pos, comp_length) == list[i].first)
|
|
|
|
{
|
2021-11-28 08:30:41 +01:00
|
|
|
src.erase(pos, comp_length);
|
|
|
|
auto replacement = list[i].second();
|
|
|
|
src.insert(pos, replacement);
|
|
|
|
pos += replacement.length() - 1;
|
2016-04-27 00:27:24 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return src;
|
|
|
|
}
|
|
|
|
|
2022-03-23 20:53:18 +01: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 16:34:35 +01:00
|
|
|
std::vector<std::string> split(std::string_view source, std::initializer_list<std::string_view> separators, bool is_skip_empty = true);
|
2023-06-06 08:11:32 +02:00
|
|
|
std::string trim(const std::string& source, std::string_view values = " \t");
|
2024-01-01 23:59:27 +01:00
|
|
|
std::string trim_front(const std::string& source, std::string_view values = " \t");
|
2023-06-06 08:11:32 +02:00
|
|
|
void trim_back(std::string& source, std::string_view values = " \t");
|
2016-04-27 00:27:24 +02:00
|
|
|
|
2016-08-13 15:36:04 +02:00
|
|
|
template <typename T>
|
2016-04-27 00:27:24 +02:00
|
|
|
std::string merge(const T& source, const std::string& separator)
|
|
|
|
{
|
2020-04-10 20:48:32 +02:00
|
|
|
if (source.empty())
|
2016-04-27 00:27:24 +02:00
|
|
|
{
|
2016-08-13 15:36:04 +02:00
|
|
|
return {};
|
2016-04-27 00:27:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
|
2016-08-13 15:36:04 +02:00
|
|
|
auto it = source.begin();
|
2016-04-27 00:27:24 +02:00
|
|
|
auto end = source.end();
|
|
|
|
for (--end; it != end; ++it)
|
|
|
|
{
|
2018-09-11 18:02:19 +02:00
|
|
|
result += std::string{*it} + separator;
|
2016-04-27 00:27:24 +02:00
|
|
|
}
|
|
|
|
|
2018-09-11 18:02:19 +02:00
|
|
|
return result + std::string{source.back()};
|
2016-04-27 00:27:24 +02:00
|
|
|
}
|
|
|
|
|
2016-08-13 15:36:04 +02:00
|
|
|
template <typename T>
|
2016-04-27 00:27:24 +02:00
|
|
|
std::string merge(std::initializer_list<T> sources, const std::string& separator)
|
|
|
|
{
|
|
|
|
if (!sources.size())
|
|
|
|
{
|
2016-08-13 15:36:04 +02:00
|
|
|
return {};
|
2016-04-27 00:27:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
bool first = true;
|
|
|
|
|
2016-08-13 15:36:04 +02:00
|
|
|
for (auto& v : sources)
|
2016-04-27 00:27:24 +02:00
|
|
|
{
|
|
|
|
if (first)
|
|
|
|
{
|
|
|
|
result = fmt::merge(v, separator);
|
2016-08-13 15:36:04 +02:00
|
|
|
first = false;
|
2016-04-27 00:27:24 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result += separator + fmt::merge(v, separator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-04-25 03:41:03 +02:00
|
|
|
std::string to_upper(std::string_view string);
|
|
|
|
std::string to_lower(std::string_view string);
|
2016-04-27 00:27:24 +02:00
|
|
|
|
2024-01-05 09:43:26 +01:00
|
|
|
std::string truncate(std::string_view src, usz length);
|
|
|
|
|
2016-08-13 15:36:04 +02:00
|
|
|
bool match(const std::string& source, const std::string& mask);
|
2023-03-17 13:43:23 +01:00
|
|
|
|
|
|
|
struct buf_to_hexstring
|
|
|
|
{
|
2023-08-26 15:47:52 +02:00
|
|
|
buf_to_hexstring(const u8* buf, usz len, usz line_length = 16, bool with_prefix = false)
|
|
|
|
: buf(buf), len(len), line_length(line_length), with_prefix(with_prefix) {}
|
2023-03-17 13:43:23 +01:00
|
|
|
|
|
|
|
const u8* buf;
|
|
|
|
usz len;
|
2023-08-26 15:47:52 +02:00
|
|
|
usz line_length;
|
|
|
|
bool with_prefix;
|
2023-03-17 13:43:23 +01:00
|
|
|
};
|
2023-06-04 18:45:04 +02:00
|
|
|
|
|
|
|
struct string_hash
|
|
|
|
{
|
|
|
|
using hash_type = std::hash<std::string_view>;
|
|
|
|
using is_transparent = void;
|
|
|
|
|
|
|
|
std::size_t operator()(const char* str) const
|
|
|
|
{
|
|
|
|
return hash_type{}(str);
|
|
|
|
}
|
|
|
|
std::size_t operator()(std::string_view str) const
|
|
|
|
{
|
|
|
|
return hash_type{}(str);
|
|
|
|
}
|
|
|
|
std::size_t operator()(std::string const& str) const
|
|
|
|
{
|
|
|
|
return hash_type{}(str);
|
|
|
|
}
|
|
|
|
};
|
2016-04-27 00:27:24 +02:00
|
|
|
}
|