2023-03-29 15:28:22 +00:00
|
|
|
#include <luna/Alloc.h>
|
|
|
|
#include <luna/CString.h>
|
2023-04-07 08:37:00 +00:00
|
|
|
#include <luna/Format.h>
|
2023-03-29 15:28:22 +00:00
|
|
|
#include <luna/String.h>
|
2023-05-01 17:29:17 +00:00
|
|
|
#include <luna/StringBuilder.h>
|
2023-04-07 08:37:00 +00:00
|
|
|
#include <luna/Vector.h>
|
2023-03-29 15:28:22 +00:00
|
|
|
|
|
|
|
String::String()
|
2023-04-28 19:16:11 +00:00
|
|
|
{
|
|
|
|
empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void String::empty()
|
2023-03-29 15:28:22 +00:00
|
|
|
{
|
|
|
|
m_inline = true;
|
2023-03-29 15:32:53 +00:00
|
|
|
m_length = 0;
|
2023-03-29 15:28:22 +00:00
|
|
|
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));
|
|
|
|
|
2023-04-28 19:16:11 +00:00
|
|
|
other.empty();
|
2023-03-29 15:28:22 +00:00
|
|
|
}
|
|
|
|
|
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));
|
|
|
|
|
2023-04-28 19:16:11 +00:00
|
|
|
other.empty();
|
2023-04-07 10:14:21 +00:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2023-03-29 15:28:22 +00:00
|
|
|
String::String(char* c_str)
|
|
|
|
{
|
2023-03-29 15:32:53 +00:00
|
|
|
check(c_str);
|
2023-03-29 15:28:22 +00:00
|
|
|
m_string = c_str;
|
|
|
|
m_inline = false;
|
2023-03-29 15:32:53 +00:00
|
|
|
m_length = strlen(m_string);
|
2023-03-29 15:28:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String::String(char* c_str, usize length)
|
|
|
|
{
|
2023-03-29 15:32:53 +00:00
|
|
|
check(c_str);
|
2023-03-29 15:28:22 +00:00
|
|
|
m_string = c_str;
|
|
|
|
m_inline = false;
|
|
|
|
m_length = length;
|
|
|
|
}
|
|
|
|
|
|
|
|
String::~String()
|
|
|
|
{
|
|
|
|
if (!m_inline) free_impl(m_string);
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<String> String::clone() const
|
|
|
|
{
|
2023-03-29 15:34:30 +00:00
|
|
|
return from_cstring(chars());
|
2023-03-29 15:28:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 };
|
|
|
|
}
|
|
|
|
|
2023-04-22 13:19:07 +00:00
|
|
|
void String::trim(StringView delim)
|
|
|
|
{
|
|
|
|
isize i = (isize)m_length;
|
|
|
|
|
|
|
|
while (i--)
|
|
|
|
{
|
|
|
|
char c = chars()[i];
|
|
|
|
if (!strchr(delim.chars(), c)) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
|
|
|
|
if (m_inline) m_inline_storage[i] = '\0';
|
|
|
|
else
|
|
|
|
m_string[i] = '\0';
|
|
|
|
|
|
|
|
m_length = (usize)i;
|
|
|
|
}
|
|
|
|
|
2023-03-29 15:28:22 +00:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2023-03-29 15:34:30 +00:00
|
|
|
Result<String> String::from_cstring(const char* str)
|
2023-03-29 15:28:22 +00:00
|
|
|
{
|
2023-05-02 08:48:38 +00:00
|
|
|
return from_string_view(StringView { str });
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<String> String::from_string_view(StringView str)
|
|
|
|
{
|
|
|
|
if (str.length() < sizeof(m_inline_storage))
|
2023-03-29 15:28:22 +00:00
|
|
|
{
|
|
|
|
String result;
|
|
|
|
result.m_inline = true;
|
2023-05-02 08:48:38 +00:00
|
|
|
result.m_length = str.length();
|
|
|
|
strncpy(result.m_inline_storage, str.chars(), sizeof(m_inline_storage));
|
2023-03-29 15:28:22 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-05-02 08:48:38 +00:00
|
|
|
char* const dup = strndup(str.chars(), str.length());
|
2023-03-29 15:28:22 +00:00
|
|
|
if (!dup) return err(ENOMEM);
|
2023-05-02 08:48:38 +00:00
|
|
|
return String { dup, str.length() };
|
2023-03-29 15:28:22 +00:00
|
|
|
}
|
2023-05-01 17:29:17 +00:00
|
|
|
|
|
|
|
Result<String> String::join(const Vector<String>& vec, StringView delim)
|
|
|
|
{
|
|
|
|
if (vec.size() == 0) return String {};
|
|
|
|
if (vec.size() == 1) return vec[0].clone();
|
|
|
|
|
|
|
|
StringBuilder sb;
|
|
|
|
TRY(sb.add(vec[0].view()));
|
|
|
|
|
|
|
|
for (usize i = 1; i < vec.size(); i++)
|
|
|
|
{
|
|
|
|
TRY(sb.add(delim));
|
|
|
|
TRY(sb.add(vec[i].view()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return sb.string();
|
|
|
|
}
|
2023-05-02 18:55:41 +00:00
|
|
|
|
2023-05-20 10:46:33 +00:00
|
|
|
Result<String> String::join(const Vector<StringView>& vec, StringView delim)
|
|
|
|
{
|
|
|
|
if (vec.size() == 0) return String {};
|
|
|
|
if (vec.size() == 1) return from_string_view(vec[0]);
|
|
|
|
|
|
|
|
StringBuilder sb;
|
|
|
|
TRY(sb.add(vec[0]));
|
|
|
|
|
|
|
|
for (usize i = 1; i < vec.size(); i++)
|
|
|
|
{
|
|
|
|
TRY(sb.add(delim));
|
|
|
|
TRY(sb.add(vec[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
return sb.string();
|
|
|
|
}
|
|
|
|
|
2023-05-02 18:55:41 +00:00
|
|
|
int String::compare(const String* a, const String* b)
|
|
|
|
{
|
|
|
|
return strcmp(a->chars(), b->chars());
|
|
|
|
}
|