includes/std: Added null-terminated strings to the strings library

This commit is contained in:
WerWolv
2024-06-20 21:21:30 +02:00
parent 28b281b403
commit bbba68cef7

View File

@@ -3,127 +3,149 @@
import std.io; import std.io;
/*! /*!
Libray to interact with strings. Libray to interact with strings.
*/ */
namespace auto std::string { namespace auto std::string {
/** /**
Base type for sized strings. Represents a string with its size preceeding it. Base type for sized strings. Represents a string with its size preceeding it.
@tparam SizeType The type of the size field. @tparam SizeType The type of the size field.
@tparam DataType The type of the characters. @tparam DataType The type of the characters.
*/ */
struct SizedStringBase<SizeType, DataType> { struct SizedStringBase<SizeType, DataType> {
SizeType size; SizeType size;
DataType data[size]; DataType data[size];
} [[sealed, format("std::string::impl::format_sized_string"), transform("std::string::impl::format_sized_string")]]; } [[sealed, format("std::string::impl::format_sized_string"), transform("std::string::impl::format_sized_string")]];
/** /**
A ASCII string with a prefixed size. A ASCII string with a prefixed size.
@tparam SizeType The type of the size field. @tparam SizeType The type of the size field.
*/ */
using SizedString<SizeType> = SizedStringBase<SizeType, char>; using SizedString<SizeType> = SizedStringBase<SizeType, char>;
/** /**
A UTF-16 string with a prefixed size. A UTF-16 string with a prefixed size.
@tparam SizeType The type of the size field. @tparam SizeType The type of the size field.
*/ */
using SizedString16<SizeType> = SizedStringBase<SizeType, char16>; using SizedString16<SizeType> = SizedStringBase<SizeType, char16>;
namespace impl { /**
Base type for null-terminated strings. Represents a string with its size determined by the first 0x00 byte found.
@tparam DataType The type of the characters.
*/
struct NullStringBase<DataType> {
DataType string[while($[$] != 0x00)];
} [[format("std::string::impl::format_null_string")]];
fn format_sized_string(ref auto string) { /**
return string.data; A null-terminated ASCII string.
}; */
using NullString = NullStringBase<char>;
} /**
A null-terminated UTF-16 string.
*/
using NullString16 = NullStringBase<char16>;
/** namespace impl {
Gets the length of a string.
@param string The string to get the length of.
@return The length of the string.
*/
fn length(str string) {
return builtin::std::string::length(string);
};
/** fn format_sized_string(ref auto string) {
Gets the character at a given index. return string.data;
@param string The string to get the character from. };
@param index The index of the character to get.
@return The character at the given index.
*/
fn at(str string, u32 index) {
return builtin::std::string::at(string, index);
};
/** fn format_null_string(ref auto null_string) {
Gets a substring of a string. return null_string.string;
@param string The string to get the substring from. };
@param pos The position of the first character of the substring.
@param count The number of characters to get. }
@return The substring.
*/ /**
fn substr(str string, u32 pos, u32 count) { Gets the length of a string.
return builtin::std::string::substr(string, pos, count); @param string The string to get the length of.
}; @return The length of the string.
*/
fn length(str string) {
return builtin::std::string::length(string);
};
/**
Gets the character at a given index.
@param string The string to get the character from.
@param index The index of the character to get.
@return The character at the given index.
*/
fn at(str string, u32 index) {
return builtin::std::string::at(string, index);
};
/**
Gets a substring of a string.
@param string The string to get the substring from.
@param pos The position of the first character of the substring.
@param count The number of characters to get.
@return The substring.
*/
fn substr(str string, u32 pos, u32 count) {
return builtin::std::string::substr(string, pos, count);
};
/** /**
Converts a string to an integer. Converts a string to an integer.
@param string The string to convert. @param string The string to convert.
@param base The base of the number. @param base The base of the number.
@return The integer. @return The integer.
*/ */
fn parse_int(str string, u8 base) { fn parse_int(str string, u8 base) {
return builtin::std::string::parse_int(string, base); return builtin::std::string::parse_int(string, base);
}; };
/** /**
Converts a string to a float. Converts a string to a float.
@param string The string to convert. @param string The string to convert.
@return The float. @return The float.
*/ */
fn parse_float(str string) { fn parse_float(str string) {
return builtin::std::string::parse_float(string); return builtin::std::string::parse_float(string);
}; };
/** /**
Converts any type to a string. Converts any type to a string.
@param x The value to convert. @param x The value to convert.
@return The string. @return The string.
*/ */
fn to_string(auto x) { fn to_string(auto x) {
return std::format("{}", x); return std::format("{}", x);
}; };
/** /**
Checks if a string starts with a given substring. Checks if a string starts with a given substring.
@param string The string to check. @param string The string to check.
@param part The substring to check for. @param part The substring to check for.
@return True if the string starts with the substring, false otherwise. @return True if the string starts with the substring, false otherwise.
*/ */
fn starts_with(str string, str part) { fn starts_with(str string, str part) {
return std::string::substr(string, 0, std::string::length(part)) == part; return std::string::substr(string, 0, std::string::length(part)) == part;
}; };
/** /**
Checks if a string ends with a given substring. Checks if a string ends with a given substring.
@param string The string to check. @param string The string to check.
@param part The substring to check for. @param part The substring to check for.
@return True if the string ends with the substring, false otherwise. @return True if the string ends with the substring, false otherwise.
*/ */
fn ends_with(str string, str part) { fn ends_with(str string, str part) {
return std::string::substr(string, std::string::length(string) - std::string::length(part), std::string::length(part)) == part; return std::string::substr(string, std::string::length(string) - std::string::length(part), std::string::length(part)) == part;
}; };
/** /**
Checks if a string contains a given substring. Checks if a string contains a given substring.
@param string The string to check. @param string The string to check.
@param part The substring to check for. @param part The substring to check for.
@return True if the string contains the substring, false otherwise. @return True if the string contains the substring, false otherwise.
*/ */
fn contains(str string, str part) { fn contains(str string, str part) {
s32 string_len = std::string::length(string); s32 string_len = std::string::length(string);
s32 part_len = std::string::length(part); s32 part_len = std::string::length(part);
@@ -136,12 +158,12 @@ namespace auto std::string {
return false; return false;
}; };
/** /**
Reverses a string. Reverses a string.
@param string The string to reverse. @param string The string to reverse.
@return The reversed string. @return The reversed string.
*/ */
fn reverse(str string) { fn reverse(str string) {
str result; str result;
s32 i; s32 i;
@@ -154,82 +176,82 @@ namespace auto std::string {
return result; return result;
}; };
/** /**
Converts a string to upper case. Converts a string to upper case.
@param string The string to convert. @param string The string to convert.
@return The converted string. @return The converted string.
*/ */
fn to_upper(str string) { fn to_upper(str string) {
str result; str result;
u32 i; u32 i;
char c; char c;
while (i < std::string::length(string)) { while (i < std::string::length(string)) {
c = std::string::at(string, i); c = std::string::at(string, i);
if (c >= 'a' && c <= 'z') if (c >= 'a' && c <= 'z')
result = result + char(c - 0x20); result = result + char(c - 0x20);
else else
result = result + c; result = result + c;
i = i + 1; i = i + 1;
} }
return result; return result;
}; };
/** /**
Converts a string to lower case. Converts a string to lower case.
@param string The string to convert. @param string The string to convert.
@return The converted string. @return The converted string.
*/ */
fn to_lower(str string) { fn to_lower(str string) {
str result; str result;
u32 i; u32 i;
char c; char c;
while (i < std::string::length(string)) { while (i < std::string::length(string)) {
c = std::string::at(string, i); c = std::string::at(string, i);
if (c >= 'A' && c <= 'Z') if (c >= 'A' && c <= 'Z')
result = result + char(c + 0x20); result = result + char(c + 0x20);
else else
result = result + c; result = result + c;
i = i + 1; i = i + 1;
} }
return result; return result;
}; };
/** /**
Replaces all occurrences of a substring with another substring. Replaces all occurrences of a substring with another substring.
@param string The string to replace in. @param string The string to replace in.
@param pattern The substring to replace. @param pattern The substring to replace.
@param replace The substring to replace with. @param replace The substring to replace with.
@return The string with the replacements. @return The string with the replacements.
*/ */
fn replace(str string, str pattern, str replace) { fn replace(str string, str pattern, str replace) {
s32 string_len = std::string::length(string); s32 string_len = std::string::length(string);
s32 pattern_len = std::string::length(pattern); s32 pattern_len = std::string::length(pattern);
if (pattern_len > string_len || pattern_len * string_len == 0 ) if (pattern_len > string_len || pattern_len * string_len == 0 )
return string; return string;
str result; str result;
s32 string_index; s32 string_index;
s32 remaining_len = string_len; s32 remaining_len = string_len;
while (pattern_len <= remaining_len) { while (pattern_len <= remaining_len) {
if (std::string::substr(string, string_index, pattern_len) == pattern) { if (std::string::substr(string, string_index, pattern_len) == pattern) {
result += replace; result += replace;
string_index += pattern_len; string_index += pattern_len;
} else { } else {
result += std::string::at(string, string_index); result += std::string::at(string, string_index);
string_index += 1; string_index += 1;
} }
remaining_len = string_len - string_index; remaining_len = string_len - string_index;
} }
result += std::string::substr(string, string_index, remaining_len ); result += std::string::substr(string, string_index, remaining_len );
return result; return result;
}; };
} }