Luna/libluna/src/String.cpp

137 lines
2.6 KiB
C++
Raw Normal View History

#include <luna/Alloc.h>
#include <luna/CString.h>
2023-04-07 08:37:00 +00:00
#include <luna/Format.h>
#include <luna/String.h>
2023-04-07 08:37:00 +00:00
#include <luna/Vector.h>
String::String()
{
m_inline = true;
m_length = 0;
memset(m_inline_storage, 0, sizeof(m_inline_storage));
}
String::String(String&& other)
{
m_inline = other.m_inline;
m_string = other.m_string;
m_length = other.m_length;
if (m_inline) memcpy(m_inline_storage, other.m_inline_storage, sizeof(m_inline_storage));
other.m_string = nullptr;
}
2023-04-07 10:14:21 +00:00
String& String::operator=(String&& other)
{
if (&other == this) return *this;
if (!m_inline) free_impl(m_string);
m_inline = other.m_inline;
m_string = other.m_string;
m_length = other.m_length;
if (m_inline) memcpy(m_inline_storage, other.m_inline_storage, sizeof(m_inline_storage));
other.m_string = nullptr;
return *this;
}
String::String(char* c_str)
{
check(c_str);
m_string = c_str;
m_inline = false;
m_length = strlen(m_string);
}
String::String(char* c_str, usize length)
{
check(c_str);
m_string = c_str;
m_inline = false;
m_length = length;
}
String::~String()
{
if (!m_inline) free_impl(m_string);
}
Result<String> String::clone() const
{
return from_cstring(chars());
}
Result<String> String::substring(usize begin, usize size) const
{
if (begin + size >= size) return err(EINVAL);
char* const dup = strndup(chars() + begin, size);
if (!dup) return err(ENOMEM);
return String { dup, size };
}
const char& String::operator[](usize index) const
{
expect(index < m_length, "index out of range");
return chars()[index];
}
2023-04-07 08:37:00 +00:00
Result<String> String::format(const String& fmt, ...)
{
va_list ap;
va_start(ap, fmt);
auto rc = vformat(fmt.view(), ap);
va_end(ap);
return rc;
}
Result<String> String::format(StringView fmt, ...)
{
va_list ap;
va_start(ap, fmt);
auto rc = vformat(fmt, ap);
va_end(ap);
return rc;
}
Result<String> String::vformat(StringView fmt, va_list ap)
{
Vector<char> buf;
TRY(cstyle_format(
fmt.chars(), [](char c, void* data) -> Result<void> { return ((Vector<char>*)data)->try_append(c); }, &buf,
ap));
TRY(buf.try_append(0));
return from_cstring(buf.data());
}
Result<String> String::from_cstring(const char* str)
{
usize len = strlen(str);
if (len < sizeof(m_inline_storage))
{
String result;
result.m_inline = true;
result.m_length = len;
strncpy(result.m_inline_storage, str, sizeof(m_inline_storage));
return result;
}
char* const dup = strdup(str);
if (!dup) return err(ENOMEM);
return String { dup };
}