Luna/libluna/include/luna/StaticString.h
apio 37547ec640
All checks were successful
continuous-integration/drone/push Build is passing
libluna: Run lint scripts
2023-04-23 10:53:48 +02:00

58 lines
1.1 KiB
C++

#pragma once
#include <luna/CString.h>
#include <luna/Types.h>
template <usize Size> class StaticString
{
public:
StaticString() = default;
StaticString(const char* string)
{
adopt(string);
}
template <usize OtherSize> StaticString(const StaticString<OtherSize>& other)
{
adopt(other.chars());
}
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 OtherSize> StaticString<Size>& operator=(const StaticString<OtherSize>& string)
{
if constexpr (OtherSize == 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 };
};