2022-12-16 17:17:15 +00:00
|
|
|
#include <luna/Alloc.h>
|
2022-12-16 19:40:04 +00:00
|
|
|
#include <luna/CString.h>
|
2022-12-16 17:17:15 +00:00
|
|
|
#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()
|
|
|
|
{
|
2022-12-16 19:37:57 +00:00
|
|
|
if (m_string) raw_free(m_string);
|
2022-12-16 17:17:15 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 18:48:22 +00:00
|
|
|
Result<OwnedStringView> OwnedStringView::clone() const
|
2022-12-16 17:17:15 +00:00
|
|
|
{
|
2023-01-10 18:03:00 +00:00
|
|
|
return from_string_literal(m_string);
|
2022-12-16 19:48:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char& OwnedStringView::operator[](usize index) const
|
|
|
|
{
|
|
|
|
expect(index < m_length, "index out of range");
|
|
|
|
return m_string[index];
|
2022-12-17 10:36:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result<OwnedStringView> OwnedStringView::from_string_literal(const char* str)
|
|
|
|
{
|
2023-01-10 18:03:00 +00:00
|
|
|
char* const dup = strdup(str);
|
2022-12-17 10:36:16 +00:00
|
|
|
if (!dup) return err(ENOMEM);
|
2022-12-21 19:22:44 +00:00
|
|
|
return OwnedStringView { dup };
|
2023-01-02 12:07:29 +00:00
|
|
|
}
|