File indexing completed on 2025-01-18 09:28:23
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_STRING_CLASSIFICATION_DETAIL_HPP
0012 #define BOOST_STRING_CLASSIFICATION_DETAIL_HPP
0013
0014 #include <boost/algorithm/string/config.hpp>
0015 #include <algorithm>
0016 #include <cstring>
0017 #include <functional>
0018 #include <locale>
0019
0020 #include <boost/range/begin.hpp>
0021 #include <boost/range/distance.hpp>
0022 #include <boost/range/end.hpp>
0023
0024 #include <boost/algorithm/string/predicate_facade.hpp>
0025 #include <boost/type_traits/remove_const.hpp>
0026
0027 namespace boost {
0028 namespace algorithm {
0029 namespace detail {
0030
0031
0032
0033
0034 struct is_classifiedF :
0035 public predicate_facade<is_classifiedF>
0036 {
0037
0038 typedef bool result_type;
0039
0040
0041 is_classifiedF(std::ctype_base::mask Type, std::locale const & Loc = std::locale()) :
0042 m_Type(Type), m_Locale(Loc) {}
0043
0044 template<typename CharT>
0045 bool operator()( CharT Ch ) const
0046 {
0047 return std::use_facet< std::ctype<CharT> >(m_Locale).is( m_Type, Ch );
0048 }
0049
0050 #if defined(BOOST_BORLANDC) && (BOOST_BORLANDC >= 0x560) && (BOOST_BORLANDC <= 0x582) && !defined(_USE_OLD_RW_STL)
0051 template<>
0052 bool operator()( char const Ch ) const
0053 {
0054 return std::use_facet< std::ctype<char> >(m_Locale).is( m_Type, Ch );
0055 }
0056 #endif
0057
0058 private:
0059 std::ctype_base::mask m_Type;
0060 std::locale m_Locale;
0061 };
0062
0063
0064
0065
0066
0067
0068 template<typename CharT>
0069 struct is_any_ofF :
0070 public predicate_facade<is_any_ofF<CharT> >
0071 {
0072 private:
0073
0074 typedef typename ::boost::remove_const<CharT>::type set_value_type;
0075
0076 public:
0077
0078 typedef bool result_type;
0079
0080
0081 template<typename RangeT>
0082 is_any_ofF( const RangeT& Range ) : m_Size(0)
0083 {
0084
0085 m_Storage.m_dynSet=0;
0086
0087 std::size_t Size=::boost::distance(Range);
0088 m_Size=Size;
0089 set_value_type* Storage=0;
0090
0091 if(use_fixed_storage(m_Size))
0092 {
0093
0094 Storage=&m_Storage.m_fixSet[0];
0095 }
0096 else
0097 {
0098
0099 m_Storage.m_dynSet=new set_value_type[m_Size];
0100 Storage=m_Storage.m_dynSet;
0101 }
0102
0103
0104 ::std::copy(::boost::begin(Range), ::boost::end(Range), Storage);
0105 ::std::sort(Storage, Storage+m_Size);
0106 }
0107
0108
0109 is_any_ofF(const is_any_ofF& Other) : m_Size(Other.m_Size)
0110 {
0111
0112 m_Storage.m_dynSet=0;
0113 const set_value_type* SrcStorage=0;
0114 set_value_type* DestStorage=0;
0115
0116 if(use_fixed_storage(m_Size))
0117 {
0118
0119 DestStorage=&m_Storage.m_fixSet[0];
0120 SrcStorage=&Other.m_Storage.m_fixSet[0];
0121 }
0122 else
0123 {
0124
0125 m_Storage.m_dynSet=new set_value_type[m_Size];
0126 DestStorage=m_Storage.m_dynSet;
0127 SrcStorage=Other.m_Storage.m_dynSet;
0128 }
0129
0130
0131 ::std::memcpy(DestStorage, SrcStorage, sizeof(set_value_type)*m_Size);
0132 }
0133
0134
0135 ~is_any_ofF()
0136 {
0137 if(!use_fixed_storage(m_Size) && m_Storage.m_dynSet!=0)
0138 {
0139 delete [] m_Storage.m_dynSet;
0140 }
0141 }
0142
0143
0144 is_any_ofF& operator=(const is_any_ofF& Other)
0145 {
0146
0147 if(this==&Other) return *this;
0148
0149
0150 const set_value_type* SrcStorage;
0151 set_value_type* DestStorage;
0152
0153 if(use_fixed_storage(Other.m_Size))
0154 {
0155
0156 DestStorage=&m_Storage.m_fixSet[0];
0157 SrcStorage=&Other.m_Storage.m_fixSet[0];
0158
0159
0160 if(!use_fixed_storage(m_Size) && m_Storage.m_dynSet!=0)
0161 {
0162 delete [] m_Storage.m_dynSet;
0163 }
0164
0165
0166 m_Size=Other.m_Size;
0167 }
0168 else
0169 {
0170
0171 SrcStorage=Other.m_Storage.m_dynSet;
0172
0173
0174 if(use_fixed_storage(m_Size))
0175 {
0176
0177 set_value_type* pTemp=new set_value_type[Other.m_Size];
0178 DestStorage=pTemp;
0179 m_Storage.m_dynSet=pTemp;
0180 m_Size=Other.m_Size;
0181 }
0182 else
0183 {
0184
0185 if(m_Storage.m_dynSet!=0 && m_Size>=Other.m_Size && m_Size<Other.m_Size*2)
0186 {
0187
0188 DestStorage=m_Storage.m_dynSet;
0189 m_Size=Other.m_Size;
0190 }
0191 else
0192 {
0193
0194 set_value_type* pTemp=new set_value_type[Other.m_Size];
0195 DestStorage=pTemp;
0196
0197
0198 if(m_Storage.m_dynSet!=0)
0199 {
0200 delete [] m_Storage.m_dynSet;
0201 }
0202
0203 m_Storage.m_dynSet=pTemp;
0204
0205 m_Size=Other.m_Size;
0206 }
0207 }
0208 }
0209
0210
0211 ::std::memcpy(DestStorage, SrcStorage, sizeof(set_value_type)*m_Size);
0212
0213 return *this;
0214 }
0215
0216
0217 template<typename Char2T>
0218 bool operator()( Char2T Ch ) const
0219 {
0220 const set_value_type* Storage=
0221 (use_fixed_storage(m_Size))
0222 ? &m_Storage.m_fixSet[0]
0223 : m_Storage.m_dynSet;
0224
0225 return ::std::binary_search(Storage, Storage+m_Size, Ch);
0226 }
0227 private:
0228
0229 static bool use_fixed_storage(std::size_t size)
0230 {
0231 return size<=sizeof(set_value_type*)*2;
0232 }
0233
0234
0235 private:
0236
0237
0238 union
0239 {
0240 set_value_type* m_dynSet;
0241 set_value_type m_fixSet[sizeof(set_value_type*)*2];
0242 }
0243 m_Storage;
0244
0245
0246 ::std::size_t m_Size;
0247 };
0248
0249
0250
0251
0252
0253
0254 template<typename CharT>
0255 struct is_from_rangeF :
0256 public predicate_facade< is_from_rangeF<CharT> >
0257 {
0258
0259 typedef bool result_type;
0260
0261
0262 is_from_rangeF( CharT From, CharT To ) : m_From(From), m_To(To) {}
0263
0264
0265 template<typename Char2T>
0266 bool operator()( Char2T Ch ) const
0267 {
0268 return ( m_From <= Ch ) && ( Ch <= m_To );
0269 }
0270
0271 private:
0272 CharT m_From;
0273 CharT m_To;
0274 };
0275
0276
0277 template<typename Pred1T, typename Pred2T>
0278 struct pred_andF :
0279 public predicate_facade< pred_andF<Pred1T,Pred2T> >
0280 {
0281 public:
0282
0283
0284 typedef bool result_type;
0285
0286
0287 pred_andF( Pred1T Pred1, Pred2T Pred2 ) :
0288 m_Pred1(Pred1), m_Pred2(Pred2) {}
0289
0290
0291 template<typename CharT>
0292 bool operator()( CharT Ch ) const
0293 {
0294 return m_Pred1(Ch) && m_Pred2(Ch);
0295 }
0296
0297 private:
0298 Pred1T m_Pred1;
0299 Pred2T m_Pred2;
0300 };
0301
0302
0303 template<typename Pred1T, typename Pred2T>
0304 struct pred_orF :
0305 public predicate_facade< pred_orF<Pred1T,Pred2T> >
0306 {
0307 public:
0308
0309 typedef bool result_type;
0310
0311
0312 pred_orF( Pred1T Pred1, Pred2T Pred2 ) :
0313 m_Pred1(Pred1), m_Pred2(Pred2) {}
0314
0315
0316 template<typename CharT>
0317 bool operator()( CharT Ch ) const
0318 {
0319 return m_Pred1(Ch) || m_Pred2(Ch);
0320 }
0321
0322 private:
0323 Pred1T m_Pred1;
0324 Pred2T m_Pred2;
0325 };
0326
0327
0328 template< typename PredT >
0329 struct pred_notF :
0330 public predicate_facade< pred_notF<PredT> >
0331 {
0332 public:
0333
0334 typedef bool result_type;
0335
0336
0337 pred_notF( PredT Pred ) : m_Pred(Pred) {}
0338
0339
0340 template<typename CharT>
0341 bool operator()( CharT Ch ) const
0342 {
0343 return !m_Pred(Ch);
0344 }
0345
0346 private:
0347 PredT m_Pred;
0348 };
0349
0350 }
0351 }
0352 }
0353
0354
0355 #endif