segment.hpp
//
// Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_LOCALE_BOUNDARY_SEGMENT_HPP_INCLUDED
#define BOOST_LOCALE_BOUNDARY_SEGMENT_HPP_INCLUDED
#include <boost/locale/config.hpp>
#ifdef BOOST_MSVC
# pragma warning(push)
# pragma warning(disable : 4275 4251 4231 4660)
#endif
#include <locale>
#include <string>
#include <iosfwd>
#include <iterator>
namespace boost {
namespace locale {
namespace boundary {
namespace details {
template<typename LeftIterator,typename RightIterator>
int compare_text(LeftIterator l_begin,LeftIterator l_end,RightIterator r_begin,RightIterator r_end)
{
typedef LeftIterator left_iterator;
typedef typename std::iterator_traits<left_iterator>::value_type char_type;
typedef std::char_traits<char_type> traits;
while(l_begin!=l_end && r_begin!=r_end) {
char_type lchar = *l_begin++;
char_type rchar = *r_begin++;
if(traits::eq(lchar,rchar))
continue;
if(traits::lt(lchar,rchar))
return -1;
else
return 1;
}
if(l_begin==l_end && r_begin==r_end)
return 0;
if(l_begin==l_end)
return -1;
else
return 1;
}
template<typename Left,typename Right>
int compare_text(Left const &l,Right const &r)
{
return compare_text(l.begin(),l.end(),r.begin(),r.end());
}
template<typename Left,typename Char>
int compare_string(Left const &l,Char const *begin)
{
Char const *end = begin;
while(*end!=0)
end++;
return compare_text(l.begin(),l.end(),begin,end);
}
template<typename Right,typename Char>
int compare_string(Char const *begin,Right const &r)
{
Char const *end = begin;
while(*end!=0)
end++;
return compare_text(begin,end,r.begin(),r.end());
}
}
template<typename IteratorType>
class segment : public std::pair<IteratorType,IteratorType> {
public:
typedef typename std::iterator_traits<IteratorType>::value_type char_type;
typedef std::basic_string<char_type> string_type;
typedef char_type value_type;
typedef IteratorType iterator;
typedef IteratorType const_iterator;
typedef typename std::iterator_traits<IteratorType>::difference_type difference_type;
segment() {}
segment(iterator b,iterator e,rule_type r) :
std::pair<IteratorType,IteratorType>(b,e),
rule_(r)
{
}
void begin(iterator const &v)
{
this->first = v;
}
void end(iterator const &v)
{
this->second = v;
}
IteratorType begin() const
{
return this->first;
}
IteratorType end() const
{
return this->second;
}
template <class T, class A>
operator std::basic_string<char_type, T, A> ()const
{
return std::basic_string<char_type, T, A>(this->first, this->second);
}
string_type str() const
{
return string_type(begin(),end());
}
size_t length() const
{
return std::distance(begin(),end());
}
bool empty() const
{
return begin() == end();
}
rule_type rule() const
{
return rule_;
}
void rule(rule_type r)
{
rule_ = r;
}
// make sure we override std::pair's operator==
bool operator==(segment const &other)
{
return details::compare_text(*this,other) == 0;
}
bool operator!=(segment const &other)
{
return details::compare_text(*this,other) != 0;
}
private:
rule_type rule_;
};
template<typename IteratorL,typename IteratorR>
bool operator==(segment<IteratorL> const &l,segment<IteratorR> const &r)
{
return details::compare_text(l,r) == 0;
}
template<typename IteratorL,typename IteratorR>
bool operator!=(segment<IteratorL> const &l,segment<IteratorR> const &r)
{
return details::compare_text(l,r) != 0;
}
template<typename IteratorL,typename IteratorR>
bool operator<(segment<IteratorL> const &l,segment<IteratorR> const &r)
{
return details::compare_text(l,r) < 0;
}
template<typename IteratorL,typename IteratorR>
bool operator<=(segment<IteratorL> const &l,segment<IteratorR> const &r)
{
return details::compare_text(l,r) <= 0;
}
template<typename IteratorL,typename IteratorR>
bool operator>(segment<IteratorL> const &l,segment<IteratorR> const &r)
{
return details::compare_text(l,r) > 0;
}
template<typename IteratorL,typename IteratorR>
bool operator>=(segment<IteratorL> const &l,segment<IteratorR> const &r)
{
return details::compare_text(l,r) >= 0;
}
template<typename CharType,typename Traits,typename Alloc,typename IteratorR>
bool operator==(std::basic_string<CharType,Traits,Alloc> const &l,segment<IteratorR> const &r)
{
return details::compare_text(l,r) == 0;
}
template<typename CharType,typename Traits,typename Alloc,typename IteratorR>
bool operator!=(std::basic_string<CharType,Traits,Alloc> const &l,segment<IteratorR> const &r)
{
return details::compare_text(l,r) != 0;
}
template<typename CharType,typename Traits,typename Alloc,typename IteratorR>
bool operator<(std::basic_string<CharType,Traits,Alloc> const &l,segment<IteratorR> const &r)
{
return details::compare_text(l,r) < 0;
}
template<typename CharType,typename Traits,typename Alloc,typename IteratorR>
bool operator<=(std::basic_string<CharType,Traits,Alloc> const &l,segment<IteratorR> const &r)
{
return details::compare_text(l,r) <= 0;
}
template<typename CharType,typename Traits,typename Alloc,typename IteratorR>
bool operator>(std::basic_string<CharType,Traits,Alloc> const &l,segment<IteratorR> const &r)
{
return details::compare_text(l,r) > 0;
}
template<typename CharType,typename Traits,typename Alloc,typename IteratorR>
bool operator>=(std::basic_string<CharType,Traits,Alloc> const &l,segment<IteratorR> const &r)
{
return details::compare_text(l,r) >= 0;
}
template<typename Iterator,typename CharType,typename Traits,typename Alloc>
bool operator==(segment<Iterator> const &l,std::basic_string<CharType,Traits,Alloc> const &r)
{
return details::compare_text(l,r) == 0;
}
template<typename Iterator,typename CharType,typename Traits,typename Alloc>
bool operator!=(segment<Iterator> const &l,std::basic_string<CharType,Traits,Alloc> const &r)
{
return details::compare_text(l,r) != 0;
}
template<typename Iterator,typename CharType,typename Traits,typename Alloc>
bool operator<(segment<Iterator> const &l,std::basic_string<CharType,Traits,Alloc> const &r)
{
return details::compare_text(l,r) < 0;
}
template<typename Iterator,typename CharType,typename Traits,typename Alloc>
bool operator<=(segment<Iterator> const &l,std::basic_string<CharType,Traits,Alloc> const &r)
{
return details::compare_text(l,r) <= 0;
}
template<typename Iterator,typename CharType,typename Traits,typename Alloc>
bool operator>(segment<Iterator> const &l,std::basic_string<CharType,Traits,Alloc> const &r)
{
return details::compare_text(l,r) > 0;
}
template<typename Iterator,typename CharType,typename Traits,typename Alloc>
bool operator>=(segment<Iterator> const &l,std::basic_string<CharType,Traits,Alloc> const &r)
{
return details::compare_text(l,r) >= 0;
}
template<typename CharType,typename IteratorR>
bool operator==(CharType const *l,segment<IteratorR> const &r)
{
return details::compare_string(l,r) == 0;
}
template<typename CharType,typename IteratorR>
bool operator!=(CharType const *l,segment<IteratorR> const &r)
{
return details::compare_string(l,r) != 0;
}
template<typename CharType,typename IteratorR>
bool operator<(CharType const *l,segment<IteratorR> const &r)
{
return details::compare_string(l,r) < 0;
}
template<typename CharType,typename IteratorR>
bool operator<=(CharType const *l,segment<IteratorR> const &r)
{
return details::compare_string(l,r) <= 0;
}
template<typename CharType,typename IteratorR>
bool operator>(CharType const *l,segment<IteratorR> const &r)
{
return details::compare_string(l,r) > 0;
}
template<typename CharType,typename IteratorR>
bool operator>=(CharType const *l,segment<IteratorR> const &r)
{
return details::compare_string(l,r) >= 0;
}
template<typename Iterator,typename CharType>
bool operator==(segment<Iterator> const &l,CharType const *r)
{
return details::compare_string(l,r) == 0;
}
template<typename Iterator,typename CharType>
bool operator!=(segment<Iterator> const &l,CharType const *r)
{
return details::compare_string(l,r) != 0;
}
template<typename Iterator,typename CharType>
bool operator<(segment<Iterator> const &l,CharType const *r)
{
return details::compare_string(l,r) < 0;
}
template<typename Iterator,typename CharType>
bool operator<=(segment<Iterator> const &l,CharType const *r)
{
return details::compare_string(l,r) <= 0;
}
template<typename Iterator,typename CharType>
bool operator>(segment<Iterator> const &l,CharType const *r)
{
return details::compare_string(l,r) > 0;
}
template<typename Iterator,typename CharType>
bool operator>=(segment<Iterator> const &l,CharType const *r)
{
return details::compare_string(l,r) >= 0;
}
typedef segment<std::string::const_iterator> ssegment;
typedef segment<std::wstring::const_iterator> wssegment;
#ifdef BOOST_LOCALE_ENABLE_CHAR16_T
typedef segment<std::u16string::const_iterator> u16ssegment;
#endif
#ifdef BOOST_LOCALE_ENABLE_CHAR32_T
typedef segment<std::u32string::const_iterator> u32ssegment;
#endif
typedef segment<char const *> csegment;
typedef segment<wchar_t const *> wcsegment;
#ifdef BOOST_LOCALE_ENABLE_CHAR16_T
typedef segment<char16_t const *> u16csegment;
#endif
#ifdef BOOST_LOCALE_ENABLE_CHAR32_T
typedef segment<char32_t const *> u32csegment;
#endif
template<typename CharType,typename TraitsType,typename Iterator>
std::basic_ostream<CharType,TraitsType> &operator<<(
std::basic_ostream<CharType,TraitsType> &out,
segment<Iterator> const &tok)
{
for(Iterator p=tok.begin(),e=tok.end();p!=e;++p)
out << *p;
return out;
}
} // boundary
} // locale
} // boost
#ifdef BOOST_MSVC
#pragma warning(pop)
#endif
#endif
// 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.