Boost.Locale
|
00001 // 00002 // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh) 00003 // 00004 // Distributed under the Boost Software License, Version 1.0. (See 00005 // accompanying file LICENSE_1_0.txt or copy at 00006 // http://www.boost.org/LICENSE_1_0.txt) 00007 // 00008 #ifndef BOOST_LOCALE_BOUNDARY_SEGMENT_HPP_INCLUDED 00009 #define BOOST_LOCALE_BOUNDARY_SEGMENT_HPP_INCLUDED 00010 #include <boost/locale/config.hpp> 00011 #ifdef BOOST_MSVC 00012 # pragma warning(push) 00013 # pragma warning(disable : 4275 4251 4231 4660) 00014 #endif 00015 #include <locale> 00016 #include <string> 00017 #include <iosfwd> 00018 #include <iterator> 00019 00020 00021 namespace boost { 00022 namespace locale { 00023 namespace boundary { 00025 namespace details { 00026 template<typename LeftIterator,typename RightIterator> 00027 int compare_text(LeftIterator l_begin,LeftIterator l_end,RightIterator r_begin,RightIterator r_end) 00028 { 00029 typedef LeftIterator left_iterator; 00030 typedef RightIterator right_iterator; 00031 typedef typename std::iterator_traits<left_iterator>::value_type char_type; 00032 typedef std::char_traits<char_type> traits; 00033 while(l_begin!=l_end && r_begin!=r_end) { 00034 char_type lchar = *l_begin++; 00035 char_type rchar = *r_begin++; 00036 if(traits::eq(lchar,rchar)) 00037 continue; 00038 if(traits::lt(lchar,rchar)) 00039 return -1; 00040 else 00041 return 1; 00042 } 00043 if(l_begin==l_end && r_begin==r_end) 00044 return 0; 00045 if(l_begin==l_end) 00046 return -1; 00047 else 00048 return 1; 00049 } 00050 00051 00052 template<typename Left,typename Right> 00053 int compare_text(Left const &l,Right const &r) 00054 { 00055 return compare_text(l.begin(),l.end(),r.begin(),r.end()); 00056 } 00057 00058 template<typename Left,typename Char> 00059 int compare_string(Left const &l,Char const *begin) 00060 { 00061 Char const *end = begin; 00062 while(*end!=0) 00063 end++; 00064 return compare_text(l.begin(),l.end(),begin,end); 00065 } 00066 00067 template<typename Right,typename Char> 00068 int compare_string(Char const *begin,Right const &r) 00069 { 00070 Char const *end = begin; 00071 while(*end!=0) 00072 end++; 00073 return compare_text(begin,end,r.begin(),r.end()); 00074 } 00075 00076 } 00078 00082 00102 template<typename IteratorType> 00103 class segment : public std::pair<IteratorType,IteratorType> { 00104 public: 00108 typedef typename std::iterator_traits<IteratorType>::value_type char_type; 00112 typedef std::basic_string<char_type> string_type; 00116 typedef char_type value_type; 00120 typedef IteratorType iterator; 00124 typedef IteratorType const_iterator; 00128 typedef typename std::iterator_traits<IteratorType>::difference_type difference_type; 00129 00133 segment() {} 00137 segment(iterator b,iterator e,rule_type r) : 00138 std::pair<IteratorType,IteratorType>(b,e), 00139 rule_(r) 00140 { 00141 } 00145 void begin(iterator const &v) 00146 { 00147 this->first = v; 00148 } 00152 void end(iterator const &v) 00153 { 00154 this->second = v; 00155 } 00156 00160 IteratorType begin() const 00161 { 00162 return this->first; 00163 } 00167 IteratorType end() const 00168 { 00169 return this->second; 00170 } 00171 00175 template <class T, class A> 00176 operator std::basic_string<char_type, T, A> ()const 00177 { 00178 return std::basic_string<char_type, T, A>(this->first, this->second); 00179 } 00180 00184 string_type str() const 00185 { 00186 return string_type(begin(),end()); 00187 } 00188 00192 00193 size_t length() const 00194 { 00195 return std::distance(begin(),end()); 00196 } 00197 00201 bool empty() const 00202 { 00203 return begin() == end(); 00204 } 00205 00209 rule_type rule() const 00210 { 00211 return rule_; 00212 } 00216 void rule(rule_type r) 00217 { 00218 rule_ = r; 00219 } 00220 00221 // make sure we override std::pair's operator== 00222 00224 bool operator==(segment const &other) 00225 { 00226 return details::compare_text(*this,other) == 0; 00227 } 00228 00230 bool operator!=(segment const &other) 00231 { 00232 return details::compare_text(*this,other) != 0; 00233 } 00234 00235 private: 00236 rule_type rule_; 00237 00238 }; 00239 00240 00242 template<typename IteratorL,typename IteratorR> 00243 bool operator==(segment<IteratorL> const &l,segment<IteratorR> const &r) 00244 { 00245 return details::compare_text(l,r) == 0; 00246 } 00248 template<typename IteratorL,typename IteratorR> 00249 bool operator!=(segment<IteratorL> const &l,segment<IteratorR> const &r) 00250 { 00251 return details::compare_text(l,r) != 0; 00252 } 00253 00255 template<typename IteratorL,typename IteratorR> 00256 bool operator<(segment<IteratorL> const &l,segment<IteratorR> const &r) 00257 { 00258 return details::compare_text(l,r) < 0; 00259 } 00261 template<typename IteratorL,typename IteratorR> 00262 bool operator<=(segment<IteratorL> const &l,segment<IteratorR> const &r) 00263 { 00264 return details::compare_text(l,r) <= 0; 00265 } 00267 template<typename IteratorL,typename IteratorR> 00268 bool operator>(segment<IteratorL> const &l,segment<IteratorR> const &r) 00269 { 00270 return details::compare_text(l,r) > 0; 00271 } 00273 template<typename IteratorL,typename IteratorR> 00274 bool operator>=(segment<IteratorL> const &l,segment<IteratorR> const &r) 00275 { 00276 return details::compare_text(l,r) >= 0; 00277 } 00278 00280 template<typename CharType,typename Traits,typename Alloc,typename IteratorR> 00281 bool operator==(std::basic_string<CharType,Traits,Alloc> const &l,segment<IteratorR> const &r) 00282 { 00283 return details::compare_text(l,r) == 0; 00284 } 00286 template<typename CharType,typename Traits,typename Alloc,typename IteratorR> 00287 bool operator!=(std::basic_string<CharType,Traits,Alloc> const &l,segment<IteratorR> const &r) 00288 { 00289 return details::compare_text(l,r) != 0; 00290 } 00291 00293 template<typename CharType,typename Traits,typename Alloc,typename IteratorR> 00294 bool operator<(std::basic_string<CharType,Traits,Alloc> const &l,segment<IteratorR> const &r) 00295 { 00296 return details::compare_text(l,r) < 0; 00297 } 00299 template<typename CharType,typename Traits,typename Alloc,typename IteratorR> 00300 bool operator<=(std::basic_string<CharType,Traits,Alloc> const &l,segment<IteratorR> const &r) 00301 { 00302 return details::compare_text(l,r) <= 0; 00303 } 00305 template<typename CharType,typename Traits,typename Alloc,typename IteratorR> 00306 bool operator>(std::basic_string<CharType,Traits,Alloc> const &l,segment<IteratorR> const &r) 00307 { 00308 return details::compare_text(l,r) > 0; 00309 } 00311 template<typename CharType,typename Traits,typename Alloc,typename IteratorR> 00312 bool operator>=(std::basic_string<CharType,Traits,Alloc> const &l,segment<IteratorR> const &r) 00313 { 00314 return details::compare_text(l,r) >= 0; 00315 } 00316 00318 template<typename Iterator,typename CharType,typename Traits,typename Alloc> 00319 bool operator==(segment<Iterator> const &l,std::basic_string<CharType,Traits,Alloc> const &r) 00320 { 00321 return details::compare_text(l,r) == 0; 00322 } 00324 template<typename Iterator,typename CharType,typename Traits,typename Alloc> 00325 bool operator!=(segment<Iterator> const &l,std::basic_string<CharType,Traits,Alloc> const &r) 00326 { 00327 return details::compare_text(l,r) != 0; 00328 } 00329 00331 template<typename Iterator,typename CharType,typename Traits,typename Alloc> 00332 bool operator<(segment<Iterator> const &l,std::basic_string<CharType,Traits,Alloc> const &r) 00333 { 00334 return details::compare_text(l,r) < 0; 00335 } 00337 template<typename Iterator,typename CharType,typename Traits,typename Alloc> 00338 bool operator<=(segment<Iterator> const &l,std::basic_string<CharType,Traits,Alloc> const &r) 00339 { 00340 return details::compare_text(l,r) <= 0; 00341 } 00343 template<typename Iterator,typename CharType,typename Traits,typename Alloc> 00344 bool operator>(segment<Iterator> const &l,std::basic_string<CharType,Traits,Alloc> const &r) 00345 { 00346 return details::compare_text(l,r) > 0; 00347 } 00349 template<typename Iterator,typename CharType,typename Traits,typename Alloc> 00350 bool operator>=(segment<Iterator> const &l,std::basic_string<CharType,Traits,Alloc> const &r) 00351 { 00352 return details::compare_text(l,r) >= 0; 00353 } 00354 00355 00357 template<typename CharType,typename IteratorR> 00358 bool operator==(CharType const *l,segment<IteratorR> const &r) 00359 { 00360 return details::compare_string(l,r) == 0; 00361 } 00363 template<typename CharType,typename IteratorR> 00364 bool operator!=(CharType const *l,segment<IteratorR> const &r) 00365 { 00366 return details::compare_string(l,r) != 0; 00367 } 00368 00370 template<typename CharType,typename IteratorR> 00371 bool operator<(CharType const *l,segment<IteratorR> const &r) 00372 { 00373 return details::compare_string(l,r) < 0; 00374 } 00376 template<typename CharType,typename IteratorR> 00377 bool operator<=(CharType const *l,segment<IteratorR> const &r) 00378 { 00379 return details::compare_string(l,r) <= 0; 00380 } 00382 template<typename CharType,typename IteratorR> 00383 bool operator>(CharType const *l,segment<IteratorR> const &r) 00384 { 00385 return details::compare_string(l,r) > 0; 00386 } 00388 template<typename CharType,typename IteratorR> 00389 bool operator>=(CharType const *l,segment<IteratorR> const &r) 00390 { 00391 return details::compare_string(l,r) >= 0; 00392 } 00393 00395 template<typename Iterator,typename CharType> 00396 bool operator==(segment<Iterator> const &l,CharType const *r) 00397 { 00398 return details::compare_string(l,r) == 0; 00399 } 00401 template<typename Iterator,typename CharType> 00402 bool operator!=(segment<Iterator> const &l,CharType const *r) 00403 { 00404 return details::compare_string(l,r) != 0; 00405 } 00406 00408 template<typename Iterator,typename CharType> 00409 bool operator<(segment<Iterator> const &l,CharType const *r) 00410 { 00411 return details::compare_string(l,r) < 0; 00412 } 00414 template<typename Iterator,typename CharType> 00415 bool operator<=(segment<Iterator> const &l,CharType const *r) 00416 { 00417 return details::compare_string(l,r) <= 0; 00418 } 00420 template<typename Iterator,typename CharType> 00421 bool operator>(segment<Iterator> const &l,CharType const *r) 00422 { 00423 return details::compare_string(l,r) > 0; 00424 } 00426 template<typename Iterator,typename CharType> 00427 bool operator>=(segment<Iterator> const &l,CharType const *r) 00428 { 00429 return details::compare_string(l,r) >= 0; 00430 } 00431 00432 00433 00434 00435 00436 00437 typedef segment<std::string::const_iterator> ssegment; 00438 typedef segment<std::wstring::const_iterator> wssegment; 00439 #ifdef BOOST_HAS_CHAR16_T 00440 typedef segment<std::u16string::const_iterator> u16ssegment; 00441 #endif 00442 #ifdef BOOST_HAS_CHAR32_T 00443 typedef segment<std::u32string::const_iterator> u32ssegment; 00444 #endif 00445 00446 typedef segment<char const *> csegment; 00447 typedef segment<wchar_t const *> wcsegment; 00448 #ifdef BOOST_HAS_CHAR16_T 00449 typedef segment<char16_t const *> u16csegment; 00450 #endif 00451 #ifdef BOOST_HAS_CHAR32_T 00452 typedef segment<char32_t const *> u32csegment; 00453 #endif 00454 00455 00456 00457 00458 00462 template<typename CharType,typename TraitsType,typename Iterator> 00463 std::basic_ostream<CharType,TraitsType> &operator<<( 00464 std::basic_ostream<CharType,TraitsType> &out, 00465 segment<Iterator> const &tok) 00466 { 00467 for(Iterator p=tok.begin(),e=tok.end();p!=e;++p) 00468 out << *p; 00469 return out; 00470 } 00471 00473 00474 } // boundary 00475 } // locale 00476 } // boost 00477 00478 #ifdef BOOST_MSVC 00479 #pragma warning(pop) 00480 #endif 00481 00482 #endif 00483 00484 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4