diff --git a/libluna/include/luna/String.h b/libluna/include/luna/String.h index cb2cd328..bb0a2ba4 100644 --- a/libluna/include/luna/String.h +++ b/libluna/include/luna/String.h @@ -9,35 +9,53 @@ class String typedef const char* ConstIterator; public: + /* Constructs a String, which will take ownership of the given c-string. To create a String that makes its own + * internal copy of the passed string, use String::from_cstring(). */ String(char* c_str); + + /* Constructs a String with precalculated length, which will take ownership of the given c-string. To create a + * String that makes its own internal copy of the passed string, use String::from_cstring(). */ String(char* c_str, usize length); + /* Constructs an empty String. */ String(); String(String&&); + + /* Implicit copying is disabled for Strings, as copy constructors cannot use value-based error handling. To copy a + * string, please use the clone() method. */ String(const String&) = delete; String& operator=(String&&); + + /* Implicit copying is disabled for Strings, as copy constructors cannot use value-based error handling. To copy a + * string, please use the clone() method. */ String& operator=(const String&) = delete; ~String(); + /* Creates a copy of this String and returns it. */ Result clone() const; + /* Creates a copy of a specific segment of this String and returns it. */ Result substring(usize begin, usize size) const; + /* Splits a String with a delimiter. */ Result> split(StringView delim) const { return view().split(delim); } + /* Splits a String into two parts with a delimiter. */ Result> split_once(char delim) const { return view().split_once(delim); } + /* Creates a single String consisting of a list of strings separated by a delimiter. */ static Result join(const Vector& vec, StringView delim); + /* Removes all trailing characters contained in delim. */ void trim(StringView delim); static Result format(const String& fmt, ...); @@ -45,10 +63,7 @@ class String static Result vformat(StringView fmt, va_list ap); static Result from_cstring(const char* str); - static Result from_string_view(StringView str) - { - return from_cstring(str.chars()); - } + static Result from_string_view(StringView str); const char* chars() const { diff --git a/libluna/src/String.cpp b/libluna/src/String.cpp index 921c428c..602e0ea9 100644 --- a/libluna/src/String.cpp +++ b/libluna/src/String.cpp @@ -145,19 +145,23 @@ Result String::vformat(StringView fmt, va_list ap) Result String::from_cstring(const char* str) { - usize len = strlen(str); - if (len < sizeof(m_inline_storage)) + return from_string_view(StringView { str }); +} + +Result String::from_string_view(StringView str) +{ + if (str.length() < sizeof(m_inline_storage)) { String result; result.m_inline = true; - result.m_length = len; - strncpy(result.m_inline_storage, str, sizeof(m_inline_storage)); + result.m_length = str.length(); + strncpy(result.m_inline_storage, str.chars(), sizeof(m_inline_storage)); return result; } - char* const dup = strdup(str); + char* const dup = strndup(str.chars(), str.length()); if (!dup) return err(ENOMEM); - return String { dup }; + return String { dup, str.length() }; } Result String::join(const Vector& vec, StringView delim)