From 0dbc76a3ca25f43232073484541504e342380d0a Mon Sep 17 00:00:00 2001 From: Noel Grandin Date: Wed, 11 Mar 2020 14:19:34 +0200 Subject: [PATCH] make FastParser always take a FastTokenHandlerBase subclass since most of the call sites already do, and we can skip the slow path this way. Change-Id: I64ed30c51324e0510818f42ef838f97c401bb6dd Reviewed-on: https://gerrit.libreoffice.org/c/core/+/90326 Tested-by: Jenkins Reviewed-by: Noel Grandin --- .../misc/SvXMLAutoCorrectTokenHandler.hxx | 3 +-- include/oox/core/fasttokenhandler.hxx | 4 +--- include/sax/fastattribs.hxx | 16 +++++++-------- oox/source/core/fasttokenhandler.cxx | 2 +- oox/source/crypto/AgileEngine.cxx | 8 +++++++- sax/CppunitTest_sax_parser.mk | 1 + sax/qa/cppunit/parser.cxx | 7 ++++++- sax/qa/cppunit/xmlimport.cxx | 3 +-- sax/source/fastparser/fastparser.cxx | 17 ++++++---------- sax/source/tools/fastattribs.cxx | 20 +++++-------------- sax/source/tools/fshelper.cxx | 2 +- sw/source/core/inc/SwXMLBlockImport.hxx | 6 ++---- unoxml/CppunitTest_unoxml_domtest.mk | 1 + unoxml/inc/node.hxx | 8 ++++---- unoxml/qa/unit/domtest.cxx | 9 +++++++-- unoxml/source/dom/document.cxx | 2 +- xmloff/inc/fasttokenhandler.hxx | 3 +-- xmloff/source/core/xmlimp.cxx | 3 +-- 18 files changed, 54 insertions(+), 61 deletions(-) diff --git a/editeng/source/misc/SvXMLAutoCorrectTokenHandler.hxx b/editeng/source/misc/SvXMLAutoCorrectTokenHandler.hxx index a6d858119ae0..69030fd38ab4 100644 --- a/editeng/source/misc/SvXMLAutoCorrectTokenHandler.hxx +++ b/editeng/source/misc/SvXMLAutoCorrectTokenHandler.hxx @@ -30,8 +30,7 @@ enum SvXMLAutoCorrectToken : sal_Int32 NAME = FastToken::NAMESPACE | XML_NAMESPACE_BLOCKLIST | XML_NAME //66737 }; -class SvXMLAutoCorrectTokenHandler : public - cppu::WeakImplHelper< css::xml::sax::XFastTokenHandler >, +class SvXMLAutoCorrectTokenHandler : public sax_fastparser::FastTokenHandlerBase { public: diff --git a/include/oox/core/fasttokenhandler.hxx b/include/oox/core/fasttokenhandler.hxx index 62f260fd5f5d..a89186c5218c 100644 --- a/include/oox/core/fasttokenhandler.hxx +++ b/include/oox/core/fasttokenhandler.hxx @@ -40,9 +40,7 @@ namespace core { that provides access to the tokens generated from the internal token name list. */ class OOX_DLLPUBLIC FastTokenHandler final : - public ::cppu::WeakImplHelper< css::lang::XServiceInfo, - css::xml::sax::XFastTokenHandler >, - public sax_fastparser::FastTokenHandlerBase + public cppu::ImplInheritanceHelper< sax_fastparser::FastTokenHandlerBase, css::lang::XServiceInfo > { public: explicit FastTokenHandler(); diff --git a/include/sax/fastattribs.hxx b/include/sax/fastattribs.hxx index 839f4c2b8cfd..60090f88f2f5 100644 --- a/include/sax/fastattribs.hxx +++ b/include/sax/fastattribs.hxx @@ -21,6 +21,7 @@ #define INCLUDED_SAX_FASTATTRIBS_HXX #include +#include #include #include @@ -49,7 +50,8 @@ struct UnknownAttribute typedef std::vector< UnknownAttribute > UnknownAttributeList; /// A native C++ interface to tokenisation -class SAX_DLLPUBLIC FastTokenHandlerBase +class SAX_DLLPUBLIC FastTokenHandlerBase : + public cppu::WeakImplHelper< css::xml::sax::XFastTokenHandler > { public: virtual ~FastTokenHandlerBase(); @@ -57,24 +59,21 @@ class SAX_DLLPUBLIC FastTokenHandlerBase /** * Client method to attempt the use of this interface if possible. - * @xTokenHandler - the UNO handle for the token lookup interface - * @pTokenHandler - a dynamic_cast version of @xTokenHandler to this interface + * @xTokenHandler - the token lookup interface * @pStr - string buffer to lookup * @nLength - optional length of chars in that buffer * * @return Tokenized form of pStr */ static sal_Int32 getTokenFromChars( - const css::uno::Reference &xTokenHandler, - const FastTokenHandlerBase *pTokenHandler /* can be NULL */, + const FastTokenHandlerBase *pTokenHandler, const char *pStr, size_t nLength ); }; class SAX_DLLPUBLIC FastAttributeList final : public cppu::WeakImplHelper< css::xml::sax::XFastAttributeList > { public: - FastAttributeList( const css::uno::Reference< css::xml::sax::XFastTokenHandler >& xTokenHandler, - FastTokenHandlerBase *pOptHandlerBase = nullptr ); + FastAttributeList( FastTokenHandlerBase *pTokenHandler ); virtual ~FastAttributeList() override; void clear(); @@ -208,8 +207,7 @@ private: std::vector< sal_Int32 > maAttributeValues; std::vector< sal_Int32 > maAttributeTokens; UnknownAttributeList maUnknownAttributes; - css::uno::Reference< css::xml::sax::XFastTokenHandler > mxTokenHandler; - FastTokenHandlerBase *mpTokenHandler; + FastTokenHandlerBase * mpTokenHandler; }; } diff --git a/oox/source/core/fasttokenhandler.cxx b/oox/source/core/fasttokenhandler.cxx index a57be30e0c2c..55b0389b8d18 100644 --- a/oox/source/core/fasttokenhandler.cxx +++ b/oox/source/core/fasttokenhandler.cxx @@ -76,7 +76,7 @@ extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface* com_sun_star_comp_oox_core_FastTokenHandler_get_implementation( uno::XComponentContext* /*pCtx*/, uno::Sequence const& /*rSeq*/) { - return cppu::acquire(new oox::core::FastTokenHandler()); + return cppu::acquire(static_cast(new oox::core::FastTokenHandler())); } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/oox/source/crypto/AgileEngine.cxx b/oox/source/crypto/AgileEngine.cxx index eb2257f94f5f..d8c184caa46c 100644 --- a/oox/source/crypto/AgileEngine.cxx +++ b/oox/source/crypto/AgileEngine.cxx @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -48,7 +49,7 @@ OUString stripNamespacePrefix(OUString const & rsInputName) return rsInputName.copy(rsInputName.indexOf(":") + 1); } -class AgileTokenHandler : public cppu::WeakImplHelper +class AgileTokenHandler : public sax_fastparser::FastTokenHandlerBase { public: virtual sal_Int32 SAL_CALL getTokenFromUTF8(const Sequence< sal_Int8 >& /*nIdentifier*/) override @@ -60,6 +61,11 @@ public: { return Sequence(); } + + virtual sal_Int32 getTokenDirect( const char * /* pToken */, sal_Int32 /* nLength */ ) const override + { + return -1; + } }; class AgileDocumentHandler : public ::cppu::WeakImplHelper diff --git a/sax/CppunitTest_sax_parser.mk b/sax/CppunitTest_sax_parser.mk index 4c1fe31015bd..59a90fd13f67 100644 --- a/sax/CppunitTest_sax_parser.mk +++ b/sax/CppunitTest_sax_parser.mk @@ -22,6 +22,7 @@ $(eval $(call gb_CppunitTest_use_libraries,sax_parser, \ unotest \ expwrap \ sal \ + sax \ test \ )) diff --git a/sax/qa/cppunit/parser.cxx b/sax/qa/cppunit/parser.cxx index 2fb3b400d000..e5fd9e2ab23c 100644 --- a/sax/qa/cppunit/parser.cxx +++ b/sax/qa/cppunit/parser.cxx @@ -16,6 +16,7 @@ #include #include +#include #include #include @@ -24,7 +25,7 @@ using namespace css::xml::sax; namespace { -class DummyTokenHandler : public cppu::WeakImplHelper< xml::sax::XFastTokenHandler > +class DummyTokenHandler : public sax_fastparser::FastTokenHandlerBase { public: DummyTokenHandler() {} @@ -38,6 +39,10 @@ public: CPPUNIT_ASSERT_MESSAGE( "getUTF8Identifier: unexpected call", false ); return uno::Sequence(); } + virtual sal_Int32 getTokenDirect( const char * /* pToken */, sal_Int32 /* nLength */ ) const override + { + return -1; + } }; class ParserTest: public test::BootstrapFixture diff --git a/sax/qa/cppunit/xmlimport.cxx b/sax/qa/cppunit/xmlimport.cxx index 91c955982746..449c9ef3b38c 100644 --- a/sax/qa/cppunit/xmlimport.cxx +++ b/sax/qa/cppunit/xmlimport.cxx @@ -251,8 +251,7 @@ void SAL_CALL NSDocumentHandler::startElement( const OUString& aName, const Refe CPPUNIT_ASSERT(false); } -class DummyTokenHandler : public cppu::WeakImplHelper< XFastTokenHandler >, - public sax_fastparser::FastTokenHandlerBase +class DummyTokenHandler : public sax_fastparser::FastTokenHandlerBase { public: const static OString tokens[]; diff --git a/sax/source/fastparser/fastparser.cxx b/sax/source/fastparser/fastparser.cxx index bca3f502defc..1c4d5531a747 100644 --- a/sax/source/fastparser/fastparser.cxx +++ b/sax/source/fastparser/fastparser.cxx @@ -121,8 +121,7 @@ struct SaxContext struct ParserData { css::uno::Reference< css::xml::sax::XFastDocumentHandler > mxDocumentHandler; - css::uno::Reference< css::xml::sax::XFastTokenHandler > mxTokenHandler; - FastTokenHandlerBase* mpTokenHandler; + rtl::Reference mxTokenHandler; css::uno::Reference< css::xml::sax::XErrorHandler > mxErrorHandler; css::uno::Reference< css::xml::sax::XFastNamespaceHandler >mxNamespaceHandler; @@ -370,7 +369,6 @@ OUString SAL_CALL FastLocatorImpl::getSystemId() } ParserData::ParserData() - : mpTokenHandler( nullptr ) {} Entity::Entity(const ParserData& rData) @@ -662,8 +660,7 @@ void FastSaxParserImpl::DefineNamespace( const OString& rPrefix, const OUString& sal_Int32 FastSaxParserImpl::GetToken( const xmlChar* pName, sal_Int32 nameLen /* = 0 */ ) { - return FastTokenHandlerBase::getTokenFromChars( getEntity().mxTokenHandler, - getEntity().mpTokenHandler, + return FastTokenHandlerBase::getTokenFromChars( getEntity(). mxTokenHandler.get(), XML_CAST( pName ), nameLen ); // uses utf-8 } @@ -883,8 +880,8 @@ void FastSaxParserImpl::setFastDocumentHandler( const Reference< XFastDocumentHa void FastSaxParserImpl::setTokenHandler( const Reference< XFastTokenHandler >& xHandler ) { - maData.mxTokenHandler = xHandler; - maData.mpTokenHandler = dynamic_cast< FastTokenHandlerBase *>( xHandler.get() ); + assert( dynamic_cast< FastTokenHandlerBase *>( xHandler.get() ) && "we expect this handler to be a subclass of FastTokenHandlerBase" ); + maData.mxTokenHandler = dynamic_cast< FastTokenHandlerBase *>( xHandler.get() ); } void FastSaxParserImpl::registerNamespace( const OUString& NamespaceURL, sal_Int32 NamespaceToken ) @@ -1118,8 +1115,7 @@ void FastSaxParserImpl::callbackStartElement(const xmlChar *localName , const xm } else rEvent.mxAttributes.set( - new FastAttributeList( rEntity.mxTokenHandler, - rEntity.mpTokenHandler ) ); + new FastAttributeList( rEntity.mxTokenHandler.get() ) ); if( rEntity.mxNamespaceHandler.is() ) { @@ -1130,8 +1126,7 @@ void FastSaxParserImpl::callbackStartElement(const xmlChar *localName , const xm } else rEvent.mxDeclAttributes.set( - new FastAttributeList( rEntity.mxTokenHandler, - rEntity.mpTokenHandler ) ); + new FastAttributeList( rEntity.mxTokenHandler.get() ) ); } OUString sNamespace; diff --git a/sax/source/tools/fastattribs.cxx b/sax/source/tools/fastattribs.cxx index 587749938c6b..3df391c86150 100644 --- a/sax/source/tools/fastattribs.cxx +++ b/sax/source/tools/fastattribs.cxx @@ -54,10 +54,8 @@ void UnknownAttribute::FillAttribute( Attribute* pAttrib ) const } } -FastAttributeList::FastAttributeList( const css::uno::Reference< css::xml::sax::XFastTokenHandler >& xTokenHandler, - sax_fastparser::FastTokenHandlerBase *pTokenHandler) -: mxTokenHandler( xTokenHandler ), - mpTokenHandler( pTokenHandler ) +FastAttributeList::FastAttributeList( sax_fastparser::FastTokenHandlerBase *pTokenHandler) +: mpTokenHandler( pTokenHandler ) { // random initial size of buffer to store attribute values mnChunkLength = 58; @@ -140,7 +138,7 @@ sal_Int32 FastAttributeList::getValueToken( ::sal_Int32 Token ) for (size_t i = 0; i < maAttributeTokens.size(); ++i) if (maAttributeTokens[i] == Token) return FastTokenHandlerBase::getTokenFromChars( - mxTokenHandler, mpTokenHandler, + mpTokenHandler, getFastAttributeValue(i), AttributeValueLength( i ) ); @@ -152,7 +150,7 @@ sal_Int32 FastAttributeList::getOptionalValueToken( ::sal_Int32 Token, ::sal_Int for (size_t i = 0; i < maAttributeTokens.size(); ++i) if (maAttributeTokens[i] == Token) return FastTokenHandlerBase::getTokenFromChars( - mxTokenHandler, mpTokenHandler, + mpTokenHandler, getFastAttributeValue(i), AttributeValueLength( i ) ); @@ -265,7 +263,6 @@ FastAttributeList::FastAttributeIter FastAttributeList::find( sal_Int32 nToken ) } sal_Int32 FastTokenHandlerBase::getTokenFromChars( - const css::uno::Reference< css::xml::sax::XFastTokenHandler > &xTokenHandler, const FastTokenHandlerBase *pTokenHandler, const char *pToken, size_t nLen /* = 0 */ ) { @@ -274,14 +271,7 @@ sal_Int32 FastTokenHandlerBase::getTokenFromChars( if( !nLen ) nLen = strlen( pToken ); - if( pTokenHandler ) - nRet = pTokenHandler->getTokenDirect( pToken, static_cast(nLen) ); - else - { - // heap allocate, copy & then free - Sequence< sal_Int8 > aSeq( reinterpret_cast(pToken), nLen ); - nRet = xTokenHandler->getTokenFromUTF8( aSeq ); - } + nRet = pTokenHandler->getTokenDirect( pToken, static_cast(nLen) ); return nRet; } diff --git a/sax/source/tools/fshelper.cxx b/sax/source/tools/fshelper.cxx index 0d265fe36d24..41857c95ef19 100644 --- a/sax/source/tools/fshelper.cxx +++ b/sax/source/tools/fshelper.cxx @@ -149,7 +149,7 @@ void FastSerializerHelper::mergeTopMarks( FastAttributeList * FastSerializerHelper::createAttrList() { - return new FastAttributeList( Reference< xml::sax::XFastTokenHandler >() ); + return new FastAttributeList( nullptr ); } diff --git a/sw/source/core/inc/SwXMLBlockImport.hxx b/sw/source/core/inc/SwXMLBlockImport.hxx index cfc2f50ba7d0..d58ce4bf149a 100644 --- a/sw/source/core/inc/SwXMLBlockImport.hxx +++ b/sw/source/core/inc/SwXMLBlockImport.hxx @@ -85,8 +85,7 @@ enum SwXMLTextBlockToken : sal_Int32 TEXT_P = FastToken::NAMESPACE | XML_NAMESPACE_TEXT | XML_P }; -class SwXMLTextBlockTokenHandler : public - cppu::WeakImplHelper< css::xml::sax::XFastTokenHandler >, +class SwXMLTextBlockTokenHandler : public sax_fastparser::FastTokenHandlerBase { public: @@ -112,8 +111,7 @@ enum SwXMLBlockListToken : sal_Int32 UNFORMATTED_TEXT = FastToken::NAMESPACE | XML_NAMESPACE_BLOCKLIST | XML_UNFORMATTED_TEXT }; -class SwXMLBlockListTokenHandler : public - cppu::WeakImplHelper< css::xml::sax::XFastTokenHandler >, +class SwXMLBlockListTokenHandler : public sax_fastparser::FastTokenHandlerBase { public: diff --git a/unoxml/CppunitTest_unoxml_domtest.mk b/unoxml/CppunitTest_unoxml_domtest.mk index 530624b0cb41..e6a6b2959d67 100644 --- a/unoxml/CppunitTest_unoxml_domtest.mk +++ b/unoxml/CppunitTest_unoxml_domtest.mk @@ -34,6 +34,7 @@ $(eval $(call gb_CppunitTest_use_libraries,unoxml_domtest, \ cppu \ cppuhelper \ sal \ + sax \ test \ unotest \ )) diff --git a/unoxml/inc/node.hxx b/unoxml/inc/node.hxx index a6fae4ac2745..f91a08421c13 100644 --- a/unoxml/inc/node.hxx +++ b/unoxml/inc/node.hxx @@ -50,13 +50,13 @@ namespace DOM struct Context { Context( const css::uno::Reference< css::xml::sax::XFastDocumentHandler >& i_xHandler, - const css::uno::Reference< css::xml::sax::XFastTokenHandler >& i_xTokenHandler ) : + sax_fastparser::FastTokenHandlerBase* pTokenHandler ) : maNamespaces( 1, std::vector() ), maNamespaceMap(101), - mxAttribList(new sax_fastparser::FastAttributeList(i_xTokenHandler)), + mxAttribList(new sax_fastparser::FastAttributeList(pTokenHandler)), mxCurrentHandler(i_xHandler), mxDocHandler(i_xHandler), - mxTokenHandler(i_xTokenHandler) + mxTokenHandler(pTokenHandler) {} struct Namespace @@ -76,7 +76,7 @@ namespace DOM ::rtl::Reference mxAttribList; css::uno::Reference mxCurrentHandler; css::uno::Reference mxDocHandler; - css::uno::Reference mxTokenHandler; + rtl::Reference mxTokenHandler; }; void pushContext(Context& io_rContext); diff --git a/unoxml/qa/unit/domtest.cxx b/unoxml/qa/unit/domtest.cxx index 31909dba6e29..b6e468defdb5 100644 --- a/unoxml/qa/unit/domtest.cxx +++ b/unoxml/qa/unit/domtest.cxx @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -168,8 +169,7 @@ struct DocumentHandler } }; -struct TokenHandler - : public ::cppu::WeakImplHelper< xml::sax::XFastTokenHandler > +struct TokenHandler : public sax_fastparser::FastTokenHandlerBase { virtual ::sal_Int32 SAL_CALL getTokenFromUTF8( const uno::Sequence< ::sal_Int8 >& Identifier ) override { @@ -182,6 +182,11 @@ struct TokenHandler false ); return uno::Sequence(); } + + virtual sal_Int32 getTokenDirect( const char * /* pToken */, sal_Int32 /* nLength */ ) const override + { + return -1; + } }; struct BasicTest : public test::BootstrapFixture diff --git a/unoxml/source/dom/document.cxx b/unoxml/source/dom/document.cxx index d74a913217d0..68f88442e4f5 100644 --- a/unoxml/source/dom/document.cxx +++ b/unoxml/source/dom/document.cxx @@ -1008,7 +1008,7 @@ namespace DOM } Context aContext(i_xHandler, - i_xTokenHandler); + dynamic_cast(i_xTokenHandler.get())); // register namespace ids for (const beans::Pair& rNs : i_rRegisterNamespaces) diff --git a/xmloff/inc/fasttokenhandler.hxx b/xmloff/inc/fasttokenhandler.hxx index b0d0fb9f92ce..8e5a1cbb1c6e 100644 --- a/xmloff/inc/fasttokenhandler.hxx +++ b/xmloff/inc/fasttokenhandler.hxx @@ -69,8 +69,7 @@ private: struct StaticTokenMap : public rtl::Static< TokenMap, StaticTokenMap > {}; -class FastTokenHandler final : public cppu::WeakImplHelper< - css::xml::sax::XFastTokenHandler >, +class FastTokenHandler final : public sax_fastparser::FastTokenHandlerBase { public: diff --git a/xmloff/source/core/xmlimp.cxx b/xmloff/source/core/xmlimp.cxx index 9dae9839b519..8cee683d86e5 100644 --- a/xmloff/source/core/xmlimp.cxx +++ b/xmloff/source/core/xmlimp.cxx @@ -2215,8 +2215,7 @@ OUString SvXMLImportFastNamespaceHandler::getNamespaceURI( const OUString&/* rNa SvXMLLegacyToFastDocHandler::SvXMLLegacyToFastDocHandler( const rtl::Reference< SvXMLImport > & rImport ) : mrImport( rImport ), - mxFastAttributes( new sax_fastparser::FastAttributeList( SvXMLImport::xTokenHandler.get(), - dynamic_cast< sax_fastparser::FastTokenHandlerBase *>( SvXMLImport::xTokenHandler.get() ) ) ) + mxFastAttributes( new sax_fastparser::FastAttributeList( SvXMLImport::xTokenHandler.get() ) ) { }