123 lines
2.6 KiB
C++
123 lines
2.6 KiB
C++
|
/**
|
||
|
* @file RefString.cpp
|
||
|
* @author apio (cloudapio.eu)
|
||
|
* @brief Reference-counted, trivially-copyable, immutable string type.
|
||
|
*
|
||
|
* @copyright Copyright (c) 2024, the Luna authors.
|
||
|
*
|
||
|
*/
|
||
|
|
||
|
#include <luna/Format.h>
|
||
|
#include <luna/RefString.h>
|
||
|
|
||
|
static const char empty_string[] = { 0 };
|
||
|
|
||
|
RefString::RefString()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
RefString::RefString(RefString&& other) : m_string_data(move(other.m_string_data))
|
||
|
{
|
||
|
}
|
||
|
|
||
|
RefString::RefString(const RefString& other) : m_string_data(other.m_string_data)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
const char* RefString::chars() const
|
||
|
{
|
||
|
if (m_string_data) return m_string_data->string;
|
||
|
return empty_string;
|
||
|
}
|
||
|
|
||
|
RefString& RefString::operator=(const RefString& other)
|
||
|
{
|
||
|
if (&other == this) return *this;
|
||
|
m_string_data = other.m_string_data;
|
||
|
return *this;
|
||
|
}
|
||
|
|
||
|
Result<RefString> RefString::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 adopt(dup, size);
|
||
|
}
|
||
|
|
||
|
const char& RefString::operator[](usize index) const
|
||
|
{
|
||
|
expect(index < length(), "index out of range");
|
||
|
return chars()[index];
|
||
|
}
|
||
|
|
||
|
Result<RefString> RefString::format(StringView fmt, ...)
|
||
|
{
|
||
|
va_list ap;
|
||
|
va_start(ap, fmt);
|
||
|
|
||
|
auto rc = vformat(fmt, ap);
|
||
|
|
||
|
va_end(ap);
|
||
|
|
||
|
return rc;
|
||
|
}
|
||
|
|
||
|
Result<RefString> RefString::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 adopt(buf.release_data());
|
||
|
}
|
||
|
|
||
|
Result<RefString> RefString::from_cstring(const char* str)
|
||
|
{
|
||
|
return from_string_view(StringView { str });
|
||
|
}
|
||
|
|
||
|
Result<RefString> RefString::from_string(const String& str)
|
||
|
{
|
||
|
return from_string_view(str.view());
|
||
|
}
|
||
|
|
||
|
Result<RefString> RefString::from_string_view(StringView str)
|
||
|
{
|
||
|
char* const dup = strndup(str.chars(), str.length());
|
||
|
if (!dup) return err(ENOMEM);
|
||
|
return adopt(dup, str.length());
|
||
|
}
|
||
|
|
||
|
int RefString::compare(const RefString* a, const RefString* b)
|
||
|
{
|
||
|
return strcmp(a->chars(), b->chars());
|
||
|
}
|
||
|
|
||
|
Result<RefString> RefString::adopt(char* c_str)
|
||
|
{
|
||
|
return adopt(c_str, strlen(c_str));
|
||
|
}
|
||
|
|
||
|
Result<RefString> RefString::adopt(char* c_str, usize length)
|
||
|
{
|
||
|
auto guard = make_scope_guard([&] { free_impl(c_str); });
|
||
|
|
||
|
auto data = TRY(make_shared<RefStringData>(c_str, length));
|
||
|
|
||
|
guard.deactivate();
|
||
|
|
||
|
RefString string;
|
||
|
string.m_string_data = data;
|
||
|
return string;
|
||
|
}
|
||
|
|
||
|
template <> u64 hash(const RefString& value, u64 salt)
|
||
|
{
|
||
|
return hash_memory(value.chars(), value.length(), salt);
|
||
|
}
|