2023-03-29 15:28:22 +00:00
|
|
|
#pragma once
|
|
|
|
#include <luna/Result.h>
|
2023-03-29 15:43:10 +00:00
|
|
|
#include <luna/StringView.h>
|
2023-04-07 08:37:00 +00:00
|
|
|
#include <stdarg.h>
|
2023-03-29 15:28:22 +00:00
|
|
|
|
|
|
|
class String
|
|
|
|
{
|
2023-03-29 17:25:11 +00:00
|
|
|
typedef char* Iterator;
|
|
|
|
typedef const char* ConstIterator;
|
|
|
|
|
2023-03-29 15:28:22 +00:00
|
|
|
public:
|
|
|
|
String(char* c_str);
|
|
|
|
String(char* c_str, usize length);
|
|
|
|
|
|
|
|
String();
|
|
|
|
|
|
|
|
String(String&&);
|
|
|
|
String(const String&) = delete;
|
|
|
|
|
2023-04-07 10:14:21 +00:00
|
|
|
String& operator=(String&&);
|
|
|
|
String& operator=(const String&) = delete;
|
|
|
|
|
2023-03-29 15:28:22 +00:00
|
|
|
~String();
|
|
|
|
|
|
|
|
Result<String> clone() const;
|
|
|
|
|
|
|
|
Result<String> substring(usize begin, usize size) const;
|
|
|
|
|
2023-04-07 13:14:46 +00:00
|
|
|
Result<Vector<String>> split(StringView delim) const;
|
2023-04-20 18:08:34 +00:00
|
|
|
Result<Vector<String>> split_once(char delim) const
|
|
|
|
{
|
|
|
|
return view().split_once(delim);
|
|
|
|
}
|
2023-04-07 13:14:46 +00:00
|
|
|
|
2023-05-01 17:29:17 +00:00
|
|
|
static Result<String> join(const Vector<String>& vec, StringView delim);
|
|
|
|
|
2023-04-22 13:19:07 +00:00
|
|
|
void trim(StringView delim);
|
|
|
|
|
2023-04-07 08:37:00 +00:00
|
|
|
static Result<String> format(const String& fmt, ...);
|
|
|
|
static Result<String> format(StringView fmt, ...);
|
2023-05-01 17:31:15 +00:00
|
|
|
static Result<String> vformat(StringView fmt, va_list ap);
|
2023-04-07 08:37:00 +00:00
|
|
|
|
2023-03-29 15:34:30 +00:00
|
|
|
static Result<String> from_cstring(const char* str);
|
2023-03-29 15:28:22 +00:00
|
|
|
|
|
|
|
const char* chars() const
|
|
|
|
{
|
|
|
|
return m_inline ? m_inline_storage : m_string;
|
|
|
|
}
|
|
|
|
|
|
|
|
usize length() const
|
|
|
|
{
|
|
|
|
return m_length;
|
|
|
|
}
|
|
|
|
|
2023-03-29 15:32:53 +00:00
|
|
|
bool is_empty() const
|
|
|
|
{
|
|
|
|
return m_length == 0;
|
|
|
|
}
|
|
|
|
|
2023-03-29 15:43:10 +00:00
|
|
|
StringView view() const
|
|
|
|
{
|
|
|
|
return { chars(), m_length };
|
|
|
|
}
|
|
|
|
|
2023-03-29 15:28:22 +00:00
|
|
|
const char& operator[](usize) const;
|
|
|
|
|
2023-03-29 17:25:11 +00:00
|
|
|
Iterator begin()
|
|
|
|
{
|
|
|
|
return m_inline ? m_inline_storage : m_string;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstIterator begin() const
|
|
|
|
{
|
|
|
|
return chars();
|
|
|
|
}
|
|
|
|
|
|
|
|
Iterator end()
|
|
|
|
{
|
|
|
|
return begin() + m_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstIterator end() const
|
|
|
|
{
|
|
|
|
return begin() + m_length;
|
|
|
|
}
|
|
|
|
|
2023-03-29 15:28:22 +00:00
|
|
|
private:
|
|
|
|
union {
|
|
|
|
char* m_string { nullptr };
|
|
|
|
char m_inline_storage[sizeof(char*)];
|
|
|
|
};
|
|
|
|
|
|
|
|
bool m_inline { true };
|
|
|
|
|
|
|
|
usize m_length { 0 };
|
2023-04-07 08:37:00 +00:00
|
|
|
|
2023-04-28 19:16:11 +00:00
|
|
|
void empty();
|
2023-03-29 15:28:22 +00:00
|
|
|
};
|