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 
682  std::vector<CYIString> Split(CYIStringView delimiter, bool ignoreEmptySubstrings = true, size_t maximumParts = NPos) const;
683 
687  CYIString SubStr(size_t pos = 0, size_t len = NPos) const;
688 
692  CYIString SubStr(const ConstIterator &begin, const ConstIterator &end) const;
693 
697  CYIString Left(size_t len) const;
698 
702  CYIString Right(size_t len) const;
703 
711  CYIString ToUpperASCII() const;
712 
720  CYIString ToLowerASCII() const;
721 
729  bool IsNumeric() const;
730 
738  size_t IndexOf(CYIStringView view, size_t pos = 0) const;
739 
747  size_t IndexOf(const char *pString, size_t pos = 0) const;
748 
756  size_t LastIndexOf(CYIStringView view, size_t pos = NPos) const;
757 
765  size_t LastIndexOf(const char *pString, size_t pos = NPos) const;
766 
770  ConstIterator Find(CYIStringView view, const ConstIterator &begin) const;
771 
775  ConstIterator Find(const char *pString, const ConstIterator &begin) const;
776 
780  ConstReverseIterator ReverseFind(CYIStringView view, const ConstReverseIterator &begin) const;
781 
785  ConstReverseIterator ReverseFind(const char *pString, const ConstReverseIterator &begin) const;
786 
794  CYIString Arg(CYIStringView text) const;
795 
805  const char *GetData() const;
806 
810  ConstIterator begin() const;
811 
815  ConstIterator end() const;
816 
820  ConstReverseIterator rbegin() const;
821 
825  ConstReverseIterator rend() const;
826 
830  void Clear();
831 
836 
844  CYIString &Append(const char *pUTF8, size_t sizeBytes = NPos);
845 
852 
857 
865  CYIString &Prepend(const char *pUTF8, size_t sizeBytes = NPos);
866 
873 
881  CYIString &Insert(CYIStringView view, size_t pos, size_t len = NPos);
882 
888  CYIString &Insert(const char *pString, size_t pos, size_t len = NPos);
889 
893  ConstIterator Insert(CYIStringView view, const ConstIterator &insertPosition);
894 
898  ConstIterator Insert(const char *pString, const ConstIterator &insertPosition);
899 
903  ConstIterator Insert(const ConstIterator &begin, const ConstIterator &end, const ConstIterator &insertPosition);
904 
908  CYIString &Erase(size_t pos = 0, size_t len = NPos);
909 
916 
922  CYIString &Replace(CYIStringView view, size_t pos, size_t len);
923 
929  CYIString &Replace(const char *pString, size_t pos, size_t len);
930 
937 
943  ConstIterator Replace(const char *pString, const ConstIterator &begin, const ConstIterator &end);
944 
952  CYIString &Replace(CYIStringView oldSubstring, CYIStringView newSubstring, size_t maximumReplacements = NPos);
953 
961  CYIString &Replace(const char *pOldSubstring, const char *pNewSubstring, size_t maximumReplacements = NPos);
962 
966  void Remove(CYIStringView text);
967 
971  void Remove(const char *pString);
972 
976  void Swap(CYIString &s);
977 
981  void Trim();
982 
986  void TrimLeft(size_t len = NPos);
987 
991  void TrimRight(size_t len = NPos);
992 
996  void RemovePrefix(size_t len);
997 
1005  bool RemovePrefix(CYIStringView prefix, CaseMode caseSensitivity = CaseMode::CaseSensitive);
1006 
1010  void RemoveSuffix(size_t len);
1011 
1019  bool RemoveSuffix(CYIStringView prefix, CaseMode caseSensitivity = CaseMode::CaseSensitive);
1020 
1030  std::unique_ptr<char[]> ToUTF8(size_t *pElementsCount = nullptr) const;
1031 
1037  std::unique_ptr<char16_t[]> ToUTF16(size_t *pElementsCount = nullptr) const;
1038 
1044  std::unique_ptr<char32_t[]> ToUTF32(size_t *pElementsCount = nullptr) const;
1045 
1053  std::unique_ptr<wchar_t[]> ToWideChars(size_t *pElementsCount = nullptr) const;
1054 
1058  const std::string &ToStdString() const;
1059 
1065  std::wstring ToStdWString() const;
1066 
1067 #if defined(__OBJC__) || defined(DOXY)
1068 
1073  NSString *ToNSString() const;
1074 
1080  CFStringRef ToCFStringRef() const;
1081 #endif
1082 
1086  std::string TakeStdString();
1087 
1098  template<class T>
1099  T To(bool *pError = nullptr) const;
1100 
1106  template<class T>
1107  T ToValue(bool *pError = nullptr) const;
1108 
1114  float ToFloat(bool *pError = nullptr) const;
1115 
1121  double ToDouble(bool *pError = nullptr) const;
1122 
1150  bool ToBool(bool *pError = nullptr) const;
1151 
1157  CYIString ToHexadecimal() const;
1158 
1159 private:
1160  std::string m_string;
1161  mutable size_t m_length; // 0 indicates that the length (in characters) is unknown
1162 };
1163 
1164 // std::hash specialization for CYIString
1165 namespace std
1166 {
1167 template<>
1168 struct hash<CYIString>;
1169 }
1170 
1171 std::ostream &operator<<(std::ostream &stream, const CYIString &val);
1172 std::istream &operator>>(std::istream &stream, CYIString &val);
1173 
1174 #ifdef DOXY
1175 
1180 
1186 template<class T>
1187 constexpr bool operator==(CYIStringView lhs, T rhs) noexcept;
1188 
1194 template<class T>
1195 constexpr bool operator==(T lhs, CYIStringView rhs) noexcept;
1196 
1202 template<class T>
1203 constexpr bool operator!=(CYIStringView lhs, T rhs) noexcept;
1204 
1210 template<class T>
1211 constexpr bool operator!=(T lhs, CYIStringView rhs) noexcept;
1212 
1213 #endif
1214 
1217 #include "utility/YiString.inl"
1218 
1219 #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
A lightweight wrapper around a sequence of Unicode characters.
Definition: YiStringView.h:57