/////////////////////////////////////////////////////////////////////////////// // algorithm.hpp // // Copyright 2007 Eric Niebler. 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_XPRESSIVE_DETAIL_UTILITY_ALGORITHM_HPP_EAN_10_04_2005 #define BOOST_XPRESSIVE_DETAIL_UTILITY_ALGORITHM_HPP_EAN_10_04_2005 // MS compatible compilers support #pragma once #if defined(_MSC_VER) && (_MSC_VER >= 1020) # pragma once #endif #include #include #include #include #include #include #include #include #include #include #include namespace boost { namespace xpressive { namespace detail { /////////////////////////////////////////////////////////////////////////////// // any // template inline bool any(InIter begin, InIter end, Pred pred) { return end != std::find_if(begin, end, pred); } /////////////////////////////////////////////////////////////////////////////// // find_nth_if // template FwdIter find_nth_if(FwdIter begin, FwdIter end, Diff count, Pred pred) { for(; begin != end; ++begin) { if(pred(*begin) && 0 == count--) { return begin; } } return end; } /////////////////////////////////////////////////////////////////////////////// // toi // template int toi(InIter &begin, InIter end, Traits const &traits, int radix = 10, int max = INT_MAX) { detail::ignore_unused(traits); int i = 0, c = 0; for(; begin != end && -1 != (c = traits.value(*begin, radix)); ++begin) { if(max < ((i *= radix) += c)) return i / radix; } return i; } /////////////////////////////////////////////////////////////////////////////// // advance_to // template inline bool advance_to_impl(BidiIter & iter, Diff diff, BidiIter end, std::bidirectional_iterator_tag) { for(; 0 < diff && iter != end; --diff) ++iter; for(; 0 > diff && iter != end; ++diff) --iter; return 0 == diff; } template inline bool advance_to_impl(RandIter & iter, Diff diff, RandIter end, std::random_access_iterator_tag) { if(0 < diff) { if((end - iter) < diff) return false; } else if(0 > diff) { if((iter - end) < -diff) return false; } iter += diff; return true; } template inline bool advance_to(Iter & iter, Diff diff, Iter end) { return detail::advance_to_impl(iter, diff, end, typename iterator_category::type()); } /////////////////////////////////////////////////////////////////////////////// // range_data // template struct range_data : range_value {}; template struct range_data : remove_const {}; template std::ptrdiff_t is_null_terminated(T const &) { return 0; } #if BOOST_VERSION >= 103500 inline std::ptrdiff_t is_null_terminated(char const *) { return 1; } #ifndef BOOST_XPRESSIVE_NO_WREGEX inline std::ptrdiff_t is_null_terminated(wchar_t const *) { return 1; } #endif #endif /////////////////////////////////////////////////////////////////////////////// // data_begin/data_end // template typename range_data::type const *data_begin(Cont const &cont) { return &*boost::begin(cont); } template typename range_data::type const *data_end(Cont const &cont) { return &*boost::begin(cont) + boost::size(cont) - is_null_terminated(cont); } template Char const *data_begin(std::basic_string const &str) { return str.data(); } template Char const *data_end(std::basic_string const &str) { return str.data() + str.size(); } template Char const *data_begin(Char const *const &sz) { return sz; } template Char const *data_end(Char const *const &sz) { Char const *tmp = sz; for(; *tmp; ++tmp) ; return tmp; } }}} #endif