483 lines
10 KiB
C++
483 lines
10 KiB
C++
/**
|
|
* @file dl_string.h
|
|
* @brief 字符串相关
|
|
*
|
|
*
|
|
* @version 1.0
|
|
* @author lveyou
|
|
* @date 23-06-23
|
|
*
|
|
* @note vs设置多字节编码,并且定义utf-8
|
|
* @note 字节流请优先使用Buffer类
|
|
*
|
|
* @note string的size和length相同。我们需要保证结尾不包含0的
|
|
* @note 现在字符串统一使用基于string的 utf-8编码,在需要对字符拆分处理时再转换到utf-32编码
|
|
* @note wstring_convert在C++17已经弃用,使用iconv来转换编码,在CodeCvt文件
|
|
*
|
|
* @note to_string进行基本类型转换,toString作为复杂类型转换,format多个变量时使用
|
|
* @note format非编译期时使用:vformat(fmt, std::make_format_args(v));
|
|
*/
|
|
#pragma once
|
|
#include <array>
|
|
#include <string>
|
|
#include <system_error>
|
|
|
|
#include <fmt/format.h>
|
|
|
|
#include "memory/dl_code_cvt.h"
|
|
#include "dl_range.h"
|
|
#include "math/dl_algorithm.h"
|
|
#include "io/dl_log.h"
|
|
#include "base/dl_type_def.h"
|
|
#include "base/dl_vector2.h"
|
|
#include "base/dl_array2.h"
|
|
|
|
namespace dl
|
|
{
|
|
|
|
//-----------------------------字符串连接-------------------
|
|
inline std::string& operator+=(std::string& a, std::string_view b)
|
|
{
|
|
a.append(b);
|
|
return a;
|
|
}
|
|
|
|
inline std::string operator+(const std::string& a, std::string_view b)
|
|
{
|
|
std::string ret = a;
|
|
return ret += b;
|
|
}
|
|
/**
|
|
* @brief to_string包装
|
|
* @note 小数默认两位
|
|
*/
|
|
template<typename T>
|
|
std::string to_string(const T& v)
|
|
{
|
|
if constexpr (std::is_floating_point_v<T>)
|
|
{
|
|
return fmt::format("{:.2f}", v);
|
|
}
|
|
else
|
|
return std::to_string(v);
|
|
}
|
|
|
|
/**
|
|
* @brief array通用to_string
|
|
*/
|
|
template<typename T, size_t N>
|
|
std::string to_string(const std::array<T, N>& arr, std::string_view delimiter = ", ")
|
|
{
|
|
static_assert(N != 0);
|
|
std::string ret;
|
|
for (size_t i = 0; i < N - 1; ++i)
|
|
{
|
|
ret += dl::to_string(arr[i]);
|
|
ret += delimiter;
|
|
}
|
|
ret += dl::to_string(arr[N - 1]);
|
|
return ret;
|
|
}
|
|
|
|
//! 错误码信息
|
|
std::string to_string(std::error_code code);
|
|
|
|
|
|
|
|
/**
|
|
* @brief byte特化(转为16进制)
|
|
*/
|
|
template<size_t N>
|
|
std::string to_string(const std::array<std::byte, N>& buf, bool add_tag = false)
|
|
{
|
|
std::string ret;
|
|
if (add_tag)
|
|
ret += "0x";
|
|
|
|
for (size_t i = 0; i < N; ++i)
|
|
{
|
|
ret += fmt::format("{:02x}", (unsigned char)buf[i]);
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
|
|
namespace String
|
|
{
|
|
|
|
|
|
/**
|
|
* @brief 字符串 -> Point
|
|
*/
|
|
template<std::size_t N>
|
|
std::array<int, N> toPoint(std::string_view str)
|
|
{
|
|
std::vector<std::string> vec;
|
|
split(std::string(str), ',', vec);
|
|
assert(vec.size() == N);
|
|
return { stoi(vec[0]), stoi(vec[1]) };
|
|
}
|
|
|
|
inline Point2 toPoint2(std::string_view str)
|
|
{
|
|
return toPoint<2>(str);
|
|
}
|
|
|
|
/**
|
|
* @brief 返回不能作为文件名的字符
|
|
*/
|
|
constexpr std::string_view GetNotFileName()
|
|
{
|
|
return R"(<>/\|:"*?)";
|
|
}
|
|
|
|
/**
|
|
* @brief 移除不能作为文件名的字符
|
|
*/
|
|
inline void EraseNotFileName(std::string& name)
|
|
{
|
|
Erase::Match(name, std::string{ String::GetNotFileName() });
|
|
}
|
|
|
|
/**
|
|
* @brief 将字符串字母变为小写
|
|
*/
|
|
inline void ToLower(std::string& str)
|
|
{
|
|
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
|
|
}
|
|
|
|
/**
|
|
* @brief 将字符串字母变为小写
|
|
*/
|
|
inline std::string ToLower(const std::string& str)
|
|
{
|
|
std::string ret = str;
|
|
std::transform(ret.begin(), ret.end(), ret.begin(), ::tolower);
|
|
return ret;
|
|
}
|
|
|
|
/**
|
|
* @brief 字符串相等,并且不区分大小写
|
|
*/
|
|
inline bool EqualToLower(std::string_view a, std::string_view b)
|
|
{
|
|
if (a.size() != b.size())
|
|
return false;
|
|
if (a.empty())
|
|
return true;
|
|
for (size_t i = 0; i < a.size(); ++i)
|
|
{
|
|
if (std::tolower(a[i]) != std::tolower(b[i]))
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* @brief 拼接字符串
|
|
*/
|
|
inline std::string Splicing(const std::vector<std::string>& vec, const std::string& delimiter, const std::string& end)
|
|
{
|
|
if (vec.empty())
|
|
return {};
|
|
if (vec.size() == 1)
|
|
return vec[0] + end;
|
|
|
|
std::string ret;
|
|
for (size_t i = 0; i < vec.size() - 1; ++i)
|
|
{
|
|
ret += vec[i];
|
|
ret += delimiter;
|
|
}
|
|
ret += vec[vec.size() - 1];
|
|
ret += end;
|
|
return ret;
|
|
}
|
|
|
|
/**
|
|
* @brief wchar_t字符返回utf8
|
|
*/
|
|
inline std::string GetUtf8ByWchar(wchar_t wc, size_t offset)
|
|
{
|
|
// 某些平台wchar_t是4字节,必须使用u32string
|
|
std::u32string s{ (char32_t)(wc + offset) };
|
|
|
|
return CodeCvt::Utf32ToUtf8(s);
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief 返回ASCII码表,通过Text直接显示
|
|
*/
|
|
//string GetAscii()
|
|
//{
|
|
// string str_ascii = format("ascii表:\n0 -> /0[t_8]");
|
|
// for (char ch = 1; ch < 128; ++ch)
|
|
// {
|
|
// if (ch == '\n')
|
|
// str_ascii += format("{} -> {}[t_8]", to_string(ch), "/n");
|
|
// else if (ch == '\t')
|
|
// str_ascii += format("{} -> {}[t_8]", to_string(ch), "/t");
|
|
// else if (ch == '[')
|
|
// str_ascii += format("{} -> {}[t_8]", to_string(ch), "[l]");
|
|
// else if (ch == ']')
|
|
// str_ascii += format("{} -> {}[t_8]", to_string(ch), "[r]");
|
|
// else
|
|
// str_ascii += format("{} -> {}[t_8]", to_string(ch), char(ch));
|
|
//
|
|
// if ((ch - 1) % 6 == 0)
|
|
// str_ascii += "\n";
|
|
// }
|
|
// return str_ascii;
|
|
//}
|
|
|
|
/**
|
|
* @brief pintf替代品,一般情况不使用
|
|
*/
|
|
std::string Sprintf(const char* format, ...);
|
|
|
|
/**
|
|
* @brief 将整数转为千位分隔符的字符串。
|
|
*/
|
|
template<typename T>
|
|
std::string SplitIntegerToThousands(T integer)
|
|
{
|
|
return r_insert_per_num(to_string(integer), ',', 3);
|
|
}
|
|
|
|
/**
|
|
* @brief 分割字符串
|
|
*/
|
|
inline std::vector<std::string> Split(const std::string& str, char d)
|
|
{
|
|
std::vector<std::string> ret;
|
|
split(str, d, ret);
|
|
return ret;
|
|
}
|
|
|
|
/**
|
|
* @brief 分割字符串
|
|
*/
|
|
inline std::vector<std::string> Split(const std::string& str, const std::string& d)
|
|
{
|
|
std::vector<std::string> ret;
|
|
split_sub(str, d, ret);
|
|
return ret;
|
|
}
|
|
|
|
/**
|
|
* @brief 移除末尾格式,移除后不包含.
|
|
* @note 例如 "img/dog.ani.png" -> "img/dot.ani"
|
|
*/
|
|
inline void EraseExtension(std::string& path_name)
|
|
{
|
|
if (Erase::TailFromLast(path_name, '.'))
|
|
path_name.pop_back();
|
|
}
|
|
[[nodiscard]]
|
|
inline std::string EraseExtension(std::string_view path_name)
|
|
{
|
|
std::string ret = std::string{ path_name };
|
|
EraseExtension(ret);
|
|
return ret;
|
|
}
|
|
|
|
/**
|
|
* @brief 替换后缀格式,没有则添加
|
|
* @param[in] fmt 格式,不含点号
|
|
* @note 例如 "img/dog.ani.png" -> "img/dot.ani.jpg"
|
|
* @note 例如 "img/dog" -> "img/dot.jpg"
|
|
*/
|
|
inline void ReplaceExtension(std::string& path_name, std::string_view fmt)
|
|
{
|
|
if (Erase::TailFromLast(path_name, '.'))
|
|
{
|
|
path_name += fmt;
|
|
return;
|
|
}
|
|
path_name += ".";
|
|
path_name += std::string{ fmt };
|
|
}
|
|
[[nodiscard]]
|
|
inline std::string ReplaceExtension(std::string_view path_name, std::string_view fmt)
|
|
{
|
|
std::string ret = std::string{ path_name };
|
|
ReplaceExtension(ret, fmt);
|
|
return ret;
|
|
}
|
|
|
|
/**
|
|
* @brief 移除 路径末尾名称(返回文件所在目录,移除后末尾为/)
|
|
* @note 例如 "img/dog.ani.png" -> "img/"
|
|
*/
|
|
inline void EraseFilename(std::string& path_name)
|
|
{
|
|
if (!Erase::TailFromLast(path_name, '/'))
|
|
log_err0("不是一个有效路径!");
|
|
}
|
|
[[nodiscard]]
|
|
inline std::string EraseFilename(std::string_view path_name)
|
|
{
|
|
std::string ret = std::string{ path_name };
|
|
EraseFilename(ret);
|
|
return ret;
|
|
}
|
|
|
|
/**
|
|
* @brief 相对路径返回真
|
|
* @param[in] path_name 路径名字
|
|
* @note 斜杠起头是绝对路径,代表根目录
|
|
*/
|
|
|
|
inline bool IsRelativePath(std::string_view path_name)
|
|
{
|
|
if (path_name.empty())
|
|
return true;
|
|
|
|
if (path_name[0] == '/')
|
|
return false;
|
|
|
|
size_t len = std::min(path_name.size(), (size_t)12);
|
|
for (size_t i = 0; i < len; ++i)
|
|
{
|
|
if (path_name[i] == ':')
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief 返回真实路径、资源名
|
|
* @param[in] path_root 资源根目录
|
|
* @param[in] path_name 绝对路径 或 资源路径
|
|
* @param[out] path_real 文件路径(可直接加载)
|
|
* @param[out] res_name 资源名
|
|
* @param[in] name 手动命名
|
|
* @note 由路径斜杠变为点(.),因为斜杠不能作为文件夹名称
|
|
*/
|
|
bool GetPathAndRes(
|
|
std::string_view path_root, std::string_view path_name,
|
|
std::string& path_real, std::string& res_name,
|
|
std::string_view name = "");
|
|
|
|
|
|
/**
|
|
* @brief 资源根目录、完整相对路径, -> 解析为资源名字
|
|
* @param[in] path_root 资源根目录
|
|
* @param[in] path_name 完整相对路径
|
|
*/
|
|
std::string GetResName(
|
|
std::string_view path_root,
|
|
std::string_view path_name);
|
|
|
|
|
|
/**
|
|
* @brief 相对路径转换,去除./和../
|
|
* @param[in] path 路径
|
|
* @param[in] path_cur 当前路径
|
|
* @note 此函数不操作文件目录,只是通过当前路径来转换
|
|
*/
|
|
std::string CvtRelativePath(std::string path_cur, std::string_view path);
|
|
|
|
/**
|
|
* @brief 路径斜杠统一为/
|
|
* @param[in] path 路径
|
|
*/
|
|
inline void CvtPathSeparator(std::string& path)
|
|
{
|
|
Every::Replace(path, '\\', '/');
|
|
}
|
|
|
|
/**
|
|
* @brief 是否含有数字(十进制)
|
|
*/
|
|
inline bool HasNumber(std::string_view str)
|
|
{
|
|
return Any::Yes(str, isdigit);
|
|
}
|
|
|
|
/**
|
|
* @brief 是否含有字母
|
|
*/
|
|
inline bool HasLetter(std::string_view str)
|
|
{
|
|
return Any::Yes(str, isalpha);
|
|
}
|
|
/**
|
|
* @brief 对2维string进行排版(保证列对齐)
|
|
* @note 例如:a, bcde, cd
|
|
*/
|
|
template<size_t W, size_t H>
|
|
std::array<std::string, H> FormatString2(const Array2<std::string, W, H>& arr)
|
|
{
|
|
//每一列得到一个最大宽度
|
|
std::array<size_t, W> mul_col{};
|
|
for (size_t j = 0; j < H; ++j)
|
|
{
|
|
for (size_t i = 0; i < W; ++i)
|
|
{
|
|
if (arr[j][i].size() > mul_col[i])
|
|
mul_col[i] = arr[j][i].size();
|
|
}
|
|
}
|
|
//合并每行
|
|
std::array<std::string, H> ret;
|
|
for (size_t i = 0; i < W; ++i)
|
|
{
|
|
std::string fmt = std::string("{:<") + std::to_string(mul_col[i] + 1) + "}";
|
|
for (size_t j = 0; j < H; ++j)
|
|
{
|
|
ret[j] += fmt::vformat(fmt, fmt::make_format_args(arr[j][i]));
|
|
}
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
/**
|
|
* @brief 对2维string进行排版(保证列对齐)
|
|
* @note 例如:a, bcde, cd
|
|
*/
|
|
std::vector<std::string> FormatString2(const Vector2<std::string>& arr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
namespace dl
|
|
{
|
|
|
|
|
|
|
|
//#if (DL_PLATFORM == DL_PLATFORM_WIN32)
|
|
///**
|
|
//* @brief array的format特化
|
|
//* @note
|
|
//*/
|
|
//template<typename T, size_t N, typename CharT>
|
|
//struct std::formatter<std::array<T, N>, CharT> : std::formatter<std::string, CharT>
|
|
//{
|
|
// template <typename FormatParseContext>
|
|
// auto parse(FormatParseContext& pc)
|
|
// {
|
|
// return pc.end();
|
|
// }
|
|
//
|
|
// template<typename FormatContext>
|
|
// auto format(array<T, N> t, FormatContext& fc)
|
|
// {
|
|
// if constexpr (N == 2)
|
|
// return std::formatter<std::string, CharT>::format(
|
|
// std::format("{}, {}", t[0], t[1]), fc);
|
|
// if constexpr (N == 3)
|
|
// return std::formatter<std::string, CharT>::format(
|
|
// std::format("{}, {}, {}", t[0], t[1], t[2]), fc);
|
|
// if constexpr (N == 4)
|
|
// return std::formatter<std::string, CharT>::format(
|
|
// std::format("{}, {}, {}, {}", t[0], t[1], t[2], t[3]), fc);
|
|
// }
|
|
//};
|
|
//#endif
|
|
} |