Repository navigation
Expand file tree
/
Copy pathstring_utils.cpp
More file actions
92 lines (76 loc) · 2.14 KB
/
Copy pathstring_utils.cpp
File metadata and controls
92 lines (76 loc) · 2.14 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
85
86
87
88
89
90
91
92
#include <iterator>
#include <sstream>
#include "string_utils.h"
namespace utils {
using std::string;
using std::stringstream;
using std::vector;
vector<string> split(const string &str, char delim, bool include_empty) {
std::stringstream ss;
ss.str(str);
std::string item;
vector<string> result;
auto inserter = std::back_inserter(result);
while (std::getline(ss, item, delim)) {
if (!item.empty() || include_empty)
*(inserter++) = item;
}
return result;
}
string join(const vector<string> &strs, const string &sep) {
std::stringstream ss;
for (size_t i = 0; i < strs.size(); i++) {
if (i)
ss << sep;
ss << strs[i];
}
return ss.str();
}
string escape(const string &orig, const string &repchars, const vector<string> &repstrs) {
stringstream out;
size_t last = 0;
size_t found = orig.find_first_of(repchars);
while (found != string::npos) {
out << orig.substr(last, found - last);
for (size_t i = 0; i < repchars.size(); i++) {
if (orig[found] == repchars[i]) {
out << repstrs[i];
break;
}
}
last = found + 1;
found = orig.find_first_of(repchars, found + 1);
}
out << orig.substr(last);
return out.str();
}
string escape(const string &orig, bool html, bool nls) {
string repchars = "\\&\"\'<>";
vector<string> repstrs;
if (html)
repstrs = {"\\", "&", """, "'", "<", ">"};
else
repstrs = {"\\\\", "&", "\\\"", "'", "<", ">"};
if (nls) {
repchars += "\n\r";
repstrs.emplace_back("\\n");
repstrs.emplace_back("\\r");
}
return escape(orig, repchars, repstrs);
}
string escape_nls(const string& orig) {
string repchars = "\n\r";
vector<string> repstrs = {"\\n", "\\r"};
return escape(orig, repchars, repstrs);
}
string escape_text(const string& orig) {
string repchars = "\\&\"\'<>";
vector<string> repstrs = {"\\\\", "&", "\\\"", "'", "<", ">"};
return escape(orig, repchars, repstrs);
}
string escape_html(const string& orig) {
string repchars = "\\&\"\'<>";
vector<string> repstrs = {"\\", "&", """, "'", "<", ">"};
return escape(orig, repchars, repstrs);
}
} // namespace utils