Iridescence 1.00
Embedded Graphic Framework
Loading...
Searching...
No Matches
ds_string.h
1/* Copyright (C) 2017 - 2024, Thornwave Labs Inc - All Rights Reserved.
2 * Unauthorized copying of this file, via any medium is strictly prohibited.
3 * Proprietary and confidential.
4 * Written by Razvan Turiac <razvan.turiac@thornwave.com>
5*/
6
7#ifndef _DS_STRING_H
8#define _DS_STRING_H
9
10
11#include <stdint.h>
12#include <stddef.h>
13
14#include <stdarg.h>
15#include <math.h>
16
17
18
26{
27public:
28 static const size_t npos = -1;
29
37 DsString(size_t max_length = 31);
38
39
51 DsString(const char* str, size_t max_length = DsString::npos);
52
53
57 DsString(const DsString &src);
58
59
64
65
70
71
76
77
82
83
87 DsString& operator=(const char* rhs);
88
89
94 DsString& operator+=(const char* rhs);
95
96
102
103
109 void append(char c, size_t count);
110
111
117 void append(const char* str);
118
119
124 void append(const char* str, size_t count);
125
126
131 inline void append(char c)
132 {
133 append(c, 1);
134 }
135
136
141 inline void append(const DsString &str)
142 {
143 append(str.mString);
144 }
145
146
153 void insert(size_t pos, char c, size_t count);
154
155
161 inline void insert(size_t pos, char c)
162 {
163 insert(pos, c, 1);
164 }
165
166
172 void insert(size_t pos, const char* str);
173
174
180 void erase(size_t pos, size_t count);
181
182
187 inline void erase_first(size_t count)
188 {
189 erase(0, count);
190 }
191
192
197 inline void erase_last(size_t count)
198 {
199 if (count <= mLength)
200 erase(mLength - count, count);
201 else
202 clear();
203 }
204
205
209 void clear(void);
210
211
218 void substr(char *substr, size_t pos, size_t count) const;
219
220
225 const char* c_str(void) const
226 {
227 return mString;
228 }
229
230
235 operator const char*() const
236 {
237 return mString;
238 }
239
240
245 size_t length(void) const
246 {
247 return mLength;
248 }
249
250
255 size_t maxLength(void) const
256 {
257 return mCapacity - 1;
258 }
259
260
265 bool empty(void) const
266 {
267 return mLength == 0;
268 }
269
270
275 char front(void)
276 {
277 return mString[0];
278 }
279
280
285 char back(void)
286 {
287 if (mLength)
288 return mString[mLength - 1];
289 else
290 return 0;
291 }
292
293
297 bool operator==(const char* rhs) const;
298
299
303 inline bool operator!=(const char* rhs) const
304 {
305 return !(*this == rhs);
306 }
307
308
312 bool operator>=(const char* rhs) const;
313
314
318 bool operator<=(const char* rhs) const;
319
320
324 inline bool operator>(const char* rhs) const
325 {
326 return !(*this <= rhs);
327 }
328
329
333 inline bool operator<(const char* rhs) const
334 {
335 return !(*this >= rhs);
336 }
337
338
343 size_t format(const char* format, ...);
344
345
351 size_t format(const char* format, va_list args);
352
353
360 size_t find(char c, size_t pos = 0);
361
362
369 size_t find(const char* substr, size_t pos = 0);
370
371
372private:
373 size_t mCapacity;
374 size_t mLength;
375 char* mString;
376};
377
378#endif
DsString is a class representing a character string.
Definition ds_string.h:26
DsString & operator+=(const char *rhs)
Addition operator - appends a string to the current DsString object.
DsString(const DsString &src)
Copy constructor.
void erase_last(size_t count)
Erases characters from the end of the string.
Definition ds_string.h:197
bool operator==(const char *rhs) const
Comparison operator.
DsString & operator=(DsString &&rhs)
Move operator.
bool operator!=(const char *rhs) const
Comparison operator.
Definition ds_string.h:303
void append(char c, size_t count)
Appends a character a number of times to the current DsString object.
size_t format(const char *format, va_list args)
Initializes the string using vprintf() style.
void append(const char *str)
Appends a C string to the current DsString object.
void erase_first(size_t count)
Erases characters from the beginning of the string.
Definition ds_string.h:187
static const size_t npos
Constant representing an invalid position/size value.
Definition ds_string.h:28
size_t format(const char *format,...)
Initializes the string using printf() style.
void append(const char *str, size_t count)
Appends a C string a number of times.
bool operator<(const char *rhs) const
Comparison operator.
Definition ds_string.h:333
const char * c_str(void) const
Returns the pointer to the underlaying C string.
Definition ds_string.h:225
size_t length(void) const
Returns the length of the string.
Definition ds_string.h:245
char back(void)
Returns the last character of the string.
Definition ds_string.h:285
void erase(size_t pos, size_t count)
Erases characters from the specified position.
DsString(DsString &&src)
Move constructor.
DsString & operator=(const char *rhs)
Assignment operator from a C string.
DsString operator+(const DsString &rhs)
Addition operator - appends a DsString to the current DsString object.
DsString & operator=(const DsString &rhs)
Assignment operator from another DsString.
void append(char c)
Appends a single character to the current DsString object.
Definition ds_string.h:131
void insert(size_t pos, char c, size_t count)
Inserts a character a number of times at a specified position in the current DsString object.
bool operator>(const char *rhs) const
Comparison operator.
Definition ds_string.h:324
DsString(const char *str, size_t max_length=DsString::npos)
Creates a DsString object with space for storing the specified string or a specified length....
void substr(char *substr, size_t pos, size_t count) const
Extracts a substring of the current string.
void clear(void)
Clears the entire string.
bool operator>=(const char *rhs) const
Comparison operator.
bool operator<=(const char *rhs) const
Comparison operator.
void insert(size_t pos, char c)
Inserts a single character at a specified position in the current DsString object.
Definition ds_string.h:161
void insert(size_t pos, const char *str)
Inserts a C string at a specified position in the current DsString object.
size_t maxLength(void) const
Returns the maximum string length this DsString can hold.
Definition ds_string.h:255
DsString(size_t max_length=31)
Creates a DsString object with space for storing a string of specified length.
bool empty(void) const
Is the string empty?
Definition ds_string.h:265
char front(void)
Returns the first character of the string.
Definition ds_string.h:275
size_t find(char c, size_t pos=0)
Searches for a character starting at a specific position.
~DsString()
Destructor.
size_t find(const char *substr, size_t pos=0)
Searches for a substring starting at a specific position.
void append(const DsString &str)
Appends a DsString object to the current DsString object.
Definition ds_string.h:141