Luna/luna/src/OwnedStringView.cpp
apio 5aa667c776
All checks were successful
continuous-integration/drone/push Build is passing
luna: Make OwnedStringView::clone() just call from_string_literal()
2023-01-10 19:03:00 +01:00

46 lines
918 B
C++

#include <luna/Alloc.h>
#include <luna/CString.h>
#include <luna/OwnedStringView.h>
OwnedStringView::OwnedStringView()
{
}
OwnedStringView::OwnedStringView(OwnedStringView&& other)
{
m_string = other.m_string;
m_length = other.m_length;
other.m_string = nullptr;
}
OwnedStringView::OwnedStringView(char* c_str)
{
m_string = c_str;
if (m_string) { m_length = strlen(m_string); }
}
OwnedStringView::~OwnedStringView()
{
if (m_string) raw_free(m_string);
}
Result<OwnedStringView> OwnedStringView::clone() const
{
return from_string_literal(m_string);
}
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* const dup = strdup(str);
if (!dup) return err(ENOMEM);
return OwnedStringView { dup };
}