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