SGL
privatestrlib.h
1 /*
2  * File: privatestrlib.h
3  * ---------------------
4  * This file exports several useful string functions that are not
5  * included in the C++ string library.
6  * This functionality is considered "private" and not to be used by students.
7  *
8  * @version 2021/04/09
9  * - moved to private SGL namespace
10  * - renamed some functions to remove 'string' prefix
11  * @version 2021/04/03
12  * - removed dependency on custom collections
13  * @version 2018/11/14
14  * - added std::to_string for bool, char, pointer, and generic template type T
15  * @version 2018/09/25
16  * - added doc comments for new documentation generation
17  * @version 2018/09/02
18  * - added padLeft, padRight
19  * @version 2016/11/09
20  * - added boolalpha to writeGenericValue (improves bool printing in
21  * collection toString output)
22  * @version 2016/10/30
23  * - added overloads that take type char instead of string:
24  * stringContains, stringIndexOf, stringJoin, stringLastIndexOf, stringReplace,
25  * stringSplit, toLowerCase, toUpperCase
26  * @version 2016/10/26
27  * - bug fix for stringLastIndexOf default index arg
28  * @version 2016/10/13
29  * - modified writeGenericValue, writeQuotedString to return ostream
30  * @version 2016/08/03
31  * - modified readGenericValue not to throw error() on parse failures
32  * (needed to support idiomatic silent-failing >> operators)
33  * @version 2015/10/26
34  * - added charToInteger/integerToChar functions
35  * @version 2015/08/02
36  * - added htmlEncode/Decode functions (not 100% perfect but works for common cases)
37  * @version 2014/10/19
38  * - alphabetized functions
39  * - added several 'inPlace' variants of existing functions that return strings
40  * @version 2014/10/08
41  * - removed dependency on 'using namespace' statement
42  */
43 
44 
45 #ifndef _private_strlib_h
46 #define _private_strlib_h
47 
48 #include <iostream>
49 #include <sstream>
50 #include <string>
51 #include <vector>
52 
53 namespace sgl {
54 namespace priv {
55 namespace strlib {
56 
60 string boolToString(bool b);
61 
65 string boolToString(int b);
66 
72 int charToInteger(char c);
73 
78 string charToString(char c);
79 
83 bool contains(const string& s, char ch);
84 
88 bool contains(const string& s, const string& substring);
89 
96 string doubleToString(double d);
97 
102 bool endsWith(const string& str, const string& suffix);
103 
108 bool endsWith(const string& str, char suffix);
109 
114 bool equalsIgnoreCase(const string& s1, const string& s2);
115 
123 string htmlDecode(const string& s);
124 
131 string htmlEncode(const string& s);
132 
139 int indexOf(const string& s, char ch, int startIndex = 0);
140 
147 int indexOf(const string& s, const string& substring, int startIndex = 0);
148 
155 char integerToChar(int n);
156 
162 string integerToString(int n, int radix = 10);
163 
170 string join(const std::vector<string>& v, char delimiter = '\n');
171 
178 string join(const std::vector<string>& v, const string& delimiter = "\n");
179 
186 int lastIndexOf(const string& s, char ch, int startIndex = (int) string::npos);
187 
194 int lastIndexOf(const string& s, const string& substring, int startIndex = (int) string::npos);
195 
201 string longToString(long n, int radix = 10);
202 
213 string padLeft(const string& s, int length, char fill = ' ');
214 
225 string padRight(const string& s, int length);
226 
231 string pointerToString(void* p);
232 
238 string realToString(double d);
239 
248 string replace(const string& str, char old, char replacement, int limit = -1);
249 
258 string replace(const string& str, const string& old, const string& replacement, int limit = -1);
259 
264 int replaceInPlace(string& str, char old, char replacement, int limit = -1);
265 
270 int replaceInPlace(string& str, const string& old, const string& replacement, int limit = -1);
271 
278 std::vector<string> split(const string& str, char delimiter, int limit = -1);
279 
286 std::vector<string> split(const string& str, const string& delimiter, int limit = -1);
287 
292 bool startsWith(const string& str, char prefix);
293 
298 bool startsWith(const string& str, const string& prefix);
299 
303 bool stringIsBool(const string& str);
304 
311 bool stringIsDouble(const string& str); // alias
312 
319 bool stringIsInteger(const string& str, int radix = 10);
320 
327 bool stringIsLong(const string& str, int radix = 10);
328 
334 bool stringIsReal(const string& str);
335 
341 bool stringToBool(const string& str);
342 
348 char stringToChar(const string& str);
349 
357 double stringToDouble(const string& str); // alias
358 
367 int stringToInteger(const string& str, int radix = 10);
368 
377 long stringToLong(const string& str, int radix = 10);
378 
386 double stringToReal(const string& str);
387 
392 char toLowerCase(char ch);
393 
398 string toLowerCase(const string& str);
399 
404 void toLowerCaseInPlace(string& str);
405 
410 char toUpperCase(char ch);
411 
416 string toUpperCase(const string& str);
417 
422 void toUpperCaseInPlace(string& str);
423 
428 string trim(const string& str);
429 
434 void trimInPlace(string& str);
435 
440 string trimEnd(const string& str);
441 
446 void trimEndInPlace(string& str);
447 
452 string trimStart(const string& str);
453 
458 void trimStartInPlace(string& str);
459 
464 string urlDecode(const string& str);
465 
470 void urlDecodeInPlace(string& str);
471 
476 string urlEncode(const string& str);
477 
482 void urlEncodeInPlace(string& str);
483 
484 } // namespace strlib
485 } // namespace priv
486 } // namespace sgl
487 
488 // add to_string overloads for some common types missing from C++ standard
489 namespace std {
490 
497 bool stob(const string& str);
498 
505 char stoc(const string& str);
506 
510 string to_string(bool b);
511 
516 string to_string(char c);
517 
522 string to_string(void* p);
523 
527 template <typename T>
528 string to_string(const T& value) {
529  std::ostringstream out;
530  out << value; // if you get an error here, your type might not have a << operator
531  return out.str();
532 }
533 } // namespace std
534 
535 #endif // _private_strlib_h
STL namespace.
Definition: console.h:45
string to_string(const GTableIndex &value)
Returns a string representation of this location, such as "r2c17".
Definition: gtable.cpp:1064