This repository has been archived by the owner on May 2, 2019. It is now read-only.
/
strings.hpp
283 lines (232 loc) · 9.7 KB
/
strings.hpp
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#ifndef _____STRINGS_HPP_8_____
#define _____STRINGS_HPP_8_____
#include<string>
#include<cstring>
#include<cwchar>
#include<algorithm>
#include<cstdlib>
#include<windows.h>
#define CP_AUTODETECT CP_ACP
template<class T>
struct basic_split_iterator {
const std::basic_string<T> str;
std::basic_string<T> delims;
int first, last;
basic_split_iterator (const std::basic_string<T>& str, const std::basic_string<T>& delims, int last=0, int first=0) ;
std::basic_string<T> operator* (void) ;
std::basic_string<T> rest (void) ;
void delim (const std::basic_string<T>& delims1) ;
const std::basic_string<T>& delim (void) ;
basic_split_iterator<T>& operator++ (void) ;
bool valid (void) ;
basic_split_iterator<T>& begin (void) ;
basic_split_iterator<T>& end (void) ;
bool operator!= (const basic_split_iterator<T>&) ;
operator bool (void) ;
};
typedef basic_split_iterator<char> split_iterator;
typedef basic_split_iterator<wchar_t> wsplit_iterator;
template <class T> basic_split_iterator<T>::basic_split_iterator (const std::basic_string<T>& str1, const std::basic_string<T>& delims1, int last1, int first1) :
str(str1), delims(delims1), first(first1), last(last1) {
++(*this);
}
template <class T> basic_split_iterator<T>& basic_split_iterator<T>::operator++ (void) {
first = str.find_first_not_of(delims, last);
last = str.find_first_of(delims, first);
return *this;
}
template <class T> std::basic_string<T> basic_split_iterator<T>::operator* (void) {
return std::basic_string<T>( str.begin()+first, last>=0? str.begin()+last : str.end() );
}
template <class T> std::basic_string<T> basic_split_iterator<T>::rest (void) {
return std::basic_string<T>( str.begin()+first, str.end());
}
template <class T> bool basic_split_iterator<T>::valid (void) {
return first>=0;
}
template <class T> const std::basic_string<T>& basic_split_iterator<T>::delim (void) { return delims; }
template <class T> void basic_split_iterator<T>::delim (const std::basic_string<T>& d) { delims=d; }
template <class T> basic_split_iterator<T>& basic_split_iterator<T>::begin () { return *this; }
template <class T> basic_split_iterator<T>& basic_split_iterator<T>::end () { return *this; }
template <class T> bool basic_split_iterator<T>::operator!= (const basic_split_iterator<T>& unused) { return valid(); }
template <class T> basic_split_iterator<T>::operator bool (void) { return valid(); }
template<class T> basic_split_iterator<T>& operator>> (basic_split_iterator<T>& it, std::basic_string<T>& out) {
out = *it;
++it;
return it;
}
template<class T> bool split (const std::basic_string<T>& src, T sep, std::basic_string<T>& dst1, std::basic_string<T>& dst2) {
int pos = src.find(sep);
if (pos<0 || pos>=src.size()) return false;
dst1 = src.substr(0, pos);
dst2 = src.substr(pos+1);
return true;
}
template<class T> bool split (const std::basic_string<T>& src, T sep, std::basic_string<T>& dst1, std::basic_string<T>& dst2, std::basic_string<T>& dst3) {
std::basic_string<T> tmp;
return split(src, sep, dst1, tmp) && split(tmp, sep, dst2, dst3);
}
template <class T> void replace (std::basic_string<T>& str, const std::basic_string<T>& needle, const std::basic_string<T>& repl) {
for (int pos=0; (pos = str.find(needle))>=0 && pos<str.size(); ) {
str.erase(str.begin()+pos, str.begin()+needle.size()+pos);
str.insert(pos, repl);
pos += repl.size();
}
}
template <class T> void replace (std::basic_string<T>& str, const T* needle, const std::basic_string<T>& repl) { replace(str, std::basic_string<T>(needle), repl); }
template <class T> void replace (std::basic_string<T>& str, const std::basic_string<T>& needle, const T* repl) { replace(str, needle, std::basic_string<T>(repl)); }
template <class T> void replace (std::basic_string<T>& str, const T* needle, const T* repl) { replace(str, std::basic_string<T>(needle), std::basic_string<T>(repl)); }
template <class T> void trim (std::basic_string<T>& s) {
int first=0, last=s.size() -1;
while (first<s.size() && s[first]<32) first++;
while (last>0 && s[last]<32) last--;
if (first>0) s.erase(s.begin(), s.begin()+first);
if (last<s.size()-1) s.erase(s.begin()+last+1, s.end());
}
template <class T> void toLowerCase (std::basic_string<T>& str) {
transform(str.begin(), str.end(), str.begin(), tolower);
}
template <class T> void toUpperCase (std::basic_string<T>& str) {
transform(str.begin(), str.end(), str.begin(), toupper);
}
// Sprintf++
int wsnprintf (std::wstring& str, int max, const std::wstring& fmt, ...) ;
int snprintf (std::string& str, int max, const std::string& fmt, ...) ;
bool isNumeric (const std::string& str) ;
// Conversion
inline int toInt (const std::string& str, int base=0) {
return strtol(str.c_str(), 0, base);
}
inline unsigned int toUnsignedInt (const std::string& str, int base=0) {
return strtoul(str.c_str(), 0, base);
}
inline int toInt (const std::wstring& str, int base=0) {
return wcstol(str.c_str(), 0, base);
}
inline unsigned int toUnsignedInt (const std::wstring& str, int base=0) {
return wcstoul(str.c_str(), 0, base);
}
inline long long toLongLong (const std::string& str, int base=0) {
return strtoll(str.c_str(), 0, base);
}
inline unsigned long long toUnsignedLongLong (const std::string& str, int base=0) {
return strtoull(str.c_str(), 0, base);
}
inline double toDouble (const std::string& str) {
return strtod(str.c_str(), 0);
}
inline float toFloat (const std::string& str) {
return strtof(str.c_str(), 0);
}
inline bool toBool (const std::string& str) {
return str=="1" || !stricmp(str.c_str(),"true") || !stricmp(str.c_str(),"on");
}
template <class T> inline size_t& stringSize (std::basic_string<T>& s) {
return ((size_t*)(s.data()))[-3];
}
inline const std::string& toString (const std::string& s) { return s; }
inline const std::wstring& toWString (const std::wstring& ws) { return ws; }
inline std::string toString (const char* str) { return std::string(str?str:""); }
inline std::wstring toWString (const wchar_t* ws) { return std::wstring(ws?ws:L""); }
inline std::wstring toWString2 (const wchar_t* ws) { return toWString(ws); }
inline std::string toString (const std::wstring& ws, int cp = CP_UTF8) {
size_t nSize = WideCharToMultiByte(cp, 0, ws.data(), ws.size(), NULL, 0, NULL, NULL);
std::string s(nSize, '\0');
stringSize(s) = WideCharToMultiByte(cp, 0, ws.data(), ws.size(), s.data(), nSize, NULL, NULL);
return s;
}
inline std::wstring toWString (const std::string& s, int cp = CP_UTF8) {
size_t nSize = MultiByteToWideChar(cp, 0, s.data(), s.size(), NULL, 0);
std::wstring ws(nSize, L'\0');
stringSize(ws) = MultiByteToWideChar(cp, 0, s.data(), s.size(), ws.data(), nSize);
return ws;
}
inline std::string toString (const wchar_t* ws) { std::wstring z(ws?ws:L""); return toString(z); }
inline std::wstring toWString (const char* str) { std::string z(str?str:""); return toWString(z); }
std::string toUTF8String (const char* input);
template <class T> inline T fromString (const std::string& s) { throw 0; }
template<> inline std::string fromString (const std::string& s) { return s; }
template<> inline std::wstring fromString (const std::string& s) { return toWString(s); }
template<> inline int fromString (const std::string& s) { return toInt(s); }
template<> inline long long fromString (const std::string& s) { return toLongLong(s); }
template<> inline unsigned long long fromString (const std::string& s) { return toUnsignedLongLong(s); }
template<> inline double fromString (const std::string& s) { return toDouble(s); }
template<> inline float fromString (const std::string& s) { return (float)toDouble(s); }
template<> inline bool fromString (const std::string& s) { return toBool(s); }
inline std::string toString (int n, int base = 10) {
char buf[64]={0};
ltoa(n, buf, base);
return buf;
}
inline std::wstring toWString (int n, int base = 10) {
return toWString(toString(n,base));
}
inline std::string toString (long long n, int base = 10) {
char buf[64]={0};
lltoa(n, buf, base);
return buf;
}
inline std::string toString (DWORD n, int base = 10) {
char buf[64]={0};
lltoa(n, buf, base);
return buf;
}
inline std::string toString (unsigned long long n, int base = 10) {
char buf[64]={0};
ulltoa(n, buf, base);
return buf;
}
inline std::string toString (double d) {
char buf[64]={0};
snprintf(buf, 63, "%.14g", d);
return buf;
}
inline std::string toString (bool b) {
return b?"true":"false";
}
template <class T> bool contains (const std::basic_string<T>& s1, const std::basic_string<T>& s2) {
if (s1.size()<=0 || s2.size()<=0) return false;
for (int i=0, j=0, n=s1.size(), m=s2.size(); i<n; i++) {
if (tolower(s1[i])==tolower(s2[j++])) {
if (j>=m) return true;
}
else j=0;
}
return false;
}
template <class T> bool startsWith (const std::basic_string<T>& s1, const std::basic_string<T>& s2) {
if (s1.size()<=0 || s2.size()<=0) return false;
for (int i=0, j=0, n=s1.size(), m=s2.size(); i<n; i++) {
if (tolower(s1[i])==tolower(s2[j++])) {
if (j>=m) return true;
}
else return false;
}
return false;
}
template <class T> bool endsWith (const std::basic_string<T>& s1, const std::basic_string<T>& s2) {
if (s1.size()<=0 || s2.size()<=0) return false;
for (int n=s1.size(), m=s2.size(), i=n-1, j=m-1; i>=0; i--) {
if (tolower(s1[i])==tolower(s2[j--])) {
if (j<0) return true;
}
else return false;
}
return false;
}
template <class T> inline bool contains (const std::basic_string<T>& s1, const T* s2) { return contains(s1, std::basic_string<T>(s2)); }
template <class T> inline bool startsWith (const std::basic_string<T>& s1, const T* s2) { return startsWith(s1, std::basic_string<T>(s2)); }
template <class T> inline bool endsWith (const std::basic_string<T>& s1, const T* s2) { return endsWith(s1, std::basic_string<T>(s2)); }
#endif
/*
#include<iostream>
using namespace std;
int main (int argc, char** argv) {
string str = "bobobababibibubu";
cout << str << endl;
replace(str, "b", "k");
cout << str << endl;
replace(str, "ko", "zi");
cout << str << endl;
}
*/