Boost.Locale
segment.hpp
1 //
2 // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
3 //
4 // Distributed under the Boost Software License, Version 1.0.
5 // https://www.boost.org/LICENSE_1_0.txt
6 
7 #ifndef BOOST_LOCALE_BOUNDARY_SEGMENT_HPP_INCLUDED
8 #define BOOST_LOCALE_BOUNDARY_SEGMENT_HPP_INCLUDED
9 
10 #include <boost/locale/config.hpp>
11 #include <boost/locale/util/string.hpp>
12 #include <iosfwd>
13 #include <iterator>
14 #include <locale>
15 #include <string>
16 
17 #ifdef BOOST_MSVC
18 # pragma warning(push)
19 # pragma warning(disable : 4275 4251 4231 4660)
20 #endif
21 
22 namespace boost { namespace locale { namespace boundary {
24  namespace detail {
25  template<typename LeftIterator, typename RightIterator>
26  int compare_text(LeftIterator l_begin, LeftIterator l_end, RightIterator r_begin, RightIterator r_end)
27  {
28  typedef LeftIterator left_iterator;
29  typedef typename std::iterator_traits<left_iterator>::value_type char_type;
30  typedef std::char_traits<char_type> traits;
31  while(l_begin != l_end && r_begin != r_end) {
32  char_type lchar = *l_begin++;
33  char_type rchar = *r_begin++;
34  if(traits::eq(lchar, rchar))
35  continue;
36  if(traits::lt(lchar, rchar))
37  return -1;
38  else
39  return 1;
40  }
41  if(l_begin == l_end && r_begin == r_end)
42  return 0;
43  if(l_begin == l_end)
44  return -1;
45  else
46  return 1;
47  }
48 
49  template<typename Left, typename Right>
50  int compare_text(const Left& l, const Right& r)
51  {
52  return compare_text(l.begin(), l.end(), r.begin(), r.end());
53  }
54 
55  template<typename Left, typename Char>
56  int compare_string(const Left& l, const Char* begin)
57  {
58  return compare_text(l.begin(), l.end(), begin, util::str_end(begin));
59  }
60 
61  template<typename Right, typename Char>
62  int compare_string(const Char* begin, const Right& r)
63  {
64  return compare_text(begin, util::str_end(begin), r.begin(), r.end());
65  }
66 
67  } // namespace detail
69 
72 
90  template<typename IteratorType>
91  class segment : public std::pair<IteratorType, IteratorType> {
92  public:
94  typedef typename std::iterator_traits<IteratorType>::value_type char_type;
96  typedef std::basic_string<char_type> string_type;
100  typedef IteratorType iterator;
102  typedef IteratorType const_iterator;
104  typedef typename std::iterator_traits<IteratorType>::difference_type difference_type;
105 
107  segment() : rule_(0) {}
109  segment(iterator b, iterator e, rule_type r) : std::pair<IteratorType, IteratorType>(b, e), rule_(r) {}
111  void begin(const iterator& v) { this->first = v; }
113  void end(const iterator& v) { this->second = v; }
114 
116  IteratorType begin() const { return this->first; }
118  IteratorType end() const { return this->second; }
119 
121  template<class T, class A>
122  operator std::basic_string<char_type, T, A>() const
123  {
124  return std::basic_string<char_type, T, A>(this->first, this->second);
125  }
126 
128  string_type str() const { return string_type(begin(), end()); }
129 
131  size_t length() const { return std::distance(begin(), end()); }
132 
134  bool empty() const { return begin() == end(); }
135 
137  rule_type rule() const { return rule_; }
139  void rule(rule_type r) { rule_ = r; }
140 
141  // make sure we override std::pair's operator==
142 
144  bool operator==(const segment& other) const { return detail::compare_text(*this, other) == 0; }
146  bool operator!=(const segment& other) const { return detail::compare_text(*this, other) != 0; }
147 
148  private:
149  rule_type rule_;
150  };
151 
153  template<typename IteratorL, typename IteratorR>
155  {
156  return detail::compare_text(l, r) == 0;
157  }
159  template<typename IteratorL, typename IteratorR>
161  {
162  return detail::compare_text(l, r) != 0;
163  }
164 
166  template<typename IteratorL, typename IteratorR>
168  {
169  return detail::compare_text(l, r) < 0;
170  }
172  template<typename IteratorL, typename IteratorR>
174  {
175  return detail::compare_text(l, r) <= 0;
176  }
178  template<typename IteratorL, typename IteratorR>
180  {
181  return detail::compare_text(l, r) > 0;
182  }
184  template<typename IteratorL, typename IteratorR>
186  {
187  return detail::compare_text(l, r) >= 0;
188  }
189 
191  template<typename CharType, typename Traits, typename Alloc, typename IteratorR>
192  bool operator==(const std::basic_string<CharType, Traits, Alloc>& l, const segment<IteratorR>& r)
193  {
194  return detail::compare_text(l, r) == 0;
195  }
197  template<typename CharType, typename Traits, typename Alloc, typename IteratorR>
198  bool operator!=(const std::basic_string<CharType, Traits, Alloc>& l, const segment<IteratorR>& r)
199  {
200  return detail::compare_text(l, r) != 0;
201  }
202 
204  template<typename CharType, typename Traits, typename Alloc, typename IteratorR>
205  bool operator<(const std::basic_string<CharType, Traits, Alloc>& l, const segment<IteratorR>& r)
206  {
207  return detail::compare_text(l, r) < 0;
208  }
210  template<typename CharType, typename Traits, typename Alloc, typename IteratorR>
211  bool operator<=(const std::basic_string<CharType, Traits, Alloc>& l, const segment<IteratorR>& r)
212  {
213  return detail::compare_text(l, r) <= 0;
214  }
216  template<typename CharType, typename Traits, typename Alloc, typename IteratorR>
217  bool operator>(const std::basic_string<CharType, Traits, Alloc>& l, const segment<IteratorR>& r)
218  {
219  return detail::compare_text(l, r) > 0;
220  }
222  template<typename CharType, typename Traits, typename Alloc, typename IteratorR>
223  bool operator>=(const std::basic_string<CharType, Traits, Alloc>& l, const segment<IteratorR>& r)
224  {
225  return detail::compare_text(l, r) >= 0;
226  }
227 
229  template<typename Iterator, typename CharType, typename Traits, typename Alloc>
230  bool operator==(const segment<Iterator>& l, const std::basic_string<CharType, Traits, Alloc>& r)
231  {
232  return detail::compare_text(l, r) == 0;
233  }
235  template<typename Iterator, typename CharType, typename Traits, typename Alloc>
236  bool operator!=(const segment<Iterator>& l, const std::basic_string<CharType, Traits, Alloc>& r)
237  {
238  return detail::compare_text(l, r) != 0;
239  }
240 
242  template<typename Iterator, typename CharType, typename Traits, typename Alloc>
243  bool operator<(const segment<Iterator>& l, const std::basic_string<CharType, Traits, Alloc>& r)
244  {
245  return detail::compare_text(l, r) < 0;
246  }
248  template<typename Iterator, typename CharType, typename Traits, typename Alloc>
249  bool operator<=(const segment<Iterator>& l, const std::basic_string<CharType, Traits, Alloc>& r)
250  {
251  return detail::compare_text(l, r) <= 0;
252  }
254  template<typename Iterator, typename CharType, typename Traits, typename Alloc>
255  bool operator>(const segment<Iterator>& l, const std::basic_string<CharType, Traits, Alloc>& r)
256  {
257  return detail::compare_text(l, r) > 0;
258  }
260  template<typename Iterator, typename CharType, typename Traits, typename Alloc>
261  bool operator>=(const segment<Iterator>& l, const std::basic_string<CharType, Traits, Alloc>& r)
262  {
263  return detail::compare_text(l, r) >= 0;
264  }
265 
267  template<typename CharType, typename IteratorR>
268  bool operator==(const CharType* l, const segment<IteratorR>& r)
269  {
270  return detail::compare_string(l, r) == 0;
271  }
273  template<typename CharType, typename IteratorR>
274  bool operator!=(const CharType* l, const segment<IteratorR>& r)
275  {
276  return detail::compare_string(l, r) != 0;
277  }
278 
280  template<typename CharType, typename IteratorR>
281  bool operator<(const CharType* l, const segment<IteratorR>& r)
282  {
283  return detail::compare_string(l, r) < 0;
284  }
286  template<typename CharType, typename IteratorR>
287  bool operator<=(const CharType* l, const segment<IteratorR>& r)
288  {
289  return detail::compare_string(l, r) <= 0;
290  }
292  template<typename CharType, typename IteratorR>
293  bool operator>(const CharType* l, const segment<IteratorR>& r)
294  {
295  return detail::compare_string(l, r) > 0;
296  }
298  template<typename CharType, typename IteratorR>
299  bool operator>=(const CharType* l, const segment<IteratorR>& r)
300  {
301  return detail::compare_string(l, r) >= 0;
302  }
303 
305  template<typename Iterator, typename CharType>
306  bool operator==(const segment<Iterator>& l, const CharType* r)
307  {
308  return detail::compare_string(l, r) == 0;
309  }
311  template<typename Iterator, typename CharType>
312  bool operator!=(const segment<Iterator>& l, const CharType* r)
313  {
314  return detail::compare_string(l, r) != 0;
315  }
316 
318  template<typename Iterator, typename CharType>
319  bool operator<(const segment<Iterator>& l, const CharType* r)
320  {
321  return detail::compare_string(l, r) < 0;
322  }
324  template<typename Iterator, typename CharType>
325  bool operator<=(const segment<Iterator>& l, const CharType* r)
326  {
327  return detail::compare_string(l, r) <= 0;
328  }
330  template<typename Iterator, typename CharType>
331  bool operator>(const segment<Iterator>& l, const CharType* r)
332  {
333  return detail::compare_string(l, r) > 0;
334  }
336  template<typename Iterator, typename CharType>
337  bool operator>=(const segment<Iterator>& l, const CharType* r)
338  {
339  return detail::compare_string(l, r) >= 0;
340  }
341 
344 #ifdef BOOST_LOCALE_ENABLE_CHAR16_T
346 #endif
347 #ifdef BOOST_LOCALE_ENABLE_CHAR32_T
349 #endif
350 
353 #ifdef BOOST_LOCALE_ENABLE_CHAR16_T
355 #endif
356 #ifdef BOOST_LOCALE_ENABLE_CHAR32_T
358 #endif
359 
361  template<typename CharType, typename TraitsType, typename Iterator>
362  std::basic_ostream<CharType, TraitsType>& operator<<(std::basic_ostream<CharType, TraitsType>& out,
363  const segment<Iterator>& tok)
364  {
365  for(Iterator p = tok.begin(), e = tok.end(); p != e; ++p)
366  out << *p;
367  return out;
368  }
369 
371 
372 }}} // namespace boost::locale::boundary
373 
374 #ifdef BOOST_MSVC
375 # pragma warning(pop)
376 #endif
377 
378 #endif
IteratorType end() const
Set the end of the range.
Definition: segment.hpp:118
std::iterator_traits< IteratorType >::value_type char_type
The type of the underlying character.
Definition: segment.hpp:94
segment(iterator b, iterator e, rule_type r)
Create a segment using two iterators and a rule that represents this point.
Definition: segment.hpp:109
IteratorType begin() const
Get the start of the range.
Definition: segment.hpp:116
bool empty() const
Check if the segment is empty.
Definition: segment.hpp:134
a segment object that represents a pair of two iterators that define the range where this segment exi...
Definition: segment.hpp:91
rule_type rule() const
Get the rule that is used for selection of this segment.
Definition: segment.hpp:137
segment< std::u16string::const_iterator > u16ssegment
convenience typedef
Definition: segment.hpp:345
segment< std::string::const_iterator > ssegment
convenience typedef
Definition: segment.hpp:342
std::basic_string< char_type > string_type
The type of the string it is converted to.
Definition: segment.hpp:96
bool operator>(const segment< IteratorL > &l, const segment< IteratorR > &r)
Compare two segments.
Definition: segment.hpp:179
bool operator==(const BaseIterator &l, const boundary_point< BaseIterator > &r)
Check if the boundary point r points to same location as an iterator l.
Definition: boundary_point.hpp:88
string_type str() const
Create a string from the range explicitly.
Definition: segment.hpp:128
segment< const char * > csegment
convenience typedef
Definition: segment.hpp:351
bool operator<(const segment< IteratorL > &l, const segment< IteratorR > &r)
Compare two segments.
Definition: segment.hpp:167
uint32_t rule_type
Flags used with word boundary analysis – the type of the word, line or sentence boundary found.
Definition: types.hpp:40
void rule(rule_type r)
Set a rule that is used for segment selection.
Definition: segment.hpp:139
char_type value_type
The value that iterators return - the character itself.
Definition: segment.hpp:98
size_t length() const
Get the length of the text chunk.
Definition: segment.hpp:131
bool operator>=(const segment< IteratorL > &l, const segment< IteratorR > &r)
Compare two segments.
Definition: segment.hpp:185
bool operator!=(const BaseIterator &l, const boundary_point< BaseIterator > &r)
Check if the boundary point r points to different location from an iterator l.
Definition: boundary_point.hpp:94
Char * str_end(Char *str)
Return the end of a C-string, i.e. the pointer to the trailing NULL byte.
Definition: string.hpp:15
IteratorType const_iterator
The iterator that allows to iterate the range.
Definition: segment.hpp:102
bool operator==(const segment &other) const
Compare two segments.
Definition: segment.hpp:144
segment()
Default constructor.
Definition: segment.hpp:107
void begin(const iterator &v)
Set the start of the range.
Definition: segment.hpp:111
segment< std::wstring::const_iterator > wssegment
convenience typedef
Definition: segment.hpp:343
segment< std::u32string::const_iterator > u32ssegment
convenience typedef
Definition: segment.hpp:348
bool operator<=(const segment< IteratorL > &l, const segment< IteratorR > &r)
Compare two segments.
Definition: segment.hpp:173
bool operator!=(const segment &other) const
Compare two segments.
Definition: segment.hpp:146
std::basic_ostream< CharType, TraitsType > & operator<<(std::basic_ostream< CharType, TraitsType > &out, const segment< Iterator > &tok)
Write the segment to the stream character by character.
Definition: segment.hpp:362
IteratorType iterator
The iterator that allows to iterate the range.
Definition: segment.hpp:100
Generate boundary analysis facet.
segment< const wchar_t * > wcsegment
convenience typedef
Definition: segment.hpp:352
segment< const char32_t * > u32csegment
convenience typedef
Definition: segment.hpp:357
std::iterator_traits< IteratorType >::difference_type difference_type
The type that represent a difference between two iterators.
Definition: segment.hpp:104
void end(const iterator &v)
Set the end of the range.
Definition: segment.hpp:113
segment< const char16_t * > u16csegment
convenience typedef
Definition: segment.hpp:354