| Leonetienne/Hazelnupp Simple, easy to use, command line parameter interface

Static Public Member Functions | List of all members
Hazelnp::StringTools Class Reference

Internal helper class. More...

`#include <StringTools.h>`

## Static Public Member Functions
static bool | Contains (const std::string &str, const char c)
| Will return wether or not a given char is in a string. More...
static std::string | Replace (const std::string &str, const char find, const std::string &subst)
| Will replace a part of a string with another string. More...
static std::string | Replace (const std::string &str, const std::string &find, const std::string &subst)
| Will replace a part of a string with another string. More...
static bool | IsNumeric (const std::string &str, const bool allowDecimalPoint=false)
| Will return true if the given string consists only of digits (including signage) More...
static bool | ParseNumber (const std::string &str, bool &out_isInt, long double &out_number)
| Will convert the number in str to a number. More...
static std::vector< std::string > | SplitString (const std::string &str, const char delimiter)
| Will split a string by a delimiter char. The delimiter will be excluded! More...
static std::vector< std::string > | SplitString (const std::string &str, const std::string &delimiter)
| Will split a string by a delimiter string. The delimiter will be excluded! More...
static std::string | ToLower (const std::string &str)
| Will make a string all lower-case. More...

## Detailed Description

Internal helper class.

Feel free to use it tho.

Definition at line 11 of file StringTools.h.

## Member Function Documentation

## ◆ Contains()

bool StringTools::Contains | ( | const std::string & | str,
|  | const char | c
| ) |  |
| static

Will return wether or not a given char is in a string.

Definition at line 5 of file StringTools.cpp.

6 {
7 for (const char& i : str)
8 if (i == c)
9 return true;
10
11 return false;
12 }

## ◆ IsNumeric()

bool StringTools::IsNumeric | ( | const std::string & | str,
|  | const bool | allowDecimalPoint = `false`
| ) |  |
| static

Will return true if the given string consists only of digits (including signage)

Definition at line 56 of file StringTools.cpp.

57 {
58 if (str.length() == 0) return false;
59
60 bool alreadyParsedDecimalPoint = false;
61 std::size_t digitCount = 0;
62
63 for (std::size_t i = 0; i < str.length(); i++)
64 {
65 if (!(
66 ((str[i] >= '0') && (str[i] <= '9')) ||
67 ((str[i] == '-') && (i == 0)) ||
68 ((str[i] == '.') && (allowDecimalPoint) && (!alreadyParsedDecimalPoint) && (digitCount > 0))
69 )) return false;
70
71
72 // Here we just have to check for the character. Not for any other conditions.
73 // Why? Because if these conditions failed, the function would have already returned false.
74 if (((str[i] >= '0') && (str[i] <= '9'))) digitCount++;
75 if (str[i] == '.') alreadyParsedDecimalPoint = true;
76 }
77
78 // Even if we did not find any invalid chars, we should still return false, if we found no digits at all.
79 return digitCount > 0;
80 }

## ◆ ParseNumber()

bool StringTools::ParseNumber | ( | const std::string & | str,
|  | bool & | out_isInt,
|  | long double & | out_number
| ) |  |
| static

Will convert the number in str to a number.

Returns wether or not the operation was successful.
Also returns wether the number is an integer, or floating point. If int, cast out_number to int.

Definition at line 82 of file StringTools.cpp.

83 {
84 bool isDecimal = false;
85
86 if (str.length() == 0) return false;
87 if (Contains(str, '.')) isDecimal = true;
88
89 if (isDecimal)
90 {
91 try
92 {
93 out_number = std::stold(str);
94 out_isInt = false;
95 }
96 catch (std::invalid_argument&)
97 {
98 return false;
99 }
100 catch (std::out_of_range&)
101 {
102 return false;
103 }
104 }
105 else
106 {
107 try
108 {
109 out_number = (long double)std::stoll(str);
110 out_isInt = true;
111 }
112 catch (std::invalid_argument&)
113 {
114 return false;
115 }
116 catch (std::out_of_range&)
117 {
118 return false;
119 }
120 }
121
122 return true;
123 }

## ◆ Replace() [1/2]

