Atoms Crowd  7.0.0
WString.h
1 #pragma once
2 // ===========================================================================
3 // Copyright (c) 2015-2024 Toolchefs Ltd. All rights reserved.
4 //
5 // Use of this software is subject to the terms of the Toolchefs license
6 // agreement provided at the time of installation or download, or which
7 // otherwise accompanies this software in either electronic or hard copy form.
8 // ===========================================================================
9 #include <ToolchefsSTL/Globals.h>
10 #include <ToolchefsSTL/Allocators/Allocator.h>
11 #include <stdint.h>
12 
13 
14 namespace ToolchefsSTL
15 {
16 
17  class TOOLCHEFSSTL_EXPORT WString
18  {
19  public:
20 
21  template <class Iter>
23  {
24  ReverseIterator(Iter* ptr) : m_ptr(ptr) {}
25  ~ReverseIterator() = default;
26  Iter* operator +(size_t value) const { return m_ptr - value; }
27  Iter* operator -(size_t value) const { return m_ptr + value; }
28  Iter* operator ++() const { return --m_ptr; }
29  Iter* operator --() const { return ++m_ptr; }
30  bool operator ==(const ReverseIterator& rhs) const { return m_ptr == rhs.m_ptr; }
31  bool operator !=(const ReverseIterator& rhs) const { return m_ptr != rhs.m_ptr; }
32  Iter operator *() const { return *m_ptr; }
33  Iter* operator->() const { return m_ptr; }
34  Iter* m_ptr;
35  };
36 
37  typedef wchar_t* iterator;
38  typedef const wchar_t* const_iterator;
41  typedef uint32_t size_type;
42 
43  static constexpr const size_type DataSize{ 16 };
44  static constexpr const size_type MaxLength{ DataSize - 1 };
45  static constexpr size_type npos{ static_cast<size_type>(-1) };
46 
47  bool is_internal() const noexcept { return m_capacity <= DataSize; }
48 
49  explicit WString(Allocator* allocator = defaultAllocator()) noexcept;
50 
51  WString(const wchar_t* value, Allocator* allocator = defaultAllocator()) noexcept;
52 
53  WString(const WString& rhs, Allocator* allocator = defaultAllocator()) noexcept;
54 
55  WString(WString&& rhs) noexcept;
56 
57  WString& operator=(const WString& rhs) noexcept;
58 
59  WString& operator=(const wchar_t* rhs) noexcept;
60 
61  WString& operator=(WString&& rhs) noexcept;
62 
63  ~WString();
64 
65  inline size_type size() const noexcept { return m_length; }
66 
67  inline size_type length() const noexcept { return m_length; }
68 
69  inline size_type capacity() const noexcept { return m_capacity; }
70 
71  void reserve(size_type max_length) noexcept;
72 
73  void resize(size_type new_length) noexcept;
74 
75  void resize(size_type new_length, wchar_t value) noexcept;
76 
77  void clear();
78 
79  bool empty() const noexcept;
80 
81 
83  iterator begin() noexcept;
84  iterator end() noexcept;
85  const_iterator cbegin() const noexcept;
86  const_iterator cend() const noexcept;
87 
88  reverse_iterator rbegin() noexcept;
89  reverse_iterator rend() noexcept;
90  const_reverse_iterator crbegin() const noexcept;
91  const_reverse_iterator crend() const noexcept;
92 
94  wchar_t& back() noexcept;
95  const wchar_t& back() const noexcept;
96 
97  wchar_t& front() noexcept;
98  const wchar_t& front() const noexcept;
99 
100  wchar_t& operator[](const size_type pos) noexcept;
101 
102  const wchar_t& operator[](const size_type pos) const noexcept;
103 
104  const wchar_t* c_str() const noexcept;
105 
107  bool operator==(const WString& rhs) const noexcept;
108  bool operator!=(const WString& rhs) const noexcept;
109 
110  bool operator<(const WString& rhs) const noexcept;
111  bool operator<=(const WString& rhs) const noexcept;
112 
113  bool operator>(const WString& rhs) const noexcept;
114  bool operator>=(const WString& rhs) const noexcept;
115 
117  WString& operator+=(const WString& rhs) noexcept;
118  WString& operator+=(const wchar_t* cstr) noexcept;
119  WString& operator+=(const wchar_t chr) noexcept;
120  WString& operator+=(int num) noexcept;
121 
122  WString operator+(const WString& rhs) const noexcept;
123  WString operator+(const wchar_t* cstr) const noexcept;
124  WString operator+(const wchar_t chr) const noexcept;
125 
127  size_type find(const WString& str, size_type pos = 0) const noexcept;
128  size_type find(const wchar_t* s, size_type pos = 0) const noexcept;
129  size_type find(const wchar_t s, size_type pos = 0) const noexcept;
130 
131  size_type rfind(const WString& str, size_type pos = npos) const noexcept;
132  size_type rfind(const wchar_t* s, size_type pos = npos) const noexcept;
133  size_type rfind(const wchar_t s, size_type pos = npos) const noexcept;
134 
135  WString substr(size_type pos = 0, size_type len = npos) const noexcept;
136 
137  WString replace(const WString& src, const WString& dest) const noexcept;
138 
139  int compare(const WString& str) const noexcept;
140  int compare(const wchar_t* s) const noexcept;
141 
142  bool endswith(const WString& ending) const noexcept;
143  bool startswith(const WString& prefix)const noexcept;
144 
145  size_type count(const WString& substr, size_type start = 0, size_type end = npos) const noexcept;
146 
147  WString capitalize() const noexcept;
148  WString upper() const noexcept;
149  WString lower() const noexcept;
150  WString swapcase() const noexcept;
151  WString zfill(size_type width) const noexcept;
152 
157  bool isalnum() const noexcept;
158 
163  bool isalpha() const noexcept;
164 
169  bool isdigit() const noexcept;
170 
175  bool islower() const noexcept;
176 
181  bool isspace() const noexcept;
182 
188  bool istitle() const noexcept;
189 
194  bool isupper() const noexcept;
195 
196 
197  static WString fromInt(int num) noexcept;
198 
199  static WString fromFloat(float num) noexcept;
200 
201  static WString format(const wchar_t* format, ...);
202 
203  int toInt() const;
204 
205  double toDouble() const;
206 
207  private:
208 
209  void convert_to_external(const size_type new_capacity);
210 
211  void convert_to_internal(const size_type new_length);
212 
213  void clear_buffer();
214 
215  wchar_t* alloc_mem(size_type num_char) const noexcept;
216 
217  void free_mem(wchar_t* ptr) const noexcept;
218 
219  private:
220 
221  union
222  {
223  wchar_t internal[DataSize];
224  wchar_t* external;
225  } m_data;
226 
227  size_type m_length;
228  size_type m_capacity;
229  Allocator* m_allocator;
230  };
231 
232  inline WString operator+(const wchar_t* cstr, const WString& str) { return WString(cstr) + str; }
233 }
Definition: Allocator.h:10
Definition: WString.h:18