35 #include "basics.h" |
35 #include "basics.h" |
36 #include "list.h" |
36 #include "list.h" |
37 |
37 |
38 BEGIN_ZFC_NAMESPACE |
38 BEGIN_ZFC_NAMESPACE |
39 |
39 |
|
40 using String = std::string; |
|
41 using StringList = std::vector<std::string>; |
|
42 using namespace std::string_literals; |
40 |
43 |
41 class String |
44 std::string to_lowercase(const std::string& string); |
42 { |
45 std::string join_string_list(const std::vector<std::string>& strings, const std::string& delim); |
43 public: |
46 std::string mid(const std::string& str, int rangeBegin, int rangeEnd); |
44 typedef std::string::iterator Iterator; |
47 std::string right(const std::string& str, int length); |
45 typedef std::string::const_iterator ConstIterator; |
48 std::string vsprintf(const char* formatString, va_list args); |
46 |
49 std::string __cdecl sprintf(const char* formatString, ...) GNUATTRIBUTE((format(printf, 1, 2))); |
47 String(); |
50 std::string remove_range(const std::string& string, int start, int end); |
48 String(char a); |
51 void replace_all(std::string& str, const char* text, const char* replacement); |
49 String(const char* data); |
52 bool starts_with(const std::string& str, const std::string& other); |
50 String(const std::string& data); |
53 std::vector<std::string> split(const std::string& string, const std::string& delimeter); |
51 String(const Vector<char>& data); |
54 std::optional<long> to_int(const char* str, int base = 10); |
52 |
55 void normalize(std::string& string, int (*filter)(int) = std::isspace); |
53 void append(const char* text); |
|
54 void append(char character); |
|
55 void append(const String& text); |
|
56 ConstIterator begin() const; |
|
57 Iterator begin(); |
|
58 int compare(const String &other) const; |
|
59 int count(char character) const; |
|
60 const char* chars() const; |
|
61 void clear(); |
|
62 ConstIterator end() const; |
|
63 Iterator end(); |
|
64 bool endsWith(const String &other) const; |
|
65 int find(const char* subString, int startingPosition = 0) const; |
|
66 int find(char character, int startingPosition = 0) const; |
|
67 int indexDifference(int a, int b); |
|
68 void insert(int position, char character); |
|
69 void insert(int position, const char* string); |
|
70 bool isEmpty() const; |
|
71 bool isNumeric() const; |
|
72 void modifyIndex(int &a) const; |
|
73 int findLast(const char* subString, int startingPosition = -1) const; |
|
74 int length() const; |
|
75 bool maskAgainst(const String &pattern) const; |
|
76 String md5() const; |
|
77 String mid(int rangeBegin, int rangeEnd) const; |
|
78 void normalize(int(*filter)(int) = &isspace); |
|
79 String normalized(int(*filter)(int) = &isspace) const; |
|
80 void prepend(String text); |
|
81 void remove(int position, int length); |
|
82 void removeAt(int position); |
|
83 void removeFromEnd(int length); |
|
84 void removeFromStart(int length); |
|
85 void replace(const char* text, const char* replacement); |
|
86 void replace(int position, int amount, const String &text); |
|
87 String right(int length) const; |
|
88 void shrinkToFit(); |
|
89 class StringList split(const String &delimeter) const; |
|
90 class StringList split(char delimeter) const; |
|
91 void __cdecl sprintf(const char* fmtstr, ...); |
|
92 bool startsWith(const String &other) const; |
|
93 const std::string& stdString() const; |
|
94 void strip(char unwanted); |
|
95 void strip(const List<char> &unwanted); |
|
96 const unsigned char* toBytes() const; |
|
97 double toDouble(bool* ok = nullptr) const; |
|
98 float toFloat(bool* ok = nullptr) const; |
|
99 long toInt(bool* ok = nullptr, int base = 10) const; |
|
100 String toLowerCase() const; |
|
101 String toUpperCase() const; |
|
102 void vsprintf(const char* fmtstr, va_list args); |
|
103 |
|
104 static String fromNumber(short int a); |
|
105 static String fromNumber(int a); |
|
106 static String fromNumber(long int a); |
|
107 static String fromNumber(unsigned short int a); |
|
108 static String fromNumber(unsigned int a); |
|
109 static String fromNumber(unsigned long int a); |
|
110 static String fromNumber(double a); |
|
111 static String fromBytes(const ByteArray& bytes); |
|
112 |
|
113 String operator+(const String& data) const; |
|
114 String operator+(const char* data) const; |
|
115 String operator+(int num) const; |
|
116 String& operator+=(const String& data); |
|
117 String& operator+=(const char* data); |
|
118 String& operator+=(int num); |
|
119 String& operator+=(char data); |
|
120 char& operator[](int i); |
|
121 char operator[](int i) const; |
|
122 bool operator==(const String& other) const; |
|
123 bool operator==(const char* other) const; |
|
124 bool operator!=(const String& other) const; |
|
125 bool operator!=(const char* other) const; |
|
126 bool operator>(const String& other) const; |
|
127 bool operator<(const String& other) const; |
|
128 bool operator>=(const String& other) const; |
|
129 bool operator<=(const String& other) const; |
|
130 operator const char*() const; |
|
131 operator const std::string&() const; |
|
132 |
|
133 private: |
|
134 std::string m_string; |
|
135 }; |
|
136 |
|
137 |
|
138 class StringList : public List<String> |
|
139 { |
|
140 public: |
|
141 StringList(); |
|
142 StringList(int numvalues); |
|
143 StringList(const List<String>& other); |
|
144 String join(const String& delim); |
|
145 }; |
|
146 |
|
147 |
|
148 inline bool operator==(const char* a, const String& b); |
|
149 inline String operator+(const char* a, const String& b); |
|
150 |
|
151 // -------------------------------------------------------------------------------------------------------------------- |
|
152 |
|
153 /*! |
|
154 * \brief Constructs an empty string. |
|
155 */ |
|
156 inline String::String() {} |
|
157 |
|
158 /*! |
|
159 * \brief Constructs a string from a single character. |
|
160 * \param character Character to create a string out of. |
|
161 */ |
|
162 inline String::String(char character) |
|
163 { |
|
164 char buffer[2] = { character, '\0' }; |
|
165 m_string = buffer; |
|
166 } |
|
167 |
|
168 /*! |
|
169 * \brief Constructs a string from a char-array. |
|
170 * \param string char-array to convert. |
|
171 */ |
|
172 inline String::String(const char* string) : |
|
173 m_string(string) {} |
|
174 |
|
175 /*! |
|
176 * \brief Constructs a string out of a \c std::string . |
|
177 * \param string \c std::string to base the construction on. |
|
178 */ |
|
179 inline String::String(const std::string& string) : |
|
180 m_string(string) {} |
|
181 |
|
182 /*! |
|
183 * \brief Constructs a string out of a vector of characters. The vector does not have to be null-terminated. |
|
184 * \param charVector Vector of characters to construct the string out of. |
|
185 */ |
|
186 inline String::String(const Vector<char>& charVector) : |
|
187 m_string(charVector.data(), charVector.size()) {} |
|
188 |
|
189 /*! |
|
190 * \returns a constant iterator to the beginning of the string. |
|
191 */ |
|
192 inline String::ConstIterator String::begin() const |
|
193 { |
|
194 return m_string.cbegin(); |
|
195 } |
|
196 |
|
197 /*! |
|
198 * \returns the string's contents as a char-array. |
|
199 */ |
|
200 inline const char* String::chars() const |
|
201 { |
|
202 return m_string.c_str(); |
|
203 } |
|
204 |
|
205 /*! |
|
206 * \returns the string's constant end-iterator. |
|
207 */ |
|
208 inline String::ConstIterator String::end() const |
|
209 { |
|
210 return m_string.end(); |
|
211 } |
|
212 |
|
213 /*! |
|
214 * \returns whether or not the string is empty. |
|
215 */ |
|
216 inline bool String::isEmpty() const |
|
217 { |
|
218 return m_string[0] == '\0'; |
|
219 } |
|
220 |
|
221 /*! |
|
222 * \returns the length of the string. |
|
223 */ |
|
224 inline int String::length() const |
|
225 { |
|
226 return m_string.length(); |
|
227 } |
|
228 |
|
229 /*! |
|
230 * \returns the underlying \c std::string . |
|
231 */ |
|
232 inline const std::string& String::stdString() const |
|
233 { |
|
234 return m_string; |
|
235 } |
|
236 |
|
237 /*! |
|
238 * \brief Adds text from a char-array to the end of the string. |
|
239 * \param text Text to append. |
|
240 */ |
|
241 inline void String::append(const char* text) |
|
242 { |
|
243 m_string.append(text); |
|
244 } |
|
245 |
|
246 /*! |
|
247 * \brief Adds text to the end of the string. |
|
248 * \param text Text to append. |
|
249 */ |
|
250 inline void String::append(char character) |
|
251 { |
|
252 m_string.push_back(character); |
|
253 } |
|
254 |
|
255 /*! |
|
256 * \brief Adds text from another string to the end of this string. |
|
257 * \param text Text to append. |
|
258 */ |
|
259 inline void String::append(const String& text) |
|
260 { |
|
261 m_string.append(text.chars()); |
|
262 } |
|
263 |
|
264 /*! |
|
265 * \returns a mutable iterator to the beginning of the string. |
|
266 */ |
|
267 inline String::Iterator String::begin() |
|
268 { |
|
269 return m_string.begin(); |
|
270 } |
|
271 |
|
272 /*! |
|
273 * \brief Clears the string. |
|
274 */ |
|
275 inline void String::clear() |
|
276 { |
|
277 m_string.clear(); |
|
278 } |
|
279 |
|
280 /*! |
|
281 * \returns the string's mutable end-iterator. |
|
282 */ |
|
283 inline String::Iterator String::end() |
|
284 { |
|
285 return m_string.end(); |
|
286 } |
|
287 |
|
288 /*! |
|
289 * \brief Compares two string indices, supporting negatives as offsets from the end of string. |
|
290 * \param a First index to compare |
|
291 * \param b Second index to compare |
|
292 * \returns the difference of two indices. |
|
293 */ |
|
294 inline int String::indexDifference(int a, int b) |
|
295 { |
|
296 modifyIndex(a); |
|
297 modifyIndex(b); |
|
298 return b - a; |
|
299 } |
|
300 |
|
301 /*! |
|
302 * \brief Inserts a character into the string. |
|
303 * \param position Position in the string where to insert the character into. |
|
304 * \param character Character to insert into the string. |
|
305 */ |
|
306 inline void String::insert(int position, char character) |
|
307 { |
|
308 m_string.insert(m_string.begin() + position, character); |
|
309 } |
|
310 |
|
311 /*! |
|
312 * \brief Inserts a substring into the string. |
|
313 * \param position Position in the string where to insert the substring. |
|
314 * \param string Substring to insert. |
|
315 */ |
|
316 inline void String::insert(int position, const char* string) |
|
317 { |
|
318 m_string.insert(position, string); |
|
319 } |
|
320 |
|
321 /*! |
|
322 * \brief Modifies the given index so that if it is negative, it is translated into a positive index starting from the |
|
323 * end of the string. For example, an index of -1 will be modified to point to the last character in the string, |
|
324 * -2 to the second last, etc. |
|
325 * \param index Index to translate. |
|
326 */ |
|
327 inline void String::modifyIndex(int& index) const |
|
328 { |
|
329 if (index < 0) |
|
330 index = length() - index; |
|
331 } |
|
332 |
|
333 /*! |
|
334 * \brief Prepends the given text to the beginning of the string. |
|
335 * \param text Text to prepend. |
|
336 */ |
|
337 inline void String::prepend(String text) |
|
338 { |
|
339 m_string = (text + m_string).stdString(); |
|
340 } |
|
341 |
|
342 /*! |
|
343 * \brief Removes a range of text from the string. |
|
344 * \param position Position where to start removing text. |
|
345 * \param length Amount of characters to remove. |
|
346 */ |
|
347 inline void String::remove(int position, int length) |
|
348 { |
|
349 m_string.replace(position, length, ""); |
|
350 } |
|
351 |
|
352 /*! |
|
353 * \brief Removes a single character from the string. |
|
354 * \param position Position of the character to remove string from. |
|
355 */ |
|
356 inline void String::removeAt(int position) |
|
357 { |
|
358 m_string.erase(m_string.begin() + position); |
|
359 } |
|
360 |
|
361 /*! |
|
362 * \brief Removes a number of characters from the end of the string. |
|
363 * \param length Amount of characters to remove. |
|
364 */ |
|
365 inline void String::removeFromEnd(int length) |
|
366 { |
|
367 remove(this->length() - length, length); |
|
368 } |
|
369 |
|
370 /*! |
|
371 * \brief Removes a number of characters from the beginning of the string. |
|
372 * \param length Amount of characters to remove. |
|
373 */ |
|
374 inline void String::removeFromStart(int length) |
|
375 { |
|
376 remove(0, length); |
|
377 } |
|
378 |
|
379 /*! |
|
380 * \brief Replaces a range of text in the string with another. |
|
381 * \param position Position where to start replacing text. |
|
382 * \param amount Amount of characters to replace. |
|
383 * \param text Replacement string. |
|
384 */ |
|
385 inline void String::replace(int position, int amount, const String& text) |
|
386 { |
|
387 m_string.replace(position, amount, text.chars()); |
|
388 } |
|
389 |
|
390 /*! |
|
391 * \brief Shrinks the string so that it does not allocate more characters than necessary. |
|
392 */ |
|
393 inline void String::shrinkToFit() |
|
394 { |
|
395 m_string.shrink_to_fit(); |
|
396 } |
|
397 |
|
398 /*! |
|
399 * \brief Converts a number into a string, and returns a new string with the number appended to the end of the string. |
|
400 * \param number Number to convert and append. |
|
401 * \returns the resulting string. |
|
402 */ |
|
403 inline String String::operator+(int number) const |
|
404 { |
|
405 return *this + String::fromNumber(number); |
|
406 } |
|
407 |
|
408 /*! |
|
409 * \brief Appends text into the string. |
|
410 * \param text Text to append. |
|
411 * \returns a reference to this string. |
|
412 */ |
|
413 inline String& String::operator+=(const String& text) |
|
414 { |
|
415 append(text); |
|
416 return *this; |
|
417 } |
|
418 |
|
419 /*! |
|
420 * \brief Appends text into the string. |
|
421 * \param text Text to append. |
|
422 * \returns a reference to this string. |
|
423 */ |
|
424 inline String& String::operator+=(const char* text) |
|
425 { |
|
426 append(text); |
|
427 return *this; |
|
428 } |
|
429 |
|
430 /*! |
|
431 * \brief Converts a number into a string, and appends it into this string. |
|
432 * \param number The number to append. |
|
433 * \returns a refence to this string. |
|
434 */ |
|
435 inline String& String::operator+=(int number) |
|
436 { |
|
437 return operator+=(String::fromNumber(number)); |
|
438 } |
|
439 |
|
440 /*! |
|
441 * \brief Appends a character into this string. |
|
442 * \param character The character to append. |
|
443 * \return a reference to this string. |
|
444 */ |
|
445 inline String& String::operator+=(char character) |
|
446 { |
|
447 append(character); |
|
448 return *this; |
|
449 } |
|
450 |
|
451 /*! |
|
452 * \param index Index referring to a character of this string. |
|
453 * \returns an editable reference to the character pointed by the given index. |
|
454 */ |
|
455 inline char& String::operator[](int index) |
|
456 { |
|
457 return m_string[index]; |
|
458 } |
|
459 |
|
460 /*! |
|
461 * \param index Index referring to a character of this string. |
|
462 * \returns an const reference to the character pointed by the given index. |
|
463 */ |
|
464 inline char String::operator[](int index) const |
|
465 { |
|
466 return m_string[index]; |
|
467 } |
|
468 |
|
469 /*! |
|
470 * \param other String to compare with. |
|
471 * \returns whether or not this string is the same as the other string. |
|
472 */ |
|
473 inline bool String::operator==(const String& other) const |
|
474 { |
|
475 return stdString() == other.stdString(); |
|
476 } |
|
477 |
|
478 /*! |
|
479 * \param other String to compare with. |
|
480 * \returns whether or not this string is the same as the other string. |
|
481 */ |
|
482 inline bool String::operator==(const char* other) const |
|
483 { |
|
484 return m_string == other; |
|
485 } |
|
486 |
|
487 /*! |
|
488 * \param other String to compare with. |
|
489 * \returns whether or not this string is different than the other string. |
|
490 */ |
|
491 inline bool String::operator!=(const String& other) const |
|
492 { |
|
493 return stdString() != other.stdString(); |
|
494 } |
|
495 |
|
496 /*! |
|
497 * \param other String to compare with. |
|
498 * \returns whether or not this string is different than the other string. |
|
499 */ |
|
500 inline bool String::operator!=(const char* other) const |
|
501 { |
|
502 return m_string != other; |
|
503 } |
|
504 |
|
505 /*! |
|
506 * \param other String to compare with. |
|
507 * \return whether or not this string is lexicographically greater than the other string. |
|
508 */ |
|
509 inline bool String::operator>(const String& other) const |
|
510 { |
|
511 return stdString() > other.stdString(); |
|
512 } |
|
513 |
|
514 /*! |
|
515 * \param other String to compare with. |
|
516 * \return whether or not this string is lexicographically lesser than the other string. |
|
517 */ |
|
518 inline bool String::operator<(const String& other) const |
|
519 { |
|
520 return stdString() < other.stdString(); |
|
521 } |
|
522 |
|
523 /*! |
|
524 * \param other String to compare with. |
|
525 * \return whether or not this string is lexicographically at least as great as the other string. |
|
526 */ |
|
527 inline bool String::operator>=(const String& other) const |
|
528 { |
|
529 return stdString() >= other.stdString(); |
|
530 } |
|
531 |
|
532 /*! |
|
533 * \param other String to compare with. |
|
534 * \return whether or not this string is lexicographically at most as great as the other string. |
|
535 */ |
|
536 inline bool String::operator<=(const String& other) const |
|
537 { |
|
538 return stdString() <= other.stdString(); |
|
539 } |
|
540 |
|
541 /*! |
|
542 * \returns a char-array representation of this string. |
|
543 */ |
|
544 inline String::operator const char*() const |
|
545 { |
|
546 return chars(); |
|
547 } |
|
548 |
|
549 /*! |
|
550 * \returns the underlying \c std::string of this string. |
|
551 */ |
|
552 inline String::operator const std::string&() const |
|
553 { |
|
554 return stdString(); |
|
555 } |
|
556 |
|
557 /*! |
|
558 * \returns the underlying char-array representation of this string, casted to unsigned chars. |
|
559 */ |
|
560 inline const unsigned char* String::toBytes() const |
|
561 { |
|
562 return reinterpret_cast<const unsigned char*>(chars()); |
|
563 } |
|
564 |
|
565 /*! |
|
566 * \brief Constructs an empty string list. |
|
567 */ |
|
568 inline StringList::StringList() {} |
|
569 |
|
570 /*! |
|
571 * \brief Constructs a string list containing \c numvalues empty strings. |
|
572 * \param numvalues Amount of empty strings to fill. |
|
573 */ |
|
574 inline StringList::StringList(int numvalues) : |
|
575 List<String>(numvalues) {} |
|
576 |
|
577 /*! |
|
578 * \brief Constructs a string list from another list of strings. |
|
579 * \param other The list of strings to use for construction. |
|
580 */ |
|
581 inline StringList::StringList(const List<String>& other) : |
|
582 List<String>(other) {} |
|
583 |
|
584 /*! |
|
585 * \brief An \c operator== implementation that allows a char-array to be at the left side of a string comparison |
|
586 * with a \c String. |
|
587 * \param one A char-array representation of a string to compare. |
|
588 * \param other A string to compare. |
|
589 * \returns whether or not the two parameters are equal. |
|
590 */ |
|
591 inline bool operator==(const char* one, const String& other) |
|
592 { |
|
593 return other == one; |
|
594 } |
|
595 |
|
596 /*! |
|
597 * \brief An \c operator+ implementation that allows a char-array to be at the left side of a string catenation |
|
598 * with a \c String. |
|
599 * \param one A char-array representation of a string to catenate. |
|
600 * \param other A string to catenate. |
|
601 * \returns the catenated string. |
|
602 */ |
|
603 inline String operator+(const char* one, const String& other) |
|
604 { |
|
605 return String(one) + other; |
|
606 } |
|
607 |
|
608 |
56 |
609 END_ZFC_NAMESPACE |
57 END_ZFC_NAMESPACE |