summaryrefslogtreecommitdiff
path: root/ucb/source/inc/regexpmap.hxx
blob: f28c2e58ce063443bb505f735f9811c72ba8d726 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#ifndef INCLUDED_UCB_SOURCE_INC_REGEXPMAP_HXX
#define INCLUDED_UCB_SOURCE_INC_REGEXPMAP_HXX

#include <sal/config.h>

#include <vector>
#include <memory>

#include <rtl/ustring.hxx>
#include <sal/types.h>

#include "regexp.hxx"

namespace ucb_impl {

template< typename Val > class RegexpMap;
template< typename Val > class RegexpMapIter;


template< typename Val >
class RegexpMapEntry
{
public:
    RegexpMapEntry(OUString const & rTheRegexp,
                          Val * pTheValue):
        m_aRegexp(rTheRegexp), m_pValue(pTheValue) {}

    const OUString& getRegexp() const { return m_aRegexp; }

    Val const & getValue() const { return *m_pValue; }

    Val & getValue() { return *m_pValue; }

private:
    OUString m_aRegexp;
    Val * m_pValue;
};


template< typename Val >
struct Entry
{
    Regexp m_aRegexp;
    Val m_aValue;

    Entry(Regexp const & rTheRegexp, Val const & rTheValue):
        m_aRegexp(rTheRegexp), m_aValue(rTheValue) {}
};


template< typename Val >
class RegexpMapConstIter
{
    friend class RegexpMap< Val >; // to access m_pImpl, ctor
    friend class RegexpMapIter< Val >; // to access m_pImpl, ctor

public:
    typedef typename std::vector< Entry< Val > >::iterator ListIterator;

    RegexpMapConstIter();

    RegexpMapConstIter(RegexpMap< Val > * pTheMap, bool bBegin);

    RegexpMapConstIter(RegexpMap< Val > * pTheMap, int nTheList,
                             ListIterator aTheIndex);

    RegexpMapConstIter(RegexpMapConstIter const & rOther);

    RegexpMapConstIter & operator =(RegexpMapConstIter const & rOther);

    RegexpMapConstIter & operator ++();

    RegexpMapEntry< Val > const * operator ->() const;

