Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 1 | // Boost string_algo library find_format.hpp header file ---------------------------// |
| 2 | |
| 3 | // Copyright Pavol Droba 2002-2003. |
| 4 | // |
| 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) |
| 8 | |
| 9 | // See http://www.boost.org/ for updates, documentation, and revision history. |
| 10 | |
| 11 | #ifndef BOOST_STRING_FIND_FORMAT_DETAIL_HPP |
| 12 | #define BOOST_STRING_FIND_FORMAT_DETAIL_HPP |
| 13 | |
| 14 | #include <ndnboost/algorithm/string/config.hpp> |
| 15 | #include <ndnboost/range/iterator_range.hpp> |
| 16 | #include <ndnboost/range/const_iterator.hpp> |
| 17 | #include <ndnboost/range/iterator.hpp> |
| 18 | #include <ndnboost/algorithm/string/detail/find_format_store.hpp> |
| 19 | #include <ndnboost/algorithm/string/detail/replace_storage.hpp> |
| 20 | |
| 21 | namespace ndnboost { |
| 22 | namespace algorithm { |
| 23 | namespace detail { |
| 24 | |
| 25 | // find_format_copy (iterator variant) implementation -------------------------------// |
| 26 | |
| 27 | template< |
| 28 | typename OutputIteratorT, |
| 29 | typename InputT, |
| 30 | typename FormatterT, |
| 31 | typename FindResultT, |
| 32 | typename FormatResultT > |
| 33 | inline OutputIteratorT find_format_copy_impl2( |
| 34 | OutputIteratorT Output, |
| 35 | const InputT& Input, |
| 36 | FormatterT Formatter, |
| 37 | const FindResultT& FindResult, |
| 38 | const FormatResultT& FormatResult ) |
| 39 | { |
| 40 | typedef find_format_store< |
| 41 | BOOST_STRING_TYPENAME |
| 42 | range_const_iterator<InputT>::type, |
| 43 | FormatterT, |
| 44 | FormatResultT > store_type; |
| 45 | |
| 46 | // Create store for the find result |
| 47 | store_type M( FindResult, FormatResult, Formatter ); |
| 48 | |
| 49 | if ( !M ) |
| 50 | { |
| 51 | // Match not found - return original sequence |
| 52 | Output = std::copy( ::ndnboost::begin(Input), ::ndnboost::end(Input), Output ); |
| 53 | return Output; |
| 54 | } |
| 55 | |
| 56 | // Copy the beginning of the sequence |
| 57 | Output = std::copy( ::ndnboost::begin(Input), ::ndnboost::begin(M), Output ); |
| 58 | // Format find result |
| 59 | // Copy formatted result |
| 60 | Output = std::copy( ::ndnboost::begin(M.format_result()), ::ndnboost::end(M.format_result()), Output ); |
| 61 | // Copy the rest of the sequence |
| 62 | Output = std::copy( M.end(), ::ndnboost::end(Input), Output ); |
| 63 | |
| 64 | return Output; |
| 65 | } |
| 66 | |
| 67 | template< |
| 68 | typename OutputIteratorT, |
| 69 | typename InputT, |
| 70 | typename FormatterT, |
| 71 | typename FindResultT > |
| 72 | inline OutputIteratorT find_format_copy_impl( |
| 73 | OutputIteratorT Output, |
| 74 | const InputT& Input, |
| 75 | FormatterT Formatter, |
| 76 | const FindResultT& FindResult ) |
| 77 | { |
| 78 | if( ::ndnboost::algorithm::detail::check_find_result(Input, FindResult) ) { |
| 79 | return ::ndnboost::algorithm::detail::find_format_copy_impl2( |
| 80 | Output, |
| 81 | Input, |
| 82 | Formatter, |
| 83 | FindResult, |
| 84 | Formatter(FindResult) ); |
| 85 | } else { |
| 86 | return std::copy( ::ndnboost::begin(Input), ::ndnboost::end(Input), Output ); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | |
| 91 | // find_format_copy implementation --------------------------------------------------// |
| 92 | |
| 93 | template< |
| 94 | typename InputT, |
| 95 | typename FormatterT, |
| 96 | typename FindResultT, |
| 97 | typename FormatResultT > |
| 98 | inline InputT find_format_copy_impl2( |
| 99 | const InputT& Input, |
| 100 | FormatterT Formatter, |
| 101 | const FindResultT& FindResult, |
| 102 | const FormatResultT& FormatResult) |
| 103 | { |
| 104 | typedef find_format_store< |
| 105 | BOOST_STRING_TYPENAME |
| 106 | range_const_iterator<InputT>::type, |
| 107 | FormatterT, |
| 108 | FormatResultT > store_type; |
| 109 | |
| 110 | // Create store for the find result |
| 111 | store_type M( FindResult, FormatResult, Formatter ); |
| 112 | |
| 113 | if ( !M ) |
| 114 | { |
| 115 | // Match not found - return original sequence |
| 116 | return InputT( Input ); |
| 117 | } |
| 118 | |
| 119 | InputT Output; |
| 120 | // Copy the beginning of the sequence |
| 121 | ndnboost::algorithm::detail::insert( Output, ::ndnboost::end(Output), ::ndnboost::begin(Input), M.begin() ); |
| 122 | // Copy formatted result |
| 123 | ndnboost::algorithm::detail::insert( Output, ::ndnboost::end(Output), M.format_result() ); |
| 124 | // Copy the rest of the sequence |
| 125 | ndnboost::algorithm::detail::insert( Output, ::ndnboost::end(Output), M.end(), ::ndnboost::end(Input) ); |
| 126 | |
| 127 | return Output; |
| 128 | } |
| 129 | |
| 130 | template< |
| 131 | typename InputT, |
| 132 | typename FormatterT, |
| 133 | typename FindResultT > |
| 134 | inline InputT find_format_copy_impl( |
| 135 | const InputT& Input, |
| 136 | FormatterT Formatter, |
| 137 | const FindResultT& FindResult) |
| 138 | { |
| 139 | if( ::ndnboost::algorithm::detail::check_find_result(Input, FindResult) ) { |
| 140 | return ::ndnboost::algorithm::detail::find_format_copy_impl2( |
| 141 | Input, |
| 142 | Formatter, |
| 143 | FindResult, |
| 144 | Formatter(FindResult) ); |
| 145 | } else { |
| 146 | return Input; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // replace implementation ----------------------------------------------------// |
| 151 | |
| 152 | template< |
| 153 | typename InputT, |
| 154 | typename FormatterT, |
| 155 | typename FindResultT, |
| 156 | typename FormatResultT > |
| 157 | inline void find_format_impl2( |
| 158 | InputT& Input, |
| 159 | FormatterT Formatter, |
| 160 | const FindResultT& FindResult, |
| 161 | const FormatResultT& FormatResult) |
| 162 | { |
| 163 | typedef find_format_store< |
| 164 | BOOST_STRING_TYPENAME |
| 165 | range_iterator<InputT>::type, |
| 166 | FormatterT, |
| 167 | FormatResultT > store_type; |
| 168 | |
| 169 | // Create store for the find result |
| 170 | store_type M( FindResult, FormatResult, Formatter ); |
| 171 | |
| 172 | if ( !M ) |
| 173 | { |
| 174 | // Search not found - return original sequence |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | // Replace match |
| 179 | ::ndnboost::algorithm::detail::replace( Input, M.begin(), M.end(), M.format_result() ); |
| 180 | } |
| 181 | |
| 182 | template< |
| 183 | typename InputT, |
| 184 | typename FormatterT, |
| 185 | typename FindResultT > |
| 186 | inline void find_format_impl( |
| 187 | InputT& Input, |
| 188 | FormatterT Formatter, |
| 189 | const FindResultT& FindResult) |
| 190 | { |
| 191 | if( ::ndnboost::algorithm::detail::check_find_result(Input, FindResult) ) { |
| 192 | ::ndnboost::algorithm::detail::find_format_impl2( |
| 193 | Input, |
| 194 | Formatter, |
| 195 | FindResult, |
| 196 | Formatter(FindResult) ); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | } // namespace detail |
| 201 | } // namespace algorithm |
| 202 | } // namespace ndnboost |
| 203 | |
| 204 | #endif // BOOST_STRING_FIND_FORMAT_DETAIL_HPP |