Max OS 0.3
Loading...
Searching...
No Matches
MaxOS::String Class Reference

Dynamically sized string with various operations. More...

#include <string.h>

Public Member Functions

 String ()
 Construct a String, 0 length and only contains the null terminator.
 
 String (char c)
 Constructs a String from a single character.
 
 String (char const *string)
 Constructs a String from a pointer to an array of chars.
 
 String (uint8_t const *string, int length)
 Constructs a string from a an array of bytes (doesnt have to be null terminated)
 
 String (String const &other)
 Copy constructor for the string.
 
 String (int value)
 Constructs a string from an integer (must be base 10)
 
 String (uint64_t value)
 Constructs a string from a hex value (Excludes 0x)
 
 String (bool value)
 Constructs a String from a boolean as "true" or "false".
 
 ~String ()
 Destructor for the string, cleans up memory if needed.
 
void copy (String const &other)
 Copies the other string.
 
size_t length (bool count_ansi=true) const
 Returns the length of the string.
 
char * c_str ()
 The char pointer representation of the current string.
 
const char * c_str () const
 Returns the string as a c string.
 
bool starts_with (String const &other)
 Checks if the string starts with the other string (must contain the same characters in the same order)
 
String substring (size_t start, size_t length) const
 Get a section of the string.
 
common::Vector< Stringsplit (String const &delimiter) const
 Splits the string by the delimiter.
 
String strip (char strip_char=' ') const
 Strips the string of whitespace.
 
String center (size_t width, char fill=' ') const
 Centers the string in a specified width.
 
bool equals (String const &other) const
 Checks if one string is equal to another.
 
Stringoperator= (String const &other)
 Sets the string to the other string.
 
String operator+ (String const &other) const
 Adds the other string to the string.
 
Stringoperator+= (String const &other)
 Adds the other string to the string.
 
String operator* (int times) const
 Returns the string repeated a number of times.
 
bool operator== (String const &other) const
 Checks if one string is equal to another.
 
bool operator!= (String const &other) const
 Checks if one string is not equal to another.
 
bool operator< (String const &other) const
 Checks if the sum of the ascii values of the characters in the string is less than the sum of the ascii values of the characters in the other string.
 
bool operator> (String const &other) const
 Checks if the sum of the ascii values of the characters in the string is greater than the sum of the ascii values of the characters in the other string.
 
bool operator<= (String const &other) const
 Checks if the sum of the ascii values of the characters in the string is less than or equal to the sum of the ascii values of the characters in the other string.
 
bool operator>= (String const &other) const
 Checks if the sum of the ascii values of the characters in the string is greater than or equal to the sum of the ascii values of the characters in the other string.
 
char & operator[] (size_t index)
 Returns the character at the specified index.
 
char & operator[] (size_t index) const
 Returns the character at the specified index.
 

Static Public Member Functions

static String formatted (char const *format,...)
 Creates a formated string. s = string, x = hex, d = decimal.
 
static String formatted (char const *format, va_list parameters)
 Creates a formated string. s = string, x = hex, d = decimal.
 

Detailed Description

Dynamically sized string with various operations.

Definition at line 26 of file string.h.

Constructor & Destructor Documentation

◆ String() [1/8]

String::String ( )

Construct a String, 0 length and only contains the null terminator.

Definition at line 16 of file string.cpp.

16 {
17
18 // String that only contains the null terminator
19 m_length = 0;
20 allocate_self();
21 m_string[0] = '\0';
22
23
24}

◆ String() [2/8]

String::String ( char  c)

Constructs a String from a single character.

Parameters
cThe character

Definition at line 30 of file string.cpp.

30 {
31
32 // Create the memory
33 m_length = 1;
34 allocate_self();
35
36 // Store the char
37 m_string[0] = c;
38 m_string[m_length] = '\0';
39
40
41}

◆ String() [3/8]

String::String ( char const *  string)

Constructs a String from a pointer to an array of chars.

Parameters
stringAn array of chars, must be null terminated and of length less than 10,000

Definition at line 47 of file string.cpp.

