Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorMartin Hořeňovský2023-12-23 16:06:30 +0100
committerMartin Hořeňovský2023-12-26 14:57:29 +0100
commit822c44a20395c6194d527110d59f7513a55b885c (patch)
tree966a81f78d7f9c631c583de8ec13bb4d930f16df /src
parent2295d2c8ccc0dba4b35adb95ef8946a05996df87 (diff)
downloadcatch3-822c44a20395c6194d527110d59f7513a55b885c.tar.gz
Cleanup help string construction in Clara
* Clara::Opt::getHelpColumns returns single item It could never return multiple items, but for some reason it was wrapping that single item in a vector. * Use ReusableStringStream in Clara * Reserve HelpColumns ahead of time * Use StringRef for descriptions in HelpColumn type The combination of these changes ends up removing about 7% (~200) of allocations when Catch2 has to prepare output for `-h`.
Diffstat (limited to 'src')
-rw-r--r--src/catch2/internal/catch_clara.cpp13
-rw-r--r--src/catch2/internal/catch_clara.hpp7
2 files changed, 11 insertions, 9 deletions
diff --git a/src/catch2/internal/catch_clara.cpp b/src/catch2/internal/catch_clara.cpp
index 4745eb89..9510fcc2 100644
--- a/src/catch2/internal/catch_clara.cpp
+++ b/src/catch2/internal/catch_clara.cpp
@@ -11,6 +11,7 @@
#include <catch2/internal/catch_platform.hpp>
#include <catch2/internal/catch_string_manip.hpp>
#include <catch2/internal/catch_textflow.hpp>
+#include <catch2/internal/catch_reusable_string_stream.hpp>
#include <algorithm>
#include <ostream>
@@ -173,8 +174,8 @@ namespace Catch {
Opt::Opt(bool& ref) :
ParserRefImpl(std::make_shared<Detail::BoundFlagRef>(ref)) {}
- std::vector<Detail::HelpColumns> Opt::getHelpColumns() const {
- std::ostringstream oss;
+ Detail::HelpColumns Opt::getHelpColumns() const {
+ ReusableStringStream oss;
bool first = true;
for (auto const& opt : m_optNames) {
if (first)
@@ -185,7 +186,7 @@ namespace Catch {
}
if (!m_hint.empty())
oss << " <" << m_hint << '>';
- return { { oss.str(), static_cast<std::string>(m_description) } };
+ return { oss.str(), m_description };
}
bool Opt::isMatch(std::string const& optToken) const {
@@ -310,9 +311,9 @@ namespace Catch {
std::vector<Detail::HelpColumns> Parser::getHelpColumns() const {
std::vector<Detail::HelpColumns> cols;
+ cols.reserve( m_options.size() );
for ( auto const& o : m_options ) {
- auto childCols = o.getHelpColumns();
- cols.insert( cols.end(), childCols.begin(), childCols.end() );
+ cols.push_back(o.getHelpColumns());
}
return cols;
}
@@ -355,7 +356,7 @@ namespace Catch {
.width( optWidth )
.indent( 2 ) +
TextFlow::Spacer( 4 ) +
- TextFlow::Column( cols.right )
+ TextFlow::Column( static_cast<std::string>(cols.descriptions) )
.width( consoleWidth - 7 - optWidth );
os << row << '\n';
}
diff --git a/src/catch2/internal/catch_clara.hpp b/src/catch2/internal/catch_clara.hpp
index d8c31c68..8f6a4e9e 100644
--- a/src/catch2/internal/catch_clara.hpp
+++ b/src/catch2/internal/catch_clara.hpp
@@ -164,7 +164,8 @@ namespace Catch {
ResultType m_type;
};
- template <typename T> class ResultValueBase : public ResultBase {
+ template <typename T>
+ class ResultValueBase : public ResultBase {
public:
auto value() const -> T const& {
enforceOk();
@@ -286,7 +287,7 @@ namespace Catch {
struct HelpColumns {
std::string left;
- std::string right;
+ StringRef descriptions;
};
template <typename T>
@@ -571,7 +572,7 @@ namespace Catch {
return *this;
}
- std::vector<Detail::HelpColumns> getHelpColumns() const;
+ Detail::HelpColumns getHelpColumns() const;
bool isMatch(std::string const& optToken) const;