std::string StringTools::Replace | ( | const std::string & | str,
|  | const char | find,
|  | const std::string & | subst
| ) |  |
| static

Will replace a part of a string with another string.

Definition at line 14 of file StringTools.cpp.

15 {
16 std::stringstream ss;
17
18 for (std::size_t i = 0; i < str.length(); i++)
19 {
20 if (str[i] != find) ss << str[i];
21 else ss << subst;
22 }
23
24 return ss.str();
25 }

## ◆ Replace() [2/2]

std::string StringTools::Replace | ( | const std::string & | str,
|  | const std::string & | find,
|  | const std::string & | subst
| ) |  |
| static

Will replace a part of a string with another string.

Definition at line 27 of file StringTools.cpp.

28 {
29 if (find.length() == 0) return str;
30
31 std::stringstream ss;
32
33 std::size_t posFound = 0;
34 std::size_t lastFound = 0;
35
36 while (posFound != std::string::npos)
37 {
38 lastFound = posFound;
39 posFound = str.find(find, posFound);
40
41 if (posFound != std::string::npos)
42 {
43 ss << str.substr(lastFound, posFound - lastFound) << subst;
44 posFound += find.length();
45 }
46 else
47 {
48 ss << str.substr(lastFound, (str.length()) - lastFound);
49 }
50 }
51
52 return ss.str();
53 }

## ◆ SplitString() [1/2]

std::vector< std::string > StringTools::SplitString | ( | const std::string & | str,
|  | const char | delimiter
| ) |  |
| static

Will split a string by a delimiter char. The delimiter will be excluded!

Definition at line 125 of file StringTools.cpp.

126 {
127 if (str.length() == 0) return std::vector<std::string>();
128
129 return SplitString(str, delimiter);
130 }

## ◆ SplitString() [2/2]

std::vector< std::string > StringTools::SplitString | ( | const std::string & | str,
|  | const std::string & | delimiter
| ) |  |
| static

Will split a string by a delimiter string. The delimiter will be excluded!

Definition at line 132 of file StringTools.cpp.

133 {
134 if (str.length() == 0) return std::vector<std::string>();
135
136 std::vector<std::string> parts;
137
138 if (delimiter.length() == 0) // If the delimiter is "" (empty), just split between every single char. Not useful, but logical
139 {
140 for (std::size_t i = 0; i < str.length(); i++)
141 {
142 parts.push_back(std::string({ str[i] }));
143 }
144 return parts;
145 }
146
147 std::size_t posFound = 0;
148 std::size_t lastFound = 0;
149
150 while (posFound != std::string::npos)
151 {
152 lastFound = posFound;
153 posFound = str.find(delimiter, posFound);
154
155 std::string found;
156
157 if (posFound != std::string::npos)
158 {
159 found = str.substr(lastFound, posFound - lastFound);
160 posFound += delimiter.length();
161 }
162 else
163 {
164 found = str.substr(lastFound, str.length() - lastFound);
165 }
166
167 parts.push_back(found);
168 }
169
170 return parts;
171 }

## ◆ ToLower()

std::string StringTools::ToLower | ( | const std::string & | str | ) |
| static

Will make a string all lower-case.

Definition at line 173 of file StringTools.cpp.

174 {
175 std::stringstream ss;
176 for (std::size_t i = 0; i < str.length(); i++)
177 {
178 if ((str[i] >= 'A') && (str[i] <= 'Z')) ss << (char)(((int)str[i]) + 32);
179 else if (str[i] == -60) ss << (char)-28; // AE => ae
180 else if (str[i] == -42) ss << (char)-10; // OE => oe
181 else if (str[i] == -36) ss << (char)-4; // UE => ue
182 else ss << str[i];
183 }
184
185 return ss.str();
186 }

The documentation for this class was generated from the following files:

- Hazelnupp/StringTools.h
- Hazelnupp/StringTools.cpp

Hazelnp::StringTools::Contains
static bool Contains(const std::string &str, const char c)
Will return wether or not a given char is in a string.
Definition: StringTools.cpp:5
Hazelnp::StringTools::SplitString
static std::vector< std::string > SplitString(const std::string &str, const char delimiter)
Will split a string by a delimiter char. The delimiter will be excluded!
Definition: StringTools.cpp:125