47 {
48
49 // Get the length of the string, prevent longer than 10000 because this should mean something's gone wrong
50 m_length = 0;
51 while (string[m_length] != '\0' && m_length <= 10000)
52 m_length++;
53 allocate_self();
54
55 // Copy the string
56 for (size_t i = 0; i < m_length; i++)
57 m_string[i] = string[i];
58
59 // If the length is more than 10,000 Replace the end with a warning incase future use actually requires that
60 const char* warning = "MAXOS: String length exceeded 10000 - might be a bug";
61 if (m_length > 10000)
62 for (int i = 0; i < 52; i++)
63 m_string[m_length - 52 + i] = warning[i];
64
65 m_string[m_length] = '\0';
66}

◆ String() [4/8]

String::String ( uint8_t const *  string,
int  length 
)

Constructs a string from a an array of bytes (doesnt have to be null terminated)

Parameters
stringThe string bytes
lengthHow large the string byte buffer is

Definition at line 74 of file string.cpp.

74 {
75 // Allocate memory for the string (and null terminator)
76 m_length = length;
77 allocate_self();
78
79 // Copy the string
80 for (int i = 0; i < length; i++)
81 m_string[i] = string[i];
82
83 // Write the null terminator
84 m_string[length] = '\0';
85}
size_t length(bool count_ansi=true) const
Returns the length of the string.
Definition string.cpp:344

References length().

◆ String() [5/8]

String::String ( String const &  other)

Copy constructor for the string.

Parameters
otherString to copy from

Definition at line 146 of file string.cpp.

146 {
147 copy(other);
148}
void copy(String const &other)
Copies the other string.
Definition string.cpp:166

References copy().

◆ String() [6/8]

String::String ( int  value)

Constructs a string from an integer (must be base 10)

Parameters
valueThe integer value

Definition at line 92 of file string.cpp.

92 {
93
94 // Convert to a string
95 const char* str = itoa(10, value);
96 m_length = strlen(str);
97
98 // Create space to store
99 allocate_self();
100
101 // Store the string
102 for (size_t i = 0; i < m_length; i++)
103 m_string[i] = str[i];
104 m_string[m_length] = '\0';
105
106}
char * itoa(int base, int64_t number)
Converts integer to string.
Definition string.cpp:703
int strlen(const char *str)
Gets the length of a string.
Definition string.cpp:689

References itoa(), and strlen().

◆ String() [7/8]

String::String ( uint64_t  value)

Constructs a string from a hex value (Excludes 0x)

Parameters
valueThe hex value

Definition at line 113 of file string.cpp.

113 {
114
115 // Convert to a string
116 const char* str = htoa(value);
117 m_length = strlen(str);
118
119 // Create space to store
120 allocate_self();
121
122 // Store the string
123 for (size_t i = 0; i < m_length; i++)
124 m_string[i] = str[i];
125 m_string[m_length] = '\0';
126}
char * htoa(uint64_t number)
Converts hex to string.
Definition string.cpp:738

References htoa(), and strlen().

◆ String() [8/8]

String::String ( bool  value)

Constructs a String from a boolean as "true" or "false".

Parameters
valueThe bool value

Definition at line 132 of file string.cpp.

132 {
133
134 if (value)
135 *this = string("true");
136 else
137 *this = string("false");
138
139}
class MaxOS::String string
Typedef for String.

◆ ~String()

String::~String ( )

Destructor for the string, cleans up memory if needed.

Definition at line 153 of file string.cpp.

153 {
154
155 // Free the memory
156 if (!m_using_small)
157 delete[] m_string;
158
159}

Member Function Documentation

◆ c_str() [1/2]

char * String::c_str ( )

The char pointer representation of the current string.

Returns
The char* string

Definition at line 234 of file string.cpp.

234 {
235
236 return m_string;
237}

Referenced by strcmp(), strcmp(), strcmp(), strncmp(), strncmp(), and strncmp().

◆ c_str() [2/2]

const char * String::c_str ( ) const

Returns the string as a c string.

Returns
The string as an array of characters

Definition at line 244 of file string.cpp.

244 {
245
246 return m_string;
247}

◆ center()

String String::center ( size_t  width,
char  fill = ' ' 
) const

Centers the string in a specified width.

Parameters
widthThe width of the string
fillThe character to fill the string with
Returns
The centered string

Definition at line 563 of file string.cpp.

