GDevelop Core
Core library for developing platforms and tools compatible with GDevelop.
String.h
1 /*
2  * GDevelop Core
3  * Copyright 2015 Victor Levasseur ([email protected]).
4  * This project is released under the MIT License.
5  */
6 
7 // NOLINTBEGIN
8 
9 #ifndef GDCORE_UTF8_STRING_H
10 #define GDCORE_UTF8_STRING_H
11 
12 #include <functional>
13 #include <iostream>
14 #include <iterator>
15 #include <sstream>
16 #include <string>
17 #include <vector>
18 
19 #include "GDCore/Utf8/utf8.h"
20 
21 namespace gd
22 {
23 
24 class String;
25 
32 class GD_CORE_API String
33 {
34 
35 public:
36 
37  using value_type = char32_t;
38  using reference = char32_t&;
39  using const_reference = const char32_t&;
40  using pointer = char32_t*;
41  using const_pointer = const char32_t*;
42 
43  using size_type = std::string::size_type;
44  using difference_type = std::string::difference_type;
45 
46  static constexpr size_type npos = -1;
47 
48  template<class T>
49  class GD_CORE_API StringIterator : public std::iterator<std::bidirectional_iterator_tag, String::value_type, String::difference_type>
50  {
51  friend class String;
52  friend class StringIterator<const T>;
53 
54  public:
55  StringIterator() : strIt() {};
56 
57  StringIterator(const StringIterator<T> &other) : strIt(other.strIt) {}
58  template<class U> StringIterator(const StringIterator<U> &other) : strIt(other.strIt) {} //Convert from const_iterator to iterator
59 
60  StringIterator<T>& operator=(const StringIterator<T> &other) { strIt = other.strIt; return *this; }
61 
62  String::value_type operator*() {return ::utf8::unchecked::peek_next(strIt);}
63 
64  StringIterator<T>& operator++() { ::utf8::unchecked::next(strIt); return *this; }
65  StringIterator<T> operator++(int) { StringIterator<T> tmp(*this); operator++(); return tmp; }
66  StringIterator<T>& operator--() { ::utf8::unchecked::prior(strIt); return *this; }
67  StringIterator<T> operator--(int) { StringIterator<T> tmp(*this); operator--(); return tmp; }
68 
69  bool operator==(const StringIterator<T> &other) { return (strIt == other.strIt); }
70  bool operator!=(const StringIterator<T> &other) { return !operator==(other); }
71 
72  bool operator<(const StringIterator<T> &other) { return (strIt < other.strIt); }
73  bool operator<=(const StringIterator<T> &other) { return (strIt <= other.strIt); }
74  bool operator>(const StringIterator<T> &other) { return (strIt > other.strIt); }
75  bool operator>=(const StringIterator<T> &other) { return (strIt >= other.strIt); }
76 
77  T base() const {return strIt;}
78 
79  private:
80  StringIterator(T strIt) : strIt(strIt) {};
81  T strIt;
82  };
83 
86  using reverse_iterator = std::reverse_iterator<iterator>;
87  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
88 
97  String();
98 
110  String(const char *characters);
111 
121  String(const std::u32string &string);
122 
141  String& operator=(const char *characters);
142 
143  String& operator=(const std::u32string &string);
144 
157  bool empty() const { return m_string.size() == 0; }
158 
162  size_type size() const;
163 
167  size_type length() const { return size(); };
168 
174  void clear() { m_string.clear(); }
175 
176  void reserve(gd::String::size_type size) { m_string.reserve(size); }
177 
190  String::iterator begin();
191 
195  String::const_iterator begin() const;
196 
200  String::iterator end();
201 
205  String::const_iterator end() const;
206 
220  template<typename T>
221  static String From(T value)
222  {
223  static_assert(!std::is_same<T, std::string>::value, "Can't use gd::String::From with std::string.");
224 
225  std::ostringstream oss;
226  oss.precision(17);
227  oss << value;
228  return gd::String(oss.str().c_str());
229  }
230 
235  template<typename T>
236  T To() const
237  {
238  static_assert(!std::is_same<T, std::string>::value, "Can't use gd::String::To with std::string.");
239 
240  T value;
241  std::istringstream oss(m_string);
242  oss >> value;
243  return value;
244  }
245 
261  static String FromLocale( const std::string &localizedString );
262 
266  static String FromUTF32( const std::u32string &string );
267 
271  static String FromUTF8( const std::string &utf8Str );
272 
276  static String FromWide( const std::wstring &wstr );
277 
292  std::string ToLocale() const;
293 
297  std::u32string ToUTF32() const;
298 
302  std::string ToUTF8() const;
303 
308  std::wstring ToWide() const;
309 
322  bool IsValid() const;
323 
328  String& ReplaceInvalid( value_type replacement = 0xfffd );
329 
345  value_type operator[]( const size_type position ) const;
346 
350  std::string& Raw() { return m_string; }
351 
355  const std::string& Raw() const { return m_string; }
356 
360  const char* c_str() const { return m_string.c_str(); }
361 
371  String& operator+=( const String &other );
372 
373  String& operator+=( const char *other );
374 
375  String& operator+=( value_type character );
376 
383  void push_back( value_type character );
384 
391  void pop_back();
392 
400  String& insert( size_type pos, const String &str );
401 
409  String& replace( iterator i1, iterator i2, const String &str );
410 
418  String& replace( iterator i1, iterator i2, size_type n, const char c );
419 
427  String& replace( size_type pos, size_type len, const String &str );
428 
436  String& replace( size_type pos, size_type len, const char c );
437 
446  String& replace_if( iterator i1, iterator i2, std::function<bool(char32_t)> p, const String &str );
447 
455  String& RemoveConsecutiveOccurrences(iterator i1, iterator i2, const char c);
456 
463  iterator erase( iterator first, iterator last );
464 
470  iterator erase( iterator p );
471 
478  void erase( size_type pos = 0, size_type len = npos );
479 
503  std::vector<String> Split( value_type delimiter ) const;
504 
514  String CaseFold() const;
515 
520  String UpperCase() const;
521 
526  String LowerCase() const;
527 
531  String CapitalizeFirstLetter() const;
532 
539  String FindAndReplace(String search, String replacement, bool all = true) const;
540 
545  String LeftTrim(const gd::String& chars = " \t\n\v\f\r")
546  {
547  String trimmedString(*this);
548  trimmedString.erase(0, trimmedString.find_first_not_of(chars));
549  return trimmedString;
550  }
551 
556  String RightTrim(const gd::String& chars = " \t\n\v\f\r")
557  {
558  String trimmedString(*this);
559  trimmedString.erase(trimmedString.find_last_not_of(chars) + 1);
560  return trimmedString;
561  }
562 
567  String Trim(const gd::String& chars = " \t\n\v\f\r")
568  {
569  return LeftTrim(chars).RightTrim(chars);
570  }
571 
575  enum NormForm
576  {
577  NFD,
578  NFC,
581  };
582 
587  String& Normalize(NormForm form = NFC);
588 
592  String substr( size_type start = 0, size_type length = npos ) const;
593 
597  size_type find( const String &search, size_type pos = 0 ) const;
598 
602  size_type find( const char *search, size_type pos = 0 ) const;
603 
607  size_type find( const value_type search, size_type pos = 0 ) const;
608 
612  size_type rfind( const String &search, size_type pos = npos ) const;
613 
617  size_type rfind( const char *search, size_type pos = npos ) const;
618 
622  size_type rfind( const value_type &search, size_type pos = npos ) const;
623 
631  size_type find_first_of( const String &match, size_type startPos = 0 ) const;
632 
640  size_type find_first_not_of( const String &not_match, size_type startPos = 0 ) const;
641 
650  size_type find_last_of( const String &match, size_type endPos = npos ) const;
651 
660  size_type find_last_not_of( const String &not_match, size_type endPos = npos ) const;
661 
665  int compare( const String &other ) const;
666 
674  size_type FindCaseInsensitive( const String &search, size_type pos = 0 ) const;
675 
680 private:
681  std::string m_string;
682 
683 };
684 
694 String GD_CORE_API operator+(String lhs, const String &rhs);
695 
701 String GD_CORE_API operator+(String lhs, const char *rhs);
702 
708 String GD_CORE_API operator+(const char *lhs, const String &rhs);
709 
710 const String& GD_CORE_API operator||(const String &lhs, const String &rhs);
711 
712 String GD_CORE_API operator||(String lhs, const char *rhs);
713 
724 bool GD_CORE_API operator==( const String &lhs, const String &rhs );
726 bool GD_CORE_API operator==( const String &lhs, const char *rhs );
728 bool GD_CORE_API operator==( const char *lhs, const String &rhs );
729 
731 bool GD_CORE_API operator!=( const String &lhs, const String &rhs );
733 bool GD_CORE_API operator!=( const String &lhs, const char *rhs );
735 bool GD_CORE_API operator!=( const char *lhs, const String &rhs );
736 
738 bool GD_CORE_API operator<( const String &lhs, const String &rhs );
740 bool GD_CORE_API operator<( const String &lhs, const char *rhs );
742 bool GD_CORE_API operator<( const char *lhs, const String &rhs );
743 
745 bool GD_CORE_API operator<=( const String &lhs, const String &rhs );
747 bool GD_CORE_API operator<=( const String &lhs, const char *rhs );
749 bool GD_CORE_API operator<=( const char *lhs, const String &rhs );
750 
752 bool GD_CORE_API operator>( const String &lhs, const String &rhs );
754 bool GD_CORE_API operator>( const String &lhs, const char *rhs );
756 bool GD_CORE_API operator>( const char *lhs, const String &rhs );
757 
759 bool GD_CORE_API operator>=( const String &lhs, const String &rhs );
761 bool GD_CORE_API operator>=( const String &lhs, const char *rhs );
763 bool GD_CORE_API operator>=( const char *lhs, const String &rhs );
764 
783 std::ostream& GD_CORE_API operator<<(std::ostream &os, const String &str);
784 
791 std::istream& GD_CORE_API operator>>(std::istream &is, String &str);
792 
803 bool GD_CORE_API CaseSensitiveEquiv( String lhs, String rhs, bool compat = true );
804 
810 bool GD_CORE_API CaseInsensitiveEquiv( const String &lhs, const String &rhs, bool compat = true );
811 
812 }
813 
814 namespace std
815 {
819  template <> struct GD_CORE_API hash<gd::String>
820  {
821  size_t operator()(const gd::String & x) const
822  {
823  return hash<std::string>()(x.Raw());
824  }
825  };
826 }
827 
828 #endif
829 
830 
905 // NOLINTEND
Definition: String.h:50
String represents an UTF8 encoded string.
Definition: String.h:33
const std::string & Raw() const
Get the raw UTF8-encoded std::string.
Definition: String.h:355
static String From(T value)
Method to create a gd::String from a number (float, double, int, ...)
Definition: String.h:221
std::string & Raw()
Get the raw UTF8-encoded std::string.
Definition: String.h:350
size_type find_first_not_of(const String &not_match, size_type startPos=0) const
Searches the string for the first character that doesn't match any of the characters specified in its...
Definition: String.cpp:585
void clear()
Clear the string.
Definition: String.h:174
String LeftTrim(const gd::String &chars=" \t\n\v\f\r")
Removes the specified characters (by default all the "whitespaces" and line breaks) from the beginnin...
Definition: String.h:545
String RightTrim(const gd::String &chars=" \t\n\v\f\r")
Removes the specified characters (by default all the "whitespaces" and line breaks) from the end of t...
Definition: String.h:556
String Trim(const gd::String &chars=" \t\n\v\f\r")
Removes the specified characters (by default all the "whitespaces" and line breaks) from the beginnin...
Definition: String.h:567
size_type find_last_not_of(const String &not_match, size_type endPos=npos) const
Searches the string for the last character that doesn't match any of the characters specified in its ...
Definition: String.cpp:619
bool empty() const
Returns true if the string is empty.
Definition: String.h:157
NormForm
Definition: String.h:576
@ NFD
Normalization Form Decomposition: characters are decomposed by canonical equivalence,...
Definition: String.h:577
@ NFKC
Normalization Form Compatibility Composition: characters are decomposed by compatibility,...
Definition: String.h:580
@ NFKD
Normalization Form Compatibility Decomposition: characters are decomposed by compatibility,...
Definition: String.h:579
@ NFC
Normalization Form Composition: characters are decomposed and then recomposed by canonical equivalenc...
Definition: String.h:578
iterator erase(iterator first, iterator last)
Erase the characters between first and last (last not included).
Definition: String.cpp:330
const char * c_str() const
Get the C-string.
Definition: String.h:360
T To() const
Method to convert the string to a number.
Definition: String.h:236
size_type length() const
Returns the string's length.
Definition: String.h:167
Definition: CommonTools.h:24
std::ostream & operator<<(std::ostream &os, ExpressionCompletionDescription const &value)
Turn an ExpressionCompletionDescription to a string.
Definition: ExpressionCompletionFinder.cpp:19