Skip to content

Commit

Permalink
SmallString: Add span helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek committed Nov 29, 2024
1 parent d3246de commit ff3214b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/common/small_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
9 changes: 9 additions & 0 deletions src/common/small_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <cstring>
#include <iterator>
#include <limits>
#include <span>
#include <string>
#include <string_view>

Expand Down Expand Up @@ -207,10 +208,18 @@ class SmallStringBase
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); }
Expand Down

0 comments on commit ff3214b

Please sign in to comment.