563 {
564
565 // The number of characters to add
566 size_t add = (width - m_length) / 2;
567
568 // The centered string
569 String centered;
570 centered.m_length = width;
571 centered.allocate_self();
572
573 // Fill the right side (before)
574 for (size_t i = 0; i < add; i++)
575 centered.m_string[i] = fill;
576
577 // Copy the string (middle)
578 for (size_t i = 0; i < m_length; i++)
579 centered.m_string[add + i] = m_string[i];
580
581 // Fill the left side (after)
582 for (size_t i = add + m_length; i < width; i++)
583 centered.m_string[i] = fill;
584
585 // Write the null terminator
586 centered.m_string[width] = '\0';
587
588 return centered;
589}
Dynamically sized string with various operations.
Definition string.h:26

◆ copy()

void String::copy ( String const &  other)

Copies the other string.

Parameters
otherThe other string

Definition at line 166 of file string.cpp.

166 {
167
168 // Allocate memory for the string (and null terminator)
169 m_length = other.length();
170 allocate_self();
171
172 // Copy the string
173 for (size_t i = 0; i < m_length; i++)
174 m_string[i] = other[i];
175
176 // Write the null terminator
177 m_string[m_length] = '\0';
178
179}

References length().

Referenced by operator+=(), operator=(), String(), and strip().

◆ equals()

bool String::equals ( String const &  other) const

Checks if one string is equal to another.

Parameters
otherThe other string
Returns
True if the strings are equal, false otherwise

Definition at line 375 of file string.cpp.

375 {
376
377 // Check if the lengths are equal
378 if (m_length != other.length())
379 return false;
380
381 // Check if the characters are equal
382 for (size_t i = 0; i < m_length; i++)
383 if (m_string[i] != other[i])
384 return false;
385
386 // The strings are equal
387 return true;
388
389}

References length().

Referenced by operator!=(), and operator==().

◆ formatted() [1/2]

String String::formatted ( char const *  format,
va_list  parameters 
)
static

Creates a formated string. s = string, x = hex, d = decimal.

Parameters
formatThe string containing the format
parametersThe arguments to format into the string
Returns
The string with the arguments formated inside

Definition at line 642 of file string.cpp.

642 {
643
644 String out;
645
646 // Loop through the format string
647 for (; *format != '\0'; format++) {
648
649 // If it is not a %, print the character
650 if (*format != '%') {
651 out += (string) (char) (*format);
652 continue;
653 }
654
655 // Move to the next character
656 format++;
657 switch (*format) {
658 case 'd': {
659 // Print a decimal
660 int number = va_arg (parameters, int);
661 out += (string) (number);
662 break;
663 }
664 case 'x': {
665 // Print a hex
666 uint64_t number = va_arg (parameters, uint64_t);
667 out += (string) (number);
668 break;
669 }
670 case 's': {
671 // Print a string
672 char* str = va_arg (parameters, char*);
673 out += (string) (str);
674 break;
675 }
676 }
677 }
678
679 return out;
680
681}

◆ formatted() [2/2]

String String::formatted ( char const *  format,
  ... 
)
static

Creates a formated string. s = string, x = hex, d = decimal.

Parameters
formatThe string containing the format
...The arguments to format into the string
Returns
The string with the arguments formated inside

Definition at line 624 of file string.cpp.

624 {
625
626 // Create a pointer to the data
627 va_list parameters;
628 va_start(parameters, format);
629
630 return formatted(format, parameters);
631
632}
static String formatted(char const *format,...)
Creates a formated string. s = string, x = hex, d = decimal.
Definition string.cpp:624

References formatted().

Referenced by MaxOS::Logger::ASSERT(), formatted(), and MaxOS::Logger::printf().

◆ length()

size_t String::length ( bool  count_ansi = true) const

Returns the length of the string.

Parameters
count_ansiWhether to count the ansi characters (default true)
Returns
The length of the string

Definition at line 344 of file string.cpp.

344 {
345
346 // If ansi characters are not to be counted
347 if (count_ansi)
348 return m_length;
349
350 // Calculate the length of the string without ansi characters
351 int total_length = 0;
352 int clean_length = 0;
353 while (m_string[total_length] != '\0') {
354
355 // If the character is an ansi character, skip it
356 if (m_string[total_length] == '\033')
357 while (m_string[total_length] != 'm')
358 total_length++;
359
360 // Increment the length
361 clean_length++;
362 total_length++;
363 }
364
365 // Return the length
366 return clean_length;
367}

