Boost.Locale: boost/locale/boundary/segment.hpp Source File

| Boost.Locale

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files
- Examples

- File List

segment.hpp
1 //
2 // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //
8 #ifndef BOOST_LOCALE_BOUNDARY_SEGMENT_HPP_INCLUDED
9 #define BOOST_LOCALE_BOUNDARY_SEGMENT_HPP_INCLUDED
10 #include <boost/locale/config.hpp>
11 #ifdef BOOST_MSVC
12 # pragma warning(push)
13 # pragma warning(disable : 4275 4251 4231 4660)
14 #endif
15 #include <locale>
16 #include <string>
17 #include <iosfwd>
18 #include <iterator>
19
20
21 namespace boost {
22 namespace locale {
23 namespace boundary {
25 namespace details {
26 template<typename LeftIterator,typename RightIterator>
27 int compare_text(LeftIterator l_begin,LeftIterator l_end,RightIterator r_begin,RightIterator r_end)
28 {
29 typedef LeftIterator left_iterator;
30 typedef typename std::iterator_traits<left_iterator>::value_type char_type;
31 typedef std::char_traits<char_type> traits;
32 while(l_begin!=l_end && r_begin!=r_end) {
33 char_type lchar = *l_begin++;
34 char_type rchar = *r_begin++;
35 if(traits::eq(lchar,rchar))
36 continue;
37 if(traits::lt(lchar,rchar))
38 return -1;
39 else
40 return 1;
41 }
42 if(l_begin==l_end && r_begin==r_end)
43 return 0;
44 if(l_begin==l_end)
45 return -1;
46 else
47 return 1;
48 }
49
50
51 template<typename Left,typename Right>
52 int compare_text(Left const &l,Right const &r)
53 {
54 return compare_text(l.begin(),l.end(),r.begin(),r.end());
55 }
56
57 template<typename Left,typename Char>
58 int compare_string(Left const &l,Char const *begin)
59 {
60 Char const *end = begin;
61 while(*end!=0)
62 end++;
63 return compare_text(l.begin(),l.end(),begin,end);
64 }
65
66 template<typename Right,typename Char>
67 int compare_string(Char const *begin,Right const &r)
68 {
69 Char const *end = begin;
70 while(*end!=0)
71 end++;
72 return compare_text(begin,end,r.begin(),r.end());
73 }
74
75 }
77
81
101 template<typename IteratorType>
102 class segment : public std::pair<IteratorType,IteratorType> {
103 public:
107 typedef typename std::iterator_traits<IteratorType>::value_type char_type;
111 typedef std::basic_string<char_type> string_type;
115 typedef char_type value_type;
119 typedef IteratorType iterator;
123 typedef IteratorType const_iterator;
127 typedef typename std::iterator_traits<IteratorType>::difference_type difference_type;
128
132 segment() {}
136 segment(iterator b,iterator e,rule_type r) :
137 std::pair<IteratorType,IteratorType>(b,e),
138 rule_(r)
139 {
140 }
144 void begin(iterator const &v)
145 {
146 this->first = v;
147 }
151 void end(iterator const &v)
152 {
153 this->second = v;
154 }
155
159 IteratorType begin() const
160 {
161 return this->first;
162 }
166 IteratorType end() const
167 {
168 return this->second;
169 }
170
174 template <class T, class A>
175 operator std::basic_string<char_type, T, A> ()const
176 {
177 return std::basic_string<char_type, T, A>(this->first, this->second);
178 }
179
183 string_type str() const
184 {
185 return string_type(begin(),end());
186 }
187
191
192 size_t length() const
193 {
194 return std::distance(begin(),end());
195 }
196
200 bool empty() const
201 {
202 return begin() == end();
203 }
204
208 rule_type rule() const
209 {
210 return rule_;
211 }
215 void rule(rule_type r)
216 {
217 rule_ = r;
218 }
219
220 // make sure we override std::pair's operator==
221
223 bool operator==(segment const &other)
224 {
225 return details::compare_text(*this,other) == 0;
226 }
227
229 bool operator!=(segment const &other)
230 {
231 return details::compare_text(*this,other) != 0;
232 }
233
234 private:
235 rule_type rule_;
236
237 };
238
239
241 template<typename IteratorL,typename IteratorR>
242 bool operator==(segment<IteratorL> const &l,segment<IteratorR> const &r)
243 {
244 return details::compare_text(l,r) == 0;
245 }
247 template<typename IteratorL,typename IteratorR>
248 bool operator!=(segment<IteratorL> const &l,segment<IteratorR> const &r)
249 {
250 return details::compare_text(l,r) != 0;
251 }
252
254 template<typename IteratorL,typename IteratorR>
255 bool operator<(segment<IteratorL> const &l,segment<IteratorR> const &r)
256 {
257 return details::compare_text(l,r) < 0;
258 }
260 template<typename IteratorL,typename IteratorR>
261 bool operator<=(segment<IteratorL> const &l,segment<IteratorR> const &r)
262 {
263 return details::compare_text(l,r) <= 0;
264 }
266 template<typename IteratorL,typename IteratorR>
267 bool operator>(segment<IteratorL> const &l,segment<IteratorR> const &r)
268 {
269 return details::compare_text(l,r) > 0;
270 }
272 template<typename IteratorL,typename IteratorR>
273 bool operator>=(segment<IteratorL> const &l,segment<IteratorR> const &r)
274 {
275 return details::compare_text(l,r) >= 0;
276 }
277
279 template<typename CharType,typename Traits,typename Alloc,typename IteratorR>
280 bool operator==(std::basic_string<CharType,Traits,Alloc> const &l,segment<IteratorR> const &r)
281 {
282 return details::compare_text(l,r) == 0;
283 }
285 template<typename CharType,typename Traits,typename Alloc,typename IteratorR>
286 bool operator!=(std::basic_string<CharType,Traits,Alloc> const &l,segment<IteratorR> const &r)
287 {
288 return details::compare_text(l,r) != 0;
289 }
290
292 template<typename CharType,typename Traits,typename Alloc,typename IteratorR>
293 bool operator<(std::basic_string<CharType,Traits,Alloc> const &l,segment<IteratorR> const &r)
294 {
295 return details::compare_text(l,r) < 0;
296 }
298 template<typename CharType,typename Traits,typename Alloc,typename IteratorR>
299 bool operator<=(std::basic_string<CharType,Traits,Alloc> const &l,segment<IteratorR> const &r)
300 {
301 return details::compare_text(l,r) <= 0;
302 }
304 template<typename CharType,typename Traits,typename Alloc,typename IteratorR>
305 bool operator>(std::basic_string<CharType,Traits,Alloc> const &l,segment<IteratorR> const &r)
306 {
307 return details::compare_text(l,r) > 0;
308 }
310 template<typename CharType,typename Traits,typename Alloc,typename IteratorR>
311 bool operator>=(std::basic_string<CharType,Traits,Alloc> const &l,segment<IteratorR> const &r)
312 {
313 return details::compare_text(l,r) >= 0;
314 }
315
317 template<typename Iterator,typename CharType,typename Traits,typename Alloc>
318 bool operator==(segment<Iterator> const &l,std::basic_string<CharType,Traits,Alloc> const &r)
319 {
320 return details::compare_text(l,r) == 0;
321 }
323 template<typename Iterator,typename CharType,typename Traits,typename Alloc>
324 bool operator!=(segment<Iterator> const &l,std::basic_string<CharType,Traits,Alloc> const &r)
325 {
326 return details::compare_text(l,r) != 0;
327 }
328
330 template<typename Iterator,typename CharType,typename Traits,typename Alloc>
331 bool operator<(segment<Iterator> const &l,std::basic_string<CharType,Traits,Alloc> const &r)
332 {
333 return details::compare_text(l,r) < 0;
334 }
336 template<typename Iterator,typename CharType,typename Traits,typename Alloc>
337 bool operator<=(segment<Iterator> const &l,std::basic_string<CharType,Traits,Alloc> const &r)
338 {
339 return details::compare_text(l,r) <= 0;
340 }
342 template<typename Iterator,typename CharType,typename Traits,typename Alloc>
343 bool operator>(segment<Iterator> const &l,std::basic_string<CharType,Traits,Alloc> const &r)
344 {
345 return details::compare_text(l,r) > 0;
346 }
348 template<typename Iterator,typename CharType,typename Traits,typename Alloc>
349 bool operator>=(segment<Iterator> const &l,std::basic_string<CharType,Traits,Alloc> const &r)
350 {
351 return details::compare_text(l,r) >= 0;
352 }
353
354
356 template<typename CharType,typename IteratorR>
357 bool operator==(CharType const *l,segment<IteratorR> const &r)
358 {
359 return details::compare_string(l,r) == 0;
360 }
362 template<typename CharType,typename IteratorR>
363 bool operator!=(CharType const *l,segment<IteratorR> const &r)
364 {
365 return details::compare_string(l,r) != 0;
366 }
367
369 template<typename CharType,typename IteratorR>
370 bool operator<(CharType const *l,segment<IteratorR> const &r)
371 {
372 return details::compare_string(l,r) < 0;
373 }
375 template<typename CharType,typename IteratorR>
376 bool operator<=(CharType const *l,segment<IteratorR> const &r)
377 {
378 return details::compare_string(l,r) <= 0;
379 }
381 template<typename CharType,typename IteratorR>
382 bool operator>(CharType const *l,segment<IteratorR> const &r)
383 {
384 return details::compare_string(l,r) > 0;
385 }
387 template<typename CharType,typename IteratorR>
388 bool operator>=(CharType const *l,segment<IteratorR> const &r)
389 {
390 return details::compare_string(l,r) >= 0;
391 }
392
394 template<typename Iterator,typename CharType>
395 bool operator==(segment<Iterator> const &l,CharType const *r)
396 {
397 return details::compare_string(l,r) == 0;
398 }
400 template<typename Iterator,typename CharType>
401 bool operator!=(segment<Iterator> const &l,CharType const *r)
402 {
403 return details::compare_string(l,r) != 0;
404 }
405
407 template<typename Iterator,typename CharType>
408 bool operator<(segment<Iterator> const &l,CharType const *r)
409 {
410 return details::compare_string(l,r) < 0;
411 }
413 template<typename Iterator,typename CharType>
414 bool operator<=(segment<Iterator> const &l,CharType const *r)
415 {
416 return details::compare_string(l,r) <= 0;
417 }
419 template<typename Iterator,typename CharType>
420 bool operator>(segment<Iterator> const &l,CharType const *r)
421 {
422 return details::compare_string(l,r) > 0;
423 }
425 template<typename Iterator,typename CharType>
426 bool operator>=(segment<Iterator> const &l,CharType const *r)
427 {
428 return details::compare_string(l,r) >= 0;
429 }
430
431
432
433
434
435
436 typedef segment<std::string::const_iterator> ssegment;
437 typedef segment<std::wstring::const_iterator> wssegment;
438 #ifdef BOOST_LOCALE_ENABLE_CHAR16_T
439 typedef segment<std::u16string::const_iterator> u16ssegment;
440 #endif
441 #ifdef BOOST_LOCALE_ENABLE_CHAR32_T
442 typedef segment<std::u32string::const_iterator> u32ssegment;
443 #endif
444
445 typedef segment<char const *> csegment;
446 typedef segment<wchar_t const *> wcsegment;
447 #ifdef BOOST_LOCALE_ENABLE_CHAR16_T
448 typedef segment<char16_t const *> u16csegment;
449 #endif
450 #ifdef BOOST_LOCALE_ENABLE_CHAR32_T
451 typedef segment<char32_t const *> u32csegment;
452 #endif
453
454
455
456
457
461 template<typename CharType,typename TraitsType,typename Iterator>
462 std::basic_ostream<CharType,TraitsType> &operator<<(
463 std::basic_ostream<CharType,TraitsType> &out,
464 segment<Iterator> const &tok)
465 {
466 for(Iterator p=tok.begin(),e=tok.end();p!=e;++p)
467 out << *p;
468 return out;
469 }
470
472
473 } // boundary
474 } // locale
475 } // boost
476
477 #ifdef BOOST_MSVC
478 #pragma warning(pop)
479 #endif
480
481 #endif
482
483 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
boost::locale::boundary::csegment
segment< char const * > csegment
convenience typedef
Definition: segment.hpp:445
boost::locale::boundary::segment::char_type
std::iterator_traits< IteratorType >::value_type char_type
Definition: segment.hpp:107
boost::locale::boundary::segment::segment
segment(iterator b, iterator e, rule_type r)
Definition: segment.hpp:136
boost::locale::boundary::segment
a segment object that represents a pair of two iterators that define the range where this segment exi...
Definition: segment.hpp:102
boost::locale::boundary::wcsegment
segment< wchar_t const * > wcsegment
convenience typedef
Definition: segment.hpp:446
boost::locale::boundary::segment::end
IteratorType end() const
Definition: segment.hpp:166
boost::locale::boundary::segment::operator==
bool operator==(segment const &other)
Compare two segments.
Definition: segment.hpp:223
boost::locale::boundary::u16csegment
segment< char16_t const * > u16csegment
convenience typedef
Definition: segment.hpp:448
boost::locale::boundary::u16ssegment
segment< std::u16string::const_iterator > u16ssegment
convenience typedef
Definition: segment.hpp:439
boost::locale::boundary::ssegment
segment< std::string::const_iterator > ssegment
convenience typedef
Definition: segment.hpp:436
boost::locale::boundary::segment::string_type
std::basic_string< char_type > string_type
Definition: segment.hpp:111
boost::locale::boundary::segment::str
string_type str() const
Definition: segment.hpp:183
boost::locale::boundary::segment::empty
bool empty() const
Definition: segment.hpp:200
boost::locale::boundary::segment::operator!=
bool operator!=(segment const &other)
Compare two segments.
Definition: segment.hpp:229
boost::locale::boundary::rule_type
uint32_t rule_type
Flags used with word boundary analysis – the type of the word, line or sentence boundary found...
Definition: types.hpp:51
boost::locale::boundary::segment::rule
void rule(rule_type r)
Definition: segment.hpp:215
boost::locale::boundary::operator>=
bool operator>=(segment< IteratorL > const &l, segment< IteratorR > const &r)
Compare two segments.
Definition: segment.hpp:273
boost::locale::boundary::segment::value_type
char_type value_type
Definition: segment.hpp:115
boost::locale::boundary::segment::rule
rule_type rule() const
Definition: segment.hpp:208
boost::locale::boundary::operator==
bool operator==(BaseIterator const &l, boundary_point< BaseIterator > const &r)
Definition: boundary_point.hpp:142
boost::locale::boundary::operator<<
std::basic_ostream< CharType, TraitsType > & operator<<(std::basic_ostream< CharType, TraitsType > &out, segment< Iterator > const &tok)
Definition: segment.hpp:462
boost::locale::boundary::segment::begin
IteratorType begin() const
Definition: segment.hpp:159
boost::locale::boundary::segment::const_iterator
IteratorType const_iterator
Definition: segment.hpp:123
boost::locale::boundary::segment::begin
void begin(iterator const &v)
Definition: segment.hpp:144
boost::locale::boundary::segment::segment
segment()
Definition: segment.hpp:132
boost::locale::boundary::wssegment
segment< std::wstring::const_iterator > wssegment
convenience typedef
Definition: segment.hpp:437
boost::locale::boundary::u32ssegment
segment< std::u32string::const_iterator > u32ssegment
convenience typedef
Definition: segment.hpp:442
boost::locale::boundary::u32csegment
segment< char32_t const * > u32csegment
convenience typedef
Definition: segment.hpp:451
boost::locale::boundary::segment::end
void end(iterator const &v)
Definition: segment.hpp:151
boost::locale::boundary::segment::iterator
IteratorType iterator
Definition: segment.hpp:119
boost::locale::boundary::segment::length
size_t length() const
Definition: segment.hpp:192
boost::locale::boundary::operator>
bool operator>(segment< IteratorL > const &l, segment< IteratorR > const &r)
Compare two segments.
Definition: segment.hpp:267
boost::locale::boundary::segment::difference_type
std::iterator_traits< IteratorType >::difference_type difference_type
Definition: segment.hpp:127
boost::locale::boundary::operator!=
bool operator!=(BaseIterator const &l, boundary_point< BaseIterator > const &r)
Definition: boundary_point.hpp:150
- © Copyright 2009-2012 Artyom Beilis, Distributed under the Boost Software License, Version 1.0.