SmallString: Add span helpers

This commit is contained in:
Stenzek 2024-11-29 17:06:20 +10:00
parent d3246deb77
commit ff3214b8f7
No known key found for this signature in database
2 changed files with 29 additions and 0 deletions

View File

@ -506,6 +506,26 @@ std::wstring SmallStringBase::wstring() const
#endif
std::span<const char> SmallStringBase::cspan() const
{
return std::span<const char>(m_buffer, m_length);
}
std::span<char> SmallStringBase::span()
{
return std::span<char>(m_buffer, m_length);
}
std::span<const u8> SmallStringBase::cbspan() const
{
return std::span<const u8>(reinterpret_cast<const u8*>(m_buffer), m_length);
}
std::span<u8> SmallStringBase::bspan()
{
return std::span<u8>(reinterpret_cast<u8*>(m_buffer), m_length);
}
void SmallStringBase::vformat(fmt::string_view fmt, fmt::format_args args)
{
clear();

View File

@ -12,6 +12,7 @@
#include <cstring>
#include <iterator>
#include <limits>
#include <span>
#include <string>
#include <string_view>
@ -207,10 +208,18 @@ public:
std::wstring wstring() const;
#endif
// span creators
std::span<const char> cspan() const;
std::span<char> span();
std::span<const u8> cbspan() const;
std::span<u8> bspan();
// accessor operators
ALWAYS_INLINE operator const char*() const { return c_str(); }
ALWAYS_INLINE operator char*() { return data(); }
ALWAYS_INLINE operator std::string_view() const { return view(); }
ALWAYS_INLINE operator std::span<const char>() const { return cspan(); }
ALWAYS_INLINE operator std::span<char>() { return span(); }
// comparative operators
ALWAYS_INLINE bool operator==(const char* str) const { return equals(str); }