Files
DNF_DEV/source/Tool/Tool_String.cpp

57 lines
1.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "Tool/Tool_String.h"
#include <SDL2/SDL.h>
#include <memory>
#include <stdexcept>
#include <vector>
#include <iconv.h>
#include <regex>
#include "Tool_String.h"
std::string Tool_toLowerCase(const std::string &str)
{
std::string result = str;
// 使用transform算法遍历字符串并转换为小写
std::transform(result.begin(), result.end(), result.begin(),
[](unsigned char c)
{ return std::tolower(c); });
return result;
}
std::string Tool_RegRealPath(const std::string &Path)
{
// 检查路径中是否包含"../"
if (Path.find("../") == std::string::npos)
{
return Path;
}
// 正则表达式:匹配形如"xxx/../"的模式
// [^/]+ 匹配非斜杠的字符序列(表示目录名)
// /../ 匹配"/../"
std::regex pattern("[^/]+/\\.\\./");
std::string processedPath = Path;
std::smatch match;
// 循环处理所有匹配的模式
while (std::regex_search(processedPath, match, pattern))
{
// 替换匹配到的部分(删除"xxx/../"
processedPath = std::regex_replace(processedPath, pattern, "", std::regex_constants::format_first_only);
}
return processedPath;
}
std::string Tool_TruncatePath(const std::string &path)
{
// 查找最后一个 '/' 的位置
size_t lastSlashPos = path.find_last_of('/');
// 如果找到了 '/',返回从开始到该位置(包含)的子串
if (lastSlashPos != std::string::npos)
{
return path.substr(0, lastSlashPos + 1); // +1 是为了包含 '/' 本身
}
// 如果没有找到 '/',返回原字符串(或根据需求返回空)
return path;
}