/src/libreoffice/oox/source/token/tokenmap.cxx
Line | Count | Source (jump to first uncovered line) |
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 | | * This file incorporates work covered by the following license notice: |
10 | | * |
11 | | * Licensed to the Apache Software Foundation (ASF) under one or more |
12 | | * contributor license agreements. See the NOTICE file distributed |
13 | | * with this work for additional information regarding copyright |
14 | | * ownership. The ASF licenses this file to you under the Apache |
15 | | * License, Version 2.0 (the "License"); you may not use this file |
16 | | * except in compliance with the License. You may obtain a copy of |
17 | | * the License at http://www.apache.org/licenses/LICENSE-2.0 . |
18 | | */ |
19 | | |
20 | | #include <sal/config.h> |
21 | | |
22 | | #include <array> |
23 | | |
24 | | #include <oox/token/tokenmap.hxx> |
25 | | |
26 | | #include <string.h> |
27 | | #include <rtl/string.hxx> |
28 | | #include <oox/token/tokens.hxx> |
29 | | |
30 | | namespace oox { |
31 | | |
32 | | namespace { |
33 | | // include auto-generated Perfect_Hash |
34 | | #if defined __clang__ |
35 | | #pragma GCC diagnostic push |
36 | | #pragma GCC diagnostic ignored "-Wimplicit-fallthrough" |
37 | | #if __has_warning("-Wdeprecated-register") |
38 | | #pragma GCC diagnostic ignored "-Wdeprecated-register" |
39 | | #endif |
40 | | #endif |
41 | | #include <tokenhash.inc> |
42 | | #if defined __clang__ |
43 | | #pragma GCC diagnostic pop |
44 | | #endif |
45 | | } // namespace |
46 | | |
47 | | static sal_Int32 getTokenPerfectHash(const char* pStr, sal_Int32 nLength) |
48 | 26.8M | { |
49 | 26.8M | const struct xmltoken* pToken = Perfect_Hash::in_word_set( pStr, nLength ); |
50 | 26.8M | return pToken ? pToken->nToken : XML_TOKEN_INVALID; |
51 | 26.8M | } |
52 | | |
53 | | css::uno::Sequence<sal_Int8> const& TokenMap::getUtf8TokenName(sal_Int32 nToken) |
54 | 1.92k | { |
55 | 1.92k | static const auto saTokenNames = []() |
56 | 1.92k | { |
57 | 3 | static constexpr std::string_view sppcTokenNames[] = { |
58 | | // include auto-generated C array with token names as C strings |
59 | 3 | #include <tokennames.inc> |
60 | 3 | }; |
61 | 3 | static_assert(std::size(sppcTokenNames) == XML_TOKEN_COUNT); |
62 | | |
63 | 3 | std::vector<css::uno::Sequence<sal_Int8>> aTokenNames; |
64 | 3 | aTokenNames.reserve(std::size(sppcTokenNames)); |
65 | 3 | std::transform( |
66 | 3 | std::begin(sppcTokenNames), std::end(sppcTokenNames), std::back_inserter(aTokenNames), |
67 | 3 | [](auto aUtf8Token) |
68 | 17.8k | { |
69 | 17.8k | return css::uno::Sequence<sal_Int8>( |
70 | 17.8k | reinterpret_cast<const sal_Int8*>(aUtf8Token.data()), aUtf8Token.size()); |
71 | 17.8k | }); |
72 | 3 | return aTokenNames; |
73 | 3 | }(); |
74 | | |
75 | 1.92k | SAL_WARN_IF(nToken < 0 || nToken >= XML_TOKEN_COUNT, "oox", "Wrong nToken parameter"); |
76 | 1.92k | if (0 <= nToken && nToken < XML_TOKEN_COUNT) |
77 | 1.92k | return saTokenNames[nToken]; |
78 | 0 | static const css::uno::Sequence<sal_Int8> EMPTY_BYTE_SEQ; |
79 | 0 | return EMPTY_BYTE_SEQ; |
80 | 1.92k | } |
81 | | |
82 | | |
83 | | /** Returns the token identifier for a UTF8 string passed in pToken */ |
84 | | sal_Int32 TokenMap::getTokenFromUtf8(std::string_view token) |
85 | 36.7M | { |
86 | 36.7M | static const auto snAlphaTokens = []() |
87 | 36.7M | { |
88 | 3 | std::array<sal_Int32, 26> nAlphaTokens{}; |
89 | 81 | for (char c = 'a'; c <= 'z'; c++) |
90 | 78 | nAlphaTokens[c - 'a'] = getTokenPerfectHash(&c, 1); |
91 | 3 | return nAlphaTokens; |
92 | 3 | }(); |
93 | | |
94 | | // 50% of OOXML tokens are primarily 1 lower-case character, a-z |
95 | 36.7M | if (token.size() == 1) |
96 | 10.6M | { |
97 | 10.6M | char c = token[0]; |
98 | 10.6M | if (c >= 'a' && c <= 'z') |
99 | 9.88M | return snAlphaTokens[c - 'a']; |
100 | 10.6M | } |
101 | 26.8M | return getTokenPerfectHash(token.data(), token.size()); |
102 | 36.7M | } |
103 | | |
104 | | } // namespace oox |
105 | | |
106 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |