Implement OwnedStringView::from_string_literal
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2022-12-17 11:36:16 +01:00
parent df9a13cbfb
commit cf3b2176f0
Signed by: apio
GPG Key ID: B8A7D06E42258954
2 changed files with 9 additions and 0 deletions

View File

@ -12,6 +12,8 @@ class OwnedStringView
Result<OwnedStringView> clone() const;
static Result<OwnedStringView> from_string_literal(const char* str);
const char* chars() const
{
return m_string;

View File

@ -40,3 +40,10 @@ const char& OwnedStringView::operator[](usize index) const
expect(index < m_length, "index out of range");
return m_string[index];
}
Result<OwnedStringView> OwnedStringView::from_string_literal(const char* str)
{
char* dup = strdup(str);
if (!dup) return err(ENOMEM);
return OwnedStringView{dup};
}