    bool equals(RegexpMapConstIter const & rOther) const;
        // for free operator ==(), operator !=()

protected:
    RegexpMapEntry< Val > & get() const;

private:
    mutable RegexpMapEntry< Val > m_aEntry;
    typename std::vector< Entry< Val > >::iterator m_aIndex;
    RegexpMap< Val > * m_pMap;
    int m_nList;
    mutable bool m_bEntrySet;
};

template< typename Val >
RegexpMapConstIter< Val >::RegexpMapConstIter():
    m_aEntry(OUString(), 0),
    m_pMap(nullptr),
    m_nList(-1),
    m_bEntrySet(false)
{}

template< typename Val >
RegexpMapConstIter< Val >::RegexpMapConstIter(RegexpMap< Val > * pTheMap,
                                            bool bBegin):
    m_aEntry(OUString(), 0),
    m_pMap(pTheMap),
    m_bEntrySet(false)
{
    if (bBegin)
    {
        m_nList = -1;
        if (!m_pMap->m_pDefault)
            operator++();
    }
    else
    {
        m_nList = Regexp::KIND_DOMAIN;
        m_aIndex = m_pMap->m_aList[Regexp::KIND_DOMAIN].end();
    }
}

template< typename Val >
inline RegexpMapConstIter< Val >::RegexpMapConstIter(RegexpMap< Val > * pTheMap,
                                                   int nTheList,
                                                   ListIterator aTheIndex):
    m_aEntry(OUString(), 0),
    m_aIndex(aTheIndex),
    m_pMap(pTheMap),
    m_nList(nTheList),
    m_bEntrySet(false)
{}

template< typename Val >
RegexpMapConstIter< Val >::RegexpMapConstIter(RegexpMapConstIter const &
                                                  rOther):
    m_aEntry(rOther.m_aEntry), m_pMap(rOther.m_pMap), m_nList(rOther.m_nList),
    m_bEntrySet(rOther.m_bEntrySet)
{
    if (m_nList != -1)
        m_aIndex = rOther.m_aIndex;
}

template< typename Val >
RegexpMapConstIter< Val > &
RegexpMapConstIter< Val >::operator =(RegexpMapConstIter const & rOther)
{
    if (this != &rOther)
    {
        m_aEntry = rOther.m_aEntry;
        m_pMap = rOther.m_pMap;
        m_nList = rOther.m_nList;
        m_bEntrySet = rOther.m_bEntrySet;
        if (m_nList == -1)
            m_aIndex = typename std::vector< Entry<Val> >::iterator();
        else
            m_aIndex = rOther.m_aIndex;
    }
    return *this;
}

template< typename Val >
RegexpMapConstIter< Val > & RegexpMapConstIter< Val >::operator ++()
{
    switch (m_nList)
    {
        case Regexp::KIND_DOMAIN:
            if (m_aIndex == m_pMap->m_aList[m_nList].end())
                return *this;
            [[fallthrough]];
        default:
            ++m_aIndex;
            if (m_nList == Regexp::KIND_DOMAIN
                || m_aIndex != m_pMap->m_aList[m_nList].end())
                break;
            [[fallthrough]];
        case -1:
            do
            {
                ++m_nList;
                m_aIndex = m_pMap->m_aList[m_nList].begin();
            }
            while (m_nList < Regexp::KIND_DOMAIN
                   && m_aIndex == m_pMap->m_aList[m_nList].end());
            break;
    }
    m_bEntrySet = false;
    return *this;
}

template< typename Val >
RegexpMapEntry< Val > & RegexpMapConstIter< Val >::get() const
{
    if (!m_bEntrySet)
    {
        Entry< Val > const & rTheEntry
            = m_nList == -1 ? *m_pMap->m_pDefault : *m_aIndex;
        m_aEntry
            = RegexpMapEntry< Val >(rTheEntry.m_aRegexp.getRegexp(),
                                    const_cast< Val * >(&rTheEntry.m_aValue));
        m_bEntrySet = true;
    }
    return m_aEntry;
}

template< typename Val >
RegexpMapEntry< Val > const * RegexpMapConstIter< Val >::operator ->() const
{
    return &get();
}

template< typename Val >
bool RegexpMapConstIter< Val >::equals(RegexpMapConstIter const & rOther)
    const
{
    return m_pMap == rOther.m_pMap
           && m_nList == rOther.m_nList
           && (m_nList == -1 || m_aIndex == rOther.m_aIndex);
}


template< typename Val >
class RegexpMapIter: public RegexpMapConstIter< Val >
{
    friend class RegexpMap< Val >; // to access ctor

public:
    RegexpMapIter() {}

    RegexpMapIter(RegexpMap< Val > * pTheMap, bool bBegin):
        RegexpMapConstIter<Val>(pTheMap, bBegin)
    {}

    RegexpMapIter(RegexpMap< Val > * pTheMap, int nTheList, typename RegexpMapConstIter< Val >::ListIterator aTheIndex):
        RegexpMapConstIter<Val>(pTheMap, nTheList, aTheIndex)
    {}

    RegexpMapEntry< Val > * operator ->();

    RegexpMapEntry< Val > const * operator ->() const;
};

template< typename Val >
RegexpMapEntry< Val > * RegexpMapIter< Val >::operator ->()
{
    return &RegexpMapConstIter<Val>::get();
}

template< typename Val >
RegexpMapEntry< Val > const * RegexpMapIter< Val >::operator ->() const
{
    return &RegexpMapConstIter<Val>::get();
}


template< typename Val >
class RegexpMap
{
friend class RegexpMapConstIter<Val>;
public:
    typedef sal_uInt32 size_type;
    typedef RegexpMapIter< Val > iterator;
    typedef RegexpMapConstIter< Val > const_iterator;

    void add(OUString const & rKey, Val const & rValue);

    iterator find(OUString const & rKey);

    void erase(iterator const & rPos);

    iterator begin();

    const_iterator begin() const;

    iterator end();

    const_iterator end() const;

    size_type size() const;

