From f75a5605eb793bdc6f1babfd539e983041cbe25d Mon Sep 17 00:00:00 2001 From: Stenzek Date: Thu, 11 Apr 2024 13:42:00 +1000 Subject: [PATCH] SmallString: Add vformat overloads --- src/common/error.h | 26 ++++++++++++++++++++++++++ src/common/small_string.cpp | 8 +++++++- src/common/small_string.h | 14 +++++++++++++- 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/common/error.h b/src/common/error.h index 7a19401af..80f49ed40 100644 --- a/src/common/error.h +++ b/src/common/error.h @@ -3,6 +3,7 @@ #pragma once +#include "small_string.h" #include "types.h" #include "fmt/core.h" @@ -94,6 +95,31 @@ public: static void AddPrefix(Error* errptr, std::string_view prefix); static void AddSuffix(Error* errptr, std::string_view prefix); + template + void AddPrefixFmt(fmt::format_string fmt, T&&... args) + { + AddPrefix(TinyString::from_vformat(fmt::string_view(fmt), fmt::make_format_args(args...))); + } + + template + void AddSuffixFmt(fmt::format_string fmt, T&&... args) + { + AddSuffix(TinyString::from_vformat(fmt::string_view(fmt), fmt::make_format_args(args...))); + } + + template + static void AddPrefixFmt(Error* errptr, fmt::format_string fmt, T&&... args) + { + if (errptr) + Error::AddPrefix(errptr, TinyString::from_vformat(fmt::string_view(fmt), fmt::make_format_args(args...))); + } + template + static void AddSuffixFmt(Error* errptr, fmt::format_string fmt, T&&... args) + { + if (errptr) + Error::AddSuffix(errptr, TinyString::from_vformat(fmt::string_view(fmt), fmt::make_format_args(args...))); + } + Error& operator=(const Error& e); Error& operator=(Error&& e); bool operator==(const Error& e) const; diff --git a/src/common/small_string.cpp b/src/common/small_string.cpp index 94acb4833..0dc55acba 100644 --- a/src/common/small_string.cpp +++ b/src/common/small_string.cpp @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin +// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin // SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0) #include "small_string.h" @@ -439,6 +439,12 @@ void SmallStringBase::assign(const std::string_view str) append(str.data(), static_cast(str.size())); } +void SmallStringBase::vformat(fmt::string_view fmt, fmt::format_args args) +{ + clear(); + fmt::vformat_to(std::back_inserter(*this), fmt, args); +} + bool SmallStringBase::equals(const char* str) const { if (m_length == 0) diff --git a/src/common/small_string.h b/src/common/small_string.h index b92711def..b7aa193e3 100644 --- a/src/common/small_string.h +++ b/src/common/small_string.h @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin +// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin // SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0) #pragma once @@ -98,6 +98,8 @@ public: template void format(fmt::format_string fmt, T&&... args); + void vformat(fmt::string_view fmt, fmt::format_args args); + // compare one string to another bool equals(const char* str) const; bool equals(const SmallStringBase& str) const; @@ -316,6 +318,8 @@ public: template static SmallStackString from_format(fmt::format_string fmt, T&&... args); + static SmallStackString from_vformat(fmt::string_view fmt, fmt::format_args args); + private: char m_stack_buffer[L + 1]; @@ -360,6 +364,14 @@ ALWAYS_INLINE SmallStackString SmallStackString::from_format(fmt::format_s return ret; } +template +ALWAYS_INLINE SmallStackString SmallStackString::from_vformat(fmt::string_view fmt, fmt::format_args args) +{ + SmallStackString ret; + fmt::vformat_to(std::back_inserter(ret), fmt, args); + return ret; +} + // stack string types using TinyString = SmallStackString<64>; using SmallString = SmallStackString<256>;