2 // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
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)
8 #ifndef BOOST_LOCALE_BOUNDARY_SEGMENT_HPP_INCLUDED
9 #define BOOST_LOCALE_BOUNDARY_SEGMENT_HPP_INCLUDED
10 #include <boost/locale/config.hpp>
12 # pragma warning(push)
13 # pragma warning(disable : 4275 4251 4231 4660)
26 template<typename LeftIterator,typename RightIterator>
27 int compare_text(LeftIterator l_begin,LeftIterator l_end,RightIterator r_begin,RightIterator r_end)
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))
37 if(traits::lt(lchar,rchar))
42 if(l_begin==l_end && r_begin==r_end)
51 template<typename Left,typename Right>
52 int compare_text(Left const &l,Right const &r)
54 return compare_text(l.begin(),l.end(),r.begin(),r.end());
57 template<typename Left,typename Char>
58 int compare_string(Left const &l,Char const *begin)
60 Char const *end = begin;
63 return compare_text(l.begin(),l.end(),begin,end);
66 template<typename Right,typename Char>
67 int compare_string(Char const *begin,Right const &r)
69 Char const *end = begin;
72 return compare_text(begin,end,r.begin(),r.end());
79 /// \addtogroup boundary
83 /// \brief a segment object that represents a pair of two iterators that define the range where
84 /// this segment exits and a rule that defines it.
86 /// This type of object is dereferenced by the iterators of segment_index. Using a rule() member function
87 /// you can get a specific rule this segment was selected with. For example, when you use
88 /// word boundary analysis, you can check if the specific word contains Kana letters by checking (rule() & \ref word_kana)!=0
89 /// For a sentence analysis you can check if the sentence is selected because a sentence terminator is found (\ref sentence_term) or
90 /// there is a line break (\ref sentence_sep).
92 /// This object can be automatically converted to std::basic_string with the same type of character. It is also
93 /// valid range that has begin() and end() member functions returning iterators on the location of the segment.
97 /// - \ref segment_index
98 /// - \ref boundary_point
99 /// - \ref boundary_point_index
101 template<typename IteratorType>
102 class segment : public std::pair<IteratorType,IteratorType> {
105 /// The type of the underlying character
107 typedef typename std::iterator_traits<IteratorType>::value_type char_type;
109 /// The type of the string it is converted to
111 typedef std::basic_string<char_type> string_type;
113 /// The value that iterators return - the character itself
115 typedef char_type value_type;
117 /// The iterator that allows to iterate the range
119 typedef IteratorType iterator;
121 /// The iterator that allows to iterate the range
123 typedef IteratorType const_iterator;
125 /// The type that represent a difference between two iterators
127 typedef typename std::iterator_traits<IteratorType>::difference_type difference_type;
130 /// Default constructor
134 /// Create a segment using two iterators and a rule that represents this point
136 segment(iterator b,iterator e,rule_type r) :
137 std::pair<IteratorType,IteratorType>(b,e),
142 /// Set the start of the range
144 void begin(iterator const &v)
149 /// Set the end of the range
151 void end(iterator const &v)
157 /// Get the start of the range
159 IteratorType begin() const
164 /// Set the end of the range
166 IteratorType end() const
172 /// Convert the range to a string automatically
174 template <class T, class A>
175 operator std::basic_string<char_type, T, A> ()const
177 return std::basic_string<char_type, T, A>(this->first, this->second);
181 /// Create a string from the range explicitly
183 string_type str() const
185 return string_type(begin(),end());
189 /// Get the length of the text chunk
192 size_t length() const
194 return std::distance(begin(),end());
198 /// Check if the segment is empty
202 return begin() == end();
206 /// Get the rule that is used for selection of this segment.
208 rule_type rule() const
213 /// Set a rule that is used for segment selection
215 void rule(rule_type r)
220 // make sure we override std::pair's operator==
222 /// Compare two segments
223 bool operator==(segment const &other)
225 return details::compare_text(*this,other) == 0;
228 /// Compare two segments
229 bool operator!=(segment const &other)
231 return details::compare_text(*this,other) != 0;
240 /// Compare two segments
241 template<typename IteratorL,typename IteratorR>
242 bool operator==(segment<IteratorL> const &l,segment<IteratorR> const &r)
244 return details::compare_text(l,r) == 0;
246 /// Compare two segments
247 template<typename IteratorL,typename IteratorR>
248 bool operator!=(segment<IteratorL> const &l,segment<IteratorR> const &r)
250 return details::compare_text(l,r) != 0;
253 /// Compare two segments
254 template<typename IteratorL,typename IteratorR>
255 bool operator<(segment<IteratorL> const &l,segment<IteratorR> const &r)
257 return details::compare_text(l,r) < 0;
259 /// Compare two segments
260 template<typename IteratorL,typename IteratorR>
261 bool operator<=(segment<IteratorL> const &l,segment<IteratorR> const &r)
263 return details::compare_text(l,r) <= 0;
265 /// Compare two segments
266 template<typename IteratorL,typename IteratorR>
267 bool operator>(segment<IteratorL> const &l,segment<IteratorR> const &r)
269 return details::compare_text(l,r) > 0;
271 /// Compare two segments
272 template<typename IteratorL,typename IteratorR>
273 bool operator>=(segment<IteratorL> const &l,segment<IteratorR> const &r)
275 return details::compare_text(l,r) >= 0;
278 /// Compare string and segment
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)
282 return details::compare_text(l,r) == 0;
284 /// Compare string and segment
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)
288 return details::compare_text(l,r) != 0;
291 /// Compare string and segment
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)
295 return details::compare_text(l,r) < 0;
297 /// Compare string and segment
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)
301 return details::compare_text(l,r) <= 0;
303 /// Compare string and segment
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)
307 return details::compare_text(l,r) > 0;
309 /// Compare string and segment
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)
313 return details::compare_text(l,r) >= 0;
316 /// Compare string and segment
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)
320 return details::compare_text(l,r) == 0;
322 /// Compare string and segment
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)
326 return details::compare_text(l,r) != 0;
329 /// Compare string and segment
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)
333 return details::compare_text(l,r) < 0;
335 /// Compare string and segment
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)
339 return details::compare_text(l,r) <= 0;
341 /// Compare string and segment
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)
345 return details::compare_text(l,r) > 0;
347 /// Compare string and segment
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)
351 return details::compare_text(l,r) >= 0;
355 /// Compare C string and segment
356 template<typename CharType,typename IteratorR>
357 bool operator==(CharType const *l,segment<IteratorR> const &r)
359 return details::compare_string(l,r) == 0;
361 /// Compare C string and segment
362 template<typename CharType,typename IteratorR>
363 bool operator!=(CharType const *l,segment<IteratorR> const &r)
365 return details::compare_string(l,r) != 0;
368 /// Compare C string and segment
369 template<typename CharType,typename IteratorR>
370 bool operator<(CharType const *l,segment<IteratorR> const &r)
372 return details::compare_string(l,r) < 0;
374 /// Compare C string and segment
375 template<typename CharType,typename IteratorR>
376 bool operator<=(CharType const *l,segment<IteratorR> const &r)
378 return details::compare_string(l,r) <= 0;
380 /// Compare C string and segment
381 template<typename CharType,typename IteratorR>
382 bool operator>(CharType const *l,segment<IteratorR> const &r)
384 return details::compare_string(l,r) > 0;
386 /// Compare C string and segment
387 template<typename CharType,typename IteratorR>
388 bool operator>=(CharType const *l,segment<IteratorR> const &r)
390 return details::compare_string(l,r) >= 0;
393 /// Compare C string and segment
394 template<typename Iterator,typename CharType>
395 bool operator==(segment<Iterator> const &l,CharType const *r)
397 return details::compare_string(l,r) == 0;
399 /// Compare C string and segment
400 template<typename Iterator,typename CharType>
401 bool operator!=(segment<Iterator> const &l,CharType const *r)
403 return details::compare_string(l,r) != 0;
406 /// Compare C string and segment
407 template<typename Iterator,typename CharType>
408 bool operator<(segment<Iterator> const &l,CharType const *r)
410 return details::compare_string(l,r) < 0;
412 /// Compare C string and segment
413 template<typename Iterator,typename CharType>
414 bool operator<=(segment<Iterator> const &l,CharType const *r)
416 return details::compare_string(l,r) <= 0;
418 /// Compare C string and segment
419 template<typename Iterator,typename CharType>
420 bool operator>(segment<Iterator> const &l,CharType const *r)
422 return details::compare_string(l,r) > 0;
424 /// Compare C string and segment
425 template<typename Iterator,typename CharType>
426 bool operator>=(segment<Iterator> const &l,CharType const *r)
428 return details::compare_string(l,r) >= 0;
436 typedef segment<std::string::const_iterator> ssegment; ///< convenience typedef
437 typedef segment<std::wstring::const_iterator> wssegment; ///< convenience typedef
438 #ifdef BOOST_LOCALE_ENABLE_CHAR16_T
439 typedef segment<std::u16string::const_iterator> u16ssegment;///< convenience typedef
441 #ifdef BOOST_LOCALE_ENABLE_CHAR32_T
442 typedef segment<std::u32string::const_iterator> u32ssegment;///< convenience typedef
445 typedef segment<char const *> csegment; ///< convenience typedef
446 typedef segment<wchar_t const *> wcsegment; ///< convenience typedef
447 #ifdef BOOST_LOCALE_ENABLE_CHAR16_T
448 typedef segment<char16_t const *> u16csegment; ///< convenience typedef
450 #ifdef BOOST_LOCALE_ENABLE_CHAR32_T
451 typedef segment<char32_t const *> u32csegment; ///< convenience typedef
459 /// Write the segment to the stream character by character
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)
466 for(Iterator p=tok.begin(),e=tok.end();p!=e;++p)
483 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4