23#include <system_error>
25#include <fmt/format.h>
37inline std::string&
operator+=(std::string& a, std::string_view b)
43inline std::string
operator+(
const std::string& a, std::string_view b)
53template<
typename T,
size_t N>
54std::string
to_string(
const std::array<T, N>& arr, std::string_view delimiter =
", ")
56 static_assert(
N != 0);
58 for (
size_t i = 0; i <
N - 1; ++i)
60 ret += std::to_string(arr[i]);
63 ret += std::to_string(arr[
N - 1]);
71template<
size_t W,
size_t H>
72std::array<std::string, H>
to_string(
const std::array<std::array<std::string, W>,
H>& arr)
75 std::array<size_t, W> mul_col{};
76 for (
size_t j = 0; j <
H; ++j)
78 for (
size_t i = 0; i <
W; ++i)
80 if (arr[j][i].size() > mul_col[i])
81 mul_col[i] = arr[j][i].size();
85 std::array<std::string, H> ret;
86 for (
size_t i = 0; i <
W; ++i)
88 std::string fmt = std::string(
"{:<") + std::to_string(mul_col[i] + 1) +
"}";
89 for (
size_t j = 0; j <
H; ++j)
91 ret[j] += fmt::vformat(fmt, fmt::make_format_args(arr[j][i]));
98std::string
to_string(std::error_code code);
103template<
size_t N = 2>
108 if constexpr (
N == 2)
110 return fmt::format(
"{:.2f}", v);
119std::string
to_string(
const std::array<std::byte, N>& buf,
bool add_tag =
false)
125 for (
size_t i = 0; i <
N; ++i)
127 ret += fmt::format(
"{:02x}", (
unsigned char)buf[i]);
140template<std::
size_t N>
141std::array<int, N>
toPoint(std::string_view str)
143 std::vector<std::string> vec;
144 split(std::string(str),
',', vec);
145 assert(vec.size() == N);
146 return { stoi(vec[0]), stoi(vec[1]) };
149inline Point2 toPoint2(std::string_view str)
151 return toPoint<2>(str);
157constexpr std::string_view GetNotFileName()
159 return R
"(<>/\|:"*?)";
165inline void EraseNotFileName(std::string& name)
167 Erase::Match(name, std::string{ String::GetNotFileName() });
173inline void ToLower(std::string& str)
175 std::transform(str.begin(), str.end(), str.begin(), ::tolower);
181inline std::string ToLower(
const std::string& str)
183 std::string ret = str;
184 std::transform(ret.begin(), ret.end(), ret.begin(), ::tolower);
191inline bool EqualToLower(std::string_view a, std::string_view b)
193 if (a.size() != b.size())
197 for (
size_t i = 0; i < a.size(); ++i)
199 if (std::tolower(a[i]) != std::tolower(b[i]))
208inline std::string Splicing(
const std::vector<std::string>& vec,
const std::string& delimiter,
const std::string& end)
216 for (
size_t i = 0; i < vec.size() - 1; ++i)
221 ret += vec[vec.size() - 1];
229inline std::string GetUtf8ByWchar(
wchar_t wc,
size_t offset)
232 std::u32string s{ (char32_t)(wc + offset) };
234 return CodeCvt::Utf32ToUtf8(s);
266std::string Sprintf(
const char* format, ...);
272std::string SplitIntegerToThousands(T integer)
280inline std::vector<std::string> Split(
const std::string& str,
char d)
282 std::vector<std::string> ret;
290inline std::vector<std::string> Split(
const std::string& str,
const std::string& d)
292 std::vector<std::string> ret;
301inline void EraseExtension(std::string& path_name)
303 if (Erase::TailFromLast(path_name,
'.'))
304 path_name.pop_back();
307inline std::string EraseExtension(std::string_view path_name)
309 std::string ret = std::string{ path_name };
320inline void ReplaceExtension(std::string& path_name, std::string_view fmt)
322 if (Erase::TailFromLast(path_name,
'.'))
328 path_name += std::string{ fmt };
331inline std::string ReplaceExtension(std::string_view path_name, std::string_view fmt)
333 std::string ret = std::string{ path_name };
334 ReplaceExtension(ret, fmt);
342inline void EraseFilename(std::string& path_name)
344 if (!Erase::TailFromLast(path_name,
'/'))
348inline std::string EraseFilename(std::string_view path_name)
350 std::string ret = std::string{ path_name };
361inline bool IsRelativePath(std::string_view path_name)
363 if (path_name.empty())
366 if (path_name[0] ==
'/')
369 size_t len = std::min(path_name.size(), (
size_t)12);
370 for (
size_t i = 0; i < len; ++i)
372 if (path_name[i] ==
':')
389 std::string_view path_root, std::string_view path_name,
390 std::string& path_real, std::string& res_name,
391 std::string_view name =
"");
399std::string GetResName(
400 std::string_view path_root,
401 std::string_view path_name);
410std::string CvtRelativePath(std::string path_cur, std::string_view path);
416inline void CvtPathSlash(std::string& path)
418 Every::Replace(path,
'\\',
'/');
424inline bool HasNumber(std::string_view str)
426 return Any::Yes(str, isdigit);
432inline bool HasLetter(std::string_view str)
434 return Any::Yes(str, isalpha);
T r_insert_per_num(const T &container, V v, size_t per_num)
从后面往前,隔n个元素就插入1个值,首尾不会出现分割符
std::string to_string(const SpriteID &id)
void split_sub(const T &container, const T &delimiter, std::vector< T > &ret)
以 子串 分割 容器
std::array< int, 2 > Point2
constexpr std::array< Point::value_type, N > toPoint(const std::array< T, N > &a)
void split(const T &container, V delimiter, std::vector< T > &ret)
以 元素 分割 容器
constexpr std::array< T, N > operator+(const std::array< T, N > &a, const std::array< T, N > &b)
constexpr std::array< T, N > & operator+=(std::array< T, N > &a, const std::array< T, N > &b)
void log_err0(std::string_view str, const std::source_location location=std::source_location::current())