From 41ad0fda11d0898ce3aaeee9db4e7bc16eec5051 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Sun, 30 May 2021 13:05:07 +0200 Subject: [PATCH] Improve XmlWriter::writeAttribute overload set for string-like types Previously, string literals and `std::string`s would match the template variant, which would serialize them into a stream and then call the `StringRef` overload for resulting string. This caused bunch of codebloat and unnecessary pessimization for common usage. --- src/catch2/internal/catch_xmlwriter.cpp | 16 ++++++++++++++- src/catch2/internal/catch_xmlwriter.hpp | 27 +++++++++++++++++++------ 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/catch2/internal/catch_xmlwriter.cpp b/src/catch2/internal/catch_xmlwriter.cpp index 730f1dbd..b8635f7f 100644 --- a/src/catch2/internal/catch_xmlwriter.cpp +++ b/src/catch2/internal/catch_xmlwriter.cpp @@ -217,6 +217,14 @@ namespace { return *this; } + XmlWriter::ScopedElement& + XmlWriter::ScopedElement::writeAttribute( StringRef name, + StringRef attribute ) { + m_writer->writeAttribute( name, attribute ); + return *this; + } + + XmlWriter::XmlWriter( std::ostream& os ) : m_os( os ) { writeDeclaration(); @@ -276,7 +284,13 @@ namespace { } XmlWriter& XmlWriter::writeAttribute( StringRef name, bool attribute ) { - m_os << ' ' << name << "=\"" << ( attribute ? "true" : "false" ) << '"'; + writeAttribute(name, (attribute ? "true"_sr : "false"_sr)); + return *this; + } + + XmlWriter& XmlWriter::writeAttribute( StringRef name, + char const* attribute ) { + writeAttribute( name, StringRef( attribute ) ); return *this; } diff --git a/src/catch2/internal/catch_xmlwriter.hpp b/src/catch2/internal/catch_xmlwriter.hpp index 36a3b69b..3ba8c08e 100644 --- a/src/catch2/internal/catch_xmlwriter.hpp +++ b/src/catch2/internal/catch_xmlwriter.hpp @@ -61,7 +61,15 @@ namespace Catch { XmlFormatting fmt = XmlFormatting::Newline | XmlFormatting::Indent ); - template + ScopedElement& writeAttribute( StringRef name, + StringRef attribute ); + template ::value>> ScopedElement& writeAttribute( StringRef name, T const& attribute ) { m_writer->writeAttribute( name, attribute ); @@ -91,15 +99,22 @@ namespace Catch { //! Writes the attribute as "true/false" XmlWriter& writeAttribute( StringRef name, bool attribute ); - //! The attribute value must provide op<<(ostream&, T). Resulting + //! The attribute content is XML-encoded + XmlWriter& writeAttribute( StringRef name, char const* attribute ); + + //! The attribute value must provide op<<(ostream&, T). The resulting //! serialization is XML-encoded - template + template ::value>> XmlWriter& writeAttribute( StringRef name, T const& attribute ) { ReusableStringStream rss; rss << attribute; - // We need to explicitly convert the string to StringRef to - // guarantee the right overload is picked - return writeAttribute( name, StringRef(rss.str()) ); + return writeAttribute( name, rss.str() ); } XmlWriter& writeText( StringRef text,