From c7241bb76e383e103792b47119ae5e742120e7f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Sat, 29 May 2021 18:38:07 +0200 Subject: [PATCH] Use StringRef in XmLWriter when the text is not stored --- src/catch2/internal/catch_xmlwriter.cpp | 11 ++++++----- src/catch2/internal/catch_xmlwriter.hpp | 24 +++++++++++++++++------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/catch2/internal/catch_xmlwriter.cpp b/src/catch2/internal/catch_xmlwriter.cpp index 63e6be1f..9adbfcf7 100644 --- a/src/catch2/internal/catch_xmlwriter.cpp +++ b/src/catch2/internal/catch_xmlwriter.cpp @@ -267,18 +267,19 @@ namespace { return *this; } - XmlWriter& XmlWriter::writeAttribute( std::string const& name, std::string const& attribute ) { + XmlWriter& XmlWriter::writeAttribute( StringRef name, + StringRef attribute ) { if( !name.empty() && !attribute.empty() ) m_os << ' ' << name << "=\"" << XmlEncode( attribute, XmlEncode::ForAttributes ) << '"'; return *this; } - XmlWriter& XmlWriter::writeAttribute( std::string const& name, bool attribute ) { + XmlWriter& XmlWriter::writeAttribute( StringRef name, bool attribute ) { m_os << ' ' << name << "=\"" << ( attribute ? "true" : "false" ) << '"'; return *this; } - XmlWriter& XmlWriter::writeText( std::string const& text, XmlFormatting fmt) { + XmlWriter& XmlWriter::writeText( StringRef text, XmlFormatting fmt ) { if( !text.empty() ){ bool tagWasOpen = m_tagIsOpen; ensureTagClosed(); @@ -291,7 +292,7 @@ namespace { return *this; } - XmlWriter& XmlWriter::writeComment( std::string const& text, XmlFormatting fmt) { + XmlWriter& XmlWriter::writeComment( StringRef text, XmlFormatting fmt ) { ensureTagClosed(); if (shouldIndent(fmt)) { m_os << m_indent; @@ -301,7 +302,7 @@ namespace { return *this; } - void XmlWriter::writeStylesheetRef( std::string const& url ) { + void XmlWriter::writeStylesheetRef( StringRef url ) { m_os << R"()" << '\n'; } diff --git a/src/catch2/internal/catch_xmlwriter.hpp b/src/catch2/internal/catch_xmlwriter.hpp index 19642db2..8230c8c2 100644 --- a/src/catch2/internal/catch_xmlwriter.hpp +++ b/src/catch2/internal/catch_xmlwriter.hpp @@ -81,22 +81,32 @@ namespace Catch { XmlWriter& endElement(XmlFormatting fmt = XmlFormatting::Newline | XmlFormatting::Indent); - XmlWriter& writeAttribute( std::string const& name, std::string const& attribute ); + //! The attribute content is XML-encoded + XmlWriter& writeAttribute( StringRef name, StringRef attribute ); - XmlWriter& writeAttribute( std::string const& name, bool attribute ); + //! Writes the attribute as "true/false" + XmlWriter& writeAttribute( StringRef name, bool attribute ); + //! The attribute value must provide op<<(ostream&, T). Resulting + //! serialization is XML-encoded template - XmlWriter& writeAttribute( std::string const& name, T const& attribute ) { + XmlWriter& writeAttribute( StringRef name, T const& attribute ) { ReusableStringStream rss; rss << attribute; - return writeAttribute( name, rss.str() ); + // We need to explicitly convert the string to StringRef to + // guarantee the right overload is picked + return writeAttribute( name, StringRef(rss.str()) ); } - XmlWriter& writeText( std::string const& text, XmlFormatting fmt = XmlFormatting::Newline | XmlFormatting::Indent); + XmlWriter& writeText( StringRef text, + XmlFormatting fmt = XmlFormatting::Newline | + XmlFormatting::Indent ); - XmlWriter& writeComment(std::string const& text, XmlFormatting fmt = XmlFormatting::Newline | XmlFormatting::Indent); + XmlWriter& writeComment( StringRef text, + XmlFormatting fmt = XmlFormatting::Newline | + XmlFormatting::Indent ); - void writeStylesheetRef( std::string const& url ); + void writeStylesheetRef( StringRef url ); XmlWriter& writeBlankLine();