Atoms Crowd  7.0.0
String.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/SSE.h>
11 #include <ToolchefsSTL/Allocators/Allocator.h>
12 #include <stdint.h>
13 
14 
15 namespace ToolchefsSTL
16 {
17 
18  class TOOLCHEFSSTL_EXPORT String
19  {
20  public:
21 
22  template <class Iter>
24  {
25  ReverseIterator(Iter* ptr): m_ptr(ptr) {}
26  ~ReverseIterator() = default;
27  Iter* operator +(size_t value) const { return m_ptr - value; }
28  Iter* operator -(size_t value) const { return m_ptr + value; }
29  Iter* operator ++() const { return --m_ptr;}
30  Iter* operator --() const { return ++m_ptr;}
31  bool operator ==(const ReverseIterator& rhs) const { return m_ptr == rhs.m_ptr; }
32  bool operator !=(const ReverseIterator& rhs) const { return m_ptr != rhs.m_ptr; }
33  Iter operator *() const {return *m_ptr;}
34  Iter* operator->() const { return m_ptr;}
35  Iter* m_ptr;
36  };
37 
38  typedef char* iterator;
39  typedef const char* const_iterator;
42  typedef uint32_t size_type;
43 
44 #if __cplusplus >= 201703L
45  static constexpr const size_type DataSize{ 16 };
46  static constexpr const size_type MaxLength{ DataSize - 1 };
47  static constexpr size_type npos{ static_cast<size_type>(-1) };
48 #else
49  static const size_type DataSize;
50  static const size_type MaxLength;
51  static size_type npos;
52 #endif
53  bool is_internal() const noexcept { return m_capacity <= DataSize; }
54 
55  explicit String(Allocator* allocator = defaultAllocator()) noexcept;
56 
57  String(const char* value, Allocator* allocator = defaultAllocator()) noexcept ;
58 
59  String(const String& rhs, Allocator* allocator = defaultAllocator()) noexcept;
60 
61  String(String&& rhs) noexcept;
62 
63  String& operator=(const String& rhs) noexcept;
64 
65  String& operator=(const char* rhs) noexcept;
66 
67  String& operator=(String&& rhs) noexcept;
68 
69  ~String();
70 
71  inline size_type size() const noexcept { return m_length;}
72 
73  inline size_type length() const noexcept { return m_length;}
74 
75  inline size_type capacity() const noexcept { return m_capacity; }
76 
77  void reserve(size_type max_length) noexcept;
78 
79  void resize(size_type new_length) noexcept;
80 
81  void resize(size_type new_length, char value) noexcept;
82 
83  void clear();
84 
85  bool empty() const noexcept;
86 
87 
89  iterator begin() noexcept;
90  iterator end() noexcept;
91  const_iterator cbegin() const noexcept;
92  const_iterator cend() const noexcept;
93 
94  reverse_iterator rbegin() noexcept;
95  reverse_iterator rend() noexcept;
96  const_reverse_iterator crbegin() const noexcept;
97  const_reverse_iterator crend() const noexcept;
98 
100  char& back() noexcept;
101  const char& back() const noexcept;
102 
103  char& front() noexcept;
104  const char& front() const noexcept;
105 
106  char& operator[](const size_type pos) noexcept;
107 
108  const char& operator[](const size_type pos) const noexcept;
109 
110  const char* c_str() const noexcept;
111 
113  bool operator==(const String& rhs) const noexcept;
114  bool operator!=(const String& rhs) const noexcept;
115 
116  bool operator<(const String& rhs) const noexcept;
117  bool operator<=(const String& rhs) const noexcept;
118 
119  bool operator>(const String& rhs) const noexcept;
120  bool operator>=(const String& rhs) const noexcept;
121 
123  String& operator+=(const String& rhs) noexcept;
124  String& operator+=(const char* cstr) noexcept;
125  String& operator+=(const char chr) noexcept;
126  String& operator+=(int num) noexcept;
127 
128  String operator+(const String& rhs) const noexcept;
129  String operator+(const char* cstr) const noexcept;
130  String operator+(const char chr) const noexcept;
131 
133  size_type find(const String& str, size_type pos = 0) const noexcept;
134  size_type find(const char* s, size_type pos = 0) const noexcept;
135  size_type find(const char s, size_type pos = 0) const noexcept;
136 
137  size_type rfind(const String& str, size_type pos = npos) const noexcept;
138  size_type rfind(const char* s, size_type pos = npos) const noexcept;
139  size_type rfind(const char s, size_type pos = npos) const noexcept;
140 
141  String substr(size_type pos = 0, size_type len = npos) const noexcept;
142 
143  String replace(const String& src, const String& dest) const noexcept;
144 
145  int compare(const String& str) const noexcept;
146  int compare(const char* s) const noexcept;
147 
148  bool endswith(const String& ending) const noexcept;
149  bool startswith(const String& prefix)const noexcept;
150 
151  size_type count(const String& substr, size_type start = 0, size_type end = npos) const noexcept;
152 
153  String capitalize() const noexcept;
154  String upper() const noexcept;
155  String lower() const noexcept;
156  String swapcase() const noexcept;
157  String zfill(size_type width) const noexcept;
158 
163  bool isalnum() const noexcept;
164 
169  bool isalpha() const noexcept;
170 
175  bool isdigit() const noexcept;
176 
181  bool islower() const noexcept;
182 
187  bool isspace() const noexcept;
188 
194  bool istitle() const noexcept;
195 
200  bool isupper() const noexcept;
201 
202 
203  static String fromInt(int num) noexcept;
204 
205  static String fromFloat(float num) noexcept;
206 
207  static String format(const char* format, ...);
208 
209  int toInt() const;
210 
211  double toDouble() const;
212 
213  private:
214 
215  void convert_to_external(const size_type new_capacity);
216 
217  void convert_to_internal(const size_type new_length);
218 
219  void clear_buffer();
220 
221  char* alloc_mem(size_type num_char) const noexcept;
222 
223  void free_mem(char* ptr) const noexcept;
224 
225  private:
226 
227 #ifndef ATOMS_SSE
228 #if __cplusplus >= 201703L
229  typedef union alignas(16) __m128i {
230  char m128i_i8[DataSize];
231  } __m128i;
232 #else
233  typedef union alignas(16) __m128i {
234  char m128i_i8[16];
235  } __m128i;
236 #endif
237 #endif
238 
239  union
240  {
241  __m128i internal;
242  char* external;
243  } m_data;
244 
245  size_type m_length;
246  size_type m_capacity;
247  Allocator* m_allocator;
248  };
249 
250  inline String operator+(const char* cstr, const String& str) { return String(cstr) + str; }
251 }
Definition: String.h:19