123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383 |
- #ifndef BOOST_LOCALE_BOUNDARY_SEGMENT_HPP_INCLUDED
- #define BOOST_LOCALE_BOUNDARY_SEGMENT_HPP_INCLUDED
- #include <boost/locale/util/string.hpp>
- #include <iosfwd>
- #include <iterator>
- #include <locale>
- #include <string>
- #ifdef BOOST_MSVC
- # pragma warning(push)
- # pragma warning(disable : 4275 4251 4231 4660)
- #endif
- namespace boost { namespace locale { namespace boundary {
-
- namespace detail {
- 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(const Left& l, const Right& r)
- {
- return compare_text(l.begin(), l.end(), r.begin(), r.end());
- }
- template<typename Left, typename Char>
- int compare_string(const Left& l, const Char* begin)
- {
- return compare_text(l.begin(), l.end(), begin, util::str_end(begin));
- }
- template<typename Right, typename Char>
- int compare_string(const Char* begin, const Right& r)
- {
- return compare_text(begin, util::str_end(begin), 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() : rule_(0) {}
-
- segment(iterator b, iterator e, rule_type r) : std::pair<IteratorType, IteratorType>(b, e), rule_(r) {}
-
- void begin(const iterator& v) { this->first = v; }
-
- void end(const iterator& 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; }
-
-
- bool operator==(const segment& other) const { return detail::compare_text(*this, other) == 0; }
-
- bool operator!=(const segment& other) const { return detail::compare_text(*this, other) != 0; }
- private:
- rule_type rule_;
- };
-
- template<typename IteratorL, typename IteratorR>
- bool operator==(const segment<IteratorL>& l, const segment<IteratorR>& r)
- {
- return detail::compare_text(l, r) == 0;
- }
-
- template<typename IteratorL, typename IteratorR>
- bool operator!=(const segment<IteratorL>& l, const segment<IteratorR>& r)
- {
- return detail::compare_text(l, r) != 0;
- }
-
- template<typename IteratorL, typename IteratorR>
- bool operator<(const segment<IteratorL>& l, const segment<IteratorR>& r)
- {
- return detail::compare_text(l, r) < 0;
- }
-
- template<typename IteratorL, typename IteratorR>
- bool operator<=(const segment<IteratorL>& l, const segment<IteratorR>& r)
- {
- return detail::compare_text(l, r) <= 0;
- }
-
- template<typename IteratorL, typename IteratorR>
- bool operator>(const segment<IteratorL>& l, const segment<IteratorR>& r)
- {
- return detail::compare_text(l, r) > 0;
- }
-
- template<typename IteratorL, typename IteratorR>
- bool operator>=(const segment<IteratorL>& l, const segment<IteratorR>& r)
- {
- return detail::compare_text(l, r) >= 0;
- }
-
- template<typename CharType, typename Traits, typename Alloc, typename IteratorR>
- bool operator==(const std::basic_string<CharType, Traits, Alloc>& l, const segment<IteratorR>& r)
- {
- return detail::compare_text(l, r) == 0;
- }
-
- template<typename CharType, typename Traits, typename Alloc, typename IteratorR>
- bool operator!=(const std::basic_string<CharType, Traits, Alloc>& l, const segment<IteratorR>& r)
- {
- return detail::compare_text(l, r) != 0;
- }
-
- template<typename CharType, typename Traits, typename Alloc, typename IteratorR>
- bool operator<(const std::basic_string<CharType, Traits, Alloc>& l, const segment<IteratorR>& r)
- {
- return detail::compare_text(l, r) < 0;
- }
-
- template<typename CharType, typename Traits, typename Alloc, typename IteratorR>
- bool operator<=(const std::basic_string<CharType, Traits, Alloc>& l, const segment<IteratorR>& r)
- {
- return detail::compare_text(l, r) <= 0;
- }
-
- template<typename CharType, typename Traits, typename Alloc, typename IteratorR>
- bool operator>(const std::basic_string<CharType, Traits, Alloc>& l, const segment<IteratorR>& r)
- {
- return detail::compare_text(l, r) > 0;
- }
-
- template<typename CharType, typename Traits, typename Alloc, typename IteratorR>
- bool operator>=(const std::basic_string<CharType, Traits, Alloc>& l, const segment<IteratorR>& r)
- {
- return detail::compare_text(l, r) >= 0;
- }
-
- template<typename Iterator, typename CharType, typename Traits, typename Alloc>
- bool operator==(const segment<Iterator>& l, const std::basic_string<CharType, Traits, Alloc>& r)
- {
- return detail::compare_text(l, r) == 0;
- }
-
- template<typename Iterator, typename CharType, typename Traits, typename Alloc>
- bool operator!=(const segment<Iterator>& l, const std::basic_string<CharType, Traits, Alloc>& r)
- {
- return detail::compare_text(l, r) != 0;
- }
-
- template<typename Iterator, typename CharType, typename Traits, typename Alloc>
- bool operator<(const segment<Iterator>& l, const std::basic_string<CharType, Traits, Alloc>& r)
- {
- return detail::compare_text(l, r) < 0;
- }
-
- template<typename Iterator, typename CharType, typename Traits, typename Alloc>
- bool operator<=(const segment<Iterator>& l, const std::basic_string<CharType, Traits, Alloc>& r)
- {
- return detail::compare_text(l, r) <= 0;
- }
-
- template<typename Iterator, typename CharType, typename Traits, typename Alloc>
- bool operator>(const segment<Iterator>& l, const std::basic_string<CharType, Traits, Alloc>& r)
- {
- return detail::compare_text(l, r) > 0;
- }
-
- template<typename Iterator, typename CharType, typename Traits, typename Alloc>
- bool operator>=(const segment<Iterator>& l, const std::basic_string<CharType, Traits, Alloc>& r)
- {
- return detail::compare_text(l, r) >= 0;
- }
-
- template<typename CharType, typename IteratorR>
- bool operator==(const CharType* l, const segment<IteratorR>& r)
- {
- return detail::compare_string(l, r) == 0;
- }
-
- template<typename CharType, typename IteratorR>
- bool operator!=(const CharType* l, const segment<IteratorR>& r)
- {
- return detail::compare_string(l, r) != 0;
- }
-
- template<typename CharType, typename IteratorR>
- bool operator<(const CharType* l, const segment<IteratorR>& r)
- {
- return detail::compare_string(l, r) < 0;
- }
-
- template<typename CharType, typename IteratorR>
- bool operator<=(const CharType* l, const segment<IteratorR>& r)
- {
- return detail::compare_string(l, r) <= 0;
- }
-
- template<typename CharType, typename IteratorR>
- bool operator>(const CharType* l, const segment<IteratorR>& r)
- {
- return detail::compare_string(l, r) > 0;
- }
-
- template<typename CharType, typename IteratorR>
- bool operator>=(const CharType* l, const segment<IteratorR>& r)
- {
- return detail::compare_string(l, r) >= 0;
- }
-
- template<typename Iterator, typename CharType>
- bool operator==(const segment<Iterator>& l, const CharType* r)
- {
- return detail::compare_string(l, r) == 0;
- }
-
- template<typename Iterator, typename CharType>
- bool operator!=(const segment<Iterator>& l, const CharType* r)
- {
- return detail::compare_string(l, r) != 0;
- }
-
- template<typename Iterator, typename CharType>
- bool operator<(const segment<Iterator>& l, const CharType* r)
- {
- return detail::compare_string(l, r) < 0;
- }
-
- template<typename Iterator, typename CharType>
- bool operator<=(const segment<Iterator>& l, const CharType* r)
- {
- return detail::compare_string(l, r) <= 0;
- }
-
- template<typename Iterator, typename CharType>
- bool operator>(const segment<Iterator>& l, const CharType* r)
- {
- return detail::compare_string(l, r) > 0;
- }
-
- template<typename Iterator, typename CharType>
- bool operator>=(const segment<Iterator>& l, const CharType* r)
- {
- return detail::compare_string(l, r) >= 0;
- }
- typedef segment<std::string::const_iterator> ssegment;
- typedef segment<std::wstring::const_iterator> wssegment;
- #ifndef BOOST_LOCALE_NO_CXX20_STRING8
- typedef segment<std::u8string::const_iterator> u8ssegment;
- #endif
- #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<const char*> csegment;
- typedef segment<const wchar_t*> wcsegment;
- #ifdef __cpp_char8_t
- typedef segment<const char8_t*> u8csegment;
- #endif
- #ifdef BOOST_LOCALE_ENABLE_CHAR16_T
- typedef segment<const char16_t*> u16csegment;
- #endif
- #ifdef BOOST_LOCALE_ENABLE_CHAR32_T
- typedef segment<const char32_t*> u32csegment;
- #endif
-
- template<typename CharType, typename TraitsType, typename Iterator>
- std::basic_ostream<CharType, TraitsType>& operator<<(std::basic_ostream<CharType, TraitsType>& out,
- const segment<Iterator>& seg)
- {
- for(const auto& p : seg)
- out << p;
- return out;
- }
-
- }}}
- #ifdef BOOST_MSVC
- # pragma warning(pop)
- #endif
- #endif
|