1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
28 #include <rtl/ustring.hxx>
29 #include <sal/types.h>
35 template< typename Val
> class RegexpMap
;
36 template< typename Val
> class RegexpMapIter
;
39 template< typename Val
>
43 RegexpMapEntry(OUString
const & rTheRegexp
,
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
; }
59 template< typename Val
>
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
77 typedef typename
std::vector
< Entry
< Val
> >::iterator ListIterator
;
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 !=()
98 RegexpMapEntry
< Val
> & get() const;
101 mutable RegexpMapEntry
< Val
> m_aEntry
;
102 typename
std::vector
< Entry
< Val
> >::iterator m_aIndex
;
103 RegexpMap
< Val
> * m_pMap
;
105 mutable bool m_bEntrySet
;
108 template< typename Val
>
109 RegexpMapConstIter
< Val
>::RegexpMapConstIter():
110 m_aEntry(OUString(), 0),
116 template< typename Val
>
117 RegexpMapConstIter
< Val
>::RegexpMapConstIter(RegexpMap
< Val
> * pTheMap
,
119 m_aEntry(OUString(), 0),
126 if (!m_pMap
->m_pDefault
)
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
,
139 ListIterator aTheIndex
):
140 m_aEntry(OUString(), 0),
147 template< typename Val
>
148 RegexpMapConstIter
< Val
>::RegexpMapConstIter(RegexpMapConstIter
const &
150 m_aEntry(rOther
.m_aEntry
), m_pMap(rOther
.m_pMap
), m_nList(rOther
.m_nList
),
151 m_bEntrySet(rOther
.m_bEntrySet
)
154 m_aIndex
= rOther
.m_aIndex
;
157 template< typename Val
>
158 RegexpMapConstIter
< Val
> &
159 RegexpMapConstIter
< Val
>::operator =(RegexpMapConstIter
const & 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
;
168 m_aIndex
= typename
std::vector
< Entry
<Val
> >::iterator();
170 m_aIndex
= rOther
.m_aIndex
;
175 template< typename Val
>
176 RegexpMapConstIter
< Val
> & RegexpMapConstIter
< Val
>::operator ++()
180 case Regexp::KIND_DOMAIN
:
181 if (m_aIndex
== m_pMap
->m_aList
[m_nList
].end())
186 if (m_nList
== Regexp::KIND_DOMAIN
187 || m_aIndex
!= m_pMap
->m_aList
[m_nList
].end())
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());
204 template< typename Val
>
205 RegexpMapEntry
< Val
> & RegexpMapConstIter
< Val
>::get() const
209 Entry
< Val
> const & rTheEntry
210 = m_nList
== -1 ? *m_pMap
->m_pDefault
: *m_aIndex
;
212 = RegexpMapEntry
< Val
>(rTheEntry
.m_aRegexp
.getRegexp(),
213 const_cast< Val
* >(&rTheEntry
.m_aValue
));
219 template< typename Val
>
220 RegexpMapEntry
< Val
> const * RegexpMapConstIter
< Val
>::operator ->() const
225 template< typename Val
>
226 bool RegexpMapConstIter
< Val
>::equals(RegexpMapConstIter
const & rOther
)
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
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
>
272 friend class RegexpMapConstIter
<Val
>;
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
);
286 const_iterator
begin() const;
290 const_iterator
end() const;
292 size_type
size() const;
294 Val
const * map(OUString
const & rString
) const;
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())
312 m_pDefault
.reset( new Entry
< Val
>(aRegexp
, rValue
) );
316 std::vector
< Entry
<Val
> > & rTheList
= m_aList
[aRegexp
.getKind()];
318 for (auto const& elem
: rTheList
)
320 if (elem
.m_aRegexp
== aRegexp
)
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())
338 return RegexpMapIter
< Val
>(this, true);
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)
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
;
413 && m_pDefault
->m_aRegexp
.matches(rString
))
414 return &m_pDefault
->m_aValue
;
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: */