-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStrings.cpp
235 lines (206 loc) · 4.37 KB
/
Strings.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// class header
#include "Strings.h"
#include "Types.h"
#include <algorithm>
#include <assert.h>
std::string to_string(double d, int len, int len_after_dot)
{
assert(len > len_after_dot);
char buf[1024];
std::string format = std::string("%") + to_string(len) + '.' + to_string(len_after_dot) + 'f';
sprintf_s(buf,sizeof(buf),format.c_str(),d);
return std::string(buf);
}
std::string to_string(int i, int numdigits)
{
std::string s = to_string(i);
std::string res = padFromLeft(s,numdigits,'0');
return res;
}
std::string padFromLeft(std::string s, unsigned int len, char ch)
{
int n = len - static_cast<int>(s.length());
if (n > 0) s = s.insert(0,n,ch);
return s;
}
std::string upCase(std::string str)
{
std::transform(str.begin(),str.end(),str.begin(),::toupper);
return str;
}
std::string lowerCase(std::string str)
{
std::transform(str.begin(),str.end(),str.begin(),::tolower);
return str;
}
std::string trimLeft(std::string str, std::string chars)
{
str.erase(0,str.find_first_not_of(chars));
return str;
}
std::string trimRight(std::string str, std::string chars)
{
str.erase(str.find_last_not_of(chars)+1);
return str;
}
std::string trim(std::string str, std::string chars)
{
return trimRight(trimLeft(str,chars),chars);
}
std::string replace(std::string str, char from, char to)
{
std::string s = str;
std::replace(s.begin(),s.end(),from,to);
return s;
}
int find(std::string str, char ch)
{
size_t pos = str.find(ch);
if (pos == std::string::npos)
{
return NOT_FOUND;
} else
{
return static_cast<int>(pos);
}
}
int find(std::string str, std::string substr)
{
size_t pos = str.find(substr);
if (pos == std::string::npos)
{
return NOT_FOUND;
} else
{
return static_cast<int>(pos);
}
}
void deleteChars(std::string &str, int pos0, int numchars)
{
str.erase(pos0,numchars);
}
std::string getSubString(std::string str, int pos0, int pos1)
{
std::string res = str.substr(pos0,(pos1 - pos0 + 1));
return res;
}
int parseWords(std::string str, char divider, int pos1[], int pos2[], int maxcount)
{
int numwords = 0;
int len = static_cast<int>(str.length());
if (len > 0)
{
int curpos1 = NOT_DEFINED;
if (str[0] != divider) curpos1 = 0;
for (int i = 0; i < len; i++)
{
if (str[i] == divider)
{
if (curpos1 != NOT_DEFINED)
{
if (numwords < maxcount)
{
pos1[numwords] = curpos1;
pos2[numwords] = i - 1;
curpos1 = NOT_DEFINED;
numwords++;
} else
{
break;
}
}
} else
{
if (curpos1 == NOT_DEFINED)
{
curpos1 = i;
}
}
}
if (curpos1 != NOT_DEFINED)
{
if (numwords < maxcount)
{
pos1[numwords] = curpos1;
pos2[numwords] = len;
numwords++;
}
}
}
return numwords;
}
std::string forceExtension(std::string s, std::string ext)
{
std::size_t pos = s.rfind('.');
if (pos != std::string::npos)
{
if (ext.length())
{
return s.substr(0,pos + 1) + ext;
} else
{
return s.substr(0,pos);
}
} else
{
if (ext.length())
{
return s + "." + ext;
} else
{
return s;
}
}
}
std::string getExtension(std::string s)
{
std::string ext;
std::size_t pos = s.rfind('.');
if (pos != std::string::npos)
{
ext = s.substr(pos + 1);
}
return ext;
}
std::string addBackslash(std::string directory)
{
if (directory.length() == 0)
{
return std::string("");
} else
{
#ifdef _WIN32
if (directory.back() != '\\')
{
return directory + "\\";
#else
if (directory.back() != '/')
{
return directory + "/";
#endif
} else
{
return directory;
}
}
}
std::string justFileName(const std::string &path)
{
std::string name = path;
name.erase(0,path.find_last_of("\\/") + 1);
return name;
}
std::string justDirectory(const std::string &path)
{
std::string name = path;
name.erase(path.find_last_of("\\/") + 1);
return name;
}
bool containsOnlyChars(const std::string &s, char from, char to)
{
for (size_t i = 0; i < s.length(); i++)
{
if (s[i] < from || s[i] > to) return false;
}
return true;
}