LibreOffice Module xmloff (master)  1
fasttokenhandler.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  */
9 
10 #include <fasttokenhandler.hxx>
11 
12 #include <xmloff/token/tokens.hxx>
13 
14 namespace xmloff {
15 
16 namespace {
17 // include auto-generated Perfect_Hash
18 #if defined __clang__
19 #pragma GCC diagnostic push
20 #pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
21 #if __has_warning("-Wdeprecated-register")
22 #pragma GCC diagnostic ignored "-Wdeprecated-register"
23 #endif
24 #endif
25 #include <tokenhash.inc>
26 #if defined __clang__
27 #pragma GCC diagnostic pop
28 #endif
29 } // namespace
30 
31 namespace token {
32 
33 using namespace css;
34 
36 {
37  static TokenMap SINGLETON;
38  return SINGLETON;
39 }
40 
41 const css::uno::Sequence< sal_Int8 > TokenMap::EMPTY_BYTE_SEQ;
42 const OUString TokenMap::EMPTY_STRING;
43 
45  maTokenNamesUtf8( static_cast< size_t >( XML_TOKEN_COUNT ) ),
46  maTokenNames( static_cast< size_t >( XML_TOKEN_COUNT ) )
47 {
48  static const char* sppcTokenNames[] =
49  {
50 #include <tokennames.inc>
51  ""
52  };
53 
54  const char* const* ppcTokenName = sppcTokenNames;
55  int i = 0;
56  for( auto& rTokenName : maTokenNamesUtf8 )
57  {
58  OString aUtf8Token( *ppcTokenName );
59  rTokenName = uno::Sequence< sal_Int8 >( reinterpret_cast< const sal_Int8* >(
60  aUtf8Token.getStr() ), aUtf8Token.getLength() );
61  maTokenNames[i++] = OUString( aUtf8Token.getStr(), aUtf8Token.getLength(), RTL_TEXTENCODING_UTF8 );
62  ++ppcTokenName;
63  }
64 }
65 
67 {
68 }
69 
70 sal_Int32 TokenMap::getTokenPerfectHash( const char *pStr, sal_Int32 nLength )
71 {
72  const struct xmltoken *pToken = Perfect_Hash::in_word_set( pStr, nLength );
73  return pToken ? pToken->nToken : xmloff::XML_TOKEN_INVALID;
74 }
75 
77  mrTokenMap( StaticTokenMap() )
78 {
79 }
80 
82 {
83 }
84 
85 // XFastTokenHandler
86 uno::Sequence< sal_Int8 > FastTokenHandler::getUTF8Identifier( sal_Int32 nToken )
87 {
88  return mrTokenMap.getUtf8TokenName( nToken );
89 }
90 
91 const OUString& FastTokenHandler::getIdentifier( sal_Int32 nToken ) const
92 {
93  return mrTokenMap.getTokenName( nToken );
94 }
95 
96 sal_Int32 FastTokenHandler::getTokenFromUTF8( const uno::Sequence< sal_Int8 >& rIdentifier )
97 {
98  return TokenMap::getTokenFromUtf8( rIdentifier );
99 }
100 
101 // Much faster direct C++ shortcut
102 sal_Int32 FastTokenHandler::getTokenDirect( const char* pToken, sal_Int32 nLength ) const
103 {
104  return TokenMap::getTokenFromUTF8( pToken, nLength );
105 }
106 
107 } // namespace token
108 } // namespace xmloff
109 
110 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Sequence< sal_Int8 > const & getUtf8TokenName(sal_Int32 nToken) const
Returns the UTF-8 name of the passed token identifier as byte sequence.
signed char sal_Int8
static sal_Int32 getTokenPerfectHash(const char *pToken, sal_Int32 nLength)
static const OUString EMPTY_STRING
static const css::uno::Sequence< sal_Int8 > EMPTY_BYTE_SEQ
const OUString & getTokenName(sal_Int32 nToken) const
virtual sal_Int32 getTokenDirect(const char *pToken, sal_Int32 nLength) const override
virtual sal_Int32 SAL_CALL getTokenFromUTF8(const css::uno::Sequence< sal_Int8 > &Identifier) override
static sal_Int32 getTokenFromUtf8(const css::uno::Sequence< sal_Int8 > &rUtf8Name)
Returns the token identifier for the passed UTF-8 token name.
int i
std::vector< css::uno::Sequence< sal_Int8 > > maTokenNamesUtf8
const OUString & getIdentifier(sal_Int32 nToken) const
virtual css::uno::Sequence< sal_Int8 > SAL_CALL getUTF8Identifier(sal_Int32 nToken) override
std::vector< OUString > maTokenNames
sal_Int32 nLength
Definition: xmltoken.cxx:38
TokenMap & StaticTokenMap()
static sal_Int32 getTokenFromUTF8(const char *pToken, sal_Int32 nLength)
Returns the token identifier for a UTF-8 string.