1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
10 #include <fasttokenhandler.hxx>
12 #include <xmloff/token/tokens.hxx>
17 // include auto-generated Perfect_Hash
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"
25 #include <tokenhash.inc>
27 #pragma GCC diagnostic pop
35 static sal_Int32
getTokenPerfectHash(const char* pStr
, sal_Int32 nLength
)
37 const struct xmltoken
* pToken
= Perfect_Hash::in_word_set(pStr
, nLength
);
38 return pToken
? pToken
->nToken
: xmloff::XML_TOKEN_INVALID
;
41 static const std::pair
<css::uno::Sequence
<sal_Int8
>, OUString
>& getNames(sal_Int32 nToken
)
43 static const auto saTokenNames
= []()
45 static constexpr std::string_view sppcTokenNames
[] = {
46 #include <tokennames.inc>
48 static_assert(std::size(sppcTokenNames
) == XML_TOKEN_COUNT
);
50 std::vector
<std::pair
<css::uno::Sequence
<sal_Int8
>, OUString
>> names
;
51 names
.reserve(std::size(sppcTokenNames
));
52 std::transform(std::begin(sppcTokenNames
), std::end(sppcTokenNames
),
53 std::back_inserter(names
),
56 return std::make_pair(
57 css::uno::Sequence
<sal_Int8
>(
58 reinterpret_cast<const sal_Int8
*>(token
.data()), token
.size()),
59 OStringToOUString(token
, RTL_TEXTENCODING_UTF8
));
64 SAL_WARN_IF(nToken
< 0 || nToken
>= XML_TOKEN_COUNT
, "xmloff", "Wrong nToken parameter");
65 if (0 <= nToken
&& nToken
< XML_TOKEN_COUNT
)
66 return saTokenNames
[nToken
];
67 static const std::pair
<css::uno::Sequence
<sal_Int8
>, OUString
> EMPTY
;
71 css::uno::Sequence
<sal_Int8
> const& TokenMap::getUtf8TokenName(sal_Int32 nToken
)
73 return getNames(nToken
).first
;
76 sal_Int32
TokenMap::getTokenFromUtf8(std::string_view token
)
78 return getTokenPerfectHash(token
.data(), token
.size());
82 uno::Sequence
< sal_Int8
> FastTokenHandler::getUTF8Identifier( sal_Int32 nToken
)
84 return TokenMap::getUtf8TokenName( nToken
);
87 const OUString
& FastTokenHandler::getIdentifier(sal_Int32 nToken
)
89 return getNames(nToken
).second
;
92 sal_Int32
FastTokenHandler::getTokenFromUTF8( const uno::Sequence
< sal_Int8
>& rIdentifier
)
94 return TokenMap::getTokenFromUtf8(std::string_view(
95 reinterpret_cast<const char*>(rIdentifier
.getConstArray()), rIdentifier
.getLength()));
98 // Much faster direct C++ shortcut
99 sal_Int32
FastTokenHandler::getTokenDirect(std::string_view token
) const
101 return TokenMap::getTokenFromUtf8(token
);
105 } // namespace xmloff
107 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */