From cf3b2176f097d84a31051f33df405ee677bf84d8 Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 17 Dec 2022 11:36:16 +0100 Subject: [PATCH] Implement OwnedStringView::from_string_literal --- luna/include/luna/OwnedStringView.h | 2 ++ luna/src/OwnedStringView.cpp | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/luna/include/luna/OwnedStringView.h b/luna/include/luna/OwnedStringView.h index 8a47b597..ba457b58 100644 --- a/luna/include/luna/OwnedStringView.h +++ b/luna/include/luna/OwnedStringView.h @@ -12,6 +12,8 @@ class OwnedStringView Result clone() const; + static Result from_string_literal(const char* str); + const char* chars() const { return m_string; diff --git a/luna/src/OwnedStringView.cpp b/luna/src/OwnedStringView.cpp index 80e42fa7..0bf41af3 100644 --- a/luna/src/OwnedStringView.cpp +++ b/luna/src/OwnedStringView.cpp @@ -39,4 +39,11 @@ const char& OwnedStringView::operator[](usize index) const { expect(index < m_length, "index out of range"); return m_string[index]; +} + +Result OwnedStringView::from_string_literal(const char* str) +{ + char* dup = strdup(str); + if (!dup) return err(ENOMEM); + return OwnedStringView{dup}; } \ No newline at end of file