1 // Boost string_algo library sequence.hpp header file ---------------------------//
3 // Copyright Pavol Droba 2002-2003.
5 // Distributed under the Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 // See http://www.boost.org/ for updates, documentation, and revision history.
11 #ifndef BOOST_STRING_DETAIL_SEQUENCE_HPP
12 #define BOOST_STRING_DETAIL_SEQUENCE_HPP
14 #include <boost/algorithm/string/config.hpp>
15 #include <boost/mpl/bool.hpp>
16 #include <boost/mpl/logical.hpp>
17 #include <boost/range/begin.hpp>
18 #include <boost/range/end.hpp>
20 #include <boost/algorithm/string/sequence_traits.hpp>
26 // insert helpers -------------------------------------------------//
28 template< typename InputT, typename ForwardIteratorT >
31 BOOST_STRING_TYPENAME InputT::iterator At,
32 ForwardIteratorT Begin,
33 ForwardIteratorT End )
35 Input.insert( At, Begin, End );
38 template< typename InputT, typename InsertT >
41 BOOST_STRING_TYPENAME InputT::iterator At,
42 const InsertT& Insert )
44 ::boost::algorithm::detail::insert( Input, At, ::boost::begin(Insert), ::boost::end(Insert) );
47 // erase helper ---------------------------------------------------//
49 // Erase a range in the sequence
51 Returns the iterator pointing just after the erase subrange
53 template< typename InputT >
54 inline typename InputT::iterator erase(
56 BOOST_STRING_TYPENAME InputT::iterator From,
57 BOOST_STRING_TYPENAME InputT::iterator To )
59 return Input.erase( From, To );
62 // replace helper implementation ----------------------------------//
64 // Optimized version of replace for generic sequence containers
65 // Assumption: insert and erase are expensive
66 template< bool HasConstTimeOperations >
67 struct replace_const_time_helper
69 template< typename InputT, typename ForwardIteratorT >
72 BOOST_STRING_TYPENAME InputT::iterator From,
73 BOOST_STRING_TYPENAME InputT::iterator To,
74 ForwardIteratorT Begin,
75 ForwardIteratorT End )
77 // Copy data to the container ( as much as possible )
78 ForwardIteratorT InsertIt=Begin;
79 BOOST_STRING_TYPENAME InputT::iterator InputIt=From;
80 for(; InsertIt!=End && InputIt!=To; InsertIt++, InputIt++ )
87 // Replace sequence is longer, insert it
88 Input.insert( InputIt, InsertIt, End );
94 // Replace sequence is shorter, erase the rest
95 Input.erase( InputIt, To );
102 struct replace_const_time_helper< true >
104 // Const-time erase and insert methods -> use them
105 template< typename InputT, typename ForwardIteratorT >
108 BOOST_STRING_TYPENAME InputT::iterator From,
109 BOOST_STRING_TYPENAME InputT::iterator To,
110 ForwardIteratorT Begin,
111 ForwardIteratorT End )
113 BOOST_STRING_TYPENAME InputT::iterator At=Input.erase( From, To );
118 Input.insert( At, Begin, End );
122 Input.insert( Input.begin(), Begin, End );
128 // No native replace method
129 template< bool HasNative >
130 struct replace_native_helper
132 template< typename InputT, typename ForwardIteratorT >
135 BOOST_STRING_TYPENAME InputT::iterator From,
136 BOOST_STRING_TYPENAME InputT::iterator To,
137 ForwardIteratorT Begin,
138 ForwardIteratorT End )
140 replace_const_time_helper<
142 has_const_time_insert<InputT>,
143 has_const_time_erase<InputT> >::value >()(
144 Input, From, To, Begin, End );
148 // Container has native replace method
150 struct replace_native_helper< true >
152 template< typename InputT, typename ForwardIteratorT >
155 BOOST_STRING_TYPENAME InputT::iterator From,
156 BOOST_STRING_TYPENAME InputT::iterator To,
157 ForwardIteratorT Begin,
158 ForwardIteratorT End )
160 Input.replace( From, To, Begin, End );
164 // replace helper -------------------------------------------------//
166 template< typename InputT, typename ForwardIteratorT >
169 BOOST_STRING_TYPENAME InputT::iterator From,
170 BOOST_STRING_TYPENAME InputT::iterator To,
171 ForwardIteratorT Begin,
172 ForwardIteratorT End )
174 replace_native_helper< has_native_replace<InputT>::value >()(
175 Input, From, To, Begin, End );
178 template< typename InputT, typename InsertT >
181 BOOST_STRING_TYPENAME InputT::iterator From,
182 BOOST_STRING_TYPENAME InputT::iterator To,
183 const InsertT& Insert )
187 ::boost::algorithm::detail::replace( Input, From, To, ::boost::begin(Insert), ::boost::end(Insert) );
191 ::boost::algorithm::detail::insert( Input, From, ::boost::begin(Insert), ::boost::end(Insert) );
195 } // namespace detail
196 } // namespace algorithm
200 #endif // BOOST_STRING_DETAIL_SEQUENCE_HPP