Max OS 0.3
Loading...
Searching...
No Matches
string.h File Reference

Defines a String class for dynamically sized strings with various operations. More...

#include <cstdint>
#include <common/vector.h>
#include <stdarg.h>

Go to the source code of this file.

Classes

class  MaxOS::String
 Dynamically sized string with various operations. More...
 
class  MaxOS::StringBuilder
 Creates a string using a using a combination of parts with the '<<' operator. Simmilar to the logger. More...
 

Typedefs

typedef class MaxOS::String MaxOS::string
 Typedef for String.
 

Functions

int strlen (const char *str)
 Gets the length of a string.
 
char * itoa (int base, int64_t number)
 Converts integer to string.
 
char * htoa (uint64_t number)
 Converts hex to string.
 
bool strcmp (char const *str1, char const *str2)
 Checks if one string pointer is equal to another string pointer.
 
bool strcmp (char const *str1, MaxOS::String const &str2)
 Checks if one string pointer is equal to a String.
 
bool strcmp (MaxOS::String const &str1, char const *str2)
 Checks if one String is equal to a string pointer.
 
bool strcmp (MaxOS::String const &str1, MaxOS::String const &str2)
 Checks if one String is equal to another String (better use is of "==")
 
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 be >= this length)
 
bool strncmp (char const *str1, MaxOS::String const &str2, int length)
 Checks if one string pointer is equal to another String up to a specified length (each must be >= this length)
 
bool strncmp (MaxOS::String const &str1, char const *str2, int length)
 Checks if one String is equal to another string pointer up to a specified length (each must be >= this length)
 
bool strncmp (MaxOS::String const &str1, MaxOS::String const &str2, int length)
 Checks if one String is equal to another String up to a specified length (each must be >= this length)
 

Variables

constexpr int MaxOS::MAX_STRING_SMALL_STORAGE = 0x99
 How many characters can be stored in the small string optimization array.
 

Detailed Description

Defines a String class for dynamically sized strings with various operations.

Date
12th April 2023
Author
Max Tyson

Definition in file string.h.

Function Documentation

◆ htoa()

char * htoa ( uint64_t  number)

Converts hex to string.

Parameters
numberThe number to convert
Returns
The converted string

Definition at line 738 of file string.cpp.

738 {
739 // If there is no buffer use a default buffer
740 static char buffer[50] = { 0 };
741 int i = 49;
742
743 // Null terminate the string
744 buffer[i] = '\0';
745 --i;
746
747 if (number == 0) {
748 buffer[i] = '0';
749 return &buffer[i];
750 }
751
752 for (; number && i; --i, number /= 16)
753 buffer[i] = "0123456789ABCDEF"[number % 16];
754
755 return &buffer[i + 1];
756}

Referenced by MaxOS::String::String(), and MaxOS::common::OutputStream::write_hex().

◆ itoa()

char * itoa ( int  base,
int64_t  number 
)

Converts integer to string.

Parameters
baseThe base of the number (10 for decimal, 16 for hex)
numberThe number to convert
Returns
The converted string

Definition at line 703 of file string.cpp.

703 {
704
705 // If there is no buffer use a default buffer
706 static char buffer[50] = { 0 };
707
708 int i = 49;
709 bool is_negative = number < 0;
710
711 // Null terminate the string
712 buffer[i] = '\0';
713 --i;
714
715 if (number == 0) {
716 buffer[i] = '0';
717 return &buffer[i];
718 }
719
720
721 for (; number && i; --i, number /= base)
722 buffer[i] = "0123456789ABCDEF"[number % base];
723
724 if (is_negative) {
725 buffer[i] = '-';
726 return &buffer[i];
727 }
728
729 return &buffer[i + 1];
730}

Referenced by MaxOS::String::String(), and MaxOS::common::OutputStream::write_int().

◆ strcmp() [1/4]

bool strcmp ( char const *  str1,
char const *  str2 
)

Checks if one string pointer is equal to another string pointer.

Parameters
str1The first string
str2The second string
Returns
True if the strings are equal, false otherwise

Definition at line 765 of file string.cpp.

765 {
766
767 // Check if the strings are equal
768 for (int i = 0; str1[i] != '\0' || str2[i] != '\0'; i++)
769 if (str1[i] != str2[i])
770 return false;
771
772 // The strings are equal
773 return true;
774
775}

