MinitScript  0.9.31 PRE-BETA
StringTools.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <regex>
4 #include <string>
5 #include <string_view>
6 #include <vector>
7 
10 
11 using std::smatch;
12 using std::string;
13 using std::string_view;
14 using std::vector;
15 
16 /**
17  * String tools class
18  * @author Andreas Drewke
19  */
21 {
22 public:
23  /**
24  * Checks if string starts with prefix
25  * @param str string
26  * @param prefix prefix string
27  * @return if string starts with prefix
28  */
29  inline static const bool startsWith(const string& str, const string& prefix) {
30  return str.find(prefix) == 0;
31  }
32 
33  /**
34  * Checks if string starts with prefix
35  * @param str string
36  * @param prefix prefix string
37  * @return if string starts with prefix
38  */
39  inline static const bool viewStartsWith(const string_view& str, const string_view& prefix) {
40  return str.find(prefix) == 0;
41  }
42 
43  /**
44  * Checks if string ends with suffix
45  * @param str string
46  * @param suffix suffix string
47  * @return if string ends with suffix
48  */
49  inline static const bool endsWith(const string& str, const string& suffix) {
50  return
51  str.size() >= suffix.size() &&
52  str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
53  }
54 
55  /**
56  * Checks if string ends with suffix
57  * @param str string
58  * @param suffix suffix string
59  * @return if string ends with suffix
60  */
61  inline static const bool viewEndsWith(const string_view& str, const string_view& suffix) {
62  return
63  str.size() >= suffix.size() &&
64  str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
65  }
66 
67  /**
68  * Replace char with another char
69  * @param str string
70  * @param what what to replace
71  * @param by to replace by
72  * @param beginIndex index to begin with
73  * @return replace result
74  */
75  static const string replace(const string& str, const char what, const char by, int64_t beginIndex = 0);
76 
77  /**
78  * Replace string with another string
79  * @param str string
80  * @param what what to replace
81  * @param by to replace by
82  * @param beginIndex index to begin with
83  * @return replace result
84  */
85  static const string replace(const string& str, const string& what, const string& by, int64_t beginIndex = 0);
86 
87  /**
88  * Finds first index of given character
89  * @param str string
90  * @param what what
91  * @param beginIndex begin index
92  * @return index or string::npos if not found
93  */
94  inline static int64_t indexOf(const string& str, char what, int64_t beginIndex = 0) {
95  return str.find(what, beginIndex);
96  }
97 
98  /**
99  * Finds first index of given string
100  * @param str string
101  * @param what what
102  * @param beginIndex begin index
103  * @return index or string::npos if not found
104  */
105  inline static int64_t indexOf(const string& str, const string& what, int64_t beginIndex = 0) {
106  return str.find(what, beginIndex);
107  }
108 
109  /**
110  * Finds first index of given character
111  * @param str string
112  * @param what what
113  * @param beginIndex begin index
114  * @return index or string::npos if not found
115  */
116  inline static int64_t firstIndexOf(const string& str, char what, int64_t beginIndex = 0) {
117  return indexOf(str, what, beginIndex);
118  }
119 
120  /**
121  * Finds first index of given string
122  * @param str string
123  * @param what what
124  * @param beginIndex begin index
125  * @return index or string::npos if not found
126  */
127  inline static int64_t firstIndexOf(const string& str, const string& what, int64_t beginIndex = 0) {
128  return indexOf(str, what, beginIndex);
129  }
130 
131  /**
132  * Finds last index of given character
133  * @param str string
134  * @param what what
135  * @param endIndex end index or string::npos
136  * @return index or string::npos if not found
137  */
138  inline static int64_t lastIndexOf(const string& str, const char what, int64_t endIndex = string::npos) {
139  return str.rfind(what, endIndex);
140  }
141 
142  /**
143  * Finds last index of given string
144  * @param str string
145  * @param what what
146  * @param endIndex end index or string::npos
147  * @return index or string::npos if not found
148  */
149  inline static int64_t lastIndexOf(const string& str, const string& what, int64_t endIndex = string::npos) {
150  return str.rfind(what, endIndex);
151  }
152 
153  /**
154  * Finds first index of character provided within given string
155  * @param str string
156  * @param what what
157  * @param beginIndex begin index
158  * @return index or string::npos if not found
159  */
160  inline static int64_t firstIndexOfChar(const string& str, char what, int64_t beginIndex = 0) {
161  return str.find_first_of(what, beginIndex);
162  }
163 
164  /**
165  * Finds first index of characters provided within given string
166  * @param str string
167  * @param what what
168  * @param beginIndex begin index
169  * @return index or string::npos if not found
170  */
171  inline static int64_t firstIndexOfChars(const string& str, const string& what, int64_t beginIndex = 0) {
172  return str.find_first_of(what, beginIndex);
173  }
174 
175  /**
176  * Finds last index of character provided within given string
177  * @param str string
178  * @param what what
179  * @param endIndex end index or string::npos
180  * @return index or string::npos if not found
181  */
182  inline static int64_t lastIndexOfChar(const string& str, char what, int64_t endIndex = string::npos) {
183  return str.find_last_of(what, endIndex);
184  }
185 
186  /**
187  * Finds last index of characters provided within given string
188  * @param str string
189  * @param what what
190  * @param endIndex end index or string::npos
191  * @return index or string::npos if not found
192  */
193  inline static int64_t lastIndexOfChars(const string& str, const string& what, int64_t endIndex = string::npos) {
194  return str.find_last_of(what, endIndex);
195  }
196 
197  /**
198  * Returns substring of given string from begin index
199  * @param str string
200  * @param beginIndex begin index
201  * @return substring result
202  */
203  inline static const string substring(const string& str, int64_t beginIndex) {
204  return str.substr(beginIndex);
205  }
206 
207  /**
208  * Returns substring of given string from begin index
209  * @param str string
210  * @param beginIndex begin index
211  * @return substring result
212  */
213  inline static const string_view viewSubstring(const string_view& str, int64_t beginIndex) {
214  return str.substr(beginIndex);
215  }
216 
217  /**
218  * Returns substring of given string from begin index to end index
219  * @param str string
220  * @param beginIndex begin index
221  * @param endIndex end index
222  * @return substring result
223  */
224  inline static const string substring(const string& str, int64_t beginIndex, int64_t endIndex) {
225  return str.substr(beginIndex, endIndex - beginIndex);
226  }
227 
228  /**
229  * Returns substring of given string from begin index to end index
230  * @param str string
231  * @param beginIndex begin index
232  * @param endIndex end index
233  * @return substring result
234  */
235  inline static const string_view viewSubstring(const string_view& str, int64_t beginIndex, int64_t endIndex) {
236  return str.substr(beginIndex, endIndex - beginIndex);
237  }
238 
239  /**
240  * Checks if strings equal ignoring case
241  * @param string1 string 1
242  * @param string2 string 2
243  * @return equality
244  */
245  static bool equalsIgnoreCase(const string& string1, const string& string2);
246 
247  /**
248  * Trim string
249  * @param str string
250  * @return trimmed string
251  */
252  static const string trim(const string& str);
253 
254  /**
255  * Trim string
256  * @param str string
257  * @return trimmed string
258  */
259  static const string_view viewTrim(const string_view& str);
260 
261  /**
262  * Transform string to lower case
263  * @param str string
264  * @return lowercase string
265  */
266  static const string toLowerCase(const string& str);
267 
268  /**
269  * Transform string to upper case
270  * @param str string
271  * @return uppercase string
272  */
273  static const string toUpperCase(const string& str);
274 
275  /**
276  * Check if pattern matches whole string
277  * @param str string
278  * @param pattern pattern
279  * @param matches matches
280  * @return if pattern matches whole string
281  */
282  static bool regexMatch(const string& str, const string& pattern, smatch* matches = nullptr);
283 
284  /**
285  * Do regex pattern search
286  * @param str string
287  * @param pattern pattern
288  * @param matches matches
289  * @return if search was successful
290  */
291  static bool regexSearch(const string& str, const string& pattern, smatch* matches = nullptr);
292 
293  /**
294  * Replace regex pattern with given string
295  * @param str string
296  * @param pattern pattern
297  * @param by replace string
298  * @return replace result
299  */
300  static const string regexReplace(const string& str, const string& pattern, const string& by);
301 
302  /**
303  * Tokenize
304  * @param str string
305  * @param delimiters delimiters
306  * @param emptyTokens include empty tokens
307  * @return tokens
308  */
309  static const vector<string> tokenize(const string& str, const string& delimiters, bool emptyTokens = false);
310 
311  /**
312  * Pad a string left
313  * @param str string
314  * @param by by
315  * @param toSize to size
316  * @return padded string
317  */
318  inline static const string padLeft(const string& str, const string& by, int64_t toSize) {
319  auto result = str;
320  while (result.size() < toSize) result = by + result;
321  return result;
322  }
323 
324  /**
325  * Pad a string right
326  * @param str string
327  * @param by by
328  * @param toSize to size
329  * @return padded string
330  */
331  inline static const string padRight(const string& str, const string& by, int64_t toSize) {
332  auto result = str;
333  while (result.size() < toSize) result = result + by;
334  return result;
335  }
336 
337  /**
338  * Indent a string
339  * @param str string
340  * @param with with
341  * @param count count
342  * @return resulting string
343  */
344  inline static const string indent(const string& str, const string& with, int64_t count) {
345  string indentString;
346  for (auto i = 0; i < count; i++) indentString+= with;
347  return indentString + str;
348  }
349 
350  /**
351  * Generate a string
352  * @param what what
353  * @param count count
354  * @return resulting string
355  */
356  inline static const string generate(const string& what, int64_t count = 1) {
357  string result;
358  for (auto i = 0; i < count; i++) result+= what;
359  return result;
360  }
361 
362  /**
363  * Get UTF8 string length
364  * @param str string
365  * @return UTF8 string length
366  */
367  static int64_t getUTF8Length(const string& str);
368 
369  /**
370  * Get UTF8 character at given index
371  * @param str string
372  * @param index index
373  * @param cache UTF8 position cache
374  */
375  static const string getUTF8CharAt(const string& str, int64_t index);
376 
377  /**
378  * Get UTF8 binary buffer index
379  * @param str string
380  * @param charIdx character index
381  * @return UTF binary buffer position from given character/code point index
382  */
383  static int64_t getUTF8BinaryIndex(const string& str, int64_t charIdx);
384 
385 };
386 
static int64_t firstIndexOf(const string &str, const string &what, int64_t beginIndex=0)
Finds first index of given string.
Definition: StringTools.h:127
static const string getUTF8CharAt(const string &str, int64_t index)
Get UTF8 character at given index.
static const string_view viewTrim(const string_view &str)
Trim string.
Definition: StringTools.cpp:82
static const bool startsWith(const string &str, const string &prefix)
Checks if string starts with prefix.
Definition: StringTools.h:29
static int64_t firstIndexOf(const string &str, char what, int64_t beginIndex=0)
Finds first index of given character.
Definition: StringTools.h:116
static const string substring(const string &str, int64_t beginIndex)
Returns substring of given string from begin index.
Definition: StringTools.h:203
static int64_t getUTF8BinaryIndex(const string &str, int64_t charIdx)
Get UTF8 binary buffer index.
static const string padRight(const string &str, const string &by, int64_t toSize)
Pad a string right.
Definition: StringTools.h:331
static const string_view viewSubstring(const string_view &str, int64_t beginIndex)
Returns substring of given string from begin index.
Definition: StringTools.h:213
static const string toLowerCase(const string &str)
Transform string to lower case.
Definition: StringTools.cpp:94
static const bool viewEndsWith(const string_view &str, const string_view &suffix)
Checks if string ends with suffix.
Definition: StringTools.h:61
static const string indent(const string &str, const string &with, int64_t count)
Indent a string.
Definition: StringTools.h:344
static bool equalsIgnoreCase(const string &string1, const string &string2)
Checks if strings equal ignoring case.
Definition: StringTools.cpp:49
static const string toUpperCase(const string &str)
Transform string to upper case.
static int64_t getUTF8Length(const string &str)
Get UTF8 string length.
static int64_t lastIndexOf(const string &str, const string &what, int64_t endIndex=string::npos)
Finds last index of given string.
Definition: StringTools.h:149
static int64_t lastIndexOfChar(const string &str, char what, int64_t endIndex=string::npos)
Finds last index of character provided within given string.
Definition: StringTools.h:182
static const string replace(const string &str, const char what, const char by, int64_t beginIndex=0)
Replace char with another char.
Definition: StringTools.cpp:33
static int64_t indexOf(const string &str, const string &what, int64_t beginIndex=0)
Finds first index of given string.
Definition: StringTools.h:105
static const bool viewStartsWith(const string_view &str, const string_view &prefix)
Checks if string starts with prefix.
Definition: StringTools.h:39
static const bool endsWith(const string &str, const string &suffix)
Checks if string ends with suffix.
Definition: StringTools.h:49
static const string padLeft(const string &str, const string &by, int64_t toSize)
Pad a string left.
Definition: StringTools.h:318
static int64_t firstIndexOfChars(const string &str, const string &what, int64_t beginIndex=0)
Finds first index of characters provided within given string.
Definition: StringTools.h:171
static const string regexReplace(const string &str, const string &pattern, const string &by)
Replace regex pattern with given string.
static const string_view viewSubstring(const string_view &str, int64_t beginIndex, int64_t endIndex)
Returns substring of given string from begin index to end index.
Definition: StringTools.h:235
static int64_t lastIndexOf(const string &str, const char what, int64_t endIndex=string::npos)
Finds last index of given character.
Definition: StringTools.h:138
static const string trim(const string &str)
Trim string.
Definition: StringTools.cpp:57
static int64_t firstIndexOfChar(const string &str, char what, int64_t beginIndex=0)
Finds first index of character provided within given string.
Definition: StringTools.h:160
static bool regexSearch(const string &str, const string &pattern, smatch *matches=nullptr)
Do regex pattern search.
static const string substring(const string &str, int64_t beginIndex, int64_t endIndex)
Returns substring of given string from begin index to end index.
Definition: StringTools.h:224
static int64_t lastIndexOfChars(const string &str, const string &what, int64_t endIndex=string::npos)
Finds last index of characters provided within given string.
Definition: StringTools.h:193
static const string generate(const string &what, int64_t count=1)
Generate a string.
Definition: StringTools.h:356
static bool regexMatch(const string &str, const string &pattern, smatch *matches=nullptr)
Check if pattern matches whole string.
static int64_t indexOf(const string &str, char what, int64_t beginIndex=0)
Finds first index of given character.
Definition: StringTools.h:94
static const vector< string > tokenize(const string &str, const string &delimiters, bool emptyTokens=false)
Tokenize.