You.i Engine
YiString.h
Go to the documentation of this file.
1 // © You i Labs Inc. 2000-2020. All rights reserved.
2 #ifndef _YI_STRING_H_
3 #define _YI_STRING_H_
4 
5 #include "utility/YiStringView.h"
6 
7 #include <limits>
8 #include <ostream>
9 #include <string>
10 
36 class CYIString
37 {
38  friend class CYIStringView;
39  class ConstIteratorPriv;
40 
41 public:
43  typedef std::reverse_iterator<ConstIterator> ConstReverseIterator;
44 
51  {
52  public:
53  friend class CYIString;
54 
55  typedef std::bidirectional_iterator_tag iterator_category;
56  typedef char32_t value_type;
57  typedef ptrdiff_t difference_type;
58  typedef const char32_t *pointer;
59  typedef char32_t reference;
60 
61  ConstIterator();
62  ConstIterator(CYIStringView view, size_t charactersOffset);
63  ConstIterator(const ConstIterator &other);
66 
67  ConstIterator &operator=(const ConstIterator &other);
68 
72  value_type operator*() const;
73 
77  operator CYIStringView::ConstIterator() const;
78 
79  bool operator==(const ConstIterator &other) const;
80  bool operator!=(const ConstIterator &other) const;
81 
86 
90  const char *GetData() const;
91 
92  ConstReverseIterator Reverse() const;
93 
94  private:
95  static const size_t YI_STRING_ITERATOR_SIZE = sizeof(char *) * 3;
96 
97  ConstIterator(CYIStringView view, const char *pPosition);
98  char m_storage[YI_STRING_ITERATOR_SIZE];
99  };
100 
102  typedef std::reverse_iterator<Iterator> ReverseIterator;
103 
104 #ifdef DOXY
105 
110  enum class CaseMode
111  {
115  CaseSensitive,
116 
120  CaseInsensitiveASCII
121  };
122 #else
124 #endif
125 
129  static constexpr size_t NPos = std::numeric_limits<size_t>::max();
130 
134  static const CYIString &EmptyString();
135 
139  CYIString();
140 
144  CYIString(const CYIString &s);
145 
149  CYIString(CYIString &&s) noexcept;
150 
154  CYIString(CYIStringView stringView);
155 
159  CYIString(std::string &&string) noexcept;
160 
166  CYIString(const std::string &s);
167 
173  explicit CYIString(char32_t character);
174 
181 
187  CYIString(const std::wstring &s);
188 
189 #if defined(__OBJC__) || defined(DOXY)
190 
195  CYIString(const NSString *pString);
196 
202  CYIString(const CFStringRef string);
203 #endif
204 
210  CYIString(const char *pUTF8);
211 
219  CYIString(const char *pUTF8, size_t sizeBytes);
220 
228  CYIString(const char16_t *pUTF16, size_t size = NPos);
229 
237  CYIString(const char32_t *pUTF32, size_t size = NPos);
238 
246  CYIString(const wchar_t *pWideChars, size_t size = NPos);
247 
253  CYIString(const std::vector<char> &vector);
254 
260  CYIString(const std::vector<uint8_t> &vector);
261 
265  operator CYIStringView() const noexcept;
266 
270  CYIString &operator=(const CYIString &s);
271 
277  CYIString &operator=(const char *pUTF8);
278 
282  CYIString &operator=(CYIString &&s) noexcept;
283 
289  CYIString &operator=(std::string &&s) noexcept;
290 
294  bool operator<(const char *pOther) const;
295 
299  bool operator>(const char *pOther) const;
300 
304  bool operator<=(const char *pOther) const;
305 
309  bool operator>=(const char *pOther) const;
310 
316  char32_t operator[](size_t pos) const;
317 
321  void Reserve(size_t bytes);
322 
326  CYIString &operator+=(CYIStringView view);
327 
333  CYIString &operator+=(const char *pUTF8);
334 
338  template<class T, class = std::enable_if_t<!std::is_convertible<const T &, CYIStringView>::value
339 #if __cplusplus >= 201703L
340  && !std::is_convertible<const T &, std::string_view>::value
341 #endif
342  >>
343  CYIString operator+(T arg) const;
344 
348  CYIString operator+(const char *pUTF8) const;
349 
360  template<class T>
361  static CYIString FromValue(const T &arg, bool *pError = nullptr);
362 
375  static CYIString FromValue(bool arg, bool *pError = nullptr);
376 
383  static CYIString FromValue(int arg, bool *pError = nullptr);
384 
388  static CYIString FromValue(short arg, bool *pError = nullptr);
389 
393  static CYIString FromValue(long arg, bool *pError = nullptr);
394 
398  static CYIString FromValue(long long arg, bool *pError = nullptr);
399 
403  static CYIString FromValue(unsigned arg, bool *pError = nullptr);
404 
408  static CYIString FromValue(unsigned short arg, bool *pError = nullptr);
409 
413  static CYIString FromValue(unsigned long arg, bool *pError = nullptr);
414 
418  static CYIString FromValue(unsigned long long arg, bool *pError = nullptr);
419 
423  static CYIString FromValue(float arg, bool *pError = nullptr);
424 
428  static CYIString FromValue(double arg, bool *pError = nullptr);
429 
433  static CYIString FromValue(long double arg, bool *pError = nullptr);
434 
438  static CYIString FromValue(char arg, bool *pError = nullptr);
439 
443  static CYIString FromValue(signed char arg, bool *pError = nullptr);
444 
448  static CYIString FromValue(unsigned char arg, bool *pError = nullptr);
449 
453  static CYIString FromValue(wchar_t arg, bool *pError = nullptr);
454 
458  static CYIString FromValue(char16_t arg, bool *pError = nullptr);
459 
463  static CYIString FromValue(char32_t arg, bool *pError = nullptr);
464 
476  static CYIString FromFloat(float value, uint32_t decimalPlaces = 6, bool trailingZeroes = false, bool useSignificantDigits = false);
477 
489  static CYIString FromDouble(double value, uint32_t decimalPlaces = 6, bool trailingZeroes = false, bool useSignificantDigits = false);
490 
494  static const CYIString &FromBool(bool value);
495 
499  friend std::istream &operator>>(std::istream &stream, CYIString &val);
500 
506  bool IsEmpty() const;
507 
515  bool IsNotEmpty() const;
516 
524  size_t GetLength() const;
525 
531  size_t GetSizeInBytes() const;
532 
538  bool ContainsNonASCII() const;
539 
545  char32_t At(size_t pos) const;
546 
554  char ByteAt(size_t byteOffset) const;
555 
565  bool Contains(CYIStringView s, CaseMode caseSensitivity = CaseMode::CaseSensitive) const;
566 
576  bool Contains(const char *pString, CaseMode caseSensitivity = CaseMode::CaseSensitive) const;
577 
589  bool StartsWith(CYIStringView s, CaseMode caseSensitivity = CaseMode::CaseSensitive) const;
590 
602  bool StartsWith(const char *pString, CaseMode caseSensitivity = CaseMode::CaseSensitive) const;
603 
615  bool EndsWith(CYIStringView s, CaseMode caseSensitivity = CaseMode::CaseSensitive) const;
616 
628  bool EndsWith(const char *pString, CaseMode caseSensitivity = CaseMode::CaseSensitive) const;
629 
639  int32_t Compare(CYIStringView s, CaseMode caseSensitivity = CaseMode::CaseSensitive) const;
640 
650  int32_t Compare(const char *pString, CaseMode caseSensitivity = CaseMode::CaseSensitive) const;
651 
661  bool Equals(CYIStringView other, CaseMode caseSensitivity = CaseMode::CaseSensitive) const noexcept;
662 
673  bool EqualsCaseInsensitiveASCII(CYIStringView other) const noexcept;
674 
684  std::vector<CYIString> Split(CYIStringView delimiter, bool ignoreEmptySubstrings = true, size_t maximumParts = NPos) const;
685 
698  template<typename Container, typename Printer = bool>
699  static CYIString Join(Container &&container, CYIStringView separator, bool includeEmptyElements = true, Printer &&toStringFunction = false);
700 
704  CYIString SubStr(size_t pos = 0, size_t len = NPos) const;
705 
709  CYIString SubStr(const ConstIterator &begin, const ConstIterator &end) const;
710 
714  CYIString Left(size_t len) const;
715 
719  CYIString Right(size_t len) const;
720 
728  CYIString ToUpperASCII() const;
729 
737  CYIString ToLowerASCII() const;
738 
746  bool IsNumeric() const;
747 
755  size_t IndexOf(CYIStringView view, size_t pos = 0) const;
756 
764  size_t IndexOf(const char *pString, size_t pos = 0) const;
765 
773  size_t LastIndexOf(CYIStringView view, size_t pos = NPos) const;
774 
782  size_t LastIndexOf(const char *pString, size_t pos = NPos) const;
783 
787  ConstIterator Find(CYIStringView view, const ConstIterator &begin) const;
788 
792  ConstIterator Find(const char *pString, const ConstIterator &begin) const;
793 
797  ConstReverseIterator ReverseFind(CYIStringView view, const ConstReverseIterator &begin) const;
798 
802  ConstReverseIterator ReverseFind(const char *pString, const ConstReverseIterator &begin) const;
803 
811  CYIString Arg(CYIStringView text) const;
812 
822  const char *GetData() const;
823 
827  ConstIterator begin() const;
828 
832  ConstIterator end() const;
833 
837  ConstReverseIterator rbegin() const;
838 
842  ConstReverseIterator rend() const;
843 
847  void Clear();
848 
853 
861  CYIString &Append(const char *pUTF8, size_t sizeBytes = NPos);
862 
869 
874 
882  CYIString &Prepend(const char *pUTF8, size_t sizeBytes = NPos);
883 
890 
898  CYIString &Insert(CYIStringView view, size_t pos, size_t len = NPos);
899 
905  CYIString &Insert(const char *pString, size_t pos, size_t len = NPos);
906 
910  ConstIterator Insert(CYIStringView view, const ConstIterator &insertPosition);
911 
915  ConstIterator Insert(const char *pString, const ConstIterator &insertPosition);
916 
920  ConstIterator Insert(const ConstIterator &begin, const ConstIterator &end, const ConstIterator &insertPosition);
921 
925  CYIString &Erase(size_t pos = 0, size_t len = NPos);
926 
933 
939  CYIString &Replace(CYIStringView view, size_t pos, size_t len);
940 
946  CYIString &Replace(const char *pString, size_t pos, size_t len);
947 
954 
960  ConstIterator Replace(const char *pString, const ConstIterator &begin, const ConstIterator &end);
961 
969  CYIString &Replace(CYIStringView oldSubstring, CYIStringView newSubstring, size_t maximumReplacements = NPos);
970 
978  CYIString &Replace(const char *pOldSubstring, const char *pNewSubstring, size_t maximumReplacements = NPos);
979 
983  void Remove(CYIStringView text);
984 
988  void Remove(const char *pString);
989 
993  void Swap(CYIString &s);
994 
998  void Trim();
999 
1003  void TrimLeft(size_t len = NPos);
1004 
1008  void TrimRight(size_t len = NPos);
1009 
1013  void RemovePrefix(size_t len);
1014 
1022  bool RemovePrefix(CYIStringView prefix, CaseMode caseSensitivity = CaseMode::CaseSensitive);
1023 
1027  void RemoveSuffix(size_t len);
1028 
1036  bool RemoveSuffix(CYIStringView prefix, CaseMode caseSensitivity = CaseMode::CaseSensitive);
1037 
1047  std::unique_ptr<char[]> ToUTF8(size_t *pElementsCount = nullptr) const;
1048 
1054  std::unique_ptr<char16_t[]> ToUTF16(size_t *pElementsCount = nullptr) const;
1055 
1061  std::unique_ptr<char32_t[]> ToUTF32(size_t *pElementsCount = nullptr) const;
1062 
1070  std::unique_ptr<wchar_t[]> ToWideChars(size_t *pElementsCount = nullptr) const;
1071 
1075  const std::string &ToStdString() const;
1076 
1082  std::wstring ToStdWString() const;
1083 
1084 #if defined(__OBJC__) || defined(DOXY)
1085 
1090  NSString *ToNSString() const;
1091 
1097  CFStringRef ToCFStringRef() const;
1098 #endif
1099 
1103  std::string TakeStdString();
1104 
1115  template<class T>
1116  T To(bool *pError = nullptr) const;
1117 
1123  template<class T>
1124  T ToValue(bool *pError = nullptr) const;
1125 
1131  float ToFloat(bool *pError = nullptr) const;
1132 
1138  double ToDouble(bool *pError = nullptr) const;
1139 
1167  bool ToBool(bool *pError = nullptr) const;
1168 
1174  CYIString ToHexadecimal() const;
1175 
1176 private:
1177  std::string m_string;
1178  mutable size_t m_length; // 0 indicates that the length (in characters) is unknown
1179 };
1180 
1181 // std::hash specialization for CYIString
1182 namespace std
1183 {
1184 template<>
1185 struct hash<CYIString>;
1186 }
1187 
1188 std::ostream &operator<<(std::ostream &stream, const CYIString &val);
1189 std::istream &operator>>(std::istream &stream, CYIString &val);
1190 
1191 #ifdef DOXY
1192 
1197 
1203 template<class T>
1204 constexpr bool operator==(CYIStringView lhs, T rhs) noexcept;
1205 
1211 template<class T>
1212 constexpr bool operator==(T lhs, CYIStringView rhs) noexcept;
1213 
1219 template<class T>
1220 constexpr bool operator!=(CYIStringView lhs, T rhs) noexcept;
1221 
1227 template<class T>
1228 constexpr bool operator!=(T lhs, CYIStringView rhs) noexcept;
1229 
1230 #endif
1231 
1234 #include "utility/YiString.inl"
1235 
1236 #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)
CYIString operator+(T arg) const
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:101
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:59
CaseMode
Definition: YiStringView.h:125
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:36
std::unique_ptr< wchar_t[]> ToWideChars(size_t *pElementsCount=nullptr) const
ptrdiff_t difference_type
Definition: YiString.h:57
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:68
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:58
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:55
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
std::reverse_iterator< Iterator > ReverseIterator
Definition: YiString.h:102
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:50
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:129
bool Contains(CYIStringView s, CaseMode caseSensitivity=CaseMode::CaseSensitive) const
bool EqualsCaseInsensitiveASCII(CYIStringView other) const noexcept
CaseMode
Definition: YiString.h:110
std::reverse_iterator< ConstIterator > ConstReverseIterator
Definition: YiString.h:42
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:38
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:53
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
bool IsNotEmpty() const
CYIString ToHexadecimal() const
bool IsNumeric() const
char32_t value_type
Definition: YiString.h:56
void Reserve(size_t bytes)
bool ToBool(bool *pError=nullptr) const
void Trim()
static CYIString FromValue(const T &arg, bool *pError=nullptr)
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)
A lightweight wrapper around a sequence of Unicode characters.
Definition: YiStringView.h:57