You.i Engine
YiString.h
Go to the documentation of this file.
1 // © You i Labs Inc. All rights reserved.
2 #ifndef _YI_STRING_H_
3 #define _YI_STRING_H_
4 
5 #include "utility/YiStringView.h"
6 
32 class CYIString
33 {
34  friend class CYIStringView;
35  class ConstIteratorPriv;
36 
37 public:
39  typedef std::reverse_iterator<ConstIterator> ConstReverseIterator;
40 
47  {
48  public:
49  friend class CYIString;
50 
51  typedef std::bidirectional_iterator_tag iterator_category;
52  typedef char32_t value_type;
53  typedef ptrdiff_t difference_type;
54  typedef const char32_t *pointer;
55  typedef char32_t reference;
56 
57  ConstIterator();
58  ConstIterator(CYIStringView view, size_t charactersOffset);
59  ConstIterator(const ConstIterator &other);
62 
63  ConstIterator &operator=(const ConstIterator &other);
64 
68  value_type operator*() const;
69 
73  operator CYIStringView::ConstIterator() const;
74 
75  bool operator==(const ConstIterator &other) const;
76  bool operator!=(const ConstIterator &other) const;
77 
82 
86  const char *GetData() const;
87 
88  ConstReverseIterator Reverse() const;
89 
90  private:
91  static const size_t YI_STRING_ITERATOR_SIZE = sizeof(char *) * 3;
92 
93  ConstIterator(CYIStringView view, const char *pPosition);
94  char m_storage[YI_STRING_ITERATOR_SIZE];
95  };
96 
98  typedef std::reverse_iterator<Iterator> ReverseIterator;
99 
100 #ifdef DOXY
101 
106  enum class CaseMode
107  {
111  CaseSensitive,
112 
116  CaseInsensitiveASCII
117  };
118 #else
120 #endif
121 
125  static constexpr size_t NPos = ~size_t(0);
126 
130  static const CYIString &EmptyString();
131 
135  CYIString();
136 
140  CYIString(const CYIString &s);
141 
145  CYIString(CYIString &&s) noexcept;
146 
150  CYIString(CYIStringView stringView);
151 
155  CYIString(std::string &&string) noexcept;
156 
162  CYIString(const std::string &s);
163 
169  explicit CYIString(char32_t character);
170 
177 
183  CYIString(const std::wstring &s);
184 
185 #if defined(__OBJC__) || defined(DOXY)
186 
191  CYIString(const NSString *pString);
192 
198  CYIString(const CFStringRef string);
199 #endif
200 
206  CYIString(const char *pUTF8);
207 
215  CYIString(const char *pUTF8, size_t sizeBytes);
216 
224  CYIString(const char16_t *pUTF16, size_t size = NPos);
225 
233  CYIString(const char32_t *pUTF32, size_t size = NPos);
234 
242  CYIString(const wchar_t *pWideChars, size_t size = NPos);
243 
249  CYIString(const std::vector<char> &vector);
250 
256  CYIString(const std::vector<uint8_t> &vector);
257 
261  operator CYIStringView() const noexcept;
262 
266  CYIString &operator=(const CYIString &s);
267 
273  CYIString &operator=(const char *pUTF8);
274 
278  CYIString &operator=(CYIString &&s) noexcept;
279 
285  CYIString &operator=(std::string &&s) noexcept;
286 
290  bool operator<(const char *pOther) const;
291 
295  bool operator>(const char *pOther) const;
296 
300  bool operator<=(const char *pOther) const;
301 
305  bool operator>=(const char *pOther) const;
306 
312  char32_t operator[](size_t pos) const;
313 
317  void Reserve(size_t bytes);
318 
322  CYIString &operator+=(CYIStringView view);
323 
329  CYIString &operator+=(const char *pUTF8);
330 
336  template<class T, class = std::enable_if_t<!std::is_convertible<const T &, CYIStringView>::value
337 #if __cplusplus >= 201703L
338  && !std::is_convertible<const T &, std::string_view>::value
339 #endif
340  >>
341  CYIString operator+(const T &arg) const;
342 
346  CYIString operator+(const char *pUTF8) const;
347 
360  template<class T, class O = std::ostringstream>
361  static CYIString FromValue(const T &arg, bool *pError = nullptr);
362 
363 #ifdef DOXY
364 
376  static CYIString FromValue(bool arg, bool *pError = nullptr);
377 #endif
378 
390  static CYIString FromFloat(float value, uint32_t decimalPlaces = 6, bool trailingZeroes = false, bool useSignificantDigits = false);
391 
403  static CYIString FromDouble(double value, uint32_t decimalPlaces = 6, bool trailingZeroes = false, bool useSignificantDigits = false);
404 
408  static const CYIString &FromBool(bool value);
409 
413  friend std::istream &operator>>(std::istream &stream, CYIString &val);
414 
420  bool IsEmpty() const;
421 
429  bool IsNotEmpty() const;
430 
438  size_t GetLength() const;
439 
445  size_t GetSizeInBytes() const;
446 
452  bool ContainsNonASCII() const;
453 
459  char32_t At(size_t pos) const;
460 
468  char ByteAt(size_t byteOffset) const;
469 
479  bool Contains(CYIStringView s, CaseMode caseSensitivity = CaseMode::CaseSensitive) const;
480 
490  bool Contains(const char *pString, CaseMode caseSensitivity = CaseMode::CaseSensitive) const;
491 
503  bool StartsWith(CYIStringView s, CaseMode caseSensitivity = CaseMode::CaseSensitive) const;
504 
516  bool StartsWith(const char *pString, CaseMode caseSensitivity = CaseMode::CaseSensitive) const;
517 
529  bool EndsWith(CYIStringView s, CaseMode caseSensitivity = CaseMode::CaseSensitive) const;
530 
542  bool EndsWith(const char *pString, CaseMode caseSensitivity = CaseMode::CaseSensitive) const;
543 
553  int32_t Compare(CYIStringView s, CaseMode caseSensitivity = CaseMode::CaseSensitive) const;
554 
564  int32_t Compare(const char *pString, CaseMode caseSensitivity = CaseMode::CaseSensitive) const;
565 
575  bool Equals(CYIStringView other, CaseMode caseSensitivity = CaseMode::CaseSensitive) const noexcept;
576 
587  bool EqualsCaseInsensitiveASCII(CYIStringView other) const noexcept;
588 
598  std::vector<CYIString> Split(CYIStringView delimiter, bool ignoreEmptySubstrings = true, size_t maximumParts = NPos) const;
599 
614  template<typename Container, typename Printer = bool>
615  static CYIString Join(Container &&container, CYIStringView separator, bool includeEmptyElements = true, Printer &&toStringFunction = false);
616 
620  CYIString SubStr(size_t pos = 0, size_t len = NPos) const;
621 
625  CYIString SubStr(const ConstIterator &begin, const ConstIterator &end) const;
626 
630  CYIString Left(size_t len) const;
631 
635  CYIString Right(size_t len) const;
636 
644  CYIString ToUpperASCII() const;
645 
653  CYIString ToLowerASCII() const;
654 
662  bool IsNumeric() const;
663 
671  size_t IndexOf(CYIStringView view, size_t pos = 0) const;
672 
680  size_t IndexOf(const char *pString, size_t pos = 0) const;
681 
689  size_t LastIndexOf(CYIStringView view, size_t pos = NPos) const;
690 
698  size_t LastIndexOf(const char *pString, size_t pos = NPos) const;
699 
703  ConstIterator Find(CYIStringView view, const ConstIterator &begin) const;
704 
708  ConstIterator Find(const char *pString, const ConstIterator &begin) const;
709 
713  ConstReverseIterator ReverseFind(CYIStringView view, const ConstReverseIterator &begin) const;
714 
718  ConstReverseIterator ReverseFind(const char *pString, const ConstReverseIterator &begin) const;
719 
727  CYIString Arg(CYIStringView text) const;
728 
738  const char *GetData() const;
739 
743  ConstIterator begin() const;
744 
748  ConstIterator end() const;
749 
753  ConstReverseIterator rbegin() const;
754 
758  ConstReverseIterator rend() const;
759 
763  void Clear();
764 
769 
777  CYIString &Append(const char *pUTF8, size_t sizeBytes = NPos);
778 
785 
790 
798  CYIString &Prepend(const char *pUTF8, size_t sizeBytes = NPos);
799 
806 
814  CYIString &Insert(CYIStringView view, size_t pos, size_t len = NPos);
815 
821  CYIString &Insert(const char *pString, size_t pos, size_t len = NPos);
822 
826  ConstIterator Insert(CYIStringView view, const ConstIterator &insertPosition);
827 
831  ConstIterator Insert(const char *pString, const ConstIterator &insertPosition);
832 
836  ConstIterator Insert(const ConstIterator &begin, const ConstIterator &end, const ConstIterator &insertPosition);
837 
841  CYIString &Erase(size_t pos = 0, size_t len = NPos);
842 
849 
855  CYIString &Replace(CYIStringView view, size_t pos, size_t len);
856 
862  CYIString &Replace(const char *pString, size_t pos, size_t len);
863 
870 
876  ConstIterator Replace(const char *pString, const ConstIterator &begin, const ConstIterator &end);
877 
885  CYIString &Replace(CYIStringView oldSubstring, CYIStringView newSubstring, size_t maximumReplacements = NPos);
886 
894  CYIString &Replace(const char *pOldSubstring, const char *pNewSubstring, size_t maximumReplacements = NPos);
895 
899  void Remove(CYIStringView text);
900 
904  void Remove(const char *pString);
905 
909  void Swap(CYIString &s);
910 
914  void Trim();
915 
919  void TrimLeft(size_t len = NPos);
920 
924  void TrimRight(size_t len = NPos);
925 
929  void RemovePrefix(size_t len);
930 
938  bool RemovePrefix(CYIStringView prefix, CaseMode caseSensitivity = CaseMode::CaseSensitive);
939 
943  void RemoveSuffix(size_t len);
944 
952  bool RemoveSuffix(CYIStringView prefix, CaseMode caseSensitivity = CaseMode::CaseSensitive);
953 
963  std::unique_ptr<char[]> ToUTF8(size_t *pElementsCount = nullptr) const;
964 
970  std::unique_ptr<char16_t[]> ToUTF16(size_t *pElementsCount = nullptr) const;
971 
977  std::unique_ptr<char32_t[]> ToUTF32(size_t *pElementsCount = nullptr) const;
978 
986  std::unique_ptr<wchar_t[]> ToWideChars(size_t *pElementsCount = nullptr) const;
987 
991  const std::string &ToStdString() const;
992 
998  std::wstring ToStdWString() const;
999 
1000 #if defined(__OBJC__) || defined(DOXY)
1001 
1006  NSString *ToNSString() const;
1007 
1013  CFStringRef ToCFStringRef() const;
1014 #endif
1015 
1019  std::string TakeStdString();
1020 
1033  template<class T>
1034  T To(bool *pError = nullptr) const;
1035 
1041  template<class T>
1042  T ToValue(bool *pError = nullptr) const;
1043 
1049  float ToFloat(bool *pError = nullptr) const;
1050 
1056  double ToDouble(bool *pError = nullptr) const;
1057 
1085  bool ToBool(bool *pError = nullptr) const;
1086 
1092  CYIString ToHexadecimal() const;
1093 
1094 private:
1095  std::string m_string;
1096  mutable size_t m_length; // 0 indicates that the length (in characters) is unknown
1097 };
1098 
1099 // std::hash specialization for CYIString
1100 namespace std
1101 {
1102 template<>
1103 struct hash<CYIString>;
1104 }
1105 
1106 std::ostream &operator<<(std::ostream &stream, const CYIString &val);
1107 std::istream &operator>>(std::istream &stream, CYIString &val);
1108 
1109 #ifdef DOXY
1110 
1115 
1121 template<class T>
1122 constexpr bool operator==(CYIStringView lhs, T rhs) noexcept;
1123 
1129 template<class T>
1130 constexpr bool operator==(T lhs, CYIStringView rhs) noexcept;
1131 
1137 template<class T>
1138 constexpr bool operator!=(CYIStringView lhs, T rhs) noexcept;
1139 
1145 template<class T>
1146 constexpr bool operator!=(T lhs, CYIStringView rhs) noexcept;
1147 
1148 #endif
1149 
1152 #include "utility/internal/YiString.inl"
1153 
1154 #endif
ConstIterator end() const
void RemovePrefix(size_t len)
bool Equals(CYIStringView other, CaseMode caseSensitivity=CaseMode::CaseSensitive) const noexcept
NSString * ToNSString() const
ConstIterator & operator=(const ConstIterator &other)
ConstIterator Find(CYIStringView view, const ConstIterator &begin) const
void TrimLeft(size_t len=NPos)
std::ostream & operator<<(std::ostream &stream, const CYIString &val)
ConstIterator Iterator
Definition: YiString.h:97
CFStringRef ToCFStringRef() const
bool StartsWith(CYIStringView s, CaseMode caseSensitivity=CaseMode::CaseSensitive) const
std::unique_ptr< char[]> ToUTF8(size_t *pElementsCount=nullptr) const
char32_t reference
Definition: YiString.h:55
CaseMode
Definition: YiStringView.h:129
ConstReverseIterator Reverse() const
CYIString Right(size_t len) const
value_type operator*() const
const std::string & ToStdString() const
Container class for Unicode strings. Conceptually, a CYIString object is a sequence of Unicode charac...
Definition: YiString.h:32
std::unique_ptr< wchar_t[]> ToWideChars(size_t *pElementsCount=nullptr) const
ptrdiff_t difference_type
Definition: YiString.h:53
std::unique_ptr< char16_t[]> ToUTF16(size_t *pElementsCount=nullptr) const
bool IsEmpty() const
std::wstring ToStdWString() const
std::vector< CYIString > Split(CYIStringView delimiter, bool ignoreEmptySubstrings=true, size_t maximumParts=NPos) const
STL namespace.
CYIString ToLowerASCII() const
bool operator!=(const ConstIterator &other) const
Definition: YiStringView.h:72
double ToDouble(bool *pError=nullptr) const
ConstReverseIterator ReverseFind(CYIStringView view, const ConstReverseIterator &begin) const
friend std::istream & operator>>(std::istream &stream, CYIString &val)
CYIString & Append(CYIStringView view)
static const CYIString & EmptyString()
T ToValue(bool *pError=nullptr) const
const char32_t * pointer
Definition: YiString.h:54
CYIString & Erase(size_t pos=0, size_t len=NPos)
static const CYIString & FromBool(bool value)
T To(bool *pError=nullptr) const
std::bidirectional_iterator_tag iterator_category
Definition: YiString.h:51
size_t IndexOf(CYIStringView view, size_t pos=0) const
size_t GetSizeInBytes() const
CYIString ToUpperASCII() const
static CYIString FromDouble(double value, uint32_t decimalPlaces=6, bool trailingZeroes=false, bool useSignificantDigits=false)
void Clear()
bool EndsWith(CYIStringView s, CaseMode caseSensitivity=CaseMode::CaseSensitive) const
bool operator==(const ConstIterator &other) const
static CYIString FromValue(const T &arg, bool *pError=nullptr)
std::reverse_iterator< Iterator > ReverseIterator
Definition: YiString.h:98
ConstIterator begin() const
const char * GetData() const
size_t GetLength() const
CYIString & Replace(CYIStringView view, size_t pos, size_t len)
bool ContainsNonASCII() const
Definition: YiString.h:46
CYIString SubStr(size_t pos=0, size_t len=NPos) const
float ToFloat(bool *pError=nullptr) const
static constexpr size_t NPos
Definition: YiString.h:125
bool Contains(CYIStringView s, CaseMode caseSensitivity=CaseMode::CaseSensitive) const
bool EqualsCaseInsensitiveASCII(CYIStringView other) const noexcept
CaseMode
Definition: YiString.h:106
std::reverse_iterator< ConstIterator > ConstReverseIterator
Definition: YiString.h:38
CYIString & Insert(CYIStringView view, size_t pos, size_t len=NPos)
char32_t At(size_t pos) const
ConstReverseIterator rend() const
friend class CYIStringView
Definition: YiString.h:34
void TrimRight(size_t len=NPos)
ConstReverseIterator rbegin() const
static CYIString FromFloat(float value, uint32_t decimalPlaces=6, bool trailingZeroes=false, bool useSignificantDigits=false)
ConstIterator & operator++()
friend class CYIString
Definition: YiString.h:49
ConstIterator & operator--()
CYIString & Prepend(CYIStringView view)
void Swap(CYIString &s)
size_t LastIndexOf(CYIStringView view, size_t pos=NPos) const
char ByteAt(size_t byteOffset) const
void RemoveSuffix(size_t len)
std::unique_ptr< char32_t[]> ToUTF32(size_t *pElementsCount=nullptr) const
unsigned int size_t
Definition: YiPredefLinux.h:10
bool IsNotEmpty() const
CYIString ToHexadecimal() const
bool IsNumeric() const
char32_t value_type
Definition: YiString.h:52
void Reserve(size_t bytes)
bool ToBool(bool *pError=nullptr) const
void Trim()
int32_t Compare(CYIStringView s, CaseMode caseSensitivity=CaseMode::CaseSensitive) const
std::string TakeStdString()
void Remove(CYIStringView text)
CYIString Left(size_t len) const
CYIString Arg(CYIStringView text) const
static CYIString Join(Container &&container, CYIStringView separator, bool includeEmptyElements=true, Printer &&toStringFunction=false)
CYIString operator+(const T &arg) const
A lightweight wrapper around a sequence of Unicode characters.
Definition: YiStringView.h:61