-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathutils.cpp
More file actions
84 lines (74 loc) · 1.94 KB
/
utils.cpp
File metadata and controls
84 lines (74 loc) · 1.94 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "utils.h"
vector<string> splitAllowSeperator(vector<char>::iterator from, vector<char>::iterator to, char sep)
{
vector<string> values;
vector<char> buf;
while(from < to)
{
if (*from == sep)
{
if (from+1 != to && *(from+1) == sep)
{
++from;
}
else
{
char* value = new char[buf.size()+1];
memcpy(value, buf.data(), buf.size());
value[buf.size()]=0;
values.push_back(value);
delete[] value;
buf.clear();
++from;
continue;
}
}
buf.push_back(*from);
++from;
}
return values;
}
void stringReplace(string& target,const string& pattern,const string& candidate)
{
auto pos=0;
auto ps=pattern.size();
auto cs=candidate.size();
while((pos=target.find(pattern,pos)) != string::npos)
{
target.replace(pos,ps,candidate);
pos+=cs;
}
}
string getFileNameFromPath(const string &path)
{
auto sep = '/';
auto result = path;
if (result.at(result.length()-1) == sep)
result = result.substr(0, result.length()-1);
auto pos = result.find_last_of('/');
if (pos == string::npos)
return result;
return result.substr(pos+1);
}
bool startsWith(const string &str, const string &patten)
{
if (str.length() < patten.length())
return false;
return std::equal(patten.begin(), patten.end(), str.begin());
}
bool endsWith(const string &str, const string &patten)
{
if (str.length() < patten.length())
return false;
return std::equal(patten.rbegin(), patten.rend(), str.rbegin());
}
string toString(const vector<char> &buf)
{
auto len = buf.size();
char* value = new char[len+1];
memcpy(value, buf.data(), len);
value[len]=0;
string result = value;
delete[] value;
return result;
}