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 << value;
227  return gd::String(oss.str().c_str());
228  }
229 
234  template<typename T>
235  T To() const
236  {
237  static_assert(!std::is_same<T, std::string>::value, "Can't use gd::String::To with std::string.");
238 
239  T value;
240  std::istringstream oss(m_string);
241  oss >> value;
242  return value;
243  }
244 
260  static String FromLocale( const std::string &localizedString );
261 
265  static String FromUTF32( const std::u32string &string );
266 
270  static String FromUTF8( const std::string &utf8Str );
271 
275  static String FromWide( const std::wstring &wstr );
276 
291  std::string ToLocale() const;
292 
296  std::u32string ToUTF32() const;
297 
301  std::string ToUTF8() const;
302 
307  std::wstring ToWide() const;
308 
321  bool IsValid() const;
322 
327  String& ReplaceInvalid( value_type replacement = 0xfffd );
328 
344  value_type operator[]( const size_type position ) const;
345 
349  std::string& Raw() { return m_string; }
350 
354  const std::string& Raw() const { return m_string; }
355 
359  const char* c_str() const { return m_string.c_str(); }
360 
370  String& operator+=( const String &other );
371 
372  String& operator+=( const char *other );
373 
374  String& operator+=( value_type character );
375 
382  void push_back( value_type character );
383 
390  void pop_back();
391 
399  String& insert( size_type pos, const String &str );
400 
408  String& replace( iterator i1, iterator i2, const String &str );
409 
417  String& replace( iterator i1, iterator i2, size_type n, const char c );
418 
426  String& replace( size_type pos, size_type len, const String &str );
427 
435  String& replace( size_type pos, size_type len, const char c );
436 
445  String& replace_if( iterator i1, iterator i2, std::function<bool(char32_t)> p, const String &str );
446 
454  String& RemoveConsecutiveOccurrences(iterator i1, iterator i2, const char c);
455 
462  iterator erase( iterator first, iterator last );
463 
469  iterator erase( iterator p );
470 
477  void erase( size_type pos = 0, size_type len = npos );
478 
502  std::vector<String> Split( value_type delimiter ) const;
503 
513  String CaseFold() const;
514 
519  String UpperCase() const;
520 
525  String LowerCase() const;
526 
530  String CapitalizeFirstLetter() const;
531 
538  String FindAndReplace(String search, String replacement, bool all = true) const;
539 
544  String LeftTrim(const gd::String& chars = " \t\n\v\f\r")
545  {
546  String trimmedString(*this);
547  trimmedString.erase(0, trimmedString.find_first_not_of(chars));
548  return trimmedString;
549  }
550 
555  String RightTrim(const gd::String& chars = " \t\n\v\f\r")
556  {
557  String trimmedString(*this);
558  trimmedString.erase(trimmedString.find_last_not_of(chars) + 1);
559  return trimmedString;
560  }
561 
566  String Trim(const gd::String& chars = " \t\n\v\f\r")
567  {
568  return LeftTrim(chars).RightTrim(chars);
569  }
570 
574  enum NormForm
575  {
576  NFD,
577  NFC,
580  };
581 
586  String& Normalize(NormForm form = NFC);
587 
591  String substr( size_type start = 0, size_type length = npos ) const;
592 
596  size_type find( const String &search, size_type pos = 0 ) const;
597 
601  size_type find( const char *search, size_type pos = 0 ) const;
602 
606  size_type find( const value_type search, size_type pos = 0 ) const;
607 
611  size_type rfind( const String &search, size_type pos = npos ) const;
612 
616  size_type rfind( const char *search, size_type pos = npos ) const;
617 
621  size_type rfind( const value_type &search, size_type pos = npos ) const;
622 
630  size_type find_first_of( const String &match, size_type startPos = 0 ) const;
631 
639  size_type find_first_not_of( const String &not_match, size_type startPos = 0 ) const;
640 
649  size_type find_last_of( const String &match, size_type endPos = npos ) const;
650 
659  size_type find_last_not_of( const String &not_match, size_type endPos = npos ) const;
660 
664  int compare( const String &other ) const;
665 
673  size_type FindCaseInsensitive( const String &search, size_type pos = 0 ) const;
674 
679 private:
680  std::string m_string;
681 
682 };
683 
693 String GD_CORE_API operator+(String lhs, const String &rhs);
694 
700 String GD_CORE_API operator+(String lhs, const char *rhs);
701 
707 String GD_CORE_API operator+(const char *lhs, const String &rhs);
708 
709 const String& GD_CORE_API operator||(const String &lhs, const String &rhs);
710 
711 String GD_CORE_API operator||(String lhs, const char *rhs);
712 
723 bool GD_CORE_API operator==( const String &lhs, const String &rhs );
725 bool GD_CORE_API operator==( const String &lhs, const char *rhs );
727 bool GD_CORE_API operator==( const char *lhs, const String &rhs );
728 
730 bool GD_CORE_API operator!=( const String &lhs, const String &rhs );
732 bool GD_CORE_API operator!=( const String &lhs, const char *rhs );
734 bool GD_CORE_API operator!=( const char *lhs, const String &rhs );
735 
737 bool GD_CORE_API operator<( const String &lhs, const String &rhs );
739 bool GD_CORE_API operator<( const String &lhs, const char *rhs );
741 bool GD_CORE_API operator<( const char *lhs, const String &rhs );
742 
744 bool GD_CORE_API operator<=( const String &lhs, const String &rhs );
746 bool GD_CORE_API operator<=( const String &lhs, const char *rhs );
748 bool GD_CORE_API operator<=( const char *lhs, const String &rhs );
749 
751 bool GD_CORE_API operator>( const String &lhs, const String &rhs );
753 bool GD_CORE_API operator>( const String &lhs, const char *rhs );
755 bool GD_CORE_API operator>( const char *lhs, const String &rhs );
756 
758 bool GD_CORE_API operator>=( const String &lhs, const String &rhs );
760 bool GD_CORE_API operator>=( const String &lhs, const char *rhs );
762 bool GD_CORE_API operator>=( const char *lhs, const String &rhs );
763 
782 std::ostream& GD_CORE_API operator<<(std::ostream &os, const String &str);
783 
790 std::istream& GD_CORE_API operator>>(std::istream &is, String &str);
791 
802 bool GD_CORE_API CaseSensitiveEquiv( String lhs, String rhs, bool compat = true );
803 
809 bool GD_CORE_API CaseInsensitiveEquiv( const String &lhs, const String &rhs, bool compat = true );
810 
811 }
812 
813 namespace std
814 {
818  template <> struct GD_CORE_API hash<gd::String>
819  {
820  size_t operator()(const gd::String & x) const
821  {
822  return hash<std::string>()(x.Raw());
823  }
824  };
825 }
826 
827 #endif
828 
829 
904 // 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:354
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:349
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:544
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:555
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:566
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:575
@ NFD
Normalization Form Decomposition: characters are decomposed by canonical equivalence,...
Definition: String.h:576
@ NFKC
Normalization Form Compatibility Composition: characters are decomposed by compatibility,...
Definition: String.h:579
@ NFKD
Normalization Form Compatibility Decomposition: characters are decomposed by compatibility,...
Definition: String.h:578
@ NFC
Normalization Form Composition: characters are decomposed and then recomposed by canonical equivalenc...
Definition: String.h:577
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:359
T To() const
Method to convert the string to a number.
Definition: String.h:235
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