1 ///////////////////////////////////////////////////////////////////////////////
2 // algorithm.hpp
3 //
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)
7 
8 #ifndef BOOST_XPRESSIVE_DETAIL_UTILITY_ALGORITHM_HPP_EAN_10_04_2005
9 #define BOOST_XPRESSIVE_DETAIL_UTILITY_ALGORITHM_HPP_EAN_10_04_2005
10 
11 // MS compatible compilers support #pragma once
12 #if defined(_MSC_VER)
13 # pragma once
14 #endif
15 
16 #include <string>
17 #include <climits>
18 #include <algorithm>
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>
27 
28 namespace boost { namespace xpressive { namespace detail
29 {
30 
31 ///////////////////////////////////////////////////////////////////////////////
32 // any
33 //
34 template<typename InIter, typename Pred>
any(InIter begin,InIter end,Pred pred)35 inline bool any(InIter begin, InIter end, Pred pred)
36 {
37     return end != std::find_if(begin, end, pred);
38 }
39 
40 ///////////////////////////////////////////////////////////////////////////////
41 // find_nth_if
42 //
43 template<typename FwdIter, typename Diff, typename Pred>
44 FwdIter find_nth_if(FwdIter begin, FwdIter end, Diff count, Pred pred)
45 {
46     for(; begin != end; ++begin)
47     {
48         if(pred(*begin) && 0 == count--)
49         {
50             return begin;
51         }
52     }
53 
54     return end;
55 }
56 
57 ///////////////////////////////////////////////////////////////////////////////
58 // toi
59 //
60 template<typename InIter, typename Traits>
toi(InIter & begin,InIter end,Traits const & tr,int radix=10,int max=INT_MAX)61 int toi(InIter &begin, InIter end, Traits const &tr, int radix = 10, int max = INT_MAX)
62 {
63     detail::ignore_unused(tr);
64     int i = 0, c = 0;
65     for(; begin != end && -1 != (c = tr.value(*begin, radix)); ++begin)
66     {
67         if(max < ((i *= radix) += c))
68             return i / radix;
69     }
70     return i;
71 }
72 
73 ///////////////////////////////////////////////////////////////////////////////
74 // advance_to
75 //
76 template<typename BidiIter, typename Diff>
advance_to_impl(BidiIter & iter,Diff diff,BidiIter end,std::bidirectional_iterator_tag)77 inline bool advance_to_impl(BidiIter & iter, Diff diff, BidiIter end, std::bidirectional_iterator_tag)
78 {
79     for(; 0 < diff && iter != end; --diff)
80         ++iter;
81     for(; 0 > diff && iter != end; ++diff)
82         --iter;
83     return 0 == diff;
84 }
85 
86 template<typename RandIter, typename Diff>
advance_to_impl(RandIter & iter,Diff diff,RandIter end,std::random_access_iterator_tag)87 inline bool advance_to_impl(RandIter & iter, Diff diff, RandIter end, std::random_access_iterator_tag)
88 {
89     if(0 < diff)
90     {
91         if((end - iter) < diff)
92             return false;
93     }
94     else if(0 > diff)
95     {
96         if((iter - end) < -diff)
97             return false;
98     }
99     iter += diff;
100     return true;
101 }
102 
103 template<typename Iter, typename Diff>
advance_to(Iter & iter,Diff diff,Iter end)104 inline bool advance_to(Iter & iter, Diff diff, Iter end)
105 {
106     return detail::advance_to_impl(iter, diff, end, typename iterator_category<Iter>::type());
107 }
108 
109 ///////////////////////////////////////////////////////////////////////////////
110 // range_data
111 //
112 template<typename T>
113 struct range_data
114   : range_value<T>
115 {};
116 
117 template<typename T>
118 struct range_data<T *>
119   : remove_const<T>
120 {};
121 
is_null_terminated(T const &)122 template<typename T> std::ptrdiff_t is_null_terminated(T const &) { return 0; }
123 #if BOOST_VERSION >= 103500
is_null_terminated(char const *)124 inline std::ptrdiff_t is_null_terminated(char const *) { return 1; }
125 #ifndef BOOST_XPRESSIVE_NO_WREGEX
is_null_terminated(wchar_t const *)126 inline std::ptrdiff_t is_null_terminated(wchar_t const *) { return 1; }
127 #endif
128 #endif
129 
130 ///////////////////////////////////////////////////////////////////////////////
131 // data_begin/data_end
132 //
133 template<typename Cont>
data_begin(Cont const & cont)134 typename range_data<Cont>::type const *data_begin(Cont const &cont)
135 {
136     return &*boost::begin(cont);
137 }
138 
139 template<typename Cont>
data_end(Cont const & cont)140 typename range_data<Cont>::type const *data_end(Cont const &cont)
141 {
142     return &*boost::begin(cont) + boost::size(cont) - is_null_terminated(cont);
143 }
144 
145 template<typename Char, typename Traits, typename Alloc>
146 Char const *data_begin(std::basic_string<Char, Traits, Alloc> const &str)
147 {
148     return str.data();
149 }
150 
151 template<typename Char, typename Traits, typename Alloc>
152 Char const *data_end(std::basic_string<Char, Traits, Alloc> const &str)
153 {
154     return str.data() + str.size();
155 }
156 
157 template<typename Char>
data_begin(Char const * const & sz)158 Char const *data_begin(Char const *const &sz)
159 {
160     return sz;
161 }
162 
163 template<typename Char>
data_end(Char const * const & sz)164 Char const *data_end(Char const *const &sz)
165 {
166     Char const *tmp = sz;
167     for(; *tmp; ++tmp)
168         ;
169     return tmp;
170 }
171 
172 }}}
173 
174 #endif
175