Luna/libluna/include/luna/String.h

132 lines
3.4 KiB
C
Raw Normal View History

#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>
class String
{
typedef char* Iterator;
typedef const char* ConstIterator;
public:
/* Constructs a String, which will take ownership of the given c-string. To create a String that makes its own
* internal copy of the passed string, use String::from_cstring(). */
String(char* c_str);
/* Constructs a String with precalculated length, which will take ownership of the given c-string. To create a
* String that makes its own internal copy of the passed string, use String::from_cstring(). */
String(char* c_str, usize length);
/* Constructs an empty String. */
String();
String(String&&);
/* Implicit copying is disabled for Strings, as copy constructors cannot use value-based error handling. To copy a
* string, please use the clone() method. */
String(const String&) = delete;
2023-04-07 10:14:21 +00:00
String& operator=(String&&);
/* Implicit copying is disabled for Strings, as copy constructors cannot use value-based error handling. To copy a
* string, please use the clone() method. */
2023-04-07 10:14:21 +00:00
String& operator=(const String&) = delete;
~String();
/* Creates a copy of this String and returns it. */
Result<String> clone() const;
/* Creates a copy of a specific segment of this String and returns it. */
Result<String> substring(usize begin, usize size) const;
/* Splits a String with a delimiter. */
2023-05-01 18:05:10 +00:00
Result<Vector<String>> split(StringView delim) const
{
return view().split(delim);
}
/* Splits a String into two parts with a delimiter. */
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
/* Creates a single String consisting of a list of strings separated by a delimiter. */
2023-05-01 17:29:17 +00:00
static Result<String> join(const Vector<String>& vec, StringView delim);
/* Creates a single String consisting of a list of strings separated by a delimiter. */
static Result<String> join(const Vector<StringView>& vec, StringView delim);
/* Removes all trailing characters contained in 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, ...);
static Result<String> vformat(StringView fmt, va_list ap);
2023-04-07 08:37:00 +00:00
static Result<String> from_cstring(const char* str);
static Result<String> from_string_view(StringView str);
static int compare(const String* a, const String* b);
const char* chars() const
{
return m_inline ? m_inline_storage : m_string;
}
2023-06-03 10:15:57 +00:00
char* mutable_data()
{
return m_inline ? m_inline_storage : m_string;
}
usize length() const
{
return m_length;
}
bool is_empty() const
{
return m_length == 0;
}
2023-03-29 15:43:10 +00:00
StringView view() const
{
return { chars(), m_length };
}
const char& operator[](usize) const;
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;
}
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
void empty();
};