You.i Engine
YiStringView.h
Go to the documentation of this file.
1 // © You i Labs Inc. All rights reserved.
2 #ifndef _YI_STRING_VIEW_H_
3 #define _YI_STRING_VIEW_H_
4 
5 #include "framework/YiPredef.h"
7 
8 #include <iosfwd>
9 #include <memory>
10 #include <string>
11 
12 class CYIString;
13 
14 #include <cstddef> // for ptrdiff_t
15 
16 #if defined(__OBJC__)
17 # include <CoreFoundation/CFBase.h>
18 #endif
19 
57 {
58 public:
60  typedef std::reverse_iterator<ConstIterator> ConstReverseIterator;
61 
68  {
69  public:
70  friend class CYIStringView;
71  friend class CYIString;
72 
73  typedef std::bidirectional_iterator_tag iterator_category;
74  typedef char32_t value_type;
75  typedef ptrdiff_t difference_type;
76  typedef const char32_t *pointer;
77  typedef char32_t reference;
78 
79  ConstIterator();
80  ConstIterator(CYIStringView string, size_t charactersOffset);
81  ConstIterator(const ConstIterator &other);
83 
84  ConstIterator &operator=(const ConstIterator &other);
85 
89  value_type operator*() const;
90 
91  bool operator==(const ConstIterator &other) const;
92  bool operator!=(const ConstIterator &other) const;
93 
98 
104  const char *GetRawData() const;
105 
106  ConstReverseIterator Reverse() const;
107 
108  private:
109  static const size_t YI_STRING_ITERATOR_SIZE = sizeof(char *) * 3;
110 
111  ConstIterator(CYIStringView string, const char *pPosition);
112 
113  char m_storage[YI_STRING_ITERATOR_SIZE];
114  };
115 
117  typedef std::reverse_iterator<Iterator> ReverseIterator;
118 
124  enum class CaseMode
125  {
129  CaseSensitive,
130 
134  CaseInsensitiveASCII
135  };
136 
140  static constexpr size_t NPos = ~size_t(0);
141 
145  YI_CONSTEXPR CYIStringView() noexcept;
146 
150  YI_CONSTEXPR CYIStringView(const CYIStringView &other) noexcept;
151 
157  YI_CONSTEXPR CYIStringView(const char *pUTF8) noexcept;
158 
166  YI_CONSTEXPR CYIStringView(const char *pUTF8, size_t sizeBytes) noexcept;
167 
174 
180  CYIStringView(const std::string &other) noexcept;
181 
182 #if __cplusplus >= 201703L
183 
188  YI_CONSTEXPR CYIStringView(const std::string_view &other) noexcept;
189 #endif
190 
191 #if defined(__OBJC__) || defined(DOXY)
192 
197  CYIStringView(const NSString *pString);
198 #endif
199 
205  CYIStringView(const std::vector<char> &vector) noexcept;
206 
212  CYIStringView(const std::vector<uint8_t> &vector) noexcept;
213 
217  YI_CONSTEXPR CYIStringView &operator=(const CYIStringView &view) noexcept = default;
218 
224  YI_CONSTEXPR bool IsEmpty() const;
225 
233  YI_CONSTEXPR bool IsNotEmpty() const;
234 
244  YI_CONSTEXPR const char *GetRawData() const noexcept;
245 
251  YI_CONSTEXPR size_t GetSizeInBytes() const noexcept;
252 
260  size_t GetLength() const;
261 
269  YI_CONSTEXPR char ByteAt(size_t byteOffset) const noexcept;
270 
276  char32_t At(size_t pos) const;
277 
283  char32_t operator[](size_t pos) const;
284 
290  bool ContainsNonASCII() const;
291 
299  bool IsNumeric() const;
300 
304  ConstIterator begin() const;
305 
309  ConstIterator end() const;
310 
314  ConstReverseIterator rbegin() const;
315 
319  ConstReverseIterator rend() const;
320 
330  bool Contains(CYIStringView s, CaseMode caseSensitivity = CaseMode::CaseSensitive) const;
331 
343  bool StartsWith(CYIStringView s, CaseMode caseSensitivity = CaseMode::CaseSensitive) const;
344 
356  bool EndsWith(CYIStringView s, CaseMode caseSensitivity = CaseMode::CaseSensitive) const;
357 
367  int32_t Compare(CYIStringView other, CaseMode caseSensitivity = CaseMode::CaseSensitive) const noexcept;
368 
378  bool Equals(CYIStringView other, CaseMode caseSensitivity = CaseMode::CaseSensitive) const noexcept;
379 
390  bool EqualsCaseInsensitiveASCII(CYIStringView other) const noexcept;
391 
399  CYISmallVector<CYIStringView, 3> Split(CYIStringView delimiter, bool ignoreEmptySubstrings = true, size_t maximumParts = NPos) const;
400 
404  CYIStringView SubStr(size_t pos = 0, size_t len = NPos) const;
405 
409  CYIStringView SubStr(const ConstIterator &begin, const ConstIterator &end) const;
410 
414  CYIStringView Left(size_t len) const;
415 
419  CYIStringView Right(size_t len) const;
420 
428  CYIString ToUpperASCII() const;
429 
437  CYIString ToLowerASCII() const;
438 
446  size_t IndexOf(CYIStringView view, size_t pos = 0) const;
447 
455  size_t LastIndexOf(CYIStringView view, size_t pos = NPos) const;
456 
460  ConstIterator Find(CYIStringView view, const ConstIterator &begin) const;
461 
465  ConstReverseIterator ReverseFind(CYIStringView view, const ConstReverseIterator &begin) const;
466 
474  CYIString Arg(CYIStringView text) const;
475 
479  void Clear();
480 
484  void Swap(CYIStringView &view);
485 
489  void Trim();
490 
494  void TrimLeft(size_t len = NPos);
495 
499  void TrimRight(size_t len = NPos);
500 
504  void RemovePrefix(size_t len);
505 
513  bool RemovePrefix(CYIStringView prefix, CaseMode caseSensitivity = CaseMode::CaseSensitive);
514 
518  void RemoveSuffix(size_t len);
519 
527  bool RemoveSuffix(CYIStringView prefix, CaseMode caseSensitivity = CaseMode::CaseSensitive);
528 
538  std::unique_ptr<char[]> ToUTF8(size_t *pElementsCount = nullptr) const;
539 
545  std::unique_ptr<char16_t[]> ToUTF16(size_t *pElementsCount = nullptr) const;
546 
552  std::unique_ptr<char32_t[]> ToUTF32(size_t *pElementsCount = nullptr) const;
553 
561  std::unique_ptr<wchar_t[]> ToWideChars(size_t *pElementsCount = nullptr) const;
562 
568  CYIString ToCYIString() const;
569 
575  std::string ToStdString() const;
576 
582  std::wstring ToStdWString() const;
583 
584 #if defined(__OBJC__) || defined(DOXY)
585 
590  NSString *ToNSString() const;
591 
597  CFStringRef ToCFStringRef() const;
598 #endif
599 
612  template<class T, typename S = std::istream, typename B = std::streambuf>
613  T To(bool *pError = nullptr) const;
614 
620  template<class T>
621  T ToValue(bool *pError = nullptr) const;
622 
628  float ToFloat(bool *pError = nullptr) const;
629 
635  double ToDouble(bool *pError = nullptr) const;
636 
664  bool ToBool(bool *pError = nullptr) const;
665 
671  CYIString ToHexadecimal() const;
672 
673 private:
674  size_t m_size;
675  const char *m_pData;
676 };
677 
678 // std::hash specialization for CYIStringView. Note that CYIStringView's and CYIString's that are equal do not hash to the same value.
679 namespace std
680 {
681 template<>
682 struct hash<CYIStringView>;
683 }
684 
685 std::ostream &operator<<(std::ostream &stream, const CYIStringView &val);
686 
687 YI_CONSTEXPR bool operator==(CYIStringView lhs, CYIStringView rhs) noexcept;
688 YI_CONSTEXPR bool operator!=(CYIStringView lhs, CYIStringView rhs) noexcept;
689 
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 bool operator>=(CYIStringView lhs, CYIStringView rhs) noexcept;
694 
697 #include "utility/internal/YiStringView.inl"
698 
699 #endif
CYIStringView SubStr(size_t pos=0, size_t len=NPos) const
std::bidirectional_iterator_tag iterator_category
Definition: YiStringView.h:73
const char32_t * pointer
Definition: YiStringView.h:76
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:34
CYIString ToUpperASCII() const
CaseMode
Definition: YiStringView.h:124
CYIString ToCYIString() const
CYISmallVector< CYIStringView, 3 > Split(CYIStringView delimiter, bool ignoreEmptySubstrings=true, size_t maximumParts=NPos) const
char32_t reference
Definition: YiStringView.h:77
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:32
STL namespace.
CYIString Arg(CYIStringView text) const
value_type operator*() const
ConstReverseIterator Reverse() const
T ToValue(bool *pError=nullptr) const
Definition: YiStringView.h:67
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:140
std::wstring ToStdWString() const
bool Contains(CYIStringView s, CaseMode caseSensitivity=CaseMode::CaseSensitive) const
#define YI_CONSTEXPR
Definition: YiPredef.h:38
char32_t value_type
Definition: YiStringView.h:74
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
T To(bool *pError=nullptr) const
std::reverse_iterator< Iterator > ReverseIterator
Definition: YiStringView.h:117
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:70
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
YI_CONSTEXPR size_t GetSizeInBytes() const noexcept
unsigned int size_t
Definition: YiPredefLinux.h:10
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:59
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:75
ConstIterator Iterator
Definition: YiStringView.h:116
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:56
std::unique_ptr< char16_t[]> ToUTF16(size_t *pElementsCount=nullptr) const