-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathstring-utils.h
129 lines (103 loc) · 2.92 KB
/
string-utils.h
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <string>
#include <sstream>
#include <fstream>
#include <vector>
#include <algorithm>
#ifndef STRING_UTILS_H
#define STRING_UTILS_H
static inline std::vector<std::string> Compact(const std::vector<std::string> &tokens){
std::vector<std::string> compacted;
for(size_t i=0; i<tokens.size(); ++i) {
if (!tokens[i].empty()) {
compacted.push_back(tokens[i]);
}
}
return compacted;
}
static inline std::vector<std::string> Split(const std::string &str, const std::string &delim, const bool trim_empty = false){
size_t pos, last_pos = 0, len;
std::vector<std::string> tokens;
while(true) {
pos = str.find(delim, last_pos);
if (pos == std::string::npos) {
pos = str.size();
}
len = pos-last_pos;
if ( !trim_empty || len != 0) {
tokens.push_back(str.substr(last_pos, len));
}
if (pos == str.size()) {
break;
} else {
last_pos = pos + delim.size();
}
}
return tokens;
}
static inline std::string Join(const std::vector<std::string> &tokens, const std::string &delim, const bool trim_empty = false){
if(trim_empty) {
return Join(Compact(tokens), delim, false);
} else {
std::stringstream ss;
for(size_t i=0; i<tokens.size()-1; ++i) {
ss << tokens[i] << delim;
}
ss << tokens[tokens.size()-1];
return ss.str();
}
}
static inline std::string Trim(const std::string &str){
std::string blank = "\r\n\t ";
size_t begin = str.size(), end = 0;
for (size_t i=0; i<str.size(); ++i) {
if ( blank.find(str[i]) == std::string::npos) {
begin = i;
break;
}
}
for (size_t i=str.size(); i>0; --i) {
if ( blank.find(str[i-1]) == std::string::npos) {
end = i-1;
break;
}
}
if (begin >= end) {
return "";
} else {
return str.substr(begin, end-begin+1);
}
}
static inline std::string Repeat(const std::string &str, unsigned int times){
std::stringstream ss;
for(unsigned int i=0; i<times; ++i) {
ss << str;
}
return ss.str();
}
static inline std::string ReplaceAll(const std::string &source, const std::string &target, const std::string &replacement){
return Join(Split(source, target, false), replacement, false);
}
static inline std::string ToUpper(const std::string &str){
std::string s(str);
std::transform(s.begin(), s.end(), s.begin(), toupper);
return s;
}
static inline std::string ToLower(const std::string &str){
std::string s(str);
std::transform(s.begin(), s.end(), s.begin(), tolower);
return s;
}
static inline std::string ReadFile(const std::string &filepath) {
std::ifstream ifs(filepath.c_str());
std::string content( (std::istreambuf_iterator<char>(ifs) ),
(std::istreambuf_iterator<char>() ) );
ifs.close();
return content;
}
static inline void WriteFile(const std::string &filepath, const std::string &content) {
std::ofstream ofs(filepath.c_str());
ofs << content;
ofs.close();
return;
}
#endif