Coverage Report

Created: 2026-05-16 09:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/ucb/source/inc/regexpmap.hxx
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
#pragma once
21
22
#include <sal/config.h>
23
24
#include <vector>
25
#include <memory>
26
27
#include <rtl/ustring.hxx>
28
#include <sal/types.h>
29
30
#include "regexp.hxx"
31
32
namespace ucb_impl {
33
34
template< typename Val > class RegexpMap;
35
template< typename Val > class RegexpMapIter;
36
37
38
template< typename Val >
39
class RegexpMapEntry
40
{
41
public:
42
    RegexpMapEntry(OUString aTheRegexp,
43
                          Val * pTheValue):
44
36
        m_aRegexp(std::move(aTheRegexp)), m_pValue(pTheValue) {}
45
46
0
    const OUString& getRegexp() const { return m_aRegexp; }
47
48
0
    Val const & getValue() const { return *m_pValue; }
49
50
0
    Val & getValue() { return *m_pValue; }
51
52
private:
53
    OUString m_aRegexp;
54
    Val * m_pValue;
55
};
56
57
58
template< typename Val >
59
struct Entry
60
{
61
    Regexp m_aRegexp;
62
    Val m_aValue;
63
64
    Entry(Regexp aTheRegexp, Val aTheValue):
65
12
        m_aRegexp(std::move(aTheRegexp)), m_aValue(std::move(aTheValue)) {}
66
};
67
68
69
template< typename Val >
70
class RegexpMapConstIter
71
{
72
    friend class RegexpMap< Val >; // to access m_pImpl, ctor
73
    friend class RegexpMapIter< Val >; // to access m_pImpl, ctor
74
75
public:
76
    typedef typename std::vector< Entry< Val > >::iterator ListIterator;
77
78
    RegexpMapConstIter();
79
80
    RegexpMapConstIter(RegexpMap< Val > * pTheMap, bool bBegin);
81
82
    RegexpMapConstIter(RegexpMap< Val > * pTheMap, int nTheList,
83
                             ListIterator aTheIndex);
84
85
    RegexpMapConstIter(RegexpMapConstIter const & rOther);
86
87
    RegexpMapConstIter & operator =(RegexpMapConstIter const & rOther);
88
89
    RegexpMapConstIter & operator ++();
90
91
    RegexpMapEntry< Val > const * operator ->() const;
92
93
    bool equals(RegexpMapConstIter const & rOther) const;
94
        // for free operator ==(), operator !=()
95
96
protected:
97
    RegexpMapEntry< Val > & get() const;
98
99
private:
100
    mutable RegexpMapEntry< Val > m_aEntry;
101
    typename std::vector< Entry< Val > >::iterator m_aIndex;
102
    RegexpMap< Val > * m_pMap;
103
    int m_nList;
104
    mutable bool m_bEntrySet;
105
};
106
107
template< typename Val >
108
RegexpMapConstIter< Val >::RegexpMapConstIter():
109
12
    m_aEntry(OUString(), nullptr),
110
12
    m_pMap(nullptr),
111
12
    m_nList(-1),
112
12
    m_bEntrySet(false)
113
12
{}
114
115
template< typename Val >
116
RegexpMapConstIter< Val >::RegexpMapConstIter(RegexpMap< Val > * pTheMap,
117
                                            bool bBegin):
118
24
    m_aEntry(OUString(), nullptr),
119
24
    m_pMap(pTheMap),
120
24
    m_bEntrySet(false)
121
24
{
122
24
    if (bBegin)
123
0
    {
124
0
        m_nList = -1;
125
0
        if (!m_pMap->m_pDefault)
126
0
            operator++();
127
0
    }
128
24
    else
129
24
    {
130
24
        m_nList = Regexp::KIND_DOMAIN;
131
24
        m_aIndex = m_pMap->m_aList[Regexp::KIND_DOMAIN].end();
132
24
    }
133
24
}
134
135
template< typename Val >
136
inline RegexpMapConstIter< Val >::RegexpMapConstIter(RegexpMap< Val > * pTheMap,
137
                                                   int nTheList,
138
                                                   ListIterator aTheIndex):
139
0
    m_aEntry(OUString(), nullptr),
140
0
    m_aIndex(std::move(aTheIndex)),
141
0
    m_pMap(pTheMap),
142
0
    m_nList(nTheList),
143
0
    m_bEntrySet(false)
144
0
{}
145
146
template< typename Val >
147
RegexpMapConstIter< Val >::RegexpMapConstIter(RegexpMapConstIter const &
148
                                                  rOther):
149
0
    m_aEntry(rOther.m_aEntry), m_pMap(rOther.m_pMap), m_nList(rOther.m_nList),
150
0
    m_bEntrySet(rOther.m_bEntrySet)
151
0
{
152
0
    if (m_nList != -1)
153
0
        m_aIndex = rOther.m_aIndex;
154
0
}
155
156
template< typename Val >
157
RegexpMapConstIter< Val > &
158
RegexpMapConstIter< Val >::operator =(RegexpMapConstIter const & rOther)
159
12
{
160
12
    if (this != &rOther)
161
12
    {
162
12
        m_aEntry = rOther.m_aEntry;
163
12
        m_pMap = rOther.m_pMap;
164
12
        m_nList = rOther.m_nList;
165
12
        m_bEntrySet = rOther.m_bEntrySet;
166
12
        if (m_nList == -1)
167
0
            m_aIndex = typename std::vector< Entry<Val> >::iterator();
168
12
        else
169
12
            m_aIndex = rOther.m_aIndex;
170
12
    }
171
12
    return *this;
172
12
}
173
174
template< typename Val >
175
RegexpMapConstIter< Val > & RegexpMapConstIter< Val >::operator ++()
176
0
{
177
0
    switch (m_nList)
178
0
    {
179
0
        case Regexp::KIND_DOMAIN:
180
0
            if (m_aIndex == m_pMap->m_aList[m_nList].end())
181
0
                return *this;
182
0
            [[fallthrough]];
183
0
        default:
184
0
            ++m_aIndex;
185
0
            if (m_nList == Regexp::KIND_DOMAIN
186
0
                || m_aIndex != m_pMap->m_aList[m_nList].end())
187
0
                break;
188
0
            [[fallthrough]];
189
0
        case -1:
190
0
            do
191
0
            {
192
0
                ++m_nList;
193
0
                m_aIndex = m_pMap->m_aList[m_nList].begin();
194
0
            }
195
0
            while (m_nList < Regexp::KIND_DOMAIN
196
0
                   && m_aIndex == m_pMap->m_aList[m_nList].end());
197
0
            break;
198
0
    }
199
0
    m_bEntrySet = false;
200
0
    return *this;
201
0
}
202
203
template< typename Val >
204
RegexpMapEntry< Val > & RegexpMapConstIter< Val >::get() const
205
0
{
206
0
    if (!m_bEntrySet)
207
0
    {
208
0
        Entry< Val > const & rTheEntry
209
0
            = m_nList == -1 ? *m_pMap->m_pDefault : *m_aIndex;
210
0
        m_aEntry
211
0
            = RegexpMapEntry< Val >(rTheEntry.m_aRegexp.getRegexp(),
212
0
                                    const_cast< Val * >(&rTheEntry.m_aValue));
213
0
        m_bEntrySet = true;
214
0
    }
215
0
    return m_aEntry;
216
0
}
217
218
template< typename Val >
219
RegexpMapEntry< Val > const * RegexpMapConstIter< Val >::operator ->() const
220
0
{
221
0
    return &get();
222
0
}
223
224
template< typename Val >
225
bool RegexpMapConstIter< Val >::equals(RegexpMapConstIter const & rOther)
226
    const
227
12
{
228
12
    return m_pMap == rOther.m_pMap
229
12
           && m_nList == rOther.m_nList
230
12
           && (m_nList == -1 || m_aIndex == rOther.m_aIndex);
231
12
}
232
233
234
template< typename Val >
235
class RegexpMapIter: public RegexpMapConstIter< Val >
236
{
237
    friend class RegexpMap< Val >; // to access ctor
238
239
public:
240
12
    RegexpMapIter() {}
241
242
    RegexpMapIter(RegexpMap< Val > * pTheMap, bool bBegin):
243
24
        RegexpMapConstIter<Val>(pTheMap, bBegin)
244
24
    {}
245
246
    RegexpMapIter(RegexpMap< Val > * pTheMap, int nTheList, typename RegexpMapConstIter< Val >::ListIterator aTheIndex):
247
0
        RegexpMapConstIter<Val>(pTheMap, nTheList, aTheIndex)
248
0
    {}
249
250
    RegexpMapEntry< Val > * operator ->();
251
252
    RegexpMapEntry< Val > const * operator ->() const;
253
};
254
255
template< typename Val >
256
RegexpMapEntry< Val > * RegexpMapIter< Val >::operator ->()
257
0
{
258
0
    return &RegexpMapConstIter<Val>::get();
259
0
}
260
261
template< typename Val >
262
RegexpMapEntry< Val > const * RegexpMapIter< Val >::operator ->() const
263
{
264
    return &RegexpMapConstIter<Val>::get();
265
}
266
267
268
template< typename Val >
269
class RegexpMap
270
{
271
friend class RegexpMapConstIter<Val>;
272
public:
273
    typedef sal_uInt32 size_type;
274
    typedef RegexpMapIter< Val > iterator;
275
    typedef RegexpMapConstIter< Val > const_iterator;
276
277
    void add(OUString const & rKey, Val const & rValue);
278
279
    iterator find(OUString const & rKey);
280
281
    void erase(iterator const & rPos);
282
283
    iterator begin();
284
285
    const_iterator begin() const;
286
287
    iterator end();
288
289
    const_iterator end() const;
290
291
    size_type size() const;
292
293
    Val const * map(OUString const & rString) const;
294
295
private:
296
    std::vector< Entry<Val> > m_aList[Regexp::KIND_DOMAIN + 1];
297
    std::unique_ptr<Entry< Val >> m_pDefault;
298
};
299
300
template< typename Val >
301
void RegexpMap< Val >::add(OUString const & rKey, Val const & rValue)
302
12
{
303
12
    Regexp aRegexp(Regexp::parse(rKey));
304
305
12
    if (aRegexp.isDefault())
306
0
    {
307
0
        if (m_pDefault)
308
0
        {
309
0
            return;
310
0
        }
311
0
        m_pDefault.reset( new Entry< Val >(std::move(aRegexp), rValue) );
312
0
    }
313
12
    else
314
12
    {
315
12
        std::vector< Entry<Val> > & rTheList = m_aList[aRegexp.getKind()];
316
317
12
        for (auto const& elem : rTheList)
318
0
        {
319
0
            if (elem.m_aRegexp == aRegexp)
320
0
            {
321
0
               return;
322
0
            }
323
0
        }
324
325
12
        rTheList.push_back(Entry< Val >(std::move(aRegexp), rValue));
326
12
    }
327
12
}
328
329
template< typename Val >
330
typename RegexpMap< Val >::iterator RegexpMap< Val >::find(OUString const & rKey)
331
12
{
332
12
    Regexp aRegexp(Regexp::parse(rKey));
333
334
12
    if (aRegexp.isDefault())
335
0
    {
336
0
        if (m_pDefault)
337
0
            return RegexpMapIter< Val >(this, true);
338
0
    }
339
12
    else
340
12
    {
341
12
        std::vector< Entry<Val> > & rTheList = m_aList[aRegexp.getKind()];
342
343
12
        typename std::vector< Entry<Val> >::iterator aEnd(rTheList.end());
344
12
        for (typename std::vector< Entry<Val> >::iterator aIt(rTheList.begin()); aIt != aEnd; ++aIt)
345
0
            if (aIt->m_aRegexp == aRegexp)
346
0
                return RegexpMapIter< Val >(this, aRegexp.getKind(), aIt);
347
12
    }
348
349
12
    return RegexpMapIter< Val >(this, false);
350
12
}
351
352
template< typename Val >
353
void RegexpMap< Val >::erase(iterator const & rPos)
354
0
{
355
0
    assert(rPos.m_pMap == this);
356
0
    if (rPos.m_pMap == this)
357
0
    {
358
0
        if (rPos.m_nList == -1)
359
0
        {
360
0
            m_pDefault.reset();
361
0
        }
362
0
        else
363
0
            m_aList[rPos.m_nList].erase(rPos.m_aIndex);
364
0
    }
365
0
}
366
367
template< typename Val >
368
typename RegexpMap< Val >::iterator RegexpMap< Val >::begin()
369
0
{
370
0
    return RegexpMapIter< Val >(this, true);
371
0
}
372
373
template< typename Val >
374
typename RegexpMap< Val >::const_iterator RegexpMap< Val >::begin() const
375
{
376
    return RegexpMapConstIter< Val >(this, true);
377
}
378
379
template< typename Val >
380
typename RegexpMap< Val >::iterator RegexpMap< Val >::end()
381
12
{
382
12
    return RegexpMapIter< Val >(this, false);
383
12
}
384
385
template< typename Val >
386
typename RegexpMap< Val >::const_iterator RegexpMap< Val >::end() const
387
{
388
    return RegexpMapConstIter< Val >(this, false);
389
}
390
391
template< typename Val >
392
typename RegexpMap< Val >::size_type RegexpMap< Val >::size() const
393
0
{
394
0
    return (m_pDefault ? 1 : 0)
395
0
               + m_aList[Regexp::KIND_PREFIX].size()
396
0
               + m_aList[Regexp::KIND_AUTHORITY].size()
397
0
               + m_aList[Regexp::KIND_DOMAIN].size();
398
0
}
399
400
template< typename Val >
401
Val const * RegexpMap< Val >::map(OUString const & rString) const
402
534k
{
403
1.82M
    for (int n = Regexp::KIND_DOMAIN; n >= Regexp::KIND_PREFIX; --n)
404
1.60M
    {
405
1.60M
        std::vector< Entry<Val> > const & rTheList = m_aList[n];
406
407
1.60M
        for (auto const & rItem : rTheList)
408
339k
            if (rItem.m_aRegexp.matches(rString))
409
315k
                return &rItem.m_aValue;
410
1.60M
    }
411
219k
    if (m_pDefault
412
0
        && m_pDefault->m_aRegexp.matches(rString))
413
0
        return &m_pDefault->m_aValue;
414
219k
    return nullptr;
415
219k
}
416
417
}
418
419
420
template< typename Val >
421
inline bool operator ==(ucb_impl::RegexpMapConstIter< Val > const & rIter1,
422
                        ucb_impl::RegexpMapConstIter< Val > const & rIter2)
423
12
{
424
12
    return rIter1.equals(rIter2);
425
12
}
426
427
template< typename Val >
428
inline bool operator !=(ucb_impl::RegexpMapConstIter< Val > const & rIter1,
429
                        ucb_impl::RegexpMapConstIter< Val > const & rIter2)
430
0
{
431
0
    return !rIter1.equals(rIter2);
432
0
}
433
434
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */