SmallString: Add vformat overloads

This commit is contained in:
Stenzek 2024-04-11 13:42:00 +10:00
parent 1809885927
commit f75a5605eb
No known key found for this signature in database
3 changed files with 46 additions and 2 deletions

View file

@ -3,6 +3,7 @@
#pragma once #pragma once
#include "small_string.h"
#include "types.h" #include "types.h"
#include "fmt/core.h" #include "fmt/core.h"
@ -94,6 +95,31 @@ public:
static void AddPrefix(Error* errptr, std::string_view prefix); static void AddPrefix(Error* errptr, std::string_view prefix);
static void AddSuffix(Error* errptr, std::string_view prefix); static void AddSuffix(Error* errptr, std::string_view prefix);
template<typename... T>
void AddPrefixFmt(fmt::format_string<T...> fmt, T&&... args)
{
AddPrefix(TinyString::from_vformat(fmt::string_view(fmt), fmt::make_format_args(args...)));
}
template<typename... T>
void AddSuffixFmt(fmt::format_string<T...> fmt, T&&... args)
{
AddSuffix(TinyString::from_vformat(fmt::string_view(fmt), fmt::make_format_args(args...)));
}
template<typename... T>
static void AddPrefixFmt(Error* errptr, fmt::format_string<T...> fmt, T&&... args)
{
if (errptr)
Error::AddPrefix(errptr, TinyString::from_vformat(fmt::string_view(fmt), fmt::make_format_args(args...)));
}
template<typename... T>
static void AddSuffixFmt(Error* errptr, fmt::format_string<T...> 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=(const Error& e);
Error& operator=(Error&& e); Error& operator=(Error&& e);
bool operator==(const Error& e) const; bool operator==(const Error& e) const;

View file

@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <stenzek@gmail.com> // SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0) // SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
#include "small_string.h" #include "small_string.h"
@ -439,6 +439,12 @@ void SmallStringBase::assign(const std::string_view str)
append(str.data(), static_cast<u32>(str.size())); append(str.data(), static_cast<u32>(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 bool SmallStringBase::equals(const char* str) const
{ {
if (m_length == 0) if (m_length == 0)

View file

@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin <stenzek@gmail.com> // SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0) // SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
#pragma once #pragma once
@ -98,6 +98,8 @@ public:
template<typename... T> template<typename... T>
void format(fmt::format_string<T...> fmt, T&&... args); void format(fmt::format_string<T...> fmt, T&&... args);
void vformat(fmt::string_view fmt, fmt::format_args args);
// compare one string to another // compare one string to another
bool equals(const char* str) const; bool equals(const char* str) const;
bool equals(const SmallStringBase& str) const; bool equals(const SmallStringBase& str) const;
@ -316,6 +318,8 @@ public:
template<typename... T> template<typename... T>
static SmallStackString from_format(fmt::format_string<T...> fmt, T&&... args); static SmallStackString from_format(fmt::format_string<T...> fmt, T&&... args);
static SmallStackString from_vformat(fmt::string_view fmt, fmt::format_args args);
private: private:
char m_stack_buffer[L + 1]; char m_stack_buffer[L + 1];
@ -360,6 +364,14 @@ ALWAYS_INLINE SmallStackString<L> SmallStackString<L>::from_format(fmt::format_s
return ret; return ret;
} }
template<u32 L>
ALWAYS_INLINE SmallStackString<L> SmallStackString<L>::from_vformat(fmt::string_view fmt, fmt::format_args args)
{
SmallStackString<L> ret;
fmt::vformat_to(std::back_inserter(ret), fmt, args);
return ret;
}
// stack string types // stack string types
using TinyString = SmallStackString<64>; using TinyString = SmallStackString<64>;
using SmallString = SmallStackString<256>; using SmallString = SmallStackString<256>;