Max OS 0.3
Loading...
Searching...
No Matches
string.h
Go to the documentation of this file.
1
9#ifndef MAXOS_STRING_H
10#define MAXOS_STRING_H
11
12#include <cstdint>
13
14#include <common/vector.h>
15#include <stdarg.h>
16
17namespace MaxOS {
18
20 constexpr int MAX_STRING_SMALL_STORAGE = 0x99;
21
26 typedef class String {
27 private:
28 char* m_string = nullptr;
29 size_t m_length = 0;
30
31 char m_small_string[MAX_STRING_SMALL_STORAGE] = { 0 };
32 bool m_using_small = true;
33
34 [[nodiscard]] static int lex_value(String const& other);
35 void allocate_self();
36
37 public:
38
39 String();
40 String(char c);
41 String(char const* string);
42 String(uint8_t const* string, int length);
43 String(String const& other);
44 String(int value);
45 String(uint64_t value);
46 String(bool value);
47 ~String();
48
49 void copy(String const& other);
50
51 static String formatted(char const* format, ...);
52 static String formatted(char const* format, va_list parameters);
53
54 [[nodiscard]] size_t length(bool count_ansi = true) const;
55 [[nodiscard]] char* c_str();
56 [[nodiscard]] const char* c_str() const;
57
58 bool starts_with(String const& other);
59 [[nodiscard]] String substring(size_t start, size_t length) const;
60
61 [[nodiscard]] common::Vector<String> split(String const& delimiter) const;
62 [[nodiscard]] String strip(char strip_char = ' ') const;
63
64
65 [[nodiscard]] String center(size_t width, char fill = ' ') const;
66
67 // Operator functions
68 [[nodiscard]] bool equals(String const& other) const;
69
70 // Operators
71 String& operator =(String const& other);
72 String operator +(String const& other) const;
73 String& operator +=(String const& other);
74
75 String operator *(int times) const;
76
77 bool operator ==(String const& other) const;
78 bool operator !=(String const& other) const;
79
80 bool operator <(String const& other) const;
81 bool operator >(String const& other) const;
82
83 bool operator <=(String const& other) const;
84 bool operator >=(String const& other) const;
85
86 char& operator [](size_t index);
87 char& operator [](size_t index) const;
88
89 } string;
90
98 public:
100 operator String() const { return out; }
101
102 StringBuilder& operator <<(char const* str);
103 StringBuilder& operator <<(String const& other);
104 StringBuilder& operator <<(int value);
105 StringBuilder& operator <<(uint64_t value);
106 StringBuilder& operator <<(bool value);
107
108 };
109}
110
111// Convert functions
112int strlen(const char* str);
113char* itoa(int base, int64_t number);
114char* htoa(uint64_t number);
115
116// Compare functions
117bool strcmp(char const* str1, char const* str2);
118bool strcmp(char const* str1, MaxOS::String const& str2);
119bool strcmp(MaxOS::String const& str1, char const* str2);
120bool strcmp(MaxOS::String const& str1, MaxOS::String const& str2);
121
122// Compare limited functions
123bool strncmp(char const* str1, char const* str2, int length);
124bool strncmp(char const* str1, MaxOS::String const& str2, int length);
125bool strncmp(MaxOS::String const& str1, char const* str2, int length);
126bool strncmp(MaxOS::String const& str1, MaxOS::String const& str2, int length);
127
128#endif //MAXOS_STRING_H
Creates a string using a using a combination of parts with the '<<' operator. Simmilar to the logger.
Definition string.h:97
StringBuilder & operator<<(char const *str)
Append C-string to the StringBuilder.
Definition string.cpp:888
String out
The output string.
Definition string.h:99
Dynamically sized string with various operations.
Definition string.h:26
String operator+(String const &other) const
Adds the other string to the string.
Definition string.cpp:472
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...
Definition string.cpp:460
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 asc...
Definition string.cpp:424
bool operator==(String const &other) const
Checks if one string is equal to another.
Definition string.cpp:397
String strip(char strip_char=' ') const
Strips the string of whitespace.
Definition string.cpp:597
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 su...
Definition string.cpp:448
static String formatted(char const *format,...)
Creates a formated string. s = string, x = hex, d = decimal.
Definition string.cpp:624
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 ...
Definition string.cpp:436
String center(size_t width, char fill=' ') const
Centers the string in a specified width.
Definition string.cpp:563
void copy(String const &other)
Copies the other string.
Definition string.cpp:166
common::Vector< String > split(String const &delimiter) const
Splits the string by the delimiter.
Definition string.cpp:308
String substring(size_t start, size_t length) const
Get a section of the string.
Definition string.cpp:277
size_t length(bool count_ansi=true) const
Returns the length of the string.
Definition string.cpp:344
String()
Construct a String, 0 length and only contains the null terminator.
Definition string.cpp:16
bool starts_with(String const &other)
Checks if the string starts with the other string (must contain the same characters in the same order...
Definition string.cpp:255
String & operator=(String const &other)
Sets the string to the other string.
Definition string.cpp:218
String operator*(int times) const
Returns the string repeated a number of times.
Definition string.cpp:536
bool equals(String const &other) const
Checks if one string is equal to another.
Definition string.cpp:375
bool operator!=(String const &other) const
Checks if one string is not equal to another.
Definition string.cpp:409
~String()
Destructor for the string, cleans up memory if needed.
Definition string.cpp:153
String & operator+=(String const &other)
Adds the other string to the string.
Definition string.cpp:500
char * c_str()
The char pointer representation of the current string.
Definition string.cpp:234
char & operator[](size_t index)
Returns the character at the specified index.
Definition string.cpp:515
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
char * itoa(int base, int64_t number)
Converts integer to string.
Definition string.cpp:703
constexpr int MAX_STRING_SMALL_STORAGE
How many characters can be stored in the small string optimization array.
Definition string.h:20
bool strcmp(char const *str1, char const *str2)
Checks if one string pointer is equal to another string pointer.
Definition string.cpp:765
bool strncmp(char const *str1, char const *str2, int length)
Checks if one string pointer is equal to another string pointer up to a specified length (each must b...
Definition string.cpp:826
char * htoa(uint64_t number)
Converts hex to string.
Definition string.cpp:738
int strlen(const char *str)
Gets the length of a string.
Definition string.cpp:689
Defines a Vector class for dynamically storing an array of elements.