nss: upgrade to release 3.73
[LibreOffice.git] / ucb / source / inc / regexpmap.hxx
blobf28c2e58ce063443bb505f735f9811c72ba8d726
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_UCB_SOURCE_INC_REGEXPMAP_HXX
21 #define INCLUDED_UCB_SOURCE_INC_REGEXPMAP_HXX
23 #include <sal/config.h>
25 #include <vector>
26 #include <memory>
28 #include <rtl/ustring.hxx>
29 #include <sal/types.h>
31 #include "regexp.hxx"
33 namespace ucb_impl {
35 template< typename Val > class RegexpMap;
36 template< typename Val > class RegexpMapIter;
39 template< typename Val >
40 class RegexpMapEntry
42 public:
43 RegexpMapEntry(OUString const & rTheRegexp,
44 Val * pTheValue):
45 m_aRegexp(rTheRegexp), m_pValue(pTheValue) {}
47 const OUString& getRegexp() const { return m_aRegexp; }
49 Val const & getValue() const { return *m_pValue; }
51 Val & getValue() { return *m_pValue; }
53 private:
54 OUString m_aRegexp;
55 Val * m_pValue;
59 template< typename Val >
60 struct Entry
62 Regexp m_aRegexp;
63 Val m_aValue;
65 Entry(Regexp const & rTheRegexp, Val const & rTheValue):
66 m_aRegexp(rTheRegexp), m_aValue(rTheValue) {}
70 template< typename Val >
71 class RegexpMapConstIter
73 friend class RegexpMap< Val >; // to access m_pImpl, ctor
74 friend class RegexpMapIter< Val >; // to access m_pImpl, ctor
76 public:
77 typedef typename std::vector< Entry< Val > >::iterator ListIterator;
79 RegexpMapConstIter();
81 RegexpMapConstIter(RegexpMap< Val > * pTheMap, bool bBegin);
83 RegexpMapConstIter(RegexpMap< Val > * pTheMap, int nTheList,
84 ListIterator aTheIndex);
86 RegexpMapConstIter(RegexpMapConstIter const & rOther);
88 RegexpMapConstIter & operator =(RegexpMapConstIter const & rOther);
90 RegexpMapConstIter & operator ++();
92 RegexpMapEntry< Val > const * operator ->() const;
94 bool equals(RegexpMapConstIter const & rOther) const;
95 // for free operator ==(), operator !=()
97 protected:
98 RegexpMapEntry< Val > & get() const;
100 private:
101 mutable RegexpMapEntry< Val > m_aEntry;
102 typename std::vector< Entry< Val > >::iterator m_aIndex;
103 RegexpMap< Val > * m_pMap;
104 int m_nList;
105 mutable bool m_bEntrySet;
108 template< typename Val >
109 RegexpMapConstIter< Val >::RegexpMapConstIter():
110 m_aEntry(OUString(), 0),
111 m_pMap(nullptr),
112 m_nList(-1),
113 m_bEntrySet(false)
116 template< typename Val >
117 RegexpMapConstIter< Val >::RegexpMapConstIter(RegexpMap< Val > * pTheMap,
118 bool bBegin):
119 m_aEntry(OUString(), 0),
120 m_pMap(pTheMap),
121 m_bEntrySet(false)
123 if (bBegin)
125 m_nList = -1;
126 if (!m_pMap->m_pDefault)
127 operator++();
129 else
131 m_nList = Regexp::KIND_DOMAIN;
132 m_aIndex = m_pMap->m_aList[Regexp::KIND_DOMAIN].end();
136 template< typename Val >
137 inline RegexpMapConstIter< Val >::RegexpMapConstIter(RegexpMap< Val > * pTheMap,
138 int nTheList,
139 ListIterator aTheIndex):
140 m_aEntry(OUString(), 0),
141 m_aIndex(aTheIndex),
142 m_pMap(pTheMap),
143 m_nList(nTheList),
144 m_bEntrySet(false)
147 template< typename Val >
148 RegexpMapConstIter< Val >::RegexpMapConstIter(RegexpMapConstIter const &
149 rOther):
150 m_aEntry(rOther.m_aEntry), m_pMap(rOther.m_pMap), m_nList(rOther.m_nList),
151 m_bEntrySet(rOther.m_bEntrySet)
153 if (m_nList != -1)
154 m_aIndex = rOther.m_aIndex;
157 template< typename Val >
158 RegexpMapConstIter< Val > &
159 RegexpMapConstIter< Val >::operator =(RegexpMapConstIter const & rOther)
161 if (this != &rOther)
163 m_aEntry = rOther.m_aEntry;
164 m_pMap = rOther.m_pMap;
165 m_nList = rOther.m_nList;
166 m_bEntrySet = rOther.m_bEntrySet;
167 if (m_nList == -1)
168 m_aIndex = typename std::vector< Entry<Val> >::iterator();
169 else
170 m_aIndex = rOther.m_aIndex;
172 return *this;
175 template< typename Val >
176 RegexpMapConstIter< Val > & RegexpMapConstIter< Val >::operator ++()
178 switch (m_nList)
180 case Regexp::KIND_DOMAIN:
181 if (m_aIndex == m_pMap->m_aList[m_nList].end())
182 return *this;
183 [[fallthrough]];
184 default:
185 ++m_aIndex;
186 if (m_nList == Regexp::KIND_DOMAIN
187 || m_aIndex != m_pMap->m_aList[m_nList].end())
188 break;
189 [[fallthrough]];
190 case -1:
193 ++m_nList;
194 m_aIndex = m_pMap->m_aList[m_nList].begin();
196 while (m_nList < Regexp::KIND_DOMAIN
197 && m_aIndex == m_pMap->m_aList[m_nList].end());
198 break;
200 m_bEntrySet = false;
201 return *this;
204 template< typename Val >
205 RegexpMapEntry< Val > & RegexpMapConstIter< Val >::get() const
207 if (!m_bEntrySet)
209 Entry< Val > const & rTheEntry
210 = m_nList == -1 ? *m_pMap->m_pDefault : *m_aIndex;
211 m_aEntry
212 = RegexpMapEntry< Val >(rTheEntry.m_aRegexp.getRegexp(),
213 const_cast< Val * >(&rTheEntry.m_aValue));
214 m_bEntrySet = true;
216 return m_aEntry;
219 template< typename Val >
220 RegexpMapEntry< Val > const * RegexpMapConstIter< Val >::operator ->() const
222 return &get();
225 template< typename Val >
226 bool RegexpMapConstIter< Val >::equals(RegexpMapConstIter const & rOther)
227 const
229 return m_pMap == rOther.m_pMap
230 && m_nList == rOther.m_nList
231 && (m_nList == -1 || m_aIndex == rOther.m_aIndex);
235 template< typename Val >
236 class RegexpMapIter: public RegexpMapConstIter< Val >
238 friend class RegexpMap< Val >; // to access ctor
240 public:
241 RegexpMapIter() {}
243 RegexpMapIter(RegexpMap< Val > * pTheMap, bool bBegin):
244 RegexpMapConstIter<Val>(pTheMap, bBegin)
247 RegexpMapIter(RegexpMap< Val > * pTheMap, int nTheList, typename RegexpMapConstIter< Val >::ListIterator aTheIndex):
248 RegexpMapConstIter<Val>(pTheMap, nTheList, aTheIndex)
251 RegexpMapEntry< Val > * operator ->();
253 RegexpMapEntry< Val > const * operator ->() const;
256 template< typename Val >
257 RegexpMapEntry< Val > * RegexpMapIter< Val >::operator ->()
259 return &RegexpMapConstIter<Val>::get();
262 template< typename Val >
263 RegexpMapEntry< Val > const * RegexpMapIter< Val >::operator ->() const
265 return &RegexpMapConstIter<Val>::get();
269 template< typename Val >
270 class RegexpMap
272 friend class RegexpMapConstIter<Val>;
273 public:
274 typedef sal_uInt32 size_type;
275 typedef RegexpMapIter< Val > iterator;
276 typedef RegexpMapConstIter< Val > const_iterator;
278 void add(OUString const & rKey, Val const & rValue);
280 iterator find(OUString const & rKey);
282 void erase(iterator const & rPos);
284 iterator begin();
286 const_iterator begin() const;
288 iterator end();
290 const_iterator end() const;
292 size_type size() const;
294 Val const * map(OUString const & rString) const;
296 private:
297 std::vector< Entry<Val> > m_aList[Regexp::KIND_DOMAIN + 1];
298 std::unique_ptr<Entry< Val >> m_pDefault;
301 template< typename Val >
302 void RegexpMap< Val >::add(OUString const & rKey, Val const & rValue)
304 Regexp aRegexp(Regexp::parse(rKey));
306 if (aRegexp.isDefault())
308 if (m_pDefault)
310 return;
312 m_pDefault.reset( new Entry< Val >(aRegexp, rValue) );
314 else
316 std::vector< Entry<Val> > & rTheList = m_aList[aRegexp.getKind()];
318 for (auto const& elem : rTheList)
320 if (elem.m_aRegexp == aRegexp)
322 return;
326 rTheList.push_back(Entry< Val >(aRegexp, rValue));
330 template< typename Val >
331 typename RegexpMap< Val >::iterator RegexpMap< Val >::find(OUString const & rKey)
333 Regexp aRegexp(Regexp::parse(rKey));
335 if (aRegexp.isDefault())
337 if (m_pDefault)
338 return RegexpMapIter< Val >(this, true);
340 else
342 std::vector< Entry<Val> > & rTheList = m_aList[aRegexp.getKind()];
344 typename std::vector< Entry<Val> >::iterator aEnd(rTheList.end());
345 for (typename std::vector< Entry<Val> >::iterator aIt(rTheList.begin()); aIt != aEnd; ++aIt)
346 if (aIt->m_aRegexp == aRegexp)
347 return RegexpMapIter< Val >(this, aRegexp.getKind(), aIt);
350 return RegexpMapIter< Val >(this, false);
353 template< typename Val >
354 void RegexpMap< Val >::erase(iterator const & rPos)
356 assert(rPos.m_pMap == this);
357 if (rPos.m_pMap == this)
359 if (rPos.m_nList == -1)
361 m_pDefault.reset();
363 else
364 m_aList[rPos.m_nList].erase(rPos.m_aIndex);
368 template< typename Val >
369 typename RegexpMap< Val >::iterator RegexpMap< Val >::begin()
371 return RegexpMapIter< Val >(this, true);
374 template< typename Val >
375 typename RegexpMap< Val >::const_iterator RegexpMap< Val >::begin() const
377 return RegexpMapConstIter< Val >(this, true);
380 template< typename Val >
381 typename RegexpMap< Val >::iterator RegexpMap< Val >::end()
383 return RegexpMapIter< Val >(this, false);
386 template< typename Val >
387 typename RegexpMap< Val >::const_iterator RegexpMap< Val >::end() const
389 return RegexpMapConstIter< Val >(this, false);
392 template< typename Val >
393 typename RegexpMap< Val >::size_type RegexpMap< Val >::size() const
395 return (m_pDefault ? 1 : 0)
396 + m_aList[Regexp::KIND_PREFIX].size()
397 + m_aList[Regexp::KIND_AUTHORITY].size()
398 + m_aList[Regexp::KIND_DOMAIN].size();
401 template< typename Val >
402 Val const * RegexpMap< Val >::map(OUString const & rString) const
404 for (int n = Regexp::KIND_DOMAIN; n >= Regexp::KIND_PREFIX; --n)
406 std::vector< Entry<Val> > const & rTheList = m_aList[n];
408 for (auto const & rItem : rTheList)
409 if (rItem.m_aRegexp.matches(rString))
410 return &rItem.m_aValue;
412 if (m_pDefault
413 && m_pDefault->m_aRegexp.matches(rString))
414 return &m_pDefault->m_aValue;
415 return 0;
421 template< typename Val >
422 inline bool operator ==(ucb_impl::RegexpMapConstIter< Val > const & rIter1,
423 ucb_impl::RegexpMapConstIter< Val > const & rIter2)
425 return rIter1.equals(rIter2);
428 template< typename Val >
429 inline bool operator !=(ucb_impl::RegexpMapConstIter< Val > const & rIter1,
430 ucb_impl::RegexpMapConstIter< Val > const & rIter2)
432 return !rIter1.equals(rIter2);
435 #endif // INCLUDED_UCB_SOURCE_INC_REGEXPMAP_HXX
437 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */