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 #ifndef GDCORE_UTF8_STRING_H
8 #define GDCORE_UTF8_STRING_H
9 
10 #include <functional>
11 #include <iostream>
12 #include <iterator>
13 #include <sstream>
14 #include <string>
15 #include <vector>
16 
17 #include "GDCore/Utf8/utf8.h"
18 
19 namespace gd
20 {
21 
22 class String;
23 
30 class GD_CORE_API String
31 {
32 
33 public:
34 
35  using value_type = char32_t;
36  using reference = char32_t&;
37  using const_reference = const char32_t&;
38  using pointer = char32_t*;
39  using const_pointer = const char32_t*;
40 
41  using size_type = std::string::size_type;
42  using difference_type = std::string::difference_type;
43 
44  static constexpr size_type npos = -1;
45 
46  template<class T>
47  class GD_CORE_API StringIterator : public std::iterator<std::bidirectional_iterator_tag, String::value_type, String::difference_type>
48  {
49  friend class String;
50  friend class StringIterator<const T>;
51 
52  public:
53  StringIterator() : strIt() {};
54 
55  StringIterator(const StringIterator<T> &other) : strIt(other.strIt) {}
56  template<class U> StringIterator(const StringIterator<U> &other) : strIt(other.strIt) {} //Convert from const_iterator to iterator
57 
58  StringIterator<T>& operator=(const StringIterator<T> &other) { strIt = other.strIt; return *this; }
59 
60  String::value_type operator*() {return ::utf8::unchecked::peek_next(strIt);}
61 
62  StringIterator<T>& operator++() { ::utf8::unchecked::next(strIt); return *this; }
63  StringIterator<T> operator++(int) { StringIterator<T> tmp(*this); operator++(); return tmp; }
64  StringIterator<T>& operator--() { ::utf8::unchecked::prior(strIt); return *this; }
65  StringIterator<T> operator--(int) { StringIterator<T> tmp(*this); operator--(); return tmp; }
66 
67  bool operator==(const StringIterator<T> &other) { return (strIt == other.strIt); }
68  bool operator!=(const StringIterator<T> &other) { return !operator==(other); }
69 
70  bool operator<(const StringIterator<T> &other) { return (strIt < other.strIt); }
71  bool operator<=(const StringIterator<T> &other) { return (strIt <= other.strIt); }
72  bool operator>(const StringIterator<T> &other) { return (strIt > other.strIt); }
73  bool operator>=(const StringIterator<T> &other) { return (strIt >= other.strIt); }
74 
75  T base() const {return strIt;}
76 
77  private:
78  StringIterator(T strIt) : strIt(strIt) {};
79  T strIt;
80  };
81 
84  using reverse_iterator = std::reverse_iterator<iterator>;
85  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
86 
95  String();
96 
108  String(const char *characters);
109 
119  String(const std::u32string &string);
120 
139  String& operator=(const char *characters);
140 
141  String& operator=(const std::u32string &string);
142 
155  bool empty() const { return m_string.size() == 0; }
156 
160  size_type size() const;
161 
165  size_type length() const { return size(); };
166 
172  void clear() { m_string.clear(); }
173 
174  void reserve(gd::String::size_type size) { m_string.reserve(size); }
175 
188  String::iterator begin();
189 
193  String::const_iterator begin() const;
194 
198  String::iterator end();
199 
203  String::const_iterator end() const;
204 
218  template<typename T>
219  static String From(T value)
220  {
221  static_assert(!std::is_same<T, std::string>::value, "Can't use gd::String::From with std::string.");
222 
223  std::ostringstream oss;
224  oss << value;
225  return gd::String(oss.str().c_str());
226  }
227 
232  template<typename T>
233  T To() const
234  {
235  static_assert(!std::is_same<T, std::string>::value, "Can't use gd::String::To with std::string.");
236 
237  T value;
238  std::istringstream oss(m_string);
239  oss >> value;
240  return value;
241  }
242 
258  static String FromLocale( const std::string &localizedString );
259 
263  static String FromUTF32( const std::u32string &string );
264 
268  static String FromUTF8( const std::string &utf8Str );
269 
273  static String FromWide( const std::wstring &wstr );
274 
289  std::string ToLocale() const;
290 
294  std::u32string ToUTF32() const;
295 
299  std::string ToUTF8() const;
300 
305  std::wstring ToWide() const;
306 
319  bool IsValid() const;
320 
325  String& ReplaceInvalid( value_type replacement = 0xfffd );
326 
342  value_type operator[]( const size_type position ) const;
343 
347  std::string& Raw() { return m_string; }
348 
352  const std::string& Raw() const { return m_string; }
353 
357  const char* c_str() const { return m_string.c_str(); }
358 
368  String& operator+=( const String &other );
369 
370  String& operator+=( const char *other );
371 
372  String& operator+=( value_type character );
373 
380  void push_back( value_type character );
381 
388  void pop_back();
389 
397  String& insert( size_type pos, const String &str );
398 
406  String& replace( iterator i1, iterator i2, const String &str );
407 
415  String& replace( iterator i1, iterator i2, size_type n, const char c );
416 
424  String& replace( size_type pos, size_type len, const String &str );
425 
433  String& replace( size_type pos, size_type len, const char c );
434 
443  String& replace_if( iterator i1, iterator i2, std::function<bool(char32_t)> p, const String &str );
444 
452  String& RemoveConsecutiveOccurrences(iterator i1, iterator i2, const char c);
453 
460  iterator erase( iterator first, iterator last );
461 
467  iterator erase( iterator p );
468 
475  void erase( size_type pos = 0, size_type len = npos );
476 
500  std::vector<String> Split( value_type delimiter ) const;
501 
511  String CaseFold() const;
512 
517  String UpperCase() const;
518 
523  String LowerCase() const;
524 
528  String CapitalizeFirstLetter() const;
529 
536  String FindAndReplace(String search, String replacement, bool all = true) const;
537 
542  String LeftTrim(const gd::String& chars = " \t\n\v\f\r")
543  {
544  String trimmedString(*this);
545  trimmedString.erase(0, trimmedString.find_first_not_of(chars));
546  return trimmedString;
547  }
548 
553  String RightTrim(const gd::String& chars = " \t\n\v\f\r")
554  {
555  String trimmedString(*this);
556  trimmedString.erase(trimmedString.find_last_not_of(chars) + 1);
557  return trimmedString;
558  }
559 
564  String Trim(const gd::String& chars = " \t\n\v\f\r")
565  {
566  return LeftTrim(chars).RightTrim(chars);
567  }
568 
572  enum NormForm
573  {
574  NFD,
575  NFC,
578  };
579 
584  String& Normalize(NormForm form = NFC);
585 
589  String substr( size_type start = 0, size_type length = npos ) const;
590 
594  size_type find( const String &search, size_type pos = 0 ) const;
595 
599  size_type find( const char *search, size_type pos = 0 ) const;
600 
604  size_type find( const value_type search, size_type pos = 0 ) const;
605 
609  size_type rfind( const String &search, size_type pos = npos ) const;
610 
614  size_type rfind( const char *search, size_type pos = npos ) const;
615 
619  size_type rfind( const value_type &search, size_type pos = npos ) const;
620 
628  size_type find_first_of( const String &match, size_type startPos = 0 ) const;
629 
637  size_type find_first_not_of( const String &not_match, size_type startPos = 0 ) const;
638 
647  size_type find_last_of( const String &match, size_type endPos = npos ) const;
648 
657  size_type find_last_not_of( const String &not_match, size_type endPos = npos ) const;
658 
662  int compare( const String &other ) const;
663 
671  size_type FindCaseInsensitive( const String &search, size_type pos = 0 ) const;
672 
677 private:
678  std::string m_string;
679 
680 };
681 
691 String GD_CORE_API operator+(String lhs, const String &rhs);
692 
698 String GD_CORE_API operator+(String lhs, const char *rhs);
699 
705 String GD_CORE_API operator+(const char *lhs, const String &rhs);
706 
707 const String& GD_CORE_API operator||(const String &lhs, const String &rhs);
708 
709 String GD_CORE_API operator||(String lhs, const char *rhs);
710 
721 bool GD_CORE_API operator==( const String &lhs, const String &rhs );
723 bool GD_CORE_API operator==( const String &lhs, const char *rhs );
725 bool GD_CORE_API operator==( const char *lhs, const String &rhs );
726 
728 bool GD_CORE_API operator!=( const String &lhs, const String &rhs );
730 bool GD_CORE_API operator!=( const String &lhs, const char *rhs );
732 bool GD_CORE_API operator!=( const char *lhs, const String &rhs );
733 
735 bool GD_CORE_API operator<( const String &lhs, const String &rhs );
737 bool GD_CORE_API operator<( const String &lhs, const char *rhs );
739 bool GD_CORE_API operator<( const char *lhs, const String &rhs );
740 
742 bool GD_CORE_API operator<=( const String &lhs, const String &rhs );
744 bool GD_CORE_API operator<=( const String &lhs, const char *rhs );
746 bool GD_CORE_API operator<=( const char *lhs, const String &rhs );
747 
749 bool GD_CORE_API operator>( const String &lhs, const String &rhs );
751 bool GD_CORE_API operator>( const String &lhs, const char *rhs );
753 bool GD_CORE_API operator>( const char *lhs, const String &rhs );
754 
756 bool GD_CORE_API operator>=( const String &lhs, const String &rhs );
758 bool GD_CORE_API operator>=( const String &lhs, const char *rhs );
760 bool GD_CORE_API operator>=( const char *lhs, const String &rhs );
761 
780 std::ostream& GD_CORE_API operator<<(std::ostream &os, const String &str);
781 
788 std::istream& GD_CORE_API operator>>(std::istream &is, String &str);
789 
800 bool GD_CORE_API CaseSensitiveEquiv( String lhs, String rhs, bool compat = true );
801 
807 bool GD_CORE_API CaseInsensitiveEquiv( const String &lhs, const String &rhs, bool compat = true );
808 
809 }
810 
811 namespace std
812 {
816  template <> struct GD_CORE_API hash<gd::String>
817  {
818  size_t operator()(const gd::String & x) const
819  {
820  return hash<std::string>()(x.Raw());
821  }
822  };
823 }
824 
825 #endif
826 
827 
Definition: String.h:48
String represents an UTF8 encoded string.
Definition: String.h:31
const std::string & Raw() const
Get the raw UTF8-encoded std::string.
Definition: String.h:352
static String From(T value)
Method to create a gd::String from a number (float, double, int, ...)
Definition: String.h:219
std::string & Raw()
Get the raw UTF8-encoded std::string.
Definition: String.h:347
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:583
void clear()
Clear the string.
Definition: String.h:172
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:542
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:553
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:564
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:617
bool empty() const
Returns true if the string is empty.
Definition: String.h:155
NormForm
Definition: String.h:573
@ NFD
Normalization Form Decomposition: characters are decomposed by canonical equivalence,...
Definition: String.h:574
@ NFKC
Normalization Form Compatibility Composition: characters are decomposed by compatibility,...
Definition: String.h:577
@ NFKD
Normalization Form Compatibility Decomposition: characters are decomposed by compatibility,...
Definition: String.h:576
@ NFC
Normalization Form Composition: characters are decomposed and then recomposed by canonical equivalenc...
Definition: String.h:575
iterator erase(iterator first, iterator last)
Erase the characters between first and last (last not included).
Definition: String.cpp:328
const char * c_str() const
Get the C-string.
Definition: String.h:357
T To() const
Method to convert the string to a number.
Definition: String.h:233
size_type length() const
Returns the string's length.
Definition: String.h:165
Definition: CommonTools.h:24
std::ostream & operator<<(std::ostream &os, ExpressionCompletionDescription const &value)
Turn an ExpressionCompletionDescription to a string.
Definition: ExpressionCompletionFinder.cpp:19