From ee60ab78b3d62fd4e9ceec70541c2c922d7b0a56 Mon Sep 17 00:00:00 2001 From: apio Date: Wed, 29 Mar 2023 17:32:53 +0200 Subject: [PATCH] String: is_empty + proper initialization --- libluna/include/luna/String.h | 5 +++++ libluna/src/String.cpp | 10 +++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/libluna/include/luna/String.h b/libluna/include/luna/String.h index 9ef00ad3..64607a47 100644 --- a/libluna/include/luna/String.h +++ b/libluna/include/luna/String.h @@ -30,6 +30,11 @@ class String return m_length; } + bool is_empty() const + { + return m_length == 0; + } + const char& operator[](usize) const; private: diff --git a/libluna/src/String.cpp b/libluna/src/String.cpp index 656f8f8d..51275219 100644 --- a/libluna/src/String.cpp +++ b/libluna/src/String.cpp @@ -5,6 +5,7 @@ String::String() { m_inline = true; + m_length = 0; memset(m_inline_storage, 0, sizeof(m_inline_storage)); } @@ -22,14 +23,15 @@ String::String(String&& other) String::String(char* c_str) { + check(c_str); m_string = c_str; m_inline = false; - - if (m_string) { m_length = strlen(m_string); } + m_length = strlen(m_string); } String::String(char* c_str, usize length) { + check(c_str); m_string = c_str; m_inline = false; m_length = length; @@ -61,10 +63,12 @@ const char& String::operator[](usize index) const Result 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; result.m_inline = true; + result.m_length = len; strncpy(result.m_inline_storage, str, sizeof(m_inline_storage)); return result; }