Luna/luna/src/OwnedStringView.cpp

36 lines
670 B
C++
Raw Normal View History

#include <luna/Alloc.h>
#include <luna/OwnedStringView.h>
#include <luna/String.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) destroy_array(m_string);
}
Result<OwnedStringView> OwnedStringView::clone()
{
char* buf = TRY(make_array<char>(m_length + 1));
memcpy(buf, m_string, m_length + 1);
return OwnedStringView{buf};
}