-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstring-utils.cpp
More file actions
121 lines (103 loc) · 2.75 KB
/
string-utils.cpp
File metadata and controls
121 lines (103 loc) · 2.75 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
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
#include "string-utils.h"
#include <sstream>
#include <iomanip>
using namespace std;
string join(vector<string> strings, string separator) {
bool first = true;
stringstream ss;
for(string s:strings) {
if(!first)
ss << separator;
ss << s;
first=false;
}
return ss.str();
}
string format(double d) {
stringstream ss;
ss<<d;
return ss.str();
}
string ftos(double d, int precision) {
stringstream ss;
ss << std::fixed << std::setprecision( precision ) << d;
return ss.str();
}
string format(double d, int width, int precision) {
stringstream ss;
ss<< std::fixed << std::setw( width ) << std::setprecision( precision )
<< d;
return ss.str();
}
string to_fixed_width_string(int n, int width, char fill) {
std::ostringstream ss;
ss << std::setw( width ) << std::setfill( fill ) << n;
return ss.str();
}
// setting all to true splits all occurances
// setting to false only splits on first occurance of delim_char - if any
vector<string> split(const string& s, const string & delim_string) {
vector<string> items;
size_t start = 0;
while(start < s.length()-1) {
size_t pos = s.find(delim_string, start);
if(pos == string::npos) {
break;
}
items.push_back(s.substr(start, pos-start));
start = pos+delim_string.length();
}
items.push_back(s.substr(start, string::npos));
return items;
}
// setting all to true splits all occurances
// setting to false only splits on first occurance of delim_char - if any
vector<string> split(const string& s, char delim_char, bool all) {
string delim = string(1, delim_char);
vector<string> items;
string::const_iterator substart = s.begin(), subend;
while (true) {
subend = search(substart, s.end(), delim.begin(), delim.end());
string temp(substart, subend);
items.push_back(temp);
if (subend == s.end()) {
break;
}
substart = subend + delim.size();
if (!all) {
string temp(substart, s.end());
items.push_back(temp);
break;
}
}
return items;
}
// http://stackoverflow.com/a/217605/383967
// trim from start (in place)
void ltrim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
}
// trim from end (in place)
void rtrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
}
// trim from both ends (in place)
void trim(std::string &s) {
ltrim(s);
rtrim(s);
}
// trim from start (copying)
string ltrimmed(std::string s) {
ltrim(s);
return s;
}
// trim from end (copying)
string rtrimmed(std::string s) {
rtrim(s);
return s;
}
// trim from both ends (copying)
string trimmed(std::string s) {
trim(s);
return s;
}