    Val const * map(OUString const & rString) const;

private:
    std::vector< Entry<Val> > m_aList[Regexp::KIND_DOMAIN + 1];
    std::unique_ptr<Entry< Val >> m_pDefault;
};

template< typename Val >
void RegexpMap< Val >::add(OUString const & rKey, Val const & rValue)
{
    Regexp aRegexp(Regexp::parse(rKey));

    if (aRegexp.isDefault())
    {
        if (m_pDefault)
        {
            return;
        }
        m_pDefault.reset( new Entry< Val >(aRegexp, rValue) );
    }
    else
    {
        std::vector< Entry<Val> > & rTheList = m_aList[aRegexp.getKind()];

        for (auto const& elem : rTheList)
        {
            if (elem.m_aRegexp == aRegexp)
            {
               return;
            }
        }

        rTheList.push_back(Entry< Val >(aRegexp, rValue));
    }
}

template< typename Val >
typename RegexpMap< Val >::iterator RegexpMap< Val >::find(OUString const & rKey)
{
    Regexp aRegexp(Regexp::parse(rKey));

    if (aRegexp.isDefault())
    {
        if (m_pDefault)
            return RegexpMapIter< Val >(this, true);
    }
    else
    {
        std::vector< Entry<Val> > & rTheList = m_aList[aRegexp.getKind()];

        typename std::vector< Entry<Val> >::iterator aEnd(rTheList.end());
        for (typename std::vector< Entry<Val> >::iterator aIt(rTheList.begin()); aIt != aEnd; ++aIt)
            if (aIt->m_aRegexp == aRegexp)
                return RegexpMapIter< Val >(this, aRegexp.getKind(), aIt);
    }

    return RegexpMapIter< Val >(this, false);
}

template< typename Val >
void RegexpMap< Val >::erase(iterator const & rPos)
{
    assert(rPos.m_pMap == this);
    if (rPos.m_pMap == this)
    {
        if (rPos.m_nList == -1)
        {
            m_pDefault.reset();
        }
        else
            m_aList[rPos.m_nList].erase(rPos.m_aIndex);
    }
}

template< typename Val >
typename RegexpMap< Val >::iterator RegexpMap< Val >::begin()
{
    return RegexpMapIter< Val >(this, true);
}

template< typename Val >
typename RegexpMap< Val >::const_iterator RegexpMap< Val >::begin() const
{
    return RegexpMapConstIter< Val >(this, true);
}

template< typename Val >
typename RegexpMap< Val >::iterator RegexpMap< Val >::end()
{
    return RegexpMapIter< Val >(this, false);
}

template< typename Val >
typename RegexpMap< Val >::const_iterator RegexpMap< Val >::end() const
{
    return RegexpMapConstIter< Val >(this, false);
}

template< typename Val >
typename RegexpMap< Val >::size_type RegexpMap< Val >::size() const
{
    return (m_pDefault ? 1 : 0)
               + m_aList[Regexp::KIND_PREFIX].size()
               + m_aList[Regexp::KIND_AUTHORITY].size()
               + m_aList[Regexp::KIND_DOMAIN].size();
}

template< typename Val >
Val const * RegexpMap< Val >::map(OUString const & rString) const
{
    for (int n = Regexp::KIND_DOMAIN; n >= Regexp::KIND_PREFIX; --n)
    {
        std::vector< Entry<Val> > const & rTheList = m_aList[n];

        for (auto const & rItem : rTheList)
            if (rItem.m_aRegexp.matches(rString))
                return &rItem.m_aValue;
    }
    if (m_pDefault
        && m_pDefault->m_aRegexp.matches(rString))
        return &m_pDefault->m_aValue;
    return 0;
}

}


template< typename Val >
inline bool operator ==(ucb_impl::RegexpMapConstIter< Val > const & rIter1,
                        ucb_impl::RegexpMapConstIter< Val > const & rIter2)
{
    return rIter1.equals(rIter2);
}

template< typename Val >
inline bool operator !=(ucb_impl::RegexpMapConstIter< Val > const & rIter1,
                        ucb_impl::RegexpMapConstIter< Val > const & rIter2)
{
    return !rIter1.equals(rIter2);
}

#endif // INCLUDED_UCB_SOURCE_INC_REGEXPMAP_HXX

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */