Boost.Locale
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() {}
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>
243  {
244  return details::compare_text(l,r) == 0;
245  }
247  template<typename IteratorL,typename IteratorR>
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>
268  {
269  return details::compare_text(l,r) > 0;
270  }
272  template<typename IteratorL,typename IteratorR>
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 
438  #ifdef BOOST_LOCALE_ENABLE_CHAR16_T
440  #endif
441  #ifdef BOOST_LOCALE_ENABLE_CHAR32_T
443  #endif
444 
447  #ifdef BOOST_LOCALE_ENABLE_CHAR16_T
449  #endif
450  #ifdef BOOST_LOCALE_ENABLE_CHAR32_T
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
segment< char const * > csegment
convenience typedef
Definition: segment.hpp:445
std::iterator_traits< IteratorType >::value_type char_type
Definition: segment.hpp:107
segment(iterator b, iterator e, rule_type r)
Definition: segment.hpp:136
a segment object that represents a pair of two iterators that define the range where this segment exi...
Definition: segment.hpp:102
segment< wchar_t const * > wcsegment
convenience typedef
Definition: segment.hpp:446
IteratorType end() const
Definition: segment.hpp:166
bool operator==(segment const &other)
Compare two segments.
Definition: segment.hpp:223
segment< char16_t const * > u16csegment
convenience typedef
Definition: segment.hpp:448
segment< std::u16string::const_iterator > u16ssegment
convenience typedef
Definition: segment.hpp:439
segment< std::string::const_iterator > ssegment
convenience typedef
Definition: segment.hpp:436
std::basic_string< char_type > string_type
Definition: segment.hpp:111
string_type str() const
Definition: segment.hpp:183
bool empty() const
Definition: segment.hpp:200
bool operator!=(segment const &other)
Compare two segments.
Definition: segment.hpp:229
uint32_t rule_type
Flags used with word boundary analysis – the type of the word, line or sentence boundary found...
Definition: types.hpp:51
void rule(rule_type r)
Definition: segment.hpp:215
bool operator>=(segment< IteratorL > const &l, segment< IteratorR > const &r)
Compare two segments.
Definition: segment.hpp:273
char_type value_type
Definition: segment.hpp:115
rule_type rule() const
Definition: segment.hpp:208
bool operator==(BaseIterator const &l, boundary_point< BaseIterator > const &r)
Definition: boundary_point.hpp:142
std::basic_ostream< CharType, TraitsType > & operator<<(std::basic_ostream< CharType, TraitsType > &out, segment< Iterator > const &tok)
Definition: segment.hpp:462
IteratorType begin() const
Definition: segment.hpp:159
IteratorType const_iterator
Definition: segment.hpp:123
void begin(iterator const &v)
Definition: segment.hpp:144
segment()
Definition: segment.hpp:132
segment< std::wstring::const_iterator > wssegment
convenience typedef
Definition: segment.hpp:437
segment< std::u32string::const_iterator > u32ssegment
convenience typedef
Definition: segment.hpp:442
segment< char32_t const * > u32csegment
convenience typedef
Definition: segment.hpp:451
void end(iterator const &v)
Definition: segment.hpp:151
IteratorType iterator
Definition: segment.hpp:119
size_t length() const
Definition: segment.hpp:192
bool operator>(segment< IteratorL > const &l, segment< IteratorR > const &r)
Compare two segments.
Definition: segment.hpp:267
std::iterator_traits< IteratorType >::difference_type difference_type
Definition: segment.hpp:127
bool operator!=(BaseIterator const &l, boundary_point< BaseIterator > const &r)
Definition: boundary_point.hpp:150