Referenced by MaxOS::drivers::console::VESABootConsole::put_character(), strcmp(), strcmp(), and strcmp().

◆ strcmp() [2/4]

bool strcmp ( char const *  str1,
String const &  str2 
)

Checks if one string pointer is equal to a String.

Parameters
str1The first string
str2The second string
Returns
True if the strings are equal, false otherwise

Definition at line 784 of file string.cpp.

784 {
785
786 // Use the other strcmp function
787 return strcmp(str1, str2.c_str());
788
789}
bool strcmp(char const *str1, char const *str2)
Checks if one string pointer is equal to another string pointer.
Definition string.cpp:765

References MaxOS::String::c_str(), and strcmp().

◆ strcmp() [3/4]

bool strcmp ( String const &  str1,
char const *  str2 
)

Checks if one String is equal to a string pointer.

Parameters
str1The first string
str2The second string
Returns
True if the strings are equal, false otherwise

Definition at line 798 of file string.cpp.

798 {
799
800 // Use the other strcmp function
801 return strcmp(str1.c_str(), str2);
802}

References MaxOS::String::c_str(), and strcmp().

◆ strcmp() [4/4]

bool strcmp ( String const &  str1,
String const &  str2 
)

Checks if one String is equal to another String (better use is of "==")

Parameters
str1The first string
str2The second string
Returns
True if the strings are equal, false otherwise

Definition at line 811 of file string.cpp.

811 {
812
813 // Use the other strcmp function
814 return strcmp(str1.c_str(), str2.c_str());
815
816}

References MaxOS::String::c_str(), and strcmp().

◆ strlen()

int strlen ( const char *  str)

Gets the length of a string.

Parameters
strThe string to get the length of
Returns
The length of the string

Definition at line 689 of file string.cpp.

689 {
690 int len = 0;
691 for (; str[len] != '\0'; len++);
692 return len;
693}

Referenced by MaxOS::String::String(), and MaxOS::String::String().

◆ strncmp() [1/4]

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 be >= this length)

Parameters
str1The first string
str2The second string
lengthThe length of the string to compare
Returns
True if the strings are equal, false otherwise

Definition at line 826 of file string.cpp.

826 {
827
828 // Check if the strings are equal
829 for (int i = 0; i < length; i++)
830 if (str1[i] != str2[i])
831 return false;
832
833 // Strings are equal
834 return true;
835
836}

Referenced by MaxOS::hardwarecommunication::AdvancedConfigurationAndPowerInterface::find(), strncmp(), strncmp(), and strncmp().

◆ strncmp() [2/4]

bool strncmp ( char const *  str1,
String const &  str2,
int  length 
)

Checks if one string pointer is equal to another String up to a specified length (each must be >= this length)

Parameters
str1The first string
str2The second string
lengthThe length of the string to compare
Returns
True if the strings are equal, false otherwise

Definition at line 846 of file string.cpp.

846 {
847
848 // Use the other strncmp function
849 return strncmp(str1, str2.c_str(), length);
850
851}
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

References MaxOS::String::c_str(), and strncmp().

◆ strncmp() [3/4]

bool strncmp ( String const &  str1,
char const *  str2,
int  length 
)

Checks if one String is equal to another string pointer up to a specified length (each must be >= this length)

Parameters
str1The first string
str2The second string
lengthThe length of the string to compare
Returns
True if the strings are equal, false otherwise

Definition at line 861 of file string.cpp.

861 {
862
863 // Use the other strncmp function
864 return strncmp(str1.c_str(), str2, length);
865
866}

References MaxOS::String::c_str(), and strncmp().

◆ strncmp() [4/4]

bool strncmp ( String const &  str1,
String const &  str2,
int  length 
)

Checks if one String is equal to another String up to a specified length (each must be >= this length)

Parameters
str1The first string
str2The second string
lengthThe length of the string to compare
Returns
True if the strings are equal, false otherwise

Definition at line 876 of file string.cpp.

876 {
877
878 // Use the other strncmp function
879 return strncmp(str1.c_str(), str2.c_str(), length);
880}

References MaxOS::String::c_str(), and strncmp().

Variable Documentation

◆ MAX_STRING_SMALL_STORAGE

constexpr int MaxOS::MAX_STRING_SMALL_STORAGE = 0x99
constexpr

How many characters can be stored in the small string optimization array.

Definition at line 20 of file string.h.