Referenced by copy(), equals(), operator+(), split(), starts_with(), String(), and substring().

◆ operator!=()

bool String::operator!= ( String const &  other) const

Checks if one string is not equal to another.

Parameters
otherThe other string
Returns
True if the strings are not equal, false otherwise

Definition at line 409 of file string.cpp.

409 {
410
411 // Self assignment check
412 if (*this == other)
413 return false;
414
415 return !equals(other);
416}
bool equals(String const &other) const
Checks if one string is equal to another.
Definition string.cpp:375

References equals().

◆ operator*()

String String::operator* ( int  times) const

Returns the string repeated a number of times.

Parameters
timesThe number of times to repeat the string
Returns
The string repeated a number of times

Definition at line 536 of file string.cpp.

536 {
537
538 // The repeated string
539 String repeated;
540 repeated.m_length = m_length * times;
541 repeated.allocate_self();
542
543 // Copy the string
544 for (int i = 0; i < times; i++)
545 for (size_t j = 0; j < m_length; j++)
546 repeated.m_string[i * m_length + j] = m_string[j];
547
548 // Write the null terminator
549 repeated.m_string[repeated.m_length] = '\0';
550
551 // Return the repeated string
552 return repeated;
553
554}

◆ operator+()

String String::operator+ ( String const &  other) const

Adds the other string to the string.

Parameters
otherThe other string
Returns
The concatenated string

Definition at line 472 of file string.cpp.

472 {
473
474 // The concatenated string
475 String concatenated;
476 concatenated.m_length = m_length + other.length();
477 concatenated.allocate_self();
478
479 // Copy the first string
480 for (size_t i = 0; i < m_length; i++)
481 concatenated.m_string[i] = m_string[i];
482
483 // Copy the second string
484 for (size_t i = 0; i < other.length(); i++)
485 concatenated.m_string[m_length + i] = other[i];
486
487 // Write the null terminator
488 concatenated.m_string[concatenated.m_length] = '\0';
489
490 // Return the concatenated string
491 return concatenated;
492}

References length().

◆ operator+=()

String & String::operator+= ( String const &  other)

Adds the other string to the string.

Parameters
otherThe other string
Returns
The concatenated string

Definition at line 500 of file string.cpp.

500 {
501
502 // Add the other string to this string
503 String concatenated = *this + other;
504 copy(concatenated);
505 return *this;
506}

References copy().

◆ operator<()

bool String::operator< ( String const &  other) const

Checks if the sum of the ascii values of the characters in the string is less than the sum of the ascii values of the characters in the other string.

Parameters
otherThe other string
Returns
True if the string is less than the other, false otherwise

Definition at line 424 of file string.cpp.

424 {
425
426 return lex_value(*this) < lex_value(other);
427
428}

◆ operator<=()

bool String::operator<= ( String const &  other) const

Checks if the sum of the ascii values of the characters in the string is less than or equal to the sum of the ascii values of the characters in the other string.

Parameters
otherThe other string
Returns
True if the string is less than or equal to the other, false otherwise

Definition at line 448 of file string.cpp.

448 {
449
450 return lex_value(*this) <= lex_value(other);
451
452}

◆ operator=()

String & String::operator= ( String const &  other)

Sets the string to the other string.

Parameters
otherThe string for this one to be updated to
Returns
String The string

Definition at line 218 of file string.cpp.

218 {
219
220 // Self assignment check
221 if (this == &other)
222 return *this;
223
224 // Copy the other string
225 copy(other);
226 return *this;
227}

References copy().

◆ operator==()

bool String::operator== ( String const &  other) const

Checks if one string is equal to another.

Parameters
otherThe other string
Returns
True if the strings are equal, false otherwise

Definition at line 397 of file string.cpp.

397 {
398
399 // Check if the strings are equal
400 return equals(other);
401}

References equals().

◆ operator>()

bool String::operator> ( String const &  other) const

Checks if the sum of the ascii values of the characters in the string is greater than the sum of the ascii values of the characters in the other string.

Parameters
otherThe other string
Returns
True if the string is greater than the other, false otherwise

Definition at line 436 of file string.cpp.

436 {
437
438 return lex_value(*this) > lex_value(other);
439
440}

◆ operator>=()

