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/util/string.hpp>
11 #include <iosfwd>
12 #include <iterator>
13 #include <locale>
14 #include <string>
15 
16 #ifdef BOOST_MSVC
17 # pragma warning(push)
18 # pragma warning(disable : 4275 4251 4231 4660)
19 #endif
20 
21 namespace boost { namespace locale { namespace boundary {
23  namespace detail {
24  template<typename LeftIterator, typename RightIterator>
25  int compare_text(LeftIterator l_begin, LeftIterator l_end, RightIterator r_begin, RightIterator r_end)
26  {
27  typedef LeftIterator left_iterator;
28  typedef typename std::iterator_traits<left_iterator>::value_type char_type;
29  typedef std::char_traits<char_type> traits;
30  while(l_begin != l_end && r_begin != r_end) {
31  char_type lchar = *l_begin++;
32  char_type rchar = *r_begin++;
33  if(traits::eq(lchar, rchar))
34  continue;
35  if(traits::lt(lchar, rchar))
36  return -1;
37  else
38  return 1;
39  }
40  if(l_begin == l_end && r_begin == r_end)
41  return 0;
42  if(l_begin == l_end)
43  return -1;
44  else
45  return 1;
46  }
47 
48  template<typename Left, typename Right>
49  int compare_text(const Left& l, const Right& r)
50  {
51  return compare_text(l.begin(), l.end(), r.begin(), r.end());
52  }
53 
54  template<typename Left, typename Char>
55  int compare_string(const Left& l, const Char* begin)
56  {
57  return compare_text(l.begin(), l.end(), begin, util::str_end(begin));
58  }
59 
60  template<typename Right, typename Char>
61  int compare_string(const Char* begin, const Right& r)
62  {
63  return compare_text(begin, util::str_end(begin), r.begin(), r.end());
64  }
65 
66  } // namespace detail
68 
71 
89  template<typename IteratorType>
90  class segment : public std::pair<IteratorType, IteratorType> {
91  public:
93  typedef typename std::iterator_traits<IteratorType>::value_type char_type;
95  typedef std::basic_string<char_type> string_type;
99  typedef IteratorType iterator;
101  typedef IteratorType const_iterator;
103  typedef typename std::iterator_traits<IteratorType>::difference_type difference_type;
104 
106  segment() : rule_(0) {}
108  segment(iterator b, iterator e, rule_type r) : std::pair<IteratorType, IteratorType>(b, e), rule_(r) {}
110  void begin(const iterator& v) { this->first = v; }
112  void end(const iterator& v) { this->second = v; }
113 
115  IteratorType begin() const { return this->first; }
117  IteratorType end() const { return this->second; }
118 
120  template<class T, class A>
121  operator std::basic_string<char_type, T, A>() const
122  {
123  return std::basic_string<char_type, T, A>(this->first, this->second);
124  }
125 
127  string_type str() const { return string_type(begin(), end()); }
128 
130  size_t length() const { return std::distance(begin(), end()); }
131 
133  bool empty() const { return begin() == end(); }
134 
136  rule_type rule() const { return rule_; }
138  void rule(rule_type r) { rule_ = r; }
139 
140  // make sure we override std::pair's operator==
141 
143  bool operator==(const segment& other) const { return detail::compare_text(*this, other) == 0; }
145  bool operator!=(const segment& other) const { return detail::compare_text(*this, other) != 0; }
146 
147  private:
148  rule_type rule_;
149  };
150 
152  template<typename IteratorL, typename IteratorR>
154  {
155  return detail::compare_text(l, r) == 0;
156  }
158  template<typename IteratorL, typename IteratorR>
160  {
161  return detail::compare_text(l, r) != 0;
162  }
163 
165  template<typename IteratorL, typename IteratorR>
167  {
168  return detail::compare_text(l, r) < 0;
169  }
171  template<typename IteratorL, typename IteratorR>
173  {
174  return detail::compare_text(l, r) <= 0;
175  }
177  template<typename IteratorL, typename IteratorR>
179  {
180  return detail::compare_text(l, r) > 0;
181  }
183  template<typename IteratorL, typename IteratorR>
185  {
186  return detail::compare_text(l, r) >= 0;
187  }
188 
190  template<typename CharType, typename Traits, typename Alloc, typename IteratorR>
191  bool operator==(const std::basic_string<CharType, Traits, Alloc>& l, const segment<IteratorR>& r)
192  {
193  return detail::compare_text(l, r) == 0;
194  }
196  template<typename CharType, typename Traits, typename Alloc, typename IteratorR>
197  bool operator!=(const std::basic_string<CharType, Traits, Alloc>& l, const segment<IteratorR>& r)
198  {
199  return detail::compare_text(l, r) != 0;
200  }
201 
203  template<typename CharType, typename Traits, typename Alloc, typename IteratorR>
204  bool operator<(const std::basic_string<CharType, Traits, Alloc>& l, const segment<IteratorR>& r)
205  {
206  return detail::compare_text(l, r) < 0;
207  }
209  template<typename CharType, typename Traits, typename Alloc, typename IteratorR>
210  bool operator<=(const std::basic_string<CharType, Traits, Alloc>& l, const segment<IteratorR>& r)
211  {
212  return detail::compare_text(l, r) <= 0;
213  }
215  template<typename CharType, typename Traits, typename Alloc, typename IteratorR>
216  bool operator>(const std::basic_string<CharType, Traits, Alloc>& l, const segment<IteratorR>& r)
217  {
218  return detail::compare_text(l, r) > 0;
219  }
221  template<typename CharType, typename Traits, typename Alloc, typename IteratorR>
222  bool operator>=(const std::basic_string<CharType, Traits, Alloc>& l, const segment<IteratorR>& r)
223  {
224  return detail::compare_text(l, r) >= 0;
225  }
226 
228  template<typename Iterator, typename CharType, typename Traits, typename Alloc>
229  bool operator==(const segment<Iterator>& l, const std::basic_string<CharType, Traits, Alloc>& r)
230  {
231  return detail::compare_text(l, r) == 0;
232  }
234  template<typename Iterator, typename CharType, typename Traits, typename Alloc>
235  bool operator!=(const segment<Iterator>& l, const std::basic_string<CharType, Traits, Alloc>& r)
236  {
237  return detail::compare_text(l, r) != 0;
238  }
239 
241  template<typename Iterator, typename CharType, typename Traits, typename Alloc>
242  bool operator<(const segment<Iterator>& l, const std::basic_string<CharType, Traits, Alloc>& r)
243  {
244  return detail::compare_text(l, r) < 0;
245  }
247  template<typename Iterator, typename CharType, typename Traits, typename Alloc>
248  bool operator<=(const segment<Iterator>& l, const std::basic_string<CharType, Traits, Alloc>& r)
249  {
250  return detail::compare_text(l, r) <= 0;
251  }
253  template<typename Iterator, typename CharType, typename Traits, typename Alloc>
254  bool operator>(const segment<Iterator>& l, const std::basic_string<CharType, Traits, Alloc>& r)
255  {
256  return detail::compare_text(l, r) > 0;
257  }
259  template<typename Iterator, typename CharType, typename Traits, typename Alloc>
260  bool operator>=(const segment<Iterator>& l, const std::basic_string<CharType, Traits, Alloc>& r)
261  {
262  return detail::compare_text(l, r) >= 0;
263  }
264 
266  template<typename CharType, typename IteratorR>
267  bool operator==(const CharType* l, const segment<IteratorR>& r)
268  {
269  return detail::compare_string(l, r) == 0;
270  }
272  template<typename CharType, typename IteratorR>
273  bool operator!=(const CharType* l, const segment<IteratorR>& r)
274  {
275  return detail::compare_string(l, r) != 0;
276  }
277 
279  template<typename CharType, typename IteratorR>
280  bool operator<(const CharType* l, const segment<IteratorR>& r)
281  {
282  return detail::compare_string(l, r) < 0;
283  }
285  template<typename CharType, typename IteratorR>
286  bool operator<=(const CharType* l, const segment<IteratorR>& r)
287  {
288  return detail::compare_string(l, r) <= 0;
289  }
291  template<typename CharType, typename IteratorR>
292  bool operator>(const CharType* l, const segment<IteratorR>& r)
293  {
294  return detail::compare_string(l, r) > 0;
295  }
297  template<typename CharType, typename IteratorR>
298  bool operator>=(const CharType* l, const segment<IteratorR>& r)
299  {
300  return detail::compare_string(l, r) >= 0;
301  }
302 
304  template<typename Iterator, typename CharType>
305  bool operator==(const segment<Iterator>& l, const CharType* r)
306  {
307  return detail::compare_string(l, r) == 0;
308  }
310  template<typename Iterator, typename CharType>
311  bool operator!=(const segment<Iterator>& l, const CharType* r)
312  {
313  return detail::compare_string(l, r) != 0;
314  }
315 
317  template<typename Iterator, typename CharType>
318  bool operator<(const segment<Iterator>& l, const CharType* r)
319  {
320  return detail::compare_string(l, r) < 0;
321  }
323  template<typename Iterator, typename CharType>
324  bool operator<=(const segment<Iterator>& l, const CharType* r)
325  {
326  return detail::compare_string(l, r) <= 0;
327  }
329  template<typename Iterator, typename CharType>
330  bool operator>(const segment<Iterator>& l, const CharType* r)
331  {
332  return detail::compare_string(l, r) > 0;
333  }
335  template<typename Iterator, typename CharType>
336  bool operator>=(const segment<Iterator>& l, const CharType* r)
337  {
338  return detail::compare_string(l, r) >= 0;
339  }
340 
343 #ifdef BOOST_LOCALE_ENABLE_CHAR16_T
345 #endif
346 #ifdef BOOST_LOCALE_ENABLE_CHAR32_T
348 #endif
349 
352 #ifdef BOOST_LOCALE_ENABLE_CHAR16_T
354 #endif
355 #ifdef BOOST_LOCALE_ENABLE_CHAR32_T
357 #endif
358 
360  template<typename CharType, typename TraitsType, typename Iterator>
361  std::basic_ostream<CharType, TraitsType>& operator<<(std::basic_ostream<CharType, TraitsType>& out,
362  const segment<Iterator>& tok)
363  {
364  for(Iterator p = tok.begin(), e = tok.end(); p != e; ++p)
365  out << *p;
366  return out;
367  }
368 
370 
371 }}} // namespace boost::locale::boundary
372 
373 #ifdef BOOST_MSVC
374 # pragma warning(pop)
375 #endif
376 
377 #endif
IteratorType end() const
Set the end of the range.
Definition: segment.hpp:117
std::iterator_traits< IteratorType >::value_type char_type
The type of the underlying character.
Definition: segment.hpp:93
segment(iterator b, iterator e, rule_type r)
Create a segment using two iterators and a rule that represents this point.
Definition: segment.hpp:108
IteratorType begin() const
Get the start of the range.
Definition: segment.hpp:115
bool empty() const
Check if the segment is empty.
Definition: segment.hpp:133
a segment object that represents a pair of two iterators that define the range where this segment exi...
Definition: segment.hpp:90
rule_type rule() const
Get the rule that is used for selection of this segment.
Definition: segment.hpp:136
segment< std::u16string::const_iterator > u16ssegment
convenience typedef
Definition: segment.hpp:344
segment< std::string::const_iterator > ssegment
convenience typedef
Definition: segment.hpp:341
std::basic_string< char_type > string_type
The type of the string it is converted to.
Definition: segment.hpp:95
bool operator>(const segment< IteratorL > &l, const segment< IteratorR > &r)
Compare two segments.
Definition: segment.hpp:178
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:127
segment< const char * > csegment
convenience typedef
Definition: segment.hpp:350
bool operator<(const segment< IteratorL > &l, const segment< IteratorR > &r)
Compare two segments.
Definition: segment.hpp:166
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:138
char_type value_type
The value that iterators return - the character itself.
Definition: segment.hpp:97
size_t length() const
Get the length of the text chunk.
Definition: segment.hpp:130
bool operator>=(const segment< IteratorL > &l, const segment< IteratorR > &r)
Compare two segments.
Definition: segment.hpp:184
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:101
bool operator==(const segment &other) const
Compare two segments.
Definition: segment.hpp:143
segment()
Default constructor.
Definition: segment.hpp:106
void begin(const iterator &v)
Set the start of the range.
Definition: segment.hpp:110
segment< std::wstring::const_iterator > wssegment
convenience typedef
Definition: segment.hpp:342
segment< std::u32string::const_iterator > u32ssegment
convenience typedef
Definition: segment.hpp:347
bool operator<=(const segment< IteratorL > &l, const segment< IteratorR > &r)
Compare two segments.
Definition: segment.hpp:172
bool operator!=(const segment &other) const
Compare two segments.
Definition: segment.hpp:145
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:361
IteratorType iterator
The iterator that allows to iterate the range.
Definition: segment.hpp:99
Generate boundary analysis facet.
segment< const wchar_t * > wcsegment
convenience typedef
Definition: segment.hpp:351
segment< const char32_t * > u32csegment
convenience typedef
Definition: segment.hpp:356
std::iterator_traits< IteratorType >::difference_type difference_type
The type that represent a difference between two iterators.
Definition: segment.hpp:103
void end(const iterator &v)
Set the end of the range.
Definition: segment.hpp:112
segment< const char16_t * > u16csegment
convenience typedef
Definition: segment.hpp:353