SGL
privatestrlib.h
1 /*
2  * File: strlib.h
3  * --------------
4  * This file exports several useful string functions that are not
5  * included in the C++ string library.
6  *
7  * @version 2021/04/03
8  * - removed dependency on custom collections
9  * @version 2018/11/14
10  * - added std::to_string for bool, char, pointer, and generic template type T
11  * @version 2018/09/25
12  * - added doc comments for new documentation generation
13  * @version 2018/09/02
14  * - added padLeft, padRight
15  * @version 2016/11/09
16  * - added boolalpha to writeGenericValue (improves bool printing in
17  * collection toString output)
18  * @version 2016/10/30
19  * - added overloads that take type char instead of string:
20  * stringContains, stringIndexOf, stringJoin, stringLastIndexOf, stringReplace,
21  * stringSplit, toLowerCase, toUpperCase
22  * @version 2016/10/26
23  * - bug fix for stringLastIndexOf default index arg
24  * @version 2016/10/13
25  * - modified writeGenericValue, writeQuotedString to return ostream
26  * @version 2016/08/03
27  * - modified readGenericValue not to throw error() on parse failures
28  * (needed to support idiomatic silent-failing >> operators)
29  * @version 2015/10/26
30  * - added charToInteger/integerToChar functions
31  * @version 2015/08/02
32  * - added htmlEncode/Decode functions (not 100% perfect but works for common cases)
33  * @version 2014/10/19
34  * - alphabetized functions
35  * - added several 'inPlace' variants of existing functions that return strings
36  * @version 2014/10/08
37  * - removed dependency on 'using namespace' statement
38  */
39 
40 
41 #ifndef _strlib_h
42 #define _strlib_h
43 
44 #include <iostream>
45 #include <sstream>
46 #include <string>
47 #include <vector>
48 
52 string boolToString(bool b);
53 
57 string boolToString(int b);
58 
64 int charToInteger(char c);
65 
70 string charToString(char c);
71 
78 string doubleToString(double d);
79 
84 bool endsWith(const string& str, const string& suffix);
85 
90 bool endsWith(const string& str, char suffix);
91 
96 bool equalsIgnoreCase(const string& s1, const string& s2);
97 
105 string htmlDecode(const string& s);
106 
113 string htmlEncode(const string& s);
114 
121 char integerToChar(int n);
122 
128 string integerToString(int n, int radix = 10);
129 
135 string longToString(long n, int radix = 10);
136 
147 string padLeft(const string& s, int length, char fill = ' ');
148 
159 string padRight(const string& s, int length);
160 
165 string pointerToString(void* p);
166 
172 string realToString(double d);
173 
178 bool startsWith(const string& str, char prefix);
179 
184 bool startsWith(const string& str, const string& prefix);
185 
189 bool stringContains(const string& s, char ch);
190 
194 bool stringContains(const string& s, const string& substring);
195 
202 int stringIndexOf(const string& s, char ch, int startIndex = 0);
203 
210 int stringIndexOf(const string& s, const string& substring, int startIndex = 0);
211 
215 bool stringIsBool(const string& str);
216 
223 bool stringIsDouble(const string& str); // alias
224 
231 bool stringIsInteger(const string& str, int radix = 10);
232 
239 bool stringIsLong(const string& str, int radix = 10);
240 
246 bool stringIsReal(const string& str);
247 
254 string stringJoin(const std::vector<string>& v, char delimiter = '\n');
255 
262 string stringJoin(const std::vector<string>& v, const string& delimiter = "\n");
263 
270 int stringLastIndexOf(const string& s, char ch, int startIndex = (int) string::npos);
271 
278 int stringLastIndexOf(const string& s, const string& substring, int startIndex = (int) string::npos);
279 
288 string stringReplace(const string& str, char old, char replacement, int limit = -1);
289 
298 string stringReplace(const string& str, const string& old, const string& replacement, int limit = -1);
299 
304 int stringReplaceInPlace(string& str, char old, char replacement, int limit = -1);
305 
310 int stringReplaceInPlace(string& str, const string& old, const string& replacement, int limit = -1);
311 
318 std::vector<string> stringSplit(const string& str, char delimiter, int limit = -1);
319 
326 std::vector<string> stringSplit(const string& str, const string& delimiter, int limit = -1);
327 
333 bool stringToBool(const string& str);
334 
340 char stringToChar(const string& str);
341 
349 double stringToDouble(const string& str); // alias
350 
359 int stringToInteger(const string& str, int radix = 10);
360 
369 long stringToLong(const string& str, int radix = 10);
370 
378 double stringToReal(const string& str);
379 
384 char toLowerCase(char ch);
385 
390 string toLowerCase(const string& str);
391 
396 void toLowerCaseInPlace(string& str);
397 
402 char toUpperCase(char ch);
403 
408 string toUpperCase(const string& str);
409 
414 void toUpperCaseInPlace(string& str);
415 
420 string trim(const string& str);
421 
426 void trimInPlace(string& str);
427 
432 string trimEnd(const string& str);
433 
438 void trimEndInPlace(string& str);
439 
444 string trimStart(const string& str);
445 
450 void trimStartInPlace(string& str);
451 
456 string urlDecode(const string& str);
457 
462 void urlDecodeInPlace(string& str);
463 
468 string urlEncode(const string& str);
469 
474 void urlEncodeInPlace(string& str);
475 
476 // add to_string overloads for some common types missing from C++ standard
477 namespace std {
484 bool stob(const string& str);
485 
492 char stoc(const string& str);
493 
497 string to_string(bool b);
498 
503 string to_string(char c);
504 
509 string to_string(void* p);
510 
514 template <typename T>
515 string to_string(const T& value) {
516  std::ostringstream out;
517  out << value; // if you get an error here, your type might not have a << operator
518  return out.str();
519 }
520 } // namespace std
521 
522 #endif // _strlib_h
STL namespace.