Chris@16
|
1 // Boost string_algo library classification.hpp header file ---------------------------//
|
Chris@16
|
2
|
Chris@16
|
3 // Copyright Pavol Droba 2002-2003.
|
Chris@16
|
4 //
|
Chris@16
|
5 // Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
6 // (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
7 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
8
|
Chris@16
|
9 // See http://www.boost.org/ for updates, documentation, and revision history.
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_STRING_CLASSIFICATION_DETAIL_HPP
|
Chris@16
|
12 #define BOOST_STRING_CLASSIFICATION_DETAIL_HPP
|
Chris@16
|
13
|
Chris@16
|
14 #include <boost/algorithm/string/config.hpp>
|
Chris@16
|
15 #include <algorithm>
|
Chris@16
|
16 #include <functional>
|
Chris@16
|
17 #include <locale>
|
Chris@16
|
18
|
Chris@16
|
19 #include <boost/range/begin.hpp>
|
Chris@16
|
20 #include <boost/range/end.hpp>
|
Chris@16
|
21
|
Chris@16
|
22 #include <boost/algorithm/string/predicate_facade.hpp>
|
Chris@16
|
23 #include <boost/type_traits/remove_const.hpp>
|
Chris@16
|
24
|
Chris@16
|
25 namespace boost {
|
Chris@16
|
26 namespace algorithm {
|
Chris@16
|
27 namespace detail {
|
Chris@16
|
28
|
Chris@16
|
29 // classification functors -----------------------------------------------//
|
Chris@16
|
30
|
Chris@16
|
31 // is_classified functor
|
Chris@16
|
32 struct is_classifiedF :
|
Chris@16
|
33 public predicate_facade<is_classifiedF>
|
Chris@16
|
34 {
|
Chris@16
|
35 // Boost.ResultOf support
|
Chris@16
|
36 typedef bool result_type;
|
Chris@16
|
37
|
Chris@16
|
38 // Constructor from a locale
|
Chris@16
|
39 is_classifiedF(std::ctype_base::mask Type, std::locale const & Loc = std::locale()) :
|
Chris@16
|
40 m_Type(Type), m_Locale(Loc) {}
|
Chris@16
|
41 // Operation
|
Chris@16
|
42 template<typename CharT>
|
Chris@16
|
43 bool operator()( CharT Ch ) const
|
Chris@16
|
44 {
|
Chris@16
|
45 return std::use_facet< std::ctype<CharT> >(m_Locale).is( m_Type, Ch );
|
Chris@16
|
46 }
|
Chris@16
|
47
|
Chris@16
|
48 #if defined(__BORLANDC__) && (__BORLANDC__ >= 0x560) && (__BORLANDC__ <= 0x582) && !defined(_USE_OLD_RW_STL)
|
Chris@16
|
49 template<>
|
Chris@16
|
50 bool operator()( char const Ch ) const
|
Chris@16
|
51 {
|
Chris@16
|
52 return std::use_facet< std::ctype<char> >(m_Locale).is( m_Type, Ch );
|
Chris@16
|
53 }
|
Chris@16
|
54 #endif
|
Chris@16
|
55
|
Chris@16
|
56 private:
|
Chris@16
|
57 std::ctype_base::mask m_Type;
|
Chris@16
|
58 std::locale m_Locale;
|
Chris@16
|
59 };
|
Chris@16
|
60
|
Chris@16
|
61
|
Chris@16
|
62 // is_any_of functor
|
Chris@16
|
63 /*
|
Chris@16
|
64 returns true if the value is from the specified set
|
Chris@16
|
65 */
|
Chris@16
|
66 template<typename CharT>
|
Chris@16
|
67 struct is_any_ofF :
|
Chris@16
|
68 public predicate_facade<is_any_ofF<CharT> >
|
Chris@16
|
69 {
|
Chris@16
|
70 private:
|
Chris@16
|
71 // set cannot operate on const value-type
|
Chris@16
|
72 typedef typename ::boost::remove_const<CharT>::type set_value_type;
|
Chris@16
|
73
|
Chris@16
|
74 public:
|
Chris@16
|
75 // Boost.ResultOf support
|
Chris@16
|
76 typedef bool result_type;
|
Chris@16
|
77
|
Chris@16
|
78 // Constructor
|
Chris@16
|
79 template<typename RangeT>
|
Chris@16
|
80 is_any_ofF( const RangeT& Range ) : m_Size(0)
|
Chris@16
|
81 {
|
Chris@16
|
82 // Prepare storage
|
Chris@16
|
83 m_Storage.m_dynSet=0;
|
Chris@16
|
84
|
Chris@16
|
85 std::size_t Size=::boost::distance(Range);
|
Chris@16
|
86 m_Size=Size;
|
Chris@16
|
87 set_value_type* Storage=0;
|
Chris@16
|
88
|
Chris@16
|
89 if(use_fixed_storage(m_Size))
|
Chris@16
|
90 {
|
Chris@16
|
91 // Use fixed storage
|
Chris@16
|
92 Storage=&m_Storage.m_fixSet[0];
|
Chris@16
|
93 }
|
Chris@16
|
94 else
|
Chris@16
|
95 {
|
Chris@16
|
96 // Use dynamic storage
|
Chris@16
|
97 m_Storage.m_dynSet=new set_value_type[m_Size];
|
Chris@16
|
98 Storage=m_Storage.m_dynSet;
|
Chris@16
|
99 }
|
Chris@16
|
100
|
Chris@16
|
101 // Use fixed storage
|
Chris@16
|
102 ::std::copy(::boost::begin(Range), ::boost::end(Range), Storage);
|
Chris@16
|
103 ::std::sort(Storage, Storage+m_Size);
|
Chris@16
|
104 }
|
Chris@16
|
105
|
Chris@16
|
106 // Copy constructor
|
Chris@16
|
107 is_any_ofF(const is_any_ofF& Other) : m_Size(Other.m_Size)
|
Chris@16
|
108 {
|
Chris@16
|
109 // Prepare storage
|
Chris@16
|
110 m_Storage.m_dynSet=0;
|
Chris@16
|
111 const set_value_type* SrcStorage=0;
|
Chris@16
|
112 set_value_type* DestStorage=0;
|
Chris@16
|
113
|
Chris@16
|
114 if(use_fixed_storage(m_Size))
|
Chris@16
|
115 {
|
Chris@16
|
116 // Use fixed storage
|
Chris@16
|
117 DestStorage=&m_Storage.m_fixSet[0];
|
Chris@16
|
118 SrcStorage=&Other.m_Storage.m_fixSet[0];
|
Chris@16
|
119 }
|
Chris@16
|
120 else
|
Chris@16
|
121 {
|
Chris@16
|
122 // Use dynamic storage
|
Chris@16
|
123 m_Storage.m_dynSet=new set_value_type[m_Size];
|
Chris@16
|
124 DestStorage=m_Storage.m_dynSet;
|
Chris@16
|
125 SrcStorage=Other.m_Storage.m_dynSet;
|
Chris@16
|
126 }
|
Chris@16
|
127
|
Chris@16
|
128 // Use fixed storage
|
Chris@16
|
129 ::std::memcpy(DestStorage, SrcStorage, sizeof(set_value_type)*m_Size);
|
Chris@16
|
130 }
|
Chris@16
|
131
|
Chris@16
|
132 // Destructor
|
Chris@16
|
133 ~is_any_ofF()
|
Chris@16
|
134 {
|
Chris@16
|
135 if(!use_fixed_storage(m_Size) && m_Storage.m_dynSet!=0)
|
Chris@16
|
136 {
|
Chris@16
|
137 delete [] m_Storage.m_dynSet;
|
Chris@16
|
138 }
|
Chris@16
|
139 }
|
Chris@16
|
140
|
Chris@16
|
141 // Assignment
|
Chris@16
|
142 is_any_ofF& operator=(const is_any_ofF& Other)
|
Chris@16
|
143 {
|
Chris@16
|
144 // Handle self assignment
|
Chris@16
|
145 if(this==&Other) return *this;
|
Chris@16
|
146
|
Chris@16
|
147 // Prepare storage
|
Chris@16
|
148 const set_value_type* SrcStorage;
|
Chris@16
|
149 set_value_type* DestStorage;
|
Chris@16
|
150
|
Chris@16
|
151 if(use_fixed_storage(Other.m_Size))
|
Chris@16
|
152 {
|
Chris@16
|
153 // Use fixed storage
|
Chris@16
|
154 DestStorage=&m_Storage.m_fixSet[0];
|
Chris@16
|
155 SrcStorage=&Other.m_Storage.m_fixSet[0];
|
Chris@16
|
156
|
Chris@16
|
157 // Delete old storage if was present
|
Chris@16
|
158 if(!use_fixed_storage(m_Size) && m_Storage.m_dynSet!=0)
|
Chris@16
|
159 {
|
Chris@16
|
160 delete [] m_Storage.m_dynSet;
|
Chris@16
|
161 }
|
Chris@16
|
162
|
Chris@16
|
163 // Set new size
|
Chris@16
|
164 m_Size=Other.m_Size;
|
Chris@16
|
165 }
|
Chris@16
|
166 else
|
Chris@16
|
167 {
|
Chris@16
|
168 // Other uses dynamic storage
|
Chris@16
|
169 SrcStorage=Other.m_Storage.m_dynSet;
|
Chris@16
|
170
|
Chris@16
|
171 // Check what kind of storage are we using right now
|
Chris@16
|
172 if(use_fixed_storage(m_Size))
|
Chris@16
|
173 {
|
Chris@16
|
174 // Using fixed storage, allocate new
|
Chris@16
|
175 set_value_type* pTemp=new set_value_type[Other.m_Size];
|
Chris@16
|
176 DestStorage=pTemp;
|
Chris@16
|
177 m_Storage.m_dynSet=pTemp;
|
Chris@16
|
178 m_Size=Other.m_Size;
|
Chris@16
|
179 }
|
Chris@16
|
180 else
|
Chris@16
|
181 {
|
Chris@16
|
182 // Using dynamic storage, check if can reuse
|
Chris@16
|
183 if(m_Storage.m_dynSet!=0 && m_Size>=Other.m_Size && m_Size<Other.m_Size*2)
|
Chris@16
|
184 {
|
Chris@16
|
185 // Reuse the current storage
|
Chris@16
|
186 DestStorage=m_Storage.m_dynSet;
|
Chris@16
|
187 m_Size=Other.m_Size;
|
Chris@16
|
188 }
|
Chris@16
|
189 else
|
Chris@16
|
190 {
|
Chris@16
|
191 // Allocate the new one
|
Chris@16
|
192 set_value_type* pTemp=new set_value_type[Other.m_Size];
|
Chris@16
|
193 DestStorage=pTemp;
|
Chris@16
|
194
|
Chris@16
|
195 // Delete old storage if necessary
|
Chris@16
|
196 if(m_Storage.m_dynSet!=0)
|
Chris@16
|
197 {
|
Chris@16
|
198 delete [] m_Storage.m_dynSet;
|
Chris@16
|
199 }
|
Chris@16
|
200 // Store the new storage
|
Chris@16
|
201 m_Storage.m_dynSet=pTemp;
|
Chris@16
|
202 // Set new size
|
Chris@16
|
203 m_Size=Other.m_Size;
|
Chris@16
|
204 }
|
Chris@16
|
205 }
|
Chris@16
|
206 }
|
Chris@16
|
207
|
Chris@16
|
208 // Copy the data
|
Chris@16
|
209 ::std::memcpy(DestStorage, SrcStorage, sizeof(set_value_type)*m_Size);
|
Chris@16
|
210
|
Chris@16
|
211 return *this;
|
Chris@16
|
212 }
|
Chris@16
|
213
|
Chris@16
|
214 // Operation
|
Chris@16
|
215 template<typename Char2T>
|
Chris@16
|
216 bool operator()( Char2T Ch ) const
|
Chris@16
|
217 {
|
Chris@16
|
218 const set_value_type* Storage=
|
Chris@16
|
219 (use_fixed_storage(m_Size))
|
Chris@16
|
220 ? &m_Storage.m_fixSet[0]
|
Chris@16
|
221 : m_Storage.m_dynSet;
|
Chris@16
|
222
|
Chris@16
|
223 return ::std::binary_search(Storage, Storage+m_Size, Ch);
|
Chris@16
|
224 }
|
Chris@16
|
225 private:
|
Chris@16
|
226 // check if the size is eligible for fixed storage
|
Chris@16
|
227 static bool use_fixed_storage(std::size_t size)
|
Chris@16
|
228 {
|
Chris@16
|
229 return size<=sizeof(set_value_type*)*2;
|
Chris@16
|
230 }
|
Chris@16
|
231
|
Chris@16
|
232
|
Chris@16
|
233 private:
|
Chris@16
|
234 // storage
|
Chris@16
|
235 // The actual used storage is selected on the type
|
Chris@16
|
236 union
|
Chris@16
|
237 {
|
Chris@16
|
238 set_value_type* m_dynSet;
|
Chris@16
|
239 set_value_type m_fixSet[sizeof(set_value_type*)*2];
|
Chris@16
|
240 }
|
Chris@16
|
241 m_Storage;
|
Chris@16
|
242
|
Chris@16
|
243 // storage size
|
Chris@16
|
244 ::std::size_t m_Size;
|
Chris@16
|
245 };
|
Chris@16
|
246
|
Chris@16
|
247 // is_from_range functor
|
Chris@16
|
248 /*
|
Chris@16
|
249 returns true if the value is from the specified range.
|
Chris@16
|
250 (i.e. x>=From && x>=To)
|
Chris@16
|
251 */
|
Chris@16
|
252 template<typename CharT>
|
Chris@16
|
253 struct is_from_rangeF :
|
Chris@16
|
254 public predicate_facade< is_from_rangeF<CharT> >
|
Chris@16
|
255 {
|
Chris@16
|
256 // Boost.ResultOf support
|
Chris@16
|
257 typedef bool result_type;
|
Chris@16
|
258
|
Chris@16
|
259 // Constructor
|
Chris@16
|
260 is_from_rangeF( CharT From, CharT To ) : m_From(From), m_To(To) {}
|
Chris@16
|
261
|
Chris@16
|
262 // Operation
|
Chris@16
|
263 template<typename Char2T>
|
Chris@16
|
264 bool operator()( Char2T Ch ) const
|
Chris@16
|
265 {
|
Chris@16
|
266 return ( m_From <= Ch ) && ( Ch <= m_To );
|
Chris@16
|
267 }
|
Chris@16
|
268
|
Chris@16
|
269 private:
|
Chris@16
|
270 CharT m_From;
|
Chris@16
|
271 CharT m_To;
|
Chris@16
|
272 };
|
Chris@16
|
273
|
Chris@16
|
274 // class_and composition predicate
|
Chris@16
|
275 template<typename Pred1T, typename Pred2T>
|
Chris@16
|
276 struct pred_andF :
|
Chris@16
|
277 public predicate_facade< pred_andF<Pred1T,Pred2T> >
|
Chris@16
|
278 {
|
Chris@16
|
279 public:
|
Chris@16
|
280
|
Chris@16
|
281 // Boost.ResultOf support
|
Chris@16
|
282 typedef bool result_type;
|
Chris@16
|
283
|
Chris@16
|
284 // Constructor
|
Chris@16
|
285 pred_andF( Pred1T Pred1, Pred2T Pred2 ) :
|
Chris@16
|
286 m_Pred1(Pred1), m_Pred2(Pred2) {}
|
Chris@16
|
287
|
Chris@16
|
288 // Operation
|
Chris@16
|
289 template<typename CharT>
|
Chris@16
|
290 bool operator()( CharT Ch ) const
|
Chris@16
|
291 {
|
Chris@16
|
292 return m_Pred1(Ch) && m_Pred2(Ch);
|
Chris@16
|
293 }
|
Chris@16
|
294
|
Chris@16
|
295 private:
|
Chris@16
|
296 Pred1T m_Pred1;
|
Chris@16
|
297 Pred2T m_Pred2;
|
Chris@16
|
298 };
|
Chris@16
|
299
|
Chris@16
|
300 // class_or composition predicate
|
Chris@16
|
301 template<typename Pred1T, typename Pred2T>
|
Chris@16
|
302 struct pred_orF :
|
Chris@16
|
303 public predicate_facade< pred_orF<Pred1T,Pred2T> >
|
Chris@16
|
304 {
|
Chris@16
|
305 public:
|
Chris@16
|
306 // Boost.ResultOf support
|
Chris@16
|
307 typedef bool result_type;
|
Chris@16
|
308
|
Chris@16
|
309 // Constructor
|
Chris@16
|
310 pred_orF( Pred1T Pred1, Pred2T Pred2 ) :
|
Chris@16
|
311 m_Pred1(Pred1), m_Pred2(Pred2) {}
|
Chris@16
|
312
|
Chris@16
|
313 // Operation
|
Chris@16
|
314 template<typename CharT>
|
Chris@16
|
315 bool operator()( CharT Ch ) const
|
Chris@16
|
316 {
|
Chris@16
|
317 return m_Pred1(Ch) || m_Pred2(Ch);
|
Chris@16
|
318 }
|
Chris@16
|
319
|
Chris@16
|
320 private:
|
Chris@16
|
321 Pred1T m_Pred1;
|
Chris@16
|
322 Pred2T m_Pred2;
|
Chris@16
|
323 };
|
Chris@16
|
324
|
Chris@16
|
325 // class_not composition predicate
|
Chris@16
|
326 template< typename PredT >
|
Chris@16
|
327 struct pred_notF :
|
Chris@16
|
328 public predicate_facade< pred_notF<PredT> >
|
Chris@16
|
329 {
|
Chris@16
|
330 public:
|
Chris@16
|
331 // Boost.ResultOf support
|
Chris@16
|
332 typedef bool result_type;
|
Chris@16
|
333
|
Chris@16
|
334 // Constructor
|
Chris@16
|
335 pred_notF( PredT Pred ) : m_Pred(Pred) {}
|
Chris@16
|
336
|
Chris@16
|
337 // Operation
|
Chris@16
|
338 template<typename CharT>
|
Chris@16
|
339 bool operator()( CharT Ch ) const
|
Chris@16
|
340 {
|
Chris@16
|
341 return !m_Pred(Ch);
|
Chris@16
|
342 }
|
Chris@16
|
343
|
Chris@16
|
344 private:
|
Chris@16
|
345 PredT m_Pred;
|
Chris@16
|
346 };
|
Chris@16
|
347
|
Chris@16
|
348 } // namespace detail
|
Chris@16
|
349 } // namespace algorithm
|
Chris@16
|
350 } // namespace boost
|
Chris@16
|
351
|
Chris@16
|
352
|
Chris@16
|
353 #endif // BOOST_STRING_CLASSIFICATION_DETAIL_HPP
|