bool String::operator>= ( String const &  other) const

Checks if the sum of the ascii values of the characters in the string is greater than or equal to the sum of the ascii values of the characters in the other string.

Parameters
otherThe other string
Returns
True if the string is greater than or equal to the other, false otherwise

Definition at line 460 of file string.cpp.

460 {
461
462 return lex_value(*this) >= lex_value(other);
463
464}

◆ operator[]() [1/2]

char & String::operator[] ( size_t  index)

Returns the character at the specified index.

Parameters
indexThe index of the character
Returns
The character at the specified index

Definition at line 515 of file string.cpp.

515 {
516 return m_string[index];
517}

◆ operator[]() [2/2]

char & String::operator[] ( size_t  index) const

Returns the character at the specified index.

Parameters
indexThe index of the character
Returns
The character at the specified index

Definition at line 526 of file string.cpp.

526 {
527 return m_string[index];
528}

◆ split()

common::Vector< String > String::split ( String const &  delimiter) const

Splits the string by the delimiter.

Parameters
delimiterWhat to split the string by
Returns
A vector of strings that were split by the delimiter

Definition at line 308 of file string.cpp.

308 {
309 common::Vector<String> strings;
310
311 // Go through the string and split it by the delimiter
312 size_t start = 0;
313 for (size_t i = 0; i <= m_length - delimiter.length(); i++) {
314
315 // Check if matches at this position
316 bool matches = true;
317 for (size_t j = 0; j < delimiter.length(); j++)
318 if (m_string[i + j] != delimiter[j]) {
319 matches = false;
320 break;
321 }
322
323 if (!matches)
324 continue;
325
326 // Add the splice of the string
327 strings.push_back(substring(start, i - start));
328 start = i + delimiter.length();
329 i += delimiter.length() - 1;
330 }
331
332 // Add the last string to the vector
333 strings.push_back(substring(start, m_length - start));
334
335 return strings;
336}
String substring(size_t start, size_t length) const
Get a section of the string.
Definition string.cpp:277

References length(), and substring().

◆ starts_with()

bool String::starts_with ( String const &  other)

Checks if the string starts with the other string (must contain the same characters in the same order)

Parameters
otherThe other string
Returns
True if the string starts with the other string, false otherwise

Definition at line 255 of file string.cpp.

255 {
256
257 // Must at least be able to fit the other string
258 if (m_length < other.length())
259 return false;
260
261 // Check if the string starts with the other string
262 for (size_t i = 0; i < other.length(); i++)
263 if (m_string[i] != other[i])
264 return false;
265
266 // No string left over to check so it must contain other
267 return true;
268}

References length().

◆ strip()

String String::strip ( char  strip_char = ' ') const

Strips the string of whitespace.

Parameters
strip_charThe character to strip (default = ' ')
Returns
The stripped string (new string)

Definition at line 597 of file string.cpp.

597 {
598
599 // The stripped string
600 String stripped;
601 stripped.copy(*this);
602
603 // Search from the back for the earliest non-whitespace character
604 size_t end = m_length - 1;
605 while (end >= 0 && (m_string[end] == strip_char || m_string[end] == '\n' || m_string[end] == '\t'))
606 end--;
607
608 // Make sure there is something to strip
609 if (end < 0)
610 return stripped;
611
612 // Split the string to remove the end
613 return stripped.substring(0, end + 1);
614}

References copy(), and substring().

◆ substring()

String String::substring ( size_t  start,
size_t  length 
) const

Get a section of the string.

Parameters
startThe start of the substring
lengthThe length of the substring
Returns
The substring or empty string if out of bounds

Definition at line 277 of file string.cpp.

277 {
278
279 // Ensure the start is within bounds
280 if (start >= m_length)
281 return { };
282
283 // Ensure the length is within bounds
284 if (start + length > m_length)
285 return { };
286
287 // Allocate memory for the substring (and null terminator)
289 substring.m_length = length;
290 substring.allocate_self();
291
292 // Copy the substring
293 for (size_t i = 0; i < length; i++)
294 substring.m_string[i] = m_string[start + i];
295
296 // Write the null terminator
297 substring.m_string[length] = '\0';
298
299 return substring;
300}

References length(), and substring().

Referenced by split(), strip(), and substring().


The documentation for this class was generated from the following files: