TDME2  1.9.200
StringTools.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <string_view>
5 #include <vector>
6 
7 #include <tdme/tdme.h>
10 
11 using std::string;
12 using std::string_view;
13 using std::vector;
14 
16 
17 /**
18  * String tools class
19  * @author Andreas Drewke
20  */
22 {
23 public:
24  /**
25  * Checks if string starts with prefix
26  * @param src source string
27  * @param prefix prefix string
28  * @return bool
29  */
30  inline static const bool startsWith(const string& src, const string& prefix) {
31  return src.find(prefix) == 0;
32  }
33 
34  /**
35  * Checks if string starts with prefix
36  * @param src source string
37  * @param prefix prefix string
38  * @return bool
39  */
40  inline static const bool viewStartsWith(const string_view& src, const string& prefix) {
41  return src.find(prefix) == 0;
42  }
43 
44  /**
45  * Checks if string ends with suffix
46  * @param src source string
47  * @param suffix suffix string
48  * @return bool
49  */
50  inline static const bool endsWith(const string& src, const string& suffix) {
51  return
52  src.size() >= suffix.size() &&
53  src.compare(src.size() - suffix.size(), suffix.size(), suffix) == 0;
54  }
55 
56  /**
57  * Checks if string ends with suffix
58  * @param src source string
59  * @param suffix suffix string
60  * @return bool
61  */
62  inline static const bool viewEndsWith(const string_view& src, const string& suffix) {
63  return
64  src.size() >= suffix.size() &&
65  src.compare(src.size() - suffix.size(), suffix.size(), suffix) == 0;
66  }
67 
68  /**
69  * Replace char with another char
70  * @param src source string to be processed
71  * @param what what to replace
72  * @param by to replace by
73  * @param beginIndex index to begin with
74  * @return new string
75  */
76  static const string replace(const string& src, const char what, const char by, int beginIndex = 0);
77 
78  /**
79  * Replace string with another string
80  * @param src source string to be processed
81  * @param what what to replace
82  * @param by to replace by
83  * @param beginIndex index to begin with
84  * @return new string
85  */
86  static const string replace(const string& src, const string& what, const string& by, int beginIndex = 0);
87 
88  /**
89  * Finds index of given character
90  * @param src source string
91  * @param what what
92  * @param beginIndex index to begin with
93  * @return index or -1 if not found
94  */
95  inline static int32_t indexOf(const string& src, char what, int beginIndex = 0) {
96  return src.find(what, beginIndex);
97  }
98 
99  /**
100  * Finds first index of given string
101  * @param src source string
102  * @param what what
103  * @param beginIndex index to begin with
104  * @return index or -1 if not found
105  */
106  inline static int32_t indexOf(const string& src, const string& what, int beginIndex = 0) {
107  return src.find(what, beginIndex);
108  }
109 
110  /**
111  * Finds first index of given character
112  * @param src source string
113  * @param what what
114  * @param beginIndex index to begin with
115  * @return index or -1 if not found
116  */
117  inline static int32_t firstIndexOf(const string& src, char what, int beginIndex = 0) {
118  return src.find_first_of(what, beginIndex);
119  }
120 
121  /**
122  * Finds first index of characters provided within given string
123  * @param src source string
124  * @param what what
125  * @param beginIndex index to begin with
126  * @return index or -1 if not found
127  */
128  inline static int32_t firstIndexOf(const string& src, const string& what, int beginIndex = 0) {
129  return src.find_first_of(what, beginIndex);
130  }
131 
132  /**
133  * Finds last index of given character
134  * @param src source string
135  * @param what what
136  * @param beginIndex index to begin with
137  * @return index or -1 if not found
138  */
139  inline static int32_t lastIndexOf(const string& src, char what, int beginIndex = -1) {
140  return src.find_last_of(what, beginIndex);
141  }
142 
143  /**
144  * Finds last index of characters provided within given string
145  * @param src source string
146  * @param what what
147  * @param beginIndex index to begin with
148  * @return index or -1 if not found
149  */
150  inline static int32_t lastIndexOf(const string& src, const string& what, int beginIndex = -1) {
151  return src.find_last_of(what, beginIndex);
152  }
153 
154  /**
155  * Returns substring of given string from begin index
156  * @param src source string
157  * @param beginIndex begin index
158  * @return new string
159  */
160  inline static const string substring(const string& src, int32_t beginIndex) {
161  return src.substr(beginIndex);
162  }
163 
164  /**
165  * Returns substring of given string from begin index
166  * @param src source string
167  * @param beginIndex begin index
168  * @return new string
169  */
170  inline static const string_view viewSubstring(const string_view& src, int32_t beginIndex) {
171  return src.substr(beginIndex);
172  }
173 
174  /**
175  * Returns substring of given string from begin index to end index
176  * @param src source string
177  * @param beginIndex begin index
178  * @param endIndex end index
179  * @return new string
180  */
181  inline static const string substring(const string& src, int32_t beginIndex, int32_t endIndex) {
182  return src.substr(beginIndex, endIndex - beginIndex);
183  }
184 
185  /**
186  * Returns substring of given string from begin index to end index
187  * @param src source string
188  * @param beginIndex begin index
189  * @param endIndex end index
190  * @return new string
191  */
192  inline static const string_view viewSubstring(const string_view& src, int32_t beginIndex, int32_t endIndex) {
193  return src.substr(beginIndex, endIndex - beginIndex);
194  }
195 
196  /**
197  * Checks if string equals ignoring case
198  * @param string1 string 1
199  * @param string2 string 2
200  * @return equals
201  */
202  static bool equalsIgnoreCase(const string& string1, const string& string2);
203 
204  /**
205  * Trim string
206  * @param src source string
207  * @return trimmed string
208  */
209  static const string trim(const string& src);
210 
211  /**
212  * Trim string
213  * @param src source string
214  * @return trimmed string
215  */
216  static const string_view viewTrim(const string_view& src);
217 
218  /**
219  * Transform string to lower case
220  * @param src source string
221  * @return transformed string
222  */
223  static const string toLowerCase(const string& src);
224 
225  /**
226  * Transform string to upper case
227  * @param src source string
228  * @return transformed string
229  */
230  static const string toUpperCase(const string& src);
231 
232  /**
233  * Check if pattern matches whole string
234  * @param src source string to test
235  * @param pattern pattern
236  * @return if pattern matches whole string
237  */
238  static bool regexMatch(const string& src, const string& pattern);
239 
240  /**
241  * Do regex pattern search
242  * @param src source string to test
243  * @param pattern pattern
244  * @return if search was successful
245  */
246  static bool regexSearch(const string& src, const string& pattern);
247 
248  /**
249  * Replace regex pattern with given string
250  * @param src source string to operate on
251  * @param pattern pattern to search
252  * @param by string that will replace pattern occurrances
253  */
254  static const string regexReplace(const string& src, const string& pattern, const string& by);
255 
256  /**
257  * Tokenize
258  * @param str string to tokenize
259  * @param delimiters delimiters
260  * @param emptyTokens include empty tokens
261  * @return tokens
262  */
263  static const vector<string> tokenize(const string& str, const string& delimiters, bool emptyTokens = false);
264 
265  /**
266  * Pad a string left
267  * @param src source
268  * @param by by
269  * @param toSize to size
270  */
271  inline static const string padLeft(const string& src, const string& by, int toSize) {
272  auto result = src;
273  while (result.size() < toSize) result = by + result;
274  return result;
275  }
276 
277  /**
278  * Pad a string right
279  * @param src source
280  * @param by by
281  * @param toSize to size
282  */
283  inline static const string padRight(const string& src, const string& by, int toSize) {
284  auto result = src;
285  while (result.size() < toSize) result = result + by;
286  return result;
287  }
288 
289  /**
290  * Indent a string
291  * @param src source
292  * @param with with
293  * @param count count
294  */
295  inline static const string indent(const string& src, const string& with, int count) {
296  string indentString;
297  for (auto i = 0; i < count; i++) indentString+= with;
298  return indentString + src;
299  }
300 
301  /**
302  * Get Utf8 string length
303  * @param str string
304  * @return utf8 string length
305  */
306  inline static int getUtf8Length(const string& str) {
307  UTF8CharacterIterator u8It(str);
308  while (u8It.hasNext() == true) u8It.next();
309  return u8It.getCharacterPosition();
310  }
311 
312  /**
313  * Get Utf8 binary buffer index
314  * @param str string
315  * @param charIdx character index
316  * @return UTF binary buffer position from given character/code point index
317  */
318  inline static int getUtf8BinaryIndex(const string& str, int charIdx) {
319  UTF8CharacterIterator u8It(str);
320  u8It.seekCharacterPosition(charIdx);
321  return u8It.getBinaryPosition();
322  }
323 
324 };
325 
String tools class.
Definition: StringTools.h:22
static const bool viewStartsWith(const string_view &src, const string &prefix)
Checks if string starts with prefix.
Definition: StringTools.h:40
static const string_view viewSubstring(const string_view &src, int32_t beginIndex)
Returns substring of given string from begin index.
Definition: StringTools.h:170
static int32_t lastIndexOf(const string &src, char what, int beginIndex=-1)
Finds last index of given character.
Definition: StringTools.h:139
static int32_t firstIndexOf(const string &src, char what, int beginIndex=0)
Finds first index of given character.
Definition: StringTools.h:117
static const bool endsWith(const string &src, const string &suffix)
Checks if string ends with suffix.
Definition: StringTools.h:50
static const string_view viewSubstring(const string_view &src, int32_t beginIndex, int32_t endIndex)
Returns substring of given string from begin index to end index.
Definition: StringTools.h:192
static const string trim(const string &src)
Trim string.
Definition: StringTools.cpp:51
static int getUtf8BinaryIndex(const string &str, int charIdx)
Get Utf8 binary buffer index.
Definition: StringTools.h:318
static const string regexReplace(const string &src, const string &pattern, const string &by)
Replace regex pattern with given string.
static int getUtf8Length(const string &str)
Get Utf8 string length.
Definition: StringTools.h:306
static bool equalsIgnoreCase(const string &string1, const string &string2)
Checks if string equals ignoring case.
Definition: StringTools.cpp:43
static const string substring(const string &src, int32_t beginIndex, int32_t endIndex)
Returns substring of given string from begin index to end index.
Definition: StringTools.h:181
static const bool viewEndsWith(const string_view &src, const string &suffix)
Checks if string ends with suffix.
Definition: StringTools.h:62
static bool regexSearch(const string &src, const string &pattern)
Do regex pattern search.
static int32_t indexOf(const string &src, const string &what, int beginIndex=0)
Finds first index of given string.
Definition: StringTools.h:106
static int32_t indexOf(const string &src, char what, int beginIndex=0)
Finds index of given character.
Definition: StringTools.h:95
static const string toUpperCase(const string &src)
Transform string to upper case.
Definition: StringTools.cpp:94
static const string_view viewTrim(const string_view &src)
Trim string.
Definition: StringTools.cpp:76
static const bool startsWith(const string &src, const string &prefix)
Checks if string starts with prefix.
Definition: StringTools.h:30
static const string padLeft(const string &src, const string &by, int toSize)
Pad a string left.
Definition: StringTools.h:271
static const string substring(const string &src, int32_t beginIndex)
Returns substring of given string from begin index.
Definition: StringTools.h:160
static const string indent(const string &src, const string &with, int count)
Indent a string.
Definition: StringTools.h:295
static int32_t lastIndexOf(const string &src, const string &what, int beginIndex=-1)
Finds last index of characters provided within given string.
Definition: StringTools.h:150
static int32_t firstIndexOf(const string &src, const string &what, int beginIndex=0)
Finds first index of characters provided within given string.
Definition: StringTools.h:128
static bool regexMatch(const string &src, const string &pattern)
Check if pattern matches whole string.
static const string toLowerCase(const string &src)
Transform string to lower case.
Definition: StringTools.cpp:88
static const string replace(const string &src, const char what, const char by, int beginIndex=0)
Replace char with another char.
Definition: StringTools.cpp:27
static const string padRight(const string &src, const string &by, int toSize)
Pad a string right.
Definition: StringTools.h:283
static const vector< string > tokenize(const string &str, const string &delimiters, bool emptyTokens=false)
Tokenize.
UTF8 string character iterator.
void seekCharacterPosition(int position) const
Seek character position.