String: is_empty + proper initialization

This commit is contained in:
apio 2023-03-29 17:32:53 +02:00
parent b6c35124d6
commit ee60ab78b3
Signed by: apio
GPG Key ID: B8A7D06E42258954
2 changed files with 12 additions and 3 deletions

View File

@ -30,6 +30,11 @@ class String
return m_length; return m_length;
} }
bool is_empty() const
{
return m_length == 0;
}
const char& operator[](usize) const; const char& operator[](usize) const;
private: private:

View File

@ -5,6 +5,7 @@
String::String() String::String()
{ {
m_inline = true; m_inline = true;
m_length = 0;
memset(m_inline_storage, 0, sizeof(m_inline_storage)); memset(m_inline_storage, 0, sizeof(m_inline_storage));
} }
@ -22,14 +23,15 @@ String::String(String&& other)
String::String(char* c_str) String::String(char* c_str)
{ {
check(c_str);
m_string = c_str; m_string = c_str;
m_inline = false; m_inline = false;
m_length = strlen(m_string);
if (m_string) { m_length = strlen(m_string); }
} }
String::String(char* c_str, usize length) String::String(char* c_str, usize length)
{ {
check(c_str);
m_string = c_str; m_string = c_str;
m_inline = false; m_inline = false;
m_length = length; m_length = length;
@ -61,10 +63,12 @@ const char& String::operator[](usize index) const
Result<String> String::from_string_literal(const char* str) Result<String> String::from_string_literal(const char* str)
{ {
if (strlen(str) < sizeof(m_inline_storage)) usize len = strlen(str);
if (len < sizeof(m_inline_storage))
{ {
String result; String result;
result.m_inline = true; result.m_inline = true;
result.m_length = len;
strncpy(result.m_inline_storage, str, sizeof(m_inline_storage)); strncpy(result.m_inline_storage, str, sizeof(m_inline_storage));
return result; return result;
} }