1063 lines
24 KiB
C++
1063 lines
24 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转换为字符串,
|
||
* @note toXxx作为复杂类型转换,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"
|
||
#include "memory/dl_buffer.h"
|
||
|
||
namespace dl
|
||
{
|
||
|
||
|
||
// unordered_map支持 string_view
|
||
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); }
|
||
};
|
||
|
||
//-----------------------------字符串连接-------------------
|
||
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;
|
||
}
|
||
inline std::string operator+(std::string_view a, std::string_view b)
|
||
{
|
||
std::string ret{ a };
|
||
return ret += b;
|
||
}
|
||
/**
|
||
* @brief to_string包装
|
||
* @note 小数默认两位
|
||
*/
|
||
template<typename T>
|
||
std::string to_string(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;
|
||
}
|
||
template<typename T, size_t N>
|
||
std::string to_string(const 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;
|
||
}
|
||
template<typename T, size_t N>
|
||
std::string to_string(const ArrayS2<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.x[i]);
|
||
ret += delimiter;
|
||
ret += dl::to_string(arr.y[i]);
|
||
ret += delimiter;
|
||
}
|
||
ret += dl::to_string(arr.x[N - 1]);
|
||
ret += delimiter;
|
||
ret += dl::to_string(arr.y[N - 1]);
|
||
return ret;
|
||
}
|
||
/**
|
||
* @brief vector通用to_string
|
||
*/
|
||
template<typename T>
|
||
std::string to_string_ex(const std::vector<T>& vec, std::string_view delimiter = ", ")
|
||
{
|
||
if (vec.empty())
|
||
return {};
|
||
std::string ret;
|
||
for (size_t i = 0; i < vec.size() - 1; ++i)
|
||
{
|
||
ret += dl::to_string(vec[i]);
|
||
ret += delimiter;
|
||
}
|
||
ret += dl::to_string(vec[vec.size() - 1]);
|
||
return ret;
|
||
}
|
||
|
||
//! 错误码信息
|
||
std::string to_string(std::error_code code);
|
||
|
||
|
||
/**
|
||
* @brief buf转为utf8字符串(不进行检查)
|
||
*/
|
||
template<>
|
||
std::string to_string<BufferView>(BufferView buf);
|
||
template<>
|
||
inline std::string to_string<Buffer>(Buffer buf)
|
||
{
|
||
return to_string(BufferView{ buf });
|
||
}
|
||
|
||
/**
|
||
* @brief buf转为utf8字符串(进行检查)
|
||
*/
|
||
std::string to_string_check(BufferView buf, bool tips = true);
|
||
|
||
/**
|
||
* @brief buf转为16进制字符串
|
||
*/
|
||
std::string to_string_hex(BufferView buf, bool add_tag = false);
|
||
|
||
/**
|
||
* @brief buf转为字符串,若不为utf8则转为16进制
|
||
*/
|
||
std::string to_string_auto(BufferView buf, bool add_tag = false);
|
||
|
||
enum class StringType
|
||
{
|
||
NORMAL,
|
||
INTGER,
|
||
FLOAT
|
||
};
|
||
//! 字符串操作
|
||
namespace String
|
||
{
|
||
/**
|
||
* @brief 字符串 -> Float
|
||
* @note 失败返回0
|
||
*/
|
||
inline Float toFloat(std::string_view str, Float default_v = 0)
|
||
{
|
||
std::from_chars(str.data(), str.data() + str.size(), default_v);
|
||
return default_v;
|
||
}
|
||
/**
|
||
* @brief 转为整数,失败返回设定的默认值
|
||
* @note 不建议依赖默认值,会增加代码复杂度
|
||
*/
|
||
inline int toInt(std::string_view str, int default_v = 0)
|
||
{
|
||
std::from_chars(str.data(), str.data() + str.size(), default_v);
|
||
return default_v;
|
||
}
|
||
/**
|
||
* @brief 转为整数,失败返回设定的默认值
|
||
* @note 不建议依赖默认值,会增加代码复杂度
|
||
*/
|
||
inline int64_t toInt64(std::string_view str, int64_t default_v = 0)
|
||
{
|
||
std::from_chars(str.data(), str.data() + str.size(), default_v);
|
||
return default_v;
|
||
}
|
||
/**
|
||
* @brief 转为整数,失败不修改原值
|
||
*/
|
||
inline bool getInt(std::string_view str, int& ret)
|
||
{
|
||
std::from_chars_result r = std::from_chars(str.data(), str.data() + str.size(), ret);
|
||
return r.ec == std::errc();
|
||
}
|
||
|
||
/**
|
||
* @brief 字符串 -> Point
|
||
*/
|
||
template<std::size_t N>
|
||
Array<int, N> toPoint(std::string_view str, char d = ',')
|
||
{
|
||
std::vector<std::string_view> vec;
|
||
split(str, d, vec);
|
||
if (vec.size() == 0)
|
||
{
|
||
log_warn("分割后数量为0!");
|
||
return {};
|
||
}
|
||
else if (vec.size() == 1)
|
||
{
|
||
log_warn("分割后数量为1!");
|
||
return { toInt(vec[0]) , 0};
|
||
}
|
||
return { toInt(vec[0]), toInt(vec[1]) };
|
||
}
|
||
/**
|
||
* @brief 字符串 -> Position
|
||
*/
|
||
template<std::size_t N>
|
||
Array<Float, N> toPosition(std::string_view str, char d = ',')
|
||
{
|
||
std::vector<std::string_view> vec;
|
||
split(str, d, vec);
|
||
if (vec.size() == 0)
|
||
{
|
||
log_warn("分割后数量为0!");
|
||
return {};
|
||
}
|
||
else if (vec.size() == 1)
|
||
{
|
||
log_warn("分割后数量为1!");
|
||
return { toFloat(vec[0]) , 0 };
|
||
}
|
||
return { toFloat(vec[0]), toFloat(vec[1]) };
|
||
}
|
||
|
||
inline Point2 toPoint2(std::string_view str, char d = ',')
|
||
{
|
||
return toPoint<2>(str, d);
|
||
}
|
||
inline Position toPosition2(std::string_view str, char d = ',')
|
||
{
|
||
return toPosition<2>(str, d);
|
||
}
|
||
|
||
/**
|
||
* @brief 返回字符串类型
|
||
*/
|
||
constexpr StringType GetType(std::string_view str)
|
||
{
|
||
bool has_dot = false;
|
||
for (size_t i = 0; i < str.size(); ++i)
|
||
{
|
||
const char& ch = str[i];
|
||
if (std::isdigit(ch))
|
||
continue;
|
||
else if (i == 0 && (ch == '+' || ch == '-'))
|
||
continue;
|
||
else if (!has_dot && ch == '.')
|
||
{
|
||
has_dot = true;
|
||
continue;
|
||
}
|
||
else
|
||
return StringType::NORMAL;
|
||
}
|
||
return has_dot ? StringType::FLOAT : StringType::INTGER;
|
||
}
|
||
/**
|
||
* @brief 两侧添加引号
|
||
*/
|
||
inline std::string AddQuotes(std::string_view str, char quotes = '\"')
|
||
{
|
||
std::string ret;
|
||
ret.reserve(str.size() + 2);
|
||
ret += quotes;
|
||
ret += str;
|
||
ret += quotes;
|
||
return ret;
|
||
}
|
||
/**
|
||
* @brief 获取宽字符宽度(控制台显示优化)
|
||
*/
|
||
size_t Wcwidth(wchar_t ch);
|
||
/**
|
||
* @brief 多行文本填充字符至一样宽(控制台显示优化)
|
||
* @note 假设为等宽字符,中文算2个
|
||
*/
|
||
std::string FillEndl(std::string_view str, char ch = ' ');
|
||
/**
|
||
* @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 void ToUpper(std::string& str)
|
||
{
|
||
std::transform(str.begin(), str.end(), str.begin(), ::toupper);
|
||
}
|
||
/**
|
||
* @brief 将字符串字母变为大写
|
||
*/
|
||
inline std::string ToUpper(std::string_view str)
|
||
{
|
||
std::string ret{ str };
|
||
ToUpper(ret);
|
||
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,
|
||
std::string_view delimiter = ", ", std::string_view 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 字符串为空白字符,一般意义上(\n\t空格)
|
||
*/
|
||
inline bool IsBlank(char ch)
|
||
{
|
||
return ch == ' '
|
||
|| ch == '\n'
|
||
|| ch == '\t';
|
||
}
|
||
|
||
/**
|
||
* @brief 字符串为空白(全为\n\t空格)
|
||
*/
|
||
inline bool IsBlank(std::string_view str)
|
||
{
|
||
for (char ch : str)
|
||
{
|
||
if (!IsBlank(ch))
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* @brief 字符串删除指定字符
|
||
* @note 出于效率和方便使用,不使用Erase的函数
|
||
*/
|
||
inline std::string EraseCharAll(std::string_view str, std::string_view vec_char)
|
||
{
|
||
std::string ret;
|
||
for (auto& iter : str)
|
||
{
|
||
if (Every::NotEqual(vec_char, iter))
|
||
ret.push_back(iter);
|
||
}
|
||
return ret;
|
||
}
|
||
/**
|
||
* @brief 字符串删除指定字符,从前面N个
|
||
* @note 出于效率和方便使用,不使用Erase的函数
|
||
*/
|
||
inline std::string EraseCharN(std::string_view str, std::string_view vec_char, size_t n = 1)
|
||
{
|
||
std::string ret;
|
||
|
||
for (auto& iter : str)
|
||
{
|
||
if (n && Any::Equal(vec_char, iter))
|
||
{
|
||
--n;
|
||
continue;
|
||
}
|
||
else
|
||
ret.push_back(iter);
|
||
}
|
||
return ret;
|
||
}
|
||
/**
|
||
* @brief 字符串删除指定字符,从头部删除,直到不满足条件
|
||
* @note 出于效率和方便使用,不使用Erase的函数
|
||
*/
|
||
inline std::string EraseCharHead(std::string_view str, std::string_view vec_char)
|
||
{
|
||
std::string ret;
|
||
|
||
bool check = true;
|
||
for (auto& iter : str)
|
||
{
|
||
if (check)
|
||
{
|
||
if (Any::Equal(vec_char, iter))
|
||
;// 需要删除,仍需判断
|
||
else
|
||
{
|
||
check = false;
|
||
ret.push_back(iter);
|
||
}
|
||
}
|
||
else
|
||
ret.push_back(iter);
|
||
}
|
||
return ret;
|
||
}
|
||
/**
|
||
* @brief 字符串删除空字符
|
||
*/
|
||
inline std::string EraseBlank(std::string_view str)
|
||
{
|
||
return EraseCharAll(str, " \t\n");
|
||
}
|
||
/**
|
||
* @brief 字符串删除空字符N个,从前面
|
||
*/
|
||
inline std::string EraseBlankN(std::string_view str, size_t n = 1)
|
||
{
|
||
return EraseCharN(str, " \t\n", n);
|
||
}
|
||
/**
|
||
* @brief 字符串删除空字符
|
||
*/
|
||
inline std::string EraseBlankHead(std::string_view str)
|
||
{
|
||
return EraseCharHead(str, " \t\n");
|
||
}
|
||
|
||
/**
|
||
* @brief wchar_t字符返回utf8
|
||
*/
|
||
inline std::string GetUtf8ByWchar(wchar_t wc, size_t offset = 0)
|
||
{
|
||
// 某些平台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;
|
||
}
|
||
inline std::vector<std::string_view> Split(std::string_view str, char d)
|
||
{
|
||
std::vector<std::string_view> 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;
|
||
}
|
||
inline std::vector<std::string_view> Split(std::string_view str, std::string_view d)
|
||
{
|
||
std::vector<std::string_view> ret;
|
||
split_sub(str, d, ret);
|
||
return ret;
|
||
}
|
||
|
||
/**
|
||
* @brief 处理子字符串(通过特定字符分割)
|
||
* @param[in] str 整个字符串
|
||
* @param[in] vec_ch 匹配的字符
|
||
* @note 效率更高,连续匹配不会返回空串
|
||
*/
|
||
inline std::vector<std::string_view> SplitViewNoEmpty(std::string_view str, std::string_view vec_ch)
|
||
{
|
||
std::vector<std::string_view> ret;
|
||
size_t beg = 0;
|
||
size_t cur = 0;
|
||
for (; cur < str.size(); ++cur)
|
||
{
|
||
if (Any::Equal(vec_ch, str[cur]))
|
||
{
|
||
if (beg != cur)
|
||
ret.push_back({ str.data() + beg, str.data() + cur });
|
||
beg = cur + 1;
|
||
}
|
||
}
|
||
if (beg != cur)
|
||
ret.push_back({ str.data() + beg, str.data() + cur });
|
||
return ret;
|
||
}
|
||
inline std::vector<std::string_view> SplitViewNoEmpty(std::string_view str,
|
||
const std::vector<char>& vec_ch)
|
||
{
|
||
return SplitViewNoEmpty(str, std::string_view{ vec_ch.data(), vec_ch.size() });
|
||
}
|
||
/**
|
||
* @brief 处理子字符串(通过特定字符分割)
|
||
* @param[in] str 整个字符串
|
||
* @param[in] vec_ch 匹配的字符
|
||
* @note 连续匹配返回空串
|
||
*/
|
||
inline std::vector<std::string_view> SplitView(std::string_view str, std::string_view vec_ch)
|
||
{
|
||
std::vector<std::string_view> ret;
|
||
size_t beg = 0;
|
||
size_t cur = 0;
|
||
for (; cur < str.size(); ++cur)
|
||
{
|
||
if (Any::Equal(vec_ch, str[cur]))
|
||
{
|
||
if (beg != cur)
|
||
ret.push_back({ str.data() + beg, str.data() + cur });
|
||
else
|
||
ret.push_back({});
|
||
beg = cur + 1;
|
||
}
|
||
}
|
||
if (beg != cur)
|
||
ret.push_back({ str.data() + beg, str.data() + cur });
|
||
else
|
||
ret.push_back({});
|
||
return ret;
|
||
}
|
||
inline std::vector<std::string_view> SplitView(std::string_view str,
|
||
const std::vector<char>& vec_ch)
|
||
{
|
||
return SplitView(str, std::string_view{ vec_ch.data(), vec_ch.size() });
|
||
}
|
||
/**
|
||
* @brief 分割字符串(连续跳过匹配字符)
|
||
*/
|
||
inline std::vector<std::string> SplitNoEmpty(const std::string& str, char d)
|
||
{
|
||
std::vector<std::string> ret;
|
||
split_no_empty(str, d, ret);
|
||
return ret;
|
||
}
|
||
|
||
inline void TestSplit()
|
||
{
|
||
std::string str0 = "0,1,2,,3";
|
||
std::string str1 = ",0,1,2,,3,";
|
||
std::string str2 = ",,0,1,2,,3,";
|
||
|
||
using namespace std::literals;
|
||
{
|
||
std::vector<std::string_view> vec0 = SplitView(str0, ","sv);
|
||
std::vector<std::string_view> vec1 = SplitView(str1, ","sv);
|
||
std::vector<std::string_view> vec2 = SplitView(str2, ","sv);
|
||
|
||
assert(vec0[0] == "0" && vec0[1] == "1" && vec0[2] == "2" && vec0[3] == "" && vec0[4] == "3");
|
||
assert(vec1[0] == "" && vec1[1] == "0" && vec1[2] == "1" && vec1[3] == "2" && vec1[4] == ""
|
||
&& vec1[5] == "3" && vec1[6] == "");
|
||
assert(vec2[0] == "" && vec2[1] == "" && vec2[2] == "0" && vec2[3] == "1" && vec2[4] == "2"
|
||
&& vec2[5] == "" && vec2[6] == "3" && vec2[7] == "");
|
||
}
|
||
|
||
|
||
{
|
||
std::vector<std::string_view> vec0 = SplitViewNoEmpty(str0, ","sv);
|
||
std::vector<std::string_view> vec1 = SplitViewNoEmpty(str1, ","sv);
|
||
std::vector<std::string_view> vec2 = SplitViewNoEmpty(str2, ","sv);
|
||
|
||
assert(vec0[0] == "0" && vec0[1] == "1" && vec0[2] == "2" && vec0[3] == "3");
|
||
assert(vec1[0] == "0" && vec1[1] == "1" && vec1[2] == "2" && vec1[3] == "3");
|
||
assert(vec2[0] == "0" && vec2[1] == "1" && vec2[2] == "2" && vec2[3] == "3");
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @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 获取文件名不含后缀
|
||
*/
|
||
inline std::string GetFileStem(std::string_view path_name)
|
||
{
|
||
std::string name{ path_name };
|
||
Erase::HeadFromLast(name, '/');
|
||
if (Erase::TailFromLast(name, '.'))
|
||
name.pop_back();
|
||
return name;
|
||
}
|
||
/**
|
||
* @brief 移除路径(第一个子串)
|
||
*/
|
||
inline std::string ErasePath(std::string_view path_name, std::string_view path)
|
||
{
|
||
std::string ret;
|
||
Erase::HeadSub(ret, std::string{ path });
|
||
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 绝对路径 或 资源路径(相对path_root)
|
||
* @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_cur 当前路径
|
||
* @param[in] path 路径
|
||
* @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, '\\', '/');
|
||
}
|
||
[[nodiscard]]
|
||
inline std::string CvtPathSeparator(std::string_view path)
|
||
{
|
||
std::string ret{ path };
|
||
CvtPathSeparator(ret);
|
||
return ret;
|
||
}
|
||
/**
|
||
* @brief 路径斜杠统一为\,(优先使用/作为路径)
|
||
* @param[in] path 路径
|
||
*/
|
||
inline void CvtPathSeparatorBack(std::string& path)
|
||
{
|
||
Every::Replace(path, '/', '\\');
|
||
}
|
||
[[nodiscard]]
|
||
inline std::string CvtPathSeparatorBack(std::string_view path)
|
||
{
|
||
std::string ret{ path };
|
||
CvtPathSeparatorBack(ret);
|
||
return ret;
|
||
}
|
||
/**
|
||
* @brief 换行符统一为\n
|
||
* @param[in] str 字符串
|
||
*/
|
||
inline std::string CvtLineBreak(std::string_view str)
|
||
{
|
||
std::string ret;
|
||
for (auto& iter : str)
|
||
{
|
||
if(iter == '\r')
|
||
continue;
|
||
ret.push_back(iter);
|
||
}
|
||
return ret;
|
||
}
|
||
|
||
|
||
/**
|
||
* @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 解析命令行参数
|
||
*/
|
||
std::vector<std::string> ParseCommand(std::string_view cmd);
|
||
|
||
/**
|
||
* @brief 对2维string进行排版(保证列对齐)
|
||
* @note 例如:a, bcde, cd
|
||
*/
|
||
template<size_t W, size_t H>
|
||
Array<std::string, H> FormatString2(const Array2<std::string, W, H>& arr)
|
||
{
|
||
//每一列得到一个最大宽度
|
||
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();
|
||
}
|
||
}
|
||
//合并每行
|
||
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);
|
||
|
||
|
||
|
||
//----------------------------其余范围操作封装----------------------------
|
||
inline bool IsSub(std::string_view str, std::string_view str_sub)
|
||
{
|
||
return Sub::IsSub(str, str_sub);
|
||
}
|
||
|
||
inline bool IsBegin(std::string_view str, std::string_view str_sub)
|
||
{
|
||
return Sub::IsBegin(str, str_sub);
|
||
}
|
||
inline std::string_view Range(std::string_view str, char a, char b)
|
||
{
|
||
return Sub::Range(str, a, b);
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
|
||
|
||
namespace dl
|
||
{
|
||
|
||
|
||
inline std::string to_string(const Vector2<std::string>& vec2)
|
||
{
|
||
return String::Splicing(String::FormatString2(vec2), "\n");
|
||
}
|
||
|
||
|
||
//#if (DL_PLATFORM == DL_PLATFORM_WIN32)
|
||
///**
|
||
//* @brief array的format特化
|
||
//* @note
|
||
//*/
|
||
//template<typename T, size_t N, typename CharT>
|
||
//struct std::formatter<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
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
namespace fmt
|
||
{
|
||
//
|
||
template <typename T>
|
||
struct formatter<dl::Array<T, 2>>
|
||
{
|
||
// 解析格式化参数(这里简化,不处理自定义格式)
|
||
constexpr auto parse(fmt::format_parse_context& ctx) -> decltype(ctx.begin())
|
||
{
|
||
// 检查是否有多余的格式符
|
||
auto it = ctx.begin();
|
||
if (it != ctx.end() && *it != '}')
|
||
throw fmt::format_error("invalid format specifier for dl::Array<T, 2>");
|
||
return it;
|
||
}
|
||
|
||
// 格式化逻辑
|
||
template <typename FormatContext>
|
||
auto format(const dl::Array<T, 2>& arr, FormatContext& ctx) const -> decltype(ctx.out())
|
||
{
|
||
return fmt::format_to(ctx.out(), "{}, {}", arr.x, arr.y);
|
||
}
|
||
};
|
||
|
||
//
|
||
template <typename T>
|
||
struct formatter<dl::Array<T, 3>>
|
||
{
|
||
constexpr auto parse(fmt::format_parse_context& ctx) -> decltype(ctx.begin())
|
||
{
|
||
auto it = ctx.begin();
|
||
if (it != ctx.end() && *it != '}')
|
||
throw fmt::format_error("invalid format specifier for dl::Array<T, 3>");
|
||
return it;
|
||
}
|
||
|
||
template <typename FormatContext>
|
||
auto format(const dl::Array<T, 3>& arr, FormatContext& ctx) const -> decltype(ctx.out())
|
||
{
|
||
return fmt::format_to(ctx.out(), "{}, {}, {}", arr.x, arr.y, arr.z);
|
||
}
|
||
};
|
||
|
||
//
|
||
template <typename T>
|
||
struct formatter<dl::Array<T, 4>>
|
||
{
|
||
constexpr auto parse(fmt::format_parse_context& ctx) -> decltype(ctx.begin())
|
||
{
|
||
auto it = ctx.begin();
|
||
if (it != ctx.end() && *it != '}')
|
||
throw fmt::format_error("invalid format specifier for dl::Array<T, 4>");
|
||
return it;
|
||
}
|
||
|
||
template <typename FormatContext>
|
||
auto format(const dl::Array<T, 4>& arr, FormatContext& ctx) const -> decltype(ctx.out())
|
||
{
|
||
return fmt::format_to(ctx.out(), "{}, {}, {}, {}", arr.x, arr.y, arr.z, arr.w);
|
||
}
|
||
};
|
||
|
||
} |