From ff3214b8f7ac4dda3568e912400e3cca369be179 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Fri, 29 Nov 2024 17:06:20 +1000 Subject: [PATCH] SmallString: Add span helpers --- src/common/small_string.cpp | 20 ++++++++++++++++++++ src/common/small_string.h | 9 +++++++++ 2 files changed, 29 insertions(+) diff --git a/src/common/small_string.cpp b/src/common/small_string.cpp index d1d8b4537..f539e1f8a 100644 --- a/src/common/small_string.cpp +++ b/src/common/small_string.cpp @@ -506,6 +506,26 @@ std::wstring SmallStringBase::wstring() const #endif +std::span SmallStringBase::cspan() const +{ + return std::span(m_buffer, m_length); +} + +std::span SmallStringBase::span() +{ + return std::span(m_buffer, m_length); +} + +std::span SmallStringBase::cbspan() const +{ + return std::span(reinterpret_cast(m_buffer), m_length); +} + +std::span SmallStringBase::bspan() +{ + return std::span(reinterpret_cast(m_buffer), m_length); +} + void SmallStringBase::vformat(fmt::string_view fmt, fmt::format_args args) { clear(); diff --git a/src/common/small_string.h b/src/common/small_string.h index 67fea7a18..a631bd393 100644 --- a/src/common/small_string.h +++ b/src/common/small_string.h @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -207,10 +208,18 @@ public: std::wstring wstring() const; #endif + // span creators + std::span cspan() const; + std::span span(); + std::span cbspan() const; + std::span 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 { return cspan(); } + ALWAYS_INLINE operator std::span() { return span(); } // comparative operators ALWAYS_INLINE bool operator==(const char* str) const { return equals(str); }