From b3a84c798380aca71f872b63121a3f93fb020746 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Wed, 8 Sep 2021 00:24:36 +0200 Subject: [PATCH] Use StringRef in pluralise implementation This means that it can no longer be safely made ahead of time, but nothing in our existing code used it like that. Normally it is constructed and used in the same expression, which is now more efficient. --- src/catch2/internal/catch_string_manip.cpp | 5 ----- src/catch2/internal/catch_string_manip.hpp | 17 +++++++++++++++-- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/catch2/internal/catch_string_manip.cpp b/src/catch2/internal/catch_string_manip.cpp index df6b6c34..0444cd38 100644 --- a/src/catch2/internal/catch_string_manip.cpp +++ b/src/catch2/internal/catch_string_manip.cpp @@ -94,11 +94,6 @@ namespace Catch { return subStrings; } - pluralise::pluralise( std::size_t count, std::string const& label ) - : m_count( count ), - m_label( label ) - {} - std::ostream& operator << ( std::ostream& os, pluralise const& pluraliser ) { os << pluraliser.m_count << ' ' << pluraliser.m_label; if( pluraliser.m_count != 1 ) diff --git a/src/catch2/internal/catch_string_manip.hpp b/src/catch2/internal/catch_string_manip.hpp index d4a2fbaf..bd7d0ddf 100644 --- a/src/catch2/internal/catch_string_manip.hpp +++ b/src/catch2/internal/catch_string_manip.hpp @@ -32,13 +32,26 @@ namespace Catch { std::vector splitStringRef( StringRef str, char delimiter ); bool replaceInPlace( std::string& str, std::string const& replaceThis, std::string const& withThis ); + /** + * Helper for streaming a "count [maybe-plural-of-label]" human-friendly string + * + * Usage example: + * ```cpp + * std::cout << "Found " << pluralise(count, "error") << '\n'; + * ``` + * + * **Important:** The provided string must outlive the instance + */ struct pluralise { - pluralise( std::size_t count, std::string const& label ); + pluralise(std::size_t count, StringRef label): + m_count(count), + m_label(label) + {} friend std::ostream& operator << ( std::ostream& os, pluralise const& pluraliser ); std::size_t m_count; - std::string m_label; + StringRef m_label; }; }