diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 05:54:39 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 05:54:39 +0000 |
commit | 267c6f2ac71f92999e969232431ba04678e7437e (patch) | |
tree | 358c9467650e1d0a1d7227a21dac2e3d08b622b2 /xmloff/inc/fasttokenhandler.hxx | |
parent | Initial commit. (diff) | |
download | libreoffice-267c6f2ac71f92999e969232431ba04678e7437e.tar.xz libreoffice-267c6f2ac71f92999e969232431ba04678e7437e.zip |
Adding upstream version 4:24.2.0.upstream/4%24.2.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'xmloff/inc/fasttokenhandler.hxx')
-rw-r--r-- | xmloff/inc/fasttokenhandler.hxx | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/xmloff/inc/fasttokenhandler.hxx b/xmloff/inc/fasttokenhandler.hxx new file mode 100644 index 0000000000..528ecde175 --- /dev/null +++ b/xmloff/inc/fasttokenhandler.hxx @@ -0,0 +1,91 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#pragma once + +#include <com/sun/star/xml/sax/XFastTokenHandler.hpp> +#include <cppuhelper/implbase.hxx> +#include <sax/fastattribs.hxx> +#include <xmloff/token/tokens.hxx> +#include <sal/log.hxx> +#include <xmloff/dllapi.h> + +namespace xmloff::token { + +class TokenMap +{ +public: + explicit TokenMap(); + ~TokenMap(); + + /** Returns the UTF-8 name of the passed token identifier as byte sequence. */ + css::uno::Sequence< sal_Int8 > const & getUtf8TokenName( sal_Int32 nToken ) const + { + SAL_WARN_IF(nToken < 0 || nToken >= XML_TOKEN_COUNT, "xmloff", "Wrong nToken parameter"); + if( 0 <= nToken && nToken < XML_TOKEN_COUNT ) + return maTokenNamesUtf8[ nToken ]; + return EMPTY_BYTE_SEQ; + } + + const OUString& getTokenName( sal_Int32 nToken ) const + { + SAL_WARN_IF(nToken < 0 || nToken >= XML_TOKEN_COUNT, "xmloff", "Wrong nToken parameter"); + if( 0 <= nToken && nToken < XML_TOKEN_COUNT ) + return maTokenNames[ nToken ]; + return EMPTY_STRING; + } + + /** Returns the token identifier for the passed UTF-8 token name. */ + static sal_Int32 getTokenFromUtf8( const css::uno::Sequence< sal_Int8 >& rUtf8Name ) + { + return getTokenFromUTF8( reinterpret_cast< const char* >( + rUtf8Name.getConstArray() ), rUtf8Name.getLength() ); + } + + /** Returns the token identifier for a UTF-8 string */ + static sal_Int32 getTokenFromUTF8( const char *pToken, sal_Int32 nLength ) + { + return getTokenPerfectHash( pToken, nLength ); + } + +private: + static sal_Int32 getTokenPerfectHash( const char *pToken, sal_Int32 nLength ); + + std::vector< css::uno::Sequence< sal_Int8 > > maTokenNamesUtf8; + std::vector< OUString > maTokenNames; + + static const css::uno::Sequence< sal_Int8 > EMPTY_BYTE_SEQ; + static const OUString EMPTY_STRING; +}; + +TokenMap& StaticTokenMap(); + +class FastTokenHandler final : + public sax_fastparser::FastTokenHandlerBase +{ +public: + explicit FastTokenHandler(); + virtual ~FastTokenHandler() override; + + // XFastTokenHandler + virtual css::uno::Sequence< sal_Int8 > SAL_CALL getUTF8Identifier( sal_Int32 nToken ) override; + virtual sal_Int32 SAL_CALL getTokenFromUTF8( const css::uno::Sequence< sal_Int8 >& Identifier ) override; + + const OUString & getIdentifier( sal_Int32 nToken ) const; + + // Much faster direct C++ shortcut to the method that matters + virtual sal_Int32 getTokenDirect( const char *pToken, sal_Int32 nLength ) const override; + +private: + TokenMap& mrTokenMap; +}; + +} // namespace xmloff::token + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |