Coverage Report

Created: 2026-08-14 10:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sax/source/tools/fastattribs.cxx
Line
Count
Source
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 <algorithm>
21
22
#include <com/sun/star/xml/sax/SAXException.hpp>
23
#include <sax/fastattribs.hxx>
24
#include <utility>
25
26
using namespace ::com::sun::star::uno;
27
using namespace ::com::sun::star::xml;
28
using namespace ::com::sun::star::xml::sax;
29
namespace sax_fastparser
30
{
31
32
// wastage to keep MSVC happy vs. an in-line {}
33
FastTokenHandlerBase::~FastTokenHandlerBase()
34
185k
{
35
185k
}
36
37
UnknownAttribute::UnknownAttribute( OUString aNamespaceURL, OString aName, OString value )
38
749k
    : maNamespaceURL(std::move( aNamespaceURL )), maName(std::move( aName )), maValue(std::move( value ))
39
749k
{
40
749k
}
41
42
UnknownAttribute::UnknownAttribute( OString aName, OString value )
43
667k
    : maName(std::move( aName )), maValue(std::move( value ))
44
667k
{
45
667k
}
46
47
void UnknownAttribute::FillAttribute( Attribute* pAttrib ) const
48
721k
{
49
721k
    if( pAttrib )
50
721k
    {
51
721k
        pAttrib->Name = OStringToOUString( maName, RTL_TEXTENCODING_UTF8 );
52
721k
        pAttrib->NamespaceURL = maNamespaceURL;
53
721k
        pAttrib->Value = OStringToOUString( maValue, RTL_TEXTENCODING_UTF8 );
54
721k
    }
55
721k
}
56
57
FastAttributeList::FastAttributeList( sax_fastparser::FastTokenHandlerBase *pTokenHandler)
58
172k
: mpTokenHandler( pTokenHandler )
59
172k
{
60
    // random initial size of buffer to store attribute values
61
172k
    mnChunkLength = 58;
62
172k
    mpChunk = static_cast<char *>(malloc( mnChunkLength ));
63
172k
    maAttributeValues.push_back( 0 );
64
172k
}
65
66
FastAttributeList::FastAttributeList( const css::uno::Reference< css::xml::sax::XFastAttributeList > & xAttrList )
67
93.9k
{
68
93.9k
    const auto& rOther = castToFastAttributeList(xAttrList);
69
93.9k
    mpTokenHandler = rOther.mpTokenHandler;
70
93.9k
    mpChunk = static_cast<char *>(malloc( rOther.mnChunkLength ));
71
93.9k
    mnChunkLength = rOther.mnChunkLength;
72
93.9k
    memcpy(mpChunk, rOther.mpChunk, rOther.mnChunkLength);
73
93.9k
    maAttributeValues = rOther.maAttributeValues;
74
93.9k
    maAttributeTokens = rOther.maAttributeTokens;
75
93.9k
    maUnknownAttributes = rOther.maUnknownAttributes;
76
93.9k
}
77
78
css::uno::Reference< ::css::util::XCloneable > FastAttributeList::createClone()
79
790
{
80
790
    return new FastAttributeList(this);
81
790
}
82
83
FastAttributeList::~FastAttributeList()
84
266k
{
85
266k
    free( mpChunk );
86
266k
}
87
88
void FastAttributeList::clear()
89
35.1M
{
90
35.1M
    maAttributeTokens.clear();
91
35.1M
    maAttributeValues.resize(1);
92
35.1M
    assert(maAttributeValues[0] == 0);
93
35.1M
    maUnknownAttributes.clear();
94
35.1M
}
95
96
void FastAttributeList::add( sal_Int32 nToken, std::string_view value )
97
17.2M
{
98
17.2M
    assert(nToken != -1);
99
17.2M
    assert(nToken != 0);
100
17.2M
    assert(value.length() < SAL_MAX_INT32); // protect against absurd values
101
17.2M
    maAttributeTokens.push_back( nToken );
102
17.2M
    sal_Int32 nWritePosition = maAttributeValues.back();
103
17.2M
    maAttributeValues.push_back( maAttributeValues.back() + value.length() + 1 );
104
17.2M
    if (maAttributeValues.back() > mnChunkLength)
105
92.8k
    {
106
92.8k
        const sal_Int32 newLen = std::max(mnChunkLength * 2, maAttributeValues.back());
107
92.8k
        auto p = static_cast<char*>(realloc(mpChunk, newLen));
108
92.8k
        if (!p)
109
0
            throw std::bad_alloc();
110
111
92.8k
        mnChunkLength = newLen;
112
92.8k
        mpChunk = p;
113
114
92.8k
    }
115
17.2M
    memcpy(mpChunk + nWritePosition, value.data(), value.length());
116
17.2M
    mpChunk[nWritePosition + value.length()] = '\0';
117
17.2M
}
118
119
void FastAttributeList::add(sal_Int32 nToken, std::u16string_view sValue)
120
0
{
121
0
    add(nToken, OUStringToOString(sValue, RTL_TEXTENCODING_UTF8));
122
0
}
123
124
void FastAttributeList::addNS( sal_Int32 nNamespaceToken, sal_Int32 nToken, std::string_view rValue )
125
0
{
126
0
    sal_Int32 nCombinedToken = (nNamespaceToken << 16) | nToken;
127
0
    add( nCombinedToken, rValue );
128
0
}
129
130
void FastAttributeList::addNS(sal_Int32 nNamespaceToken, sal_Int32 nToken,
131
                                     std::u16string_view sValue)
132
0
{
133
0
    sal_Int32 nCombinedToken = (nNamespaceToken << 16) | nToken;
134
0
    add(nCombinedToken, sValue);
135
0
}
136
137
void FastAttributeList::addUnknown( const OUString& rNamespaceURL, const OString& rName, const OString& value )
138
749k
{
139
749k
    maUnknownAttributes.emplace_back( rNamespaceURL, rName, value );
140
749k
}
141
142
void FastAttributeList::addUnknown( const OString& rName, const OString& value )
143
667k
{
144
667k
    maUnknownAttributes.emplace_back( rName, value );
145
667k
}
146
147
void FastAttributeList::add( const css::uno::Reference<css::xml::sax::XFastAttributeList>& xAttrList )
148
1.94k
{
149
1.94k
    const auto& rOther = castToFastAttributeList(xAttrList);
150
1.94k
    add(rOther);
151
1.94k
}
152
153
void FastAttributeList::add( const FastAttributeList& rOther )
154
1.94k
{
155
10.4k
    for (size_t i=0; i < rOther.maAttributeTokens.size(); ++i)
156
8.52k
        add(rOther.maAttributeTokens[i], rOther.getAsViewByIndex(i));
157
1.94k
    for (const auto & i : rOther.maUnknownAttributes)
158
3.79k
        addUnknown(i.maNamespaceURL, i.maName, i.maValue);
159
1.94k
}
160
161
// XFastAttributeList
162
sal_Bool FastAttributeList::hasAttribute( ::sal_Int32 Token )
163
10.4M
{
164
10.4M
    for (sal_Int32 i : maAttributeTokens)
165
18.5M
        if (i == Token)
166
1.85M
            return true;
167
168
8.61M
    return false;
169
10.4M
}
170
171
sal_Int32 FastAttributeList::getValueToken( ::sal_Int32 Token )
172
0
{
173
0
    for (size_t i = 0, n = maAttributeTokens.size(); i < n; ++i)
174
0
        if (maAttributeTokens[i] == Token)
175
0
            return getValueTokenByIndex(i);
176
177
0
    throw SAXException("FastAttributeList::getValueToken: unknown token " + OUString::number(Token), nullptr, Any());
178
0
}
179
180
sal_Int32 FastAttributeList::getOptionalValueToken( ::sal_Int32 Token, ::sal_Int32 Default )
181
11.3M
{
182
26.0M
    for (size_t i = 0, n = maAttributeTokens.size(); i < n; ++i)
183
16.7M
        if (maAttributeTokens[i] == Token)
184
2.11M
            return getValueTokenByIndex(i);
185
186
9.24M
    return Default;
187
11.3M
}
188
189
// performance sensitive shortcuts to avoid allocation ...
190
bool FastAttributeList::getAsInteger( sal_Int32 nToken, sal_Int32 &rInt) const
191
7.51M
{
192
7.51M
    rInt = 0;
193
20.1M
    for (size_t i = 0, n = maAttributeTokens.size(); i < n; ++i)
194
16.4M
        if (maAttributeTokens[i] == nToken)
195
3.85M
        {
196
3.85M
            rInt = getAsIntegerByIndex(i);
197
3.85M
            return true;
198
3.85M
        }
199
3.66M
    return false;
200
7.51M
}
201
202
bool FastAttributeList::getAsDouble( sal_Int32 nToken, double &rDouble) const
203
516k
{
204
516k
    rDouble = 0.0;
205
1.31M
    for (size_t i = 0, n = maAttributeTokens.size(); i < n; ++i)
206
1.11M
        if (maAttributeTokens[i] == nToken)
207
312k
        {
208
312k
            rDouble = o3tl::toDouble(getAsViewByIndex(i));
209
312k
            return true;
210
312k
        }
211
204k
    return false;
212
516k
}
213
214
bool FastAttributeList::getAsView( sal_Int32 nToken, std::string_view& rPos ) const
215
1.59M
{
216
1.78M
    for (size_t i = 0, n = maAttributeTokens.size(); i < n; ++i)
217
1.72M
    {
218
1.72M
        if (maAttributeTokens[i] != nToken)
219
185k
            continue;
220
221
1.53M
        rPos = getAsViewByIndex(i);
222
1.53M
        return true;
223
1.72M
    }
224
225
63.6k
    return false;
226
1.59M
}
227
228
OUString FastAttributeList::getValue( ::sal_Int32 Token )
229
32.1k
{
230
35.9k
    for (size_t i = 0, n = maAttributeTokens.size(); i < n; ++i)
231
34.9k
        if (maAttributeTokens[i] == Token)
232
31.1k
            return getValueByIndex(i);
233
234
1.01k
    throw SAXException("FastAttributeList::getValue: unknown token " + OUString::number(Token), nullptr, Any());
235
32.1k
}
236
237
OUString FastAttributeList::getOptionalValue( ::sal_Int32 Token )
238
4.01M
{
239
7.87M
    for (size_t i = 0, n = maAttributeTokens.size(); i < n; ++i)
240
6.68M
        if (maAttributeTokens[i] == Token)
241
2.81M
            return getValueByIndex(i);
242
243
1.19M
    return OUString();
244
4.01M
}
245
246
sal_Int32 FastAttributeList::getValueTokenByIndex(sal_Int32 nTokenIndex) const
247
2.74M
{
248
2.74M
    return FastTokenHandlerBase::getTokenFromChars(mpTokenHandler, getAsViewByIndex(nTokenIndex));
249
2.74M
}
250
251
Sequence< Attribute > FastAttributeList::getUnknownAttributes(  )
252
12.7M
{
253
12.7M
    auto nSize = maUnknownAttributes.size();
254
12.7M
    if (nSize == 0)
255
12.5M
        return {};
256
217k
    Sequence< Attribute > aSeq( nSize );
257
217k
    Attribute* pAttr = aSeq.getArray();
258
217k
    for( const auto& rAttr : maUnknownAttributes )
259
721k
        rAttr.FillAttribute( pAttr++ );
260
217k
    return aSeq;
261
12.7M
}
262
Sequence< FastAttribute > FastAttributeList::getFastAttributes(  )
263
19.5k
{
264
19.5k
    Sequence< FastAttribute > aSeq( maAttributeTokens.size() );
265
19.5k
    FastAttribute* pAttr = aSeq.getArray();
266
32.0k
    for (size_t i = 0, n = maAttributeTokens.size(); i < n; ++i)
267
12.4k
    {
268
12.4k
        pAttr->Token = maAttributeTokens[i];
269
12.4k
        pAttr->Value = getValueByIndex(i);
270
12.4k
        pAttr++;
271
12.4k
    }
272
19.5k
    return aSeq;
273
19.5k
}
274
275
FastAttributeList::FastAttributeIter FastAttributeList::find( sal_Int32 nToken ) const
276
1.38M
{
277
1.59M
    for (size_t i = 0, n = maAttributeTokens.size(); i < n; ++i)
278
225k
        if( maAttributeTokens[i] == nToken )
279
14.2k
            return FastAttributeIter(*this, i);
280
1.37M
    return end();
281
1.38M
}
282
283
sal_Int32 FastTokenHandlerBase::getTokenFromChars(
284
        const FastTokenHandlerBase *pTokenHandler,
285
        std::string_view token )
286
41.9M
{
287
41.9M
    return pTokenHandler->getTokenDirect(token);
288
41.9M
}
289
290
}
291
292
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */