-
Notifications
You must be signed in to change notification settings - Fork 0
/
usastringfunctool.h
317 lines (237 loc) · 10.5 KB
/
usastringfunctool.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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/// -*- @c C/C++ headers -*-
/// -*- @c #include"usastringfunctool.h" -*-
/**
* @author Usama Azad @c <https://github.com/Usama-Azad>
*
* @dependencies: [ <vector> , <string> , <sstream> , <algorithm> , <initializer_list> ]
*
* This @c header is distributed in the hope that it will be useful,
* but @a WITHOUT_ANY_WARRANTY; without even the implied @c warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**/
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
#ifndef __StrFunc_H__
#define __StrFunc_H__
#ifdef __cplusplus
#include <vector>
#include <string>
#include <cstdlib>
#include <sstream>
#include <algorithm>
#include <initializer_list>
using namespace std;
/// @e convet_to_string.
/***********************************************************************
* @name ItoS
* @param val: Any integer.
* @return string: After converting given integer to string.
************************************************************************/
inline
string ItoS(const int &val);
/************************************************************************/
/***********************************************************************
* @name CtoS
* @param val: Any character.
* @return string: After converting given character to string.
************************************************************************/
inline
string CtoS(const char &val);
/************************************************************************/
/***********************************************************************
* @name FtoS
* @param val: Any float.
* @return string: After converting given float to string.
************************************************************************/
inline
string FtoS(const float &val);
/************************************************************************/
/***********************************************************************
* @name DtoS
* @param val: Any double.
* @return string: After converting given double to string.
************************************************************************/
inline
string DtoS(const double &val);
/************************************************************************/
/***********************************************************************
* @name LtoS
* @param val: Any long.
* @return string: After converting given long to string.
************************************************************************/
inline
string LtoS(const long &val);
/************************************************************************/
/***********************************************************************
* @name LDtoS
* @param val: Any long double.
* @return string: After converting given long double to string.
************************************************************************/
inline
string LDtoS(const long double &val);
/************************************************************************/
/***********************************************************************
* @name BtoS
* @param val: Any bool.
* @return string: After converting given bool to string.
************************************************************************/
inline
string BtoS(const bool &val);
/************************************************************************/
/// @a Convesions_Ends.
/// @e convet_from_string.
/***********************************************************************
* @name StoI
* @param str: Any string.
* @return int: After converting given string to int.
************************************************************************/
inline
int StoI(const string &str);
/************************************************************************/
/***********************************************************************
* @name StoC
* @param str: Any string.
* @return char: After converting given string to char.
************************************************************************/
inline
char StoC(const string &str);
/************************************************************************/
/***********************************************************************
* @name StoF
* @param str: Any string.
* @return float: After converting given string to float.
************************************************************************/
inline
float StoF(const string &str);
/************************************************************************/
/***********************************************************************
* @name StoD
* @param str: Any string.
* @return double: After converting given string to double.
************************************************************************/
inline
double StoD(const string &str);
/************************************************************************/
/***********************************************************************
* @name StoL
* @param str: Any string.
* @return long: After converting given string to long.
************************************************************************/
inline
long StoL(const string &str);
/************************************************************************/
/***********************************************************************
* @name StoLD
* @param str: Any string.
* @return long double: After converting given string to long double.
************************************************************************/
inline
long double StoLD(const string &str);
/************************************************************************/
/***********************************************************************
* @name StoB
* @param str: Any string.
* @return bool: After converting given string to bool.
************************************************************************/
inline
bool StoB(const string &str);
/************************************************************************/
/// @a Convesions_Ends.
#if __cplusplus >= 201402L
#define exchangeX std::exchange
#else
template<class X, class Y = X>
X _exchangeX(X& first, const Y& second)
{
X oldV = first;
first = second;
return oldV;
}
#define exchangeX _exchangeX
#endif
/***********************************************************************
* @name split
* @param str: const string refrence which is to be splited.
* @param delimiter: delimiter by which string is splited.
* @return vector<string>: vector of different chunks of splited string.
************************************************************************/
vector<string> split(const string& str, const char& delimiter = 0x020);
/************************************************************************/
#if __cplusplus >= 201102L
/***********************************************************************
* @name trim
* @param str: const string refrence which is to be trimed.
* @return string: Return a copy of the string with leading and trailing
* whitespace remove.
*
* @ltrim: it only trims only left spaces.
* @rtrim: it only trims only right spaces.
* @trim: it trims spaces from both sides.
* @strip: it works like trim() but it does not return any thing.
************************************************************************/
string ltrim(const string& str);
string rtrim(const string& str);
string _trim(const string& str);
void strip(string& s);
/************************************************************************/
#endif
/***********************************************************************
* @name reverse
* @param str: string and reverse it.
* @return string: reversed string.
************************************************************************/
string reverse(string str);
/************************************************************************/
/***********************************************************************
* @name replace
* @param str: string.
* @param prev: old char which is present in given string.
* @param next: new char which is to be replaced with old char in given string.
* @return string: new string with replaced all characters.
************************************************************************/
string replace(string str, char prev, char next);
/************************************************************************/
/***********************************************************************
* @name replace_first
* @param str: string.
* @param prev: old char which is present in given string.
* @param next: new char which is to be replaced with old char in given string.
* @return string: new string with only first replaced characters.
************************************************************************/
string replace_first(string str, char prev, char next);
/************************************************************************/
/***********************************************************************
* @name is_Upper
* @param str: string.
* @return bool: true if all chars in string is upperCase else false.
************************************************************************/
bool is_Upper(const string& str);
/************************************************************************/
/***********************************************************************
* @name is_Lower
* @param str: string.
* @return bool: true if all chars in string is lowerCase else false.
************************************************************************/
bool is_Lower(const string& str);
/************************************************************************/
/***********************************************************************
* @name to_UpperCase
* @param str: string.
* @return string: string with all chars in UpperCase.
************************************************************************/
string to_UpperCase(string str);
/************************************************************************/
/***********************************************************************
* @name to_LowerCase
* @param str: string.
* @return string: string with all chars in LowerCase.
************************************************************************/
string to_LowerCase(string str);
/************************************************************************/
#endif
#endif