You.i Engine
YiStringView.h
Go to the documentation of this file.
1 // © You i Labs Inc. 2000-2020. All rights reserved.
2 #ifndef _YI_STRING_VIEW_H_
3 #define _YI_STRING_VIEW_H_
4 
5 #include "framework/YiPredef.h"
7 
8 #include <assert.h>
9 #include <memory>
10 #include <string>
11 
12 class CYIString;
13 
14 #if YI_FULLY_SUPPORTS_CPP14_CONSTEXPR
15 # define YI_CONSTEXPR constexpr
16 #else
17 # include <cstddef>
18 # define YI_CONSTEXPR
19 #endif
20 
58 {
59 public:
61  typedef std::reverse_iterator<ConstIterator> ConstReverseIterator;
62 
69  {
70  public:
71  friend class CYIStringView;
72  friend class CYIString;
73 
74  typedef std::bidirectional_iterator_tag iterator_category;
75  typedef char32_t value_type;
76  typedef ptrdiff_t difference_type;
77  typedef const char32_t *pointer;
78  typedef char32_t reference;
79 
80  ConstIterator();
81  ConstIterator(CYIStringView string, size_t charactersOffset);
82  ConstIterator(const ConstIterator &other);
84 
85  ConstIterator &operator=(const ConstIterator &other);
86 
90  value_type operator*() const;
91 
92  bool operator==(const ConstIterator &other) const;
93  bool operator!=(const ConstIterator &other) const;
94 
99 
105  const char *GetRawData() const;
106 
107  ConstReverseIterator Reverse() const;
108 
109  private:
110  static const size_t YI_STRING_ITERATOR_SIZE = sizeof(char *) * 3;
111 
112  ConstIterator(CYIStringView string, const char *pPosition);
113 
114  char m_storage[YI_STRING_ITERATOR_SIZE];
115  };
116 
118  typedef std::reverse_iterator<Iterator> ReverseIterator;
119 
125  enum class CaseMode
126  {
130  CaseSensitive,
131 
135  CaseInsensitiveASCII
136  };
137 
141  static constexpr size_t NPos = std::numeric_limits<size_t>::max();
142 
146  YI_CONSTEXPR CYIStringView() noexcept;
147 
151  YI_CONSTEXPR CYIStringView(const CYIStringView &other) noexcept;
152 
158  YI_CONSTEXPR CYIStringView(const char *pUTF8) noexcept;
159 
167  YI_CONSTEXPR CYIStringView(const char *pUTF8, size_t sizeBytes) noexcept;
168 
175 
181  CYIStringView(const std::string &other) noexcept;
182 
183 #if __cplusplus >= 201703L
184 
189  YI_CONSTEXPR CYIStringView(const std::string_view &other) noexcept;
190 #endif
191 
192 #if defined(__OBJC__) || defined(DOXY)
193 
198  CYIStringView(const NSString *pString);
199 #endif
200 
206  CYIStringView(const std::vector<char> &vector) noexcept;
207 
213  CYIStringView(const std::vector<uint8_t> &vector) noexcept;
214 
218  YI_CONSTEXPR CYIStringView &operator=(const CYIStringView &view) noexcept = default;
219 
225  YI_CONSTEXPR bool IsEmpty() const;
226 
234  YI_CONSTEXPR bool IsNotEmpty() const;
235 
245  YI_CONSTEXPR const char *GetRawData() const noexcept;
246 
252  YI_CONSTEXPR size_t GetSizeInBytes() const noexcept;
253 
261  size_t GetLength() const;
262 
270  YI_CONSTEXPR char ByteAt(size_t byteOffset) const noexcept;
271 
277  char32_t At(size_t pos) const;
278 
284  char32_t operator[](size_t pos) const;
285 
291  bool ContainsNonASCII() const;
292 
300  bool IsNumeric() const;
301 
305  ConstIterator begin() const;
306 
310  ConstIterator end() const;
311 
315  ConstReverseIterator rbegin() const;
316 
320  ConstReverseIterator rend() const;
321 
331  bool Contains(CYIStringView s, CaseMode caseSensitivity = CaseMode::CaseSensitive) const;
332 
344  bool StartsWith(CYIStringView s, CaseMode caseSensitivity = CaseMode::CaseSensitive) const;
345 
357  bool EndsWith(CYIStringView s, CaseMode caseSensitivity = CaseMode::CaseSensitive) const;
358 
368  int32_t Compare(CYIStringView other, CaseMode caseSensitivity = CaseMode::CaseSensitive) const noexcept;
369 
379  bool Equals(CYIStringView other, CaseMode caseSensitivity = CaseMode::CaseSensitive) const noexcept;
380 
391  bool EqualsCaseInsensitiveASCII(CYIStringView other) const noexcept;
392 
400  CYISmallVector<CYIStringView, 3> Split(CYIStringView delimiter, bool ignoreEmptySubstrings = true, size_t maximumParts = NPos) const;
401 
405  CYIStringView SubStr(size_t pos = 0, size_t len = NPos) const;
406 
410  CYIStringView SubStr(const ConstIterator &begin, const ConstIterator &end) const;
411 
415  CYIStringView Left(size_t len) const;
416 
420  CYIStringView Right(size_t len) const;
421 
429  CYIString ToUpperASCII() const;
430 
438  CYIString ToLowerASCII() const;
439 
447  size_t IndexOf(CYIStringView view, size_t pos = 0) const;
448 
456  size_t LastIndexOf(CYIStringView view, size_t pos = NPos) const;
457 
461  ConstIterator Find(CYIStringView view, const ConstIterator &begin) const;
462 
466  ConstReverseIterator ReverseFind(CYIStringView view, const ConstReverseIterator &begin) const;
467 
475  CYIString Arg(CYIStringView text) const;
476 
480  void Clear();
481 
485  void Swap(CYIStringView &view);
486 
490  void Trim();
491 
495  void TrimLeft(size_t len = NPos);
496 
500  void TrimRight(size_t len = NPos);
501 
505  void RemovePrefix(size_t len);
506 
514  bool RemovePrefix(CYIStringView prefix, CaseMode caseSensitivity = CaseMode::CaseSensitive);
515 
519  void RemoveSuffix(size_t len);
520 
528  bool RemoveSuffix(CYIStringView prefix, CaseMode caseSensitivity = CaseMode::CaseSensitive);
529 
539  std::unique_ptr<char[]> ToUTF8(size_t *pElementsCount = nullptr) const;
540 
546  std::unique_ptr<char16_t[]> ToUTF16(size_t *pElementsCount = nullptr) const;
547 
553  std::unique_ptr<char32_t[]> ToUTF32(size_t *pElementsCount = nullptr) const;
554 
562  std::unique_ptr<wchar_t[]> ToWideChars(size_t *pElementsCount = nullptr) const;
563 
569  CYIString ToCYIString() const;
570 
576  std::string ToStdString() const;
577 
583  std::wstring ToStdWString() const;
584 
585 #if defined(__OBJC__) || defined(DOXY)
586 
591  NSString *ToNSString() const;
592 
598  CFStringRef ToCFStringRef() const;
599 #endif
600 
611  template<class T>
612  T To(bool *pError = nullptr) const;
613 
619  template<class T>
620  T ToValue(bool *pError = nullptr) const;
621 
627  float ToFloat(bool *pError = nullptr) const;
628 
634  double ToDouble(bool *pError = nullptr) const;
635 
663  bool ToBool(bool *pError = nullptr) const;
664 
670  CYIString ToHexadecimal() const;
671 
672 private:
673  size_t m_size;
674  const char *m_pData;
675 };
676 
677 // std::hash specialization for CYIStringView. Note that CYIStringView's and CYIString's that are equal do not hash to the same value.
678 namespace std
679 {
680 template<>
681 struct hash<CYIStringView>;
682 }
683 
684 std::ostream &operator<<(std::ostream &stream, const CYIStringView &val);
685 
686 YI_CONSTEXPR bool operator==(CYIStringView lhs, CYIStringView rhs) noexcept;
687 YI_CONSTEXPR bool operator!=(CYIStringView lhs, CYIStringView rhs) noexcept;
688 
689 bool operator<(CYIStringView lhs, CYIStringView rhs) noexcept;
690 bool operator>(CYIStringView lhs, CYIStringView rhs) noexcept;
691 bool operator<=(CYIStringView lhs, CYIStringView rhs) noexcept;
692 bool operator>=(CYIStringView lhs, CYIStringView rhs) noexcept;
693 
696 #include "utility/YiStringView.inl"
697 
698 #undef YI_CONSTEXPR
699 
700 #endif
CYIStringView SubStr(size_t pos=0, size_t len=NPos) const
std::bidirectional_iterator_tag iterator_category
Definition: YiStringView.h:74
const char32_t * pointer
Definition: YiStringView.h:77
const char * GetRawData() const
void RemovePrefix(size_t len)
std::unique_ptr< wchar_t[]> ToWideChars(size_t *pElementsCount=nullptr) const
double ToDouble(bool *pError=nullptr) const
A std::vector wrapper that makes use of pre-allocated memory for storing up to StackCapacity items...
Definition: YiSmallVector.h:32
CYIString ToUpperASCII() const
CaseMode
Definition: YiStringView.h:125
CYIString ToCYIString() const
CYISmallVector< CYIStringView, 3 > Split(CYIStringView delimiter, bool ignoreEmptySubstrings=true, size_t maximumParts=NPos) const
char32_t reference
Definition: YiStringView.h:78
std::string ToStdString() const
size_t GetLength() const
Container class for Unicode strings. Conceptually, a CYIString object is a sequence of Unicode charac...
Definition: YiString.h:36
STL namespace.
CYIString Arg(CYIStringView text) const
value_type operator*() const
ConstReverseIterator Reverse() const
#define YI_CONSTEXPR
Definition: YiStringView.h:18
T ToValue(bool *pError=nullptr) const
Definition: YiStringView.h:68
CFStringRef ToCFStringRef() const
NSString * ToNSString() const
bool operator>(CYIStringView lhs, CYIStringView rhs) noexcept
ConstIterator & operator=(const ConstIterator &other)
bool operator>=(CYIStringView lhs, CYIStringView rhs) noexcept
static constexpr size_t NPos
Definition: YiStringView.h:141
std::wstring ToStdWString() const
bool Contains(CYIStringView s, CaseMode caseSensitivity=CaseMode::CaseSensitive) const
char32_t value_type
Definition: YiStringView.h:75
int32_t Compare(CYIStringView other, CaseMode caseSensitivity=CaseMode::CaseSensitive) const noexcept
size_t LastIndexOf(CYIStringView view, size_t pos=NPos) const
void RemoveSuffix(size_t len)
bool operator==(const ConstIterator &other) const
CYIString ToLowerASCII() const
bool EndsWith(CYIStringView s, CaseMode caseSensitivity=CaseMode::CaseSensitive) const
void TrimLeft(size_t len=NPos)
bool Equals(CYIStringView other, CaseMode caseSensitivity=CaseMode::CaseSensitive) const noexcept
bool operator<=(CYIStringView lhs, CYIStringView rhs) noexcept
size_t IndexOf(CYIStringView view, size_t pos=0) const
ConstIterator end() const
ConstIterator Find(CYIStringView view, const ConstIterator &begin) const
std::reverse_iterator< Iterator > ReverseIterator
Definition: YiStringView.h:118
YI_CONSTEXPR bool IsNotEmpty() const
bool EqualsCaseInsensitiveASCII(CYIStringView other) const noexcept
bool operator!=(const ConstIterator &other) const
YI_CONSTEXPR char ByteAt(size_t byteOffset) const noexcept
bool IsNumeric() const
friend class CYIStringView
Definition: YiStringView.h:71
ConstReverseIterator rend() const
std::unique_ptr< char32_t[]> ToUTF32(size_t *pElementsCount=nullptr) const
CYIString ToHexadecimal() const
ConstIterator begin() const
std::unique_ptr< char[]> ToUTF8(size_t *pElementsCount=nullptr) const
ConstReverseIterator rbegin() const
T To(bool *pError=nullptr) const
YI_CONSTEXPR size_t GetSizeInBytes() const noexcept
float ToFloat(bool *pError=nullptr) const
void TrimRight(size_t len=NPos)
ConstReverseIterator ReverseFind(CYIStringView view, const ConstReverseIterator &begin) const
std::ostream & operator<<(std::ostream &stream, const CYIStringView &val)
void Swap(CYIStringView &view)
ConstIterator & operator++()
std::reverse_iterator< ConstIterator > ConstReverseIterator
Definition: YiStringView.h:60
bool StartsWith(CYIStringView s, CaseMode caseSensitivity=CaseMode::CaseSensitive) const
bool ContainsNonASCII() const
bool operator<(CYIStringView lhs, CYIStringView rhs) noexcept
ConstIterator & operator--()
CYIStringView Right(size_t len) const
CYIStringView Left(size_t len) const
YI_CONSTEXPR bool IsEmpty() const
ptrdiff_t difference_type
Definition: YiStringView.h:76
ConstIterator Iterator
Definition: YiStringView.h:117
char32_t At(size_t pos) const
bool ToBool(bool *pError=nullptr) const
A lightweight wrapper around a sequence of Unicode characters.
Definition: YiStringView.h:57
std::unique_ptr< char16_t[]> ToUTF16(size_t *pElementsCount=nullptr) const