luna: Add StaticString, an OOP char[]

This commit is contained in:
apio 2023-02-27 15:13:23 +01:00
parent 55b430a4fd
commit 76f9bd8112
Signed by: apio
GPG Key ID: B8A7D06E42258954

View File

@ -0,0 +1,52 @@
#pragma once
#include <luna/CString.h>
#include <luna/Types.h>
template <usize Size> class StaticString
{
public:
StaticString() = default;
StaticString(const char* string)
{
adopt(string);
}
void adopt(const char* string)
{
usize length = strlcpy(m_buffer, string, sizeof(m_buffer));
if (length > Size) { m_length = Size; }
else
m_length = length;
}
StaticString<Size>& operator=(const char* string)
{
adopt(string);
return *this;
}
template <usize OSize> StaticString<Size>& operator=(const StaticString<OSize>& string)
{
if constexpr (OSize == Size)
{
if (this == &string) return *this;
}
adopt(string.chars());
return *this;
}
const char* chars() const
{
return m_buffer;
}
usize length() const
{
return m_length;
}
private:
char m_buffer[Size + 1];
usize m_length { 0 };
};