SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
String.hpp
Go to the documentation of this file.
1#ifndef _STRING_H_
2#define _STRING_H_
3
4#include <iostream> // For std::ostream (optional)
6
7namespace Sleak {
8
9 /**
10 * @class String
11 * @brief A dynamic string class, similar to std::string but STL-agnostic.
12 *
13 * This class provides a flexible and efficient way to manage strings of characters.
14 * It is designed to be independent of the Standard Template Library (STL) and can be
15 * used in environments where STL is not available or desired.
16 *
17 * Example Use Cases:
18 * - Storing and manipulating text in game engines or embedded systems.
19 * - Implementing custom string processing algorithms.
20 * - Learning about string implementation internals.
21 *
22 * Implementation Details:
23 * - Uses a dynamically allocated array to store characters.
24 * - Provides a wide range of methods for string manipulation,
25 * including concatenation, comparison, searching, and substring extraction.
26 * - Automatically manages memory allocation and resizing.
27 * @ingroup utility
28 */
29 class String {
30 public:
31 // Constructors
32 String() noexcept : data_(nullptr), size_(0), capacity_(0) {}
33
34 String(const char* str) : String(str, strlen(str)) {}
35
36 String(const char* str, size_t len) : size_(len), capacity_(len + 1) {
37 data_ = new char[capacity_];
38 memcpy(data_, str, len);
39 data_[len] = '\0';
40 }
41
42 String(const String& other) : size_(other.size_), capacity_(other.capacity_) {
43 data_ = new char[capacity_];
44 memcpy(data_, other.data_, size_ + 1);
45 }
46
47 String(String&& other) noexcept : data_(other.data_), size_(other.size_), capacity_(other.capacity_) {
48 other.data_ = nullptr;
49 other.size_ = 0;
50 other.capacity_ = 0;
51 }
52
53 /// Frees the backing buffer.
55 deletedata_;
56 }
57
58 // Assignment operators
59 String& operator=(const String& other) {
60 if (this != &other) {
61 if (capacity_ < other.size_ + 1) {
62 deletedata_;
63 capacity_ = other.capacity_;
64 data_ = new char[capacity_];
65 }
66 size_ = other.size_;
67 memcpy(data_, other.data_, size_ + 1);
68 }
69 return *this;
70 }
71
72 String& operator=(String&& other) noexcept {
73 if (this != &other) {
74 deletedata_;
75 data_ = other.data_;
76 size_ = other.size_;
77 capacity_ = other.capacity_;
78 other.data_ = nullptr;
79 other.size_ = 0;
80 other.capacity_ = 0;
81 }
82 return *this;
83 }
84
85 // Element access
86 char& operator[](size_t index) {
87 if (index >= size_) {
88 throw IndexOutOfBoundsException("String index out of bounds");
89 }
90 return data_[index];
91 }
92
93 const char& operator[](size_t index) const {
94 if (index >= size_) {
95 throw IndexOutOfBoundsException("String index out of bounds");
96 }
97 return data_[index];
98 }
99
100 // Capacity
101 size_t size() const { return size_; }
102 size_t length() const { return size_; }
103 size_t capacity() const { return capacity_; }
104 bool empty() const { return size_ == 0; }
105
106 // Modifiers
107 void clear() {
108 size_ = 0;
109 data_[0] = '\0';
110 }
111
112 void resize(size_t newSize, char ch = '\0') {
113 if (newSize > capacity_) {
114 reserve(newSize + 1);
115 }
116 if (newSize > size_) {
117 memset(data_ + size_, ch, newSize - size_);
118 }
119 size_ = newSize;
120 data_[size_] = '\0';
121 }
122
123 void reserve(size_t newCapacity) {
124 if (newCapacity > capacity_) {
125 char* newData = new char[newCapacity];
126 memcpy(newData, data_, size_ + 1);
127 deletedata_;
128 data_ = newData;
129 capacity_ = newCapacity;
130 }
131 }
132
133 String& append(const String& other) {
134 return append(other.data_, other.size_);
135 }
136
137 String& append(const char* str) {
138 return append(str, strlen(str));
139 }
140
141 String& append(const char* str, size_t len) {
142 if (size_ + len > capacity_) {
143 reserve(size_ + len + 1);
144 }
145 memcpy(data_ + size_, str, len);
146 size_ += len;
147 data_[size_] = '\0';
148 return *this;
149 }
150
151 // String operations
152 const char* c_str() const { return data_; }
153
154 int compare(const String& other) const {
155 return strcmp(data_, other.data_);
156 }
157
158 size_t find(const String& str, size_t pos = 0) const {
159 const char* result = strstr(data_ + pos, str.data_);
160 return result ? result - data_ : npos;
161 }
162
163 String substr(size_t pos = 0, size_t len = npos) const {
164 if (pos > size_) {
165 throw IndexOutOfBoundsException("String substring: pos out of bounds");
166 }
167 len = std::min(len, size_ - pos);
168 return String(data_ + pos, len);
169 }
170
171 // Operators
172 String& operator+=(const String& other) {
173 return append(other);
174 }
175
176 String& operator+=(const char* str) {
177 return append(str);
178 }
179
180 String& operator+=(char ch) {
181 return append(&ch, 1);
182 }
183
184 friend String operator+(const String& lhs, const String& rhs) {
185 String result(lhs);
186 result += rhs;
187 return result;
188 }
189
190 friend String operator+(const String& lhs, const char* rhs) {
191 String result(lhs);
192 result += rhs;
193 return result;
194 }
195
196 friend String operator+(const char* lhs, const String& rhs) {
197 String result(lhs);
198 result += rhs;
199 return result;
200 }
201
202 friend bool operator==(const String& lhs, const String& rhs) {
203 return lhs.compare(rhs) == 0;
204 }
205
206 friend bool operator!=(const String& lhs, const String& rhs) {
207 return lhs.compare(rhs) != 0;
208 }
209
210 friend bool operator<(const String& lhs, const String& rhs) {
211 return lhs.compare(rhs) < 0;
212 }
213
214 friend bool operator<=(const String& lhs, const String& rhs) {
215 return lhs.compare(rhs) <= 0;
216 }
217
218 friend bool operator>(const String& lhs, const String& rhs) {
219 return lhs.compare(rhs) > 0;
220 }
221
222 friend bool operator>=(const String& lhs, const String& rhs) {
223 return lhs.compare(rhs) >= 0;
224 }
225
226 friend std::ostream& operator<<(std::ostream& os, const String& str) {
227 os << str.c_str();
228 return os;
229 }
230
231 private:
232 char* data_;
233 size_t size_;
234 size_t capacity_;
235
236 static const size_t npos = static_cast<size_t>(-1);
237
238 /// Frees and nulls out the backing buffer.
239 void deletedata_() {
240 delete[] data_;
241 data_ = nullptr;
242 }
243 };
244}
245
246#endif // _STRING_H_
int compare(const String &other) const
Definition String.hpp:154
size_t find(const String &str, size_t pos=0) const
Definition String.hpp:158
const char & operator[](size_t index) const
Definition String.hpp:93
const char * c_str() const
Definition String.hpp:152
friend bool operator>(const String &lhs, const String &rhs)
Definition String.hpp:218
String & operator=(const String &other)
Definition String.hpp:59
size_t length() const
Definition String.hpp:102
String(const char *str)
Definition String.hpp:34
char & operator[](size_t index)
Definition String.hpp:86
String & append(const char *str, size_t len)
Definition String.hpp:141
String(const char *str, size_t len)
Definition String.hpp:36
String(String &&other) noexcept
Definition String.hpp:47
~String()
Frees the backing buffer.
Definition String.hpp:54
friend String operator+(const String &lhs, const String &rhs)
Definition String.hpp:184
String & append(const char *str)
Definition String.hpp:137
String() noexcept
Definition String.hpp:32
bool empty() const
Definition String.hpp:104
friend bool operator<=(const String &lhs, const String &rhs)
Definition String.hpp:214
void reserve(size_t newCapacity)
Definition String.hpp:123
friend bool operator==(const String &lhs, const String &rhs)
Definition String.hpp:202
size_t capacity() const
Definition String.hpp:103
friend bool operator>=(const String &lhs, const String &rhs)
Definition String.hpp:222
String & operator+=(const char *str)
Definition String.hpp:176
friend String operator+(const String &lhs, const char *rhs)
Definition String.hpp:190
String(const String &other)
Definition String.hpp:42
String substr(size_t pos=0, size_t len=npos) const
Definition String.hpp:163
void clear()
Definition String.hpp:107
friend bool operator!=(const String &lhs, const String &rhs)
Definition String.hpp:206
friend std::ostream & operator<<(std::ostream &os, const String &str)
Definition String.hpp:226
String & operator+=(const String &other)
Definition String.hpp:172
String & append(const String &other)
Definition String.hpp:133
friend bool operator<(const String &lhs, const String &rhs)
Definition String.hpp:210
void resize(size_t newSize, char ch='\0')
Definition String.hpp:112
friend String operator+(const char *lhs, const String &rhs)
Definition String.hpp:196
String & operator+=(char ch)
Definition String.hpp:180
String & operator=(String &&other) noexcept
Definition String.hpp:72
size_t size() const
Definition String.hpp:101
Root namespace for everything the engine exposes.
Definition Camera.hpp:10