-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathutils.cpp
59 lines (51 loc) · 1.19 KB
/
utils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "utils.hpp"
void StringSplit(const std::string& str, const std::string& split, std::list<std::string>& res)
{
//std::std::regex ws_re("\\s+"); // 正则表达式,匹配空格
std::regex reg(split); // 匹配split
std::sregex_token_iterator pos(str.begin(), str.end(), reg, -1);
decltype(pos) end; // 自动推导类型
for (; pos != end; ++pos)
{
res.push_back(pos->str());
}
}
void StringReplace(std::string& str,const std::string& pat,const std::string& fmt)
{
std::regex pattern(pat);
str=regex_replace(str, pattern, fmt);
}
void RemoveRedundancyPath(std::string& str)
{
std::list<std::string> res;
std::list<std::string> pathItem;
StringSplit(str, "/", res);
std::regex pattern("[.]{2}");
std::regex pattern1("[.]{1}");
for(std::list<std::string>::iterator it=res.begin();it!=res.end();it++)
{
if(regex_match(*it,pattern1))
{
continue;
}
if(!regex_match(*it,pattern))
{
pathItem.push_back(*it);
}
else
{
pathItem.pop_back();
}
}
str.clear();
for(std::list<std::string>::iterator it=pathItem.begin();it!=pathItem.end();it++)
{
if(std::next(it) == pathItem.end())
{
str+=(*it);
}
else {
str+=(*it)+"/";
}
}
}