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 .
22 #include <sal/config.h>
27 #include <rtl/ustring.hxx>
28 #include <sal/types.h>
34 template< typename Val
> class RegexpMap
;
35 template< typename Val
> class RegexpMapIter
;
38 template< typename Val
>
42 RegexpMapEntry(OUString aTheRegexp
,
44 m_aRegexp(std::move(aTheRegexp
)), m_pValue(pTheValue
) {}
46 const OUString
& getRegexp() const { return m_aRegexp
; }
48 Val
const & getValue() const { return *m_pValue
; }
50 Val
& getValue() { return *m_pValue
; }
58 template< typename Val
>
64 Entry(Regexp aTheRegexp
, Val aTheValue
):
65 m_aRegexp(std::move(aTheRegexp
)), m_aValue(std::move(aTheValue
)) {}
69 template< typename Val
>
70 class RegexpMapConstIter
72 friend class RegexpMap
< Val
>; // to access m_pImpl, ctor
73 friend class RegexpMapIter
< Val
>; // to access m_pImpl, ctor
76 typedef typename
std::vector
< Entry
< Val
> >::iterator ListIterator
;
80 RegexpMapConstIter(RegexpMap
< Val
> * pTheMap
, bool bBegin
);
82 RegexpMapConstIter(RegexpMap
< Val
> * pTheMap
, int nTheList
,
83 ListIterator aTheIndex
);
85 RegexpMapConstIter(RegexpMapConstIter
const & rOther
);
87 RegexpMapConstIter
& operator =(RegexpMapConstIter
const & rOther
);
89 RegexpMapConstIter
& operator ++();
91 RegexpMapEntry
< Val
> const * operator ->() const;
93 bool equals(RegexpMapConstIter
const & rOther
) const;
94 // for free operator ==(), operator !=()
97 RegexpMapEntry
< Val
> & get() const;
100 mutable RegexpMapEntry
< Val
> m_aEntry
;
101 typename
std::vector
< Entry
< Val
> >::iterator m_aIndex
;
102 RegexpMap
< Val
> * m_pMap
;
104 mutable bool m_bEntrySet
;
107 template< typename Val
>
108 RegexpMapConstIter
< Val
>::RegexpMapConstIter():
109 m_aEntry(OUString(), nullptr),
115 template< typename Val
>
116 RegexpMapConstIter
< Val
>::RegexpMapConstIter(RegexpMap
< Val
> * pTheMap
,
118 m_aEntry(OUString(), nullptr),
125 if (!m_pMap
->m_pDefault
)
130 m_nList
= Regexp::KIND_DOMAIN
;
131 m_aIndex
= m_pMap
->m_aList
[Regexp::KIND_DOMAIN
].end();
135 template< typename Val
>
136 inline RegexpMapConstIter
< Val
>::RegexpMapConstIter(RegexpMap
< Val
> * pTheMap
,
138 ListIterator aTheIndex
):
139 m_aEntry(OUString(), nullptr),
140 m_aIndex(std::move(aTheIndex
)),
146 template< typename Val
>
147 RegexpMapConstIter
< Val
>::RegexpMapConstIter(RegexpMapConstIter
const &
149 m_aEntry(rOther
.m_aEntry
), m_pMap(rOther
.m_pMap
), m_nList(rOther
.m_nList
),
150 m_bEntrySet(rOther
.m_bEntrySet
)
153 m_aIndex
= rOther
.m_aIndex
;
156 template< typename Val
>
157 RegexpMapConstIter
< Val
> &
158 RegexpMapConstIter
< Val
>::operator =(RegexpMapConstIter
const & rOther
)
162 m_aEntry
= rOther
.m_aEntry
;
163 m_pMap
= rOther
.m_pMap
;
164 m_nList
= rOther
.m_nList
;
165 m_bEntrySet
= rOther
.m_bEntrySet
;
167 m_aIndex
= typename
std::vector
< Entry
<Val
> >::iterator();
169 m_aIndex
= rOther
.m_aIndex
;
174 template< typename Val
>
175 RegexpMapConstIter
< Val
> & RegexpMapConstIter
< Val
>::operator ++()
179 case Regexp::KIND_DOMAIN
:
180 if (m_aIndex
== m_pMap
->m_aList
[m_nList
].end())
185 if (m_nList
== Regexp::KIND_DOMAIN
186 || m_aIndex
!= m_pMap
->m_aList
[m_nList
].end())
193 m_aIndex
= m_pMap
->m_aList
[m_nList
].begin();
195 while (m_nList
< Regexp::KIND_DOMAIN
196 && m_aIndex
== m_pMap
->m_aList
[m_nList
].end());
203 template< typename Val
>
204 RegexpMapEntry
< Val
> & RegexpMapConstIter
< Val
>::get() const
208 Entry
< Val
> const & rTheEntry
209 = m_nList
== -1 ? *m_pMap
->m_pDefault
: *m_aIndex
;
211 = RegexpMapEntry
< Val
>(rTheEntry
.m_aRegexp
.getRegexp(),
212 const_cast< Val
* >(&rTheEntry
.m_aValue
));
218 template< typename Val
>
219 RegexpMapEntry
< Val
> const * RegexpMapConstIter
< Val
>::operator ->() const
224 template< typename Val
>
225 bool RegexpMapConstIter
< Val
>::equals(RegexpMapConstIter
const & rOther
)
228 return m_pMap
== rOther
.m_pMap
229 && m_nList
== rOther
.m_nList
230 && (m_nList
== -1 || m_aIndex
== rOther
.m_aIndex
);
234 template< typename Val
>
235 class RegexpMapIter
: public RegexpMapConstIter
< Val
>
237 friend class RegexpMap
< Val
>; // to access ctor
242 RegexpMapIter(RegexpMap
< Val
> * pTheMap
, bool bBegin
):
243 RegexpMapConstIter
<Val
>(pTheMap
, bBegin
)
246 RegexpMapIter(RegexpMap
< Val
> * pTheMap
, int nTheList
, typename RegexpMapConstIter
< Val
>::ListIterator aTheIndex
):
247 RegexpMapConstIter
<Val
>(pTheMap
, nTheList
, aTheIndex
)
250 RegexpMapEntry
< Val
> * operator ->();
252 RegexpMapEntry
< Val
> const * operator ->() const;
255 template< typename Val
>
256 RegexpMapEntry
< Val
> * RegexpMapIter
< Val
>::operator ->()
258 return &RegexpMapConstIter
<Val
>::get();
261 template< typename Val
>
262 RegexpMapEntry
< Val
> const * RegexpMapIter
< Val
>::operator ->() const
264 return &RegexpMapConstIter
<Val
>::get();
268 template< typename Val
>
271 friend class RegexpMapConstIter
<Val
>;
273 typedef sal_uInt32 size_type
;
274 typedef RegexpMapIter
< Val
> iterator
;
275 typedef RegexpMapConstIter
< Val
> const_iterator
;
277 void add(OUString
const & rKey
, Val
const & rValue
);
279 iterator
find(OUString
const & rKey
);
281 void erase(iterator
const & rPos
);
285 const_iterator
begin() const;
289 const_iterator
end() const;
291 size_type
size() const;
293 Val
const * map(OUString
const & rString
) const;
296 std::vector
< Entry
<Val
> > m_aList
[Regexp::KIND_DOMAIN
+ 1];
297 std::unique_ptr
<Entry
< Val
>> m_pDefault
;
300 template< typename Val
>
301 void RegexpMap
< Val
>::add(OUString
const & rKey
, Val
const & rValue
)
303 Regexp
aRegexp(Regexp::parse(rKey
));
305 if (aRegexp
.isDefault())
311 m_pDefault
.reset( new Entry
< Val
>(std::move(aRegexp
), rValue
) );
315 std::vector
< Entry
<Val
> > & rTheList
= m_aList
[aRegexp
.getKind()];
317 for (auto const& elem
: rTheList
)
319 if (elem
.m_aRegexp
== aRegexp
)
325 rTheList
.push_back(Entry
< Val
>(std::move(aRegexp
), rValue
));
329 template< typename Val
>
330 typename RegexpMap
< Val
>::iterator RegexpMap
< Val
>::find(OUString
const & rKey
)
332 Regexp
aRegexp(Regexp::parse(rKey
));
334 if (aRegexp
.isDefault())
337 return RegexpMapIter
< Val
>(this, true);
341 std::vector
< Entry
<Val
> > & rTheList
= m_aList
[aRegexp
.getKind()];
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
);
349 return RegexpMapIter
< Val
>(this, false);
352 template< typename Val
>
353 void RegexpMap
< Val
>::erase(iterator
const & rPos
)
355 assert(rPos
.m_pMap
== this);
356 if (rPos
.m_pMap
== this)
358 if (rPos
.m_nList
== -1)
363 m_aList
[rPos
.m_nList
].erase(rPos
.m_aIndex
);
367 template< typename Val
>
368 typename RegexpMap
< Val
>::iterator RegexpMap
< Val
>::begin()
370 return RegexpMapIter
< Val
>(this, true);
373 template< typename Val
>
374 typename RegexpMap
< Val
>::const_iterator RegexpMap
< Val
>::begin() const
376 return RegexpMapConstIter
< Val
>(this, true);
379 template< typename Val
>
380 typename RegexpMap
< Val
>::iterator RegexpMap
< Val
>::end()
382 return RegexpMapIter
< Val
>(this, false);
385 template< typename Val
>
386 typename RegexpMap
< Val
>::const_iterator RegexpMap
< Val
>::end() const
388 return RegexpMapConstIter
< Val
>(this, false);
391 template< typename Val
>
392 typename RegexpMap
< Val
>::size_type RegexpMap
< Val
>::size() const
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();
400 template< typename Val
>
401 Val
const * RegexpMap
< Val
>::map(OUString
const & rString
) const
403 for (int n
= Regexp::KIND_DOMAIN
; n
>= Regexp::KIND_PREFIX
; --n
)
405 std::vector
< Entry
<Val
> > const & rTheList
= m_aList
[n
];
407 for (auto const & rItem
: rTheList
)
408 if (rItem
.m_aRegexp
.matches(rString
))
409 return &rItem
.m_aValue
;
412 && m_pDefault
->m_aRegexp
.matches(rString
))
413 return &m_pDefault
->m_aValue
;
420 template< typename Val
>
421 inline bool operator ==(ucb_impl::RegexpMapConstIter
< Val
> const & rIter1
,
422 ucb_impl::RegexpMapConstIter
< Val
> const & rIter2
)
424 return rIter1
.equals(rIter2
);
427 template< typename Val
>
428 inline bool operator !=(ucb_impl::RegexpMapConstIter
< Val
> const & rIter1
,
429 ucb_impl::RegexpMapConstIter
< Val
> const & rIter2
)
431 return !rIter1
.equals(rIter2
);
434 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */