1 ///////////////////////////////////////////////////////////////////////////////
4 // Copyright 2008 Eric Niebler. Distributed under the Boost
5 // Software License, Version 1.0. (See accompanying file
6 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 #ifndef BOOST_XPRESSIVE_DETAIL_UTILITY_ALGORITHM_HPP_EAN_10_04_2005
9 #define BOOST_XPRESSIVE_DETAIL_UTILITY_ALGORITHM_HPP_EAN_10_04_2005
11 // MS compatible compilers support #pragma once
19 #include <boost/version.hpp>
20 #include <boost/range/end.hpp>
21 #include <boost/range/begin.hpp>
22 #include <boost/range/size.hpp>
23 #include <boost/range/value_type.hpp>
24 #include <boost/type_traits/remove_const.hpp>
25 #include <boost/iterator/iterator_traits.hpp>
26 #include <boost/xpressive/detail/utility/ignore_unused.hpp>
28 namespace boost { namespace xpressive { namespace detail
31 ///////////////////////////////////////////////////////////////////////////////
34 template<typename InIter, typename Pred>
35 inline bool any(InIter begin, InIter end, Pred pred)
37 return end != std::find_if(begin, end, pred);
40 ///////////////////////////////////////////////////////////////////////////////
43 template<typename FwdIter, typename Diff, typename Pred>
44 FwdIter find_nth_if(FwdIter begin, FwdIter end, Diff count, Pred pred)
46 for(; begin != end; ++begin)
48 if(pred(*begin) && 0 == count--)
57 ///////////////////////////////////////////////////////////////////////////////
60 template<typename InIter, typename Traits>
61 int toi(InIter &begin, InIter end, Traits const &tr, int radix = 10, int max = INT_MAX)
63 detail::ignore_unused(tr);
65 for(; begin != end && -1 != (c = tr.value(*begin, radix)); ++begin)
67 if(max < ((i *= radix) += c))
73 ///////////////////////////////////////////////////////////////////////////////
76 template<typename BidiIter, typename Diff>
77 inline bool advance_to_impl(BidiIter & iter, Diff diff, BidiIter end, std::bidirectional_iterator_tag)
79 for(; 0 < diff && iter != end; --diff)
81 for(; 0 > diff && iter != end; ++diff)
86 template<typename RandIter, typename Diff>
87 inline bool advance_to_impl(RandIter & iter, Diff diff, RandIter end, std::random_access_iterator_tag)
91 if((end - iter) < diff)
96 if((iter - end) < -diff)
103 template<typename Iter, typename Diff>
104 inline bool advance_to(Iter & iter, Diff diff, Iter end)
106 return detail::advance_to_impl(iter, diff, end, typename iterator_category<Iter>::type());
109 ///////////////////////////////////////////////////////////////////////////////
118 struct range_data<T *>
122 template<typename T> std::ptrdiff_t is_null_terminated(T const &) { return 0; }
123 #if BOOST_VERSION >= 103500
124 inline std::ptrdiff_t is_null_terminated(char const *) { return 1; }
125 #ifndef BOOST_XPRESSIVE_NO_WREGEX
126 inline std::ptrdiff_t is_null_terminated(wchar_t const *) { return 1; }
130 ///////////////////////////////////////////////////////////////////////////////
131 // data_begin/data_end
133 template<typename Cont>
134 typename range_data<Cont>::type const *data_begin(Cont const &cont)
136 return &*boost::begin(cont);
139 template<typename Cont>
140 typename range_data<Cont>::type const *data_end(Cont const &cont)
142 return &*boost::begin(cont) + boost::size(cont) - is_null_terminated(cont);
145 template<typename Char, typename Traits, typename Alloc>
146 Char const *data_begin(std::basic_string<Char, Traits, Alloc> const &str)
151 template<typename Char, typename Traits, typename Alloc>
152 Char const *data_end(std::basic_string<Char, Traits, Alloc> const &str)
154 return str.data() + str.size();
157 template<typename Char>
158 Char const *data_begin(Char const *const &sz)
163 template<typename Char>
164 Char const *data_end(Char const *const &sz)
166 Char const *tmp = sz;