[857] | 1 | // Boost string_algo library find.hpp header file ---------------------------//
|
---|
| 2 |
|
---|
| 3 | // Copyright Pavol Droba 2002-2003. Use, modification and
|
---|
| 4 | // distribution is subject to the Boost Software License, Version
|
---|
| 5 | // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
---|
| 6 | // http://www.boost.org/LICENSE_1_0.txt)
|
---|
| 7 |
|
---|
| 8 | // See http://www.boost.org for updates, documentation, and revision history.
|
---|
| 9 |
|
---|
| 10 | #ifndef BOOST_STRING_FIND_HPP
|
---|
| 11 | #define BOOST_STRING_FIND_HPP
|
---|
| 12 |
|
---|
| 13 | #include <boost/algorithm/string/config.hpp>
|
---|
| 14 |
|
---|
| 15 | #include <boost/range/iterator_range.hpp>
|
---|
| 16 | #include <boost/range/begin.hpp>
|
---|
| 17 | #include <boost/range/end.hpp>
|
---|
| 18 | #include <boost/range/iterator.hpp>
|
---|
| 19 | #include <boost/range/const_iterator.hpp>
|
---|
| 20 | #include <boost/range/result_iterator.hpp>
|
---|
| 21 |
|
---|
| 22 | #include <boost/algorithm/string/finder.hpp>
|
---|
| 23 | #include <boost/algorithm/string/compare.hpp>
|
---|
| 24 | #include <boost/algorithm/string/constants.hpp>
|
---|
| 25 |
|
---|
| 26 | /*! \file
|
---|
| 27 | Defines a set of find algorithms. The algorithms are searching
|
---|
| 28 | for a substring of the input. The result is given as an \c iterator_range
|
---|
| 29 | delimiting the substring.
|
---|
| 30 | */
|
---|
| 31 |
|
---|
| 32 | namespace boost {
|
---|
| 33 | namespace algorithm {
|
---|
| 34 |
|
---|
| 35 | // Generic find -----------------------------------------------//
|
---|
| 36 |
|
---|
| 37 | //! Generic find algorithm
|
---|
| 38 | /*!
|
---|
| 39 | Search the input using the given finder.
|
---|
| 40 |
|
---|
| 41 | \param Input A string which will be searched.
|
---|
| 42 | \param Finder Finder object used for searching.
|
---|
| 43 | \return
|
---|
| 44 | An \c iterator_range delimiting the match.
|
---|
| 45 | Returned iterator is either \c RangeT::iterator or
|
---|
| 46 | \c RangeT::const_iterator, depending on the constness of
|
---|
| 47 | the input parameter.
|
---|
| 48 | */
|
---|
| 49 | template<typename RangeT, typename FinderT>
|
---|
| 50 | inline iterator_range<
|
---|
| 51 | BOOST_STRING_TYPENAME range_result_iterator<RangeT>::type>
|
---|
| 52 | find(
|
---|
| 53 | RangeT& Input,
|
---|
| 54 | FinderT Finder)
|
---|
| 55 | {
|
---|
| 56 | return Finder(begin(Input),end(Input));
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | // find_first -----------------------------------------------//
|
---|
| 60 |
|
---|
| 61 | //! Find first algorithm
|
---|
| 62 | /*!
|
---|
| 63 | Search for the first occurence of the substring in the input.
|
---|
| 64 |
|
---|
| 65 | \param Input A string which will be searched.
|
---|
| 66 | \param Search A substring to be searched for.
|
---|
| 67 | \return
|
---|
| 68 | An \c iterator_range delimiting the match.
|
---|
| 69 | Returned iterator is either \c RangeT::iterator or
|
---|
| 70 | \c RangeT::const_iterator, depending on the constness of
|
---|
| 71 | the input parameter.
|
---|
| 72 |
|
---|
| 73 | \note This function provides the strong exception-safety guarantee
|
---|
| 74 | */
|
---|
| 75 | template<typename Range1T, typename Range2T>
|
---|
| 76 | inline iterator_range<
|
---|
| 77 | BOOST_STRING_TYPENAME range_result_iterator<Range1T>::type>
|
---|
| 78 | find_first(
|
---|
| 79 | Range1T& Input,
|
---|
| 80 | const Range2T& Search)
|
---|
| 81 | {
|
---|
| 82 | return first_finder(Search)(
|
---|
| 83 | begin(Input),end(Input));
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | //! Find first algorithm ( case insensitive )
|
---|
| 87 | /*!
|
---|
| 88 | Search for the first occurence of the substring in the input.
|
---|
| 89 | Searching is case insensitive.
|
---|
| 90 |
|
---|
| 91 | \param Input A string which will be searched.
|
---|
| 92 | \param Search A substring to be searched for.
|
---|
| 93 | \param Loc A locale used for case insensitive comparison
|
---|
| 94 | \return
|
---|
| 95 | An \c iterator_range delimiting the match.
|
---|
| 96 | Returned iterator is either \c Range1T::iterator or
|
---|
| 97 | \c Range1T::const_iterator, depending on the constness of
|
---|
| 98 | the input parameter.
|
---|
| 99 |
|
---|
| 100 | \note This function provides the strong exception-safety guarantee
|
---|
| 101 | */
|
---|
| 102 | template<typename Range1T, typename Range2T>
|
---|
| 103 | inline iterator_range<
|
---|
| 104 | BOOST_STRING_TYPENAME range_result_iterator<Range1T>::type>
|
---|
| 105 | ifind_first(
|
---|
| 106 | Range1T& Input,
|
---|
| 107 | const Range2T& Search,
|
---|
| 108 | const std::locale& Loc=std::locale())
|
---|
| 109 | {
|
---|
| 110 | return first_finder(Search,is_iequal(Loc))(
|
---|
| 111 | begin(Input),end(Input));
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | // find_last -----------------------------------------------//
|
---|
| 115 |
|
---|
| 116 | //! Find last algorithm
|
---|
| 117 | /*!
|
---|
| 118 | Search for the last occurence of the substring in the input.
|
---|
| 119 |
|
---|
| 120 | \param Input A string which will be searched.
|
---|
| 121 | \param Search A substring to be searched for.
|
---|
| 122 | \return
|
---|
| 123 | An \c iterator_range delimiting the match.
|
---|
| 124 | Returned iterator is either \c Range1T::iterator or
|
---|
| 125 | \c Range1T::const_iterator, depending on the constness of
|
---|
| 126 | the input parameter.
|
---|
| 127 |
|
---|
| 128 | \note This function provides the strong exception-safety guarantee
|
---|
| 129 | */
|
---|
| 130 | template<typename Range1T, typename Range2T>
|
---|
| 131 | inline iterator_range<
|
---|
| 132 | BOOST_STRING_TYPENAME range_result_iterator<Range1T>::type>
|
---|
| 133 | find_last(
|
---|
| 134 | Range1T& Input,
|
---|
| 135 | const Range2T& Search)
|
---|
| 136 | {
|
---|
| 137 | return last_finder(Search)(
|
---|
| 138 | begin(Input),end(Input));
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | //! Find last algorithm ( case insensitive )
|
---|
| 142 | /*!
|
---|
| 143 | Search for the last match a string in the input.
|
---|
| 144 | Searching is case insensitive.
|
---|
| 145 |
|
---|
| 146 | \param Input A string which will be searched.
|
---|
| 147 | \param Search A substring to be searched for.
|
---|
| 148 | \param Loc A locale used for case insensitive comparison
|
---|
| 149 | \return
|
---|
| 150 | An \c iterator_range delimiting the match.
|
---|
| 151 | Returned iterator is either \c Range1T::iterator or
|
---|
| 152 | \c Range1T::const_iterator, depending on the constness of
|
---|
| 153 | the input parameter.
|
---|
| 154 |
|
---|
| 155 | \note This function provides the strong exception-safety guarantee
|
---|
| 156 | */
|
---|
| 157 | template<typename Range1T, typename Range2T>
|
---|
| 158 | inline iterator_range<
|
---|
| 159 | BOOST_STRING_TYPENAME range_result_iterator<Range1T>::type>
|
---|
| 160 | ifind_last(
|
---|
| 161 | Range1T& Input,
|
---|
| 162 | const Range2T& Search,
|
---|
| 163 | const std::locale& Loc=std::locale())
|
---|
| 164 | {
|
---|
| 165 | return last_finder(Search, is_iequal(Loc))(
|
---|
| 166 | begin(Input),end(Input));
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | // find_nth ----------------------------------------------------------------------//
|
---|
| 170 |
|
---|
| 171 | //! Find n-th algorithm
|
---|
| 172 | /*!
|
---|
| 173 | Search for the n-th (zero-indexed) occurence of the substring in the
|
---|
| 174 | input.
|
---|
| 175 |
|
---|
| 176 | \param Input A string which will be searched.
|
---|
| 177 | \param Search A substring to be searched for.
|
---|
| 178 | \param Nth An index (zero-indexed) of the match to be found.
|
---|
| 179 | \return
|
---|
| 180 | An \c iterator_range delimiting the match.
|
---|
| 181 | Returned iterator is either \c Range1T::iterator or
|
---|
| 182 | \c Range1T::const_iterator, depending on the constness of
|
---|
| 183 | the input parameter.
|
---|
| 184 | */
|
---|
| 185 | template<typename Range1T, typename Range2T>
|
---|
| 186 | inline iterator_range<
|
---|
| 187 | BOOST_STRING_TYPENAME range_result_iterator<Range1T>::type>
|
---|
| 188 | find_nth(
|
---|
| 189 | Range1T& Input,
|
---|
| 190 | const Range2T& Search,
|
---|
| 191 | unsigned int Nth)
|
---|
| 192 | {
|
---|
| 193 | return nth_finder(Search,Nth)(
|
---|
| 194 | begin(Input),end(Input));
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | //! Find n-th algorithm ( case insensitive ).
|
---|
| 198 | /*!
|
---|
| 199 | Search for the n-th (zero-indexed) occurence of the substring in the
|
---|
| 200 | input. Searching is case insensitive.
|
---|
| 201 |
|
---|
| 202 | \param Input A string which will be searched.
|
---|
| 203 | \param Search A substring to be searched for.
|
---|
| 204 | \param Nth An index (zero-indexed) of the match to be found.
|
---|
| 205 | \param Loc A locale used for case insensitive comparison
|
---|
| 206 | \return
|
---|
| 207 | An \c iterator_range delimiting the match.
|
---|
| 208 | Returned iterator is either \c Range1T::iterator or
|
---|
| 209 | \c Range1T::const_iterator, depending on the constness of
|
---|
| 210 | the input parameter.
|
---|
| 211 |
|
---|
| 212 |
|
---|
| 213 | \note This function provides the strong exception-safety guarantee
|
---|
| 214 | */
|
---|
| 215 | template<typename Range1T, typename Range2T>
|
---|
| 216 | inline iterator_range<
|
---|
| 217 | BOOST_STRING_TYPENAME range_result_iterator<Range1T>::type>
|
---|
| 218 | ifind_nth(
|
---|
| 219 | Range1T& Input,
|
---|
| 220 | const Range2T& Search,
|
---|
| 221 | unsigned int Nth,
|
---|
| 222 | const std::locale& Loc=std::locale())
|
---|
| 223 | {
|
---|
| 224 | return nth_finder(Search,Nth,is_iequal(Loc))(
|
---|
| 225 | begin(Input),end(Input));
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | // find_head ----------------------------------------------------------------------//
|
---|
| 229 |
|
---|
| 230 | //! Find head algorithm
|
---|
| 231 | /*!
|
---|
| 232 | Get the head of the input. Head is a prefix of the string of the
|
---|
| 233 | given size. If the input is shorter then required, whole input if considered
|
---|
| 234 | to be the head.
|
---|
| 235 |
|
---|
| 236 | \param Input An input string
|
---|
| 237 | \param N Length of the head
|
---|
| 238 | \return
|
---|
| 239 | An \c iterator_range delimiting the match.
|
---|
| 240 | Returned iterator is either \c Range1T::iterator or
|
---|
| 241 | \c Range1T::const_iterator, depending on the constness of
|
---|
| 242 | the input parameter.
|
---|
| 243 |
|
---|
| 244 | \note This function provides the strong exception-safety guarantee
|
---|
| 245 | */
|
---|
| 246 | template<typename RangeT>
|
---|
| 247 | inline iterator_range<
|
---|
| 248 | BOOST_STRING_TYPENAME range_result_iterator<RangeT>::type>
|
---|
| 249 | find_head(
|
---|
| 250 | RangeT& Input,
|
---|
| 251 | unsigned int N)
|
---|
| 252 | {
|
---|
| 253 | return head_finder(N)(
|
---|
| 254 | begin(Input),end(Input));
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | // find_tail ----------------------------------------------------------------------//
|
---|
| 258 |
|
---|
| 259 | //! Find tail algorithm
|
---|
| 260 | /*!
|
---|
| 261 | Get the head of the input. Head is a suffix of the string of the
|
---|
| 262 | given size. If the input is shorter then required, whole input if considered
|
---|
| 263 | to be the tail.
|
---|
| 264 |
|
---|
| 265 | \param Input An input string
|
---|
| 266 | \param N Length of the tail
|
---|
| 267 | \return
|
---|
| 268 | An \c iterator_range delimiting the match.
|
---|
| 269 | Returned iterator is either \c RangeT::iterator or
|
---|
| 270 | \c RangeT::const_iterator, depending on the constness of
|
---|
| 271 | the input parameter.
|
---|
| 272 |
|
---|
| 273 |
|
---|
| 274 | \note This function provides the strong exception-safety guarantee
|
---|
| 275 | */
|
---|
| 276 | template<typename RangeT>
|
---|
| 277 | inline iterator_range<
|
---|
| 278 | BOOST_STRING_TYPENAME range_result_iterator<RangeT>::type>
|
---|
| 279 | find_tail(
|
---|
| 280 | RangeT& Input,
|
---|
| 281 | unsigned int N)
|
---|
| 282 | {
|
---|
| 283 | return tail_finder(N)(
|
---|
| 284 | begin(Input),end(Input));
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 | // find_token --------------------------------------------------------------------//
|
---|
| 288 |
|
---|
| 289 | //! Find token algorithm
|
---|
| 290 | /*!
|
---|
| 291 | Look for a given token in the string. Token is a character that matches the
|
---|
| 292 | given predicate.
|
---|
| 293 | If the "token compress mode" is enabled, adjacent tokens are considered to be one match.
|
---|
| 294 |
|
---|
| 295 | \param Input A input string.
|
---|
| 296 | \param Pred An unary predicate to identify a token
|
---|
| 297 | \param eCompress Enable/Disable compressing of adjacent tokens
|
---|
| 298 | \return
|
---|
| 299 | An \c iterator_range delimiting the match.
|
---|
| 300 | Returned iterator is either \c RangeT::iterator or
|
---|
| 301 | \c RangeT::const_iterator, depending on the constness of
|
---|
| 302 | the input parameter.
|
---|
| 303 |
|
---|
| 304 | \note This function provides the strong exception-safety guarantee
|
---|
| 305 | */
|
---|
| 306 | template<typename RangeT, typename PredicateT>
|
---|
| 307 | inline iterator_range<
|
---|
| 308 | BOOST_STRING_TYPENAME range_result_iterator<RangeT>::type>
|
---|
| 309 | find_token(
|
---|
| 310 | RangeT& Input,
|
---|
| 311 | PredicateT Pred,
|
---|
| 312 | token_compress_mode_type eCompress=token_compress_off)
|
---|
| 313 | {
|
---|
| 314 | return token_finder(Pred, eCompress)(
|
---|
| 315 | begin(Input),end(Input));
|
---|
| 316 | }
|
---|
| 317 |
|
---|
| 318 | } // namespace algorithm
|
---|
| 319 |
|
---|
| 320 | // pull names to the boost namespace
|
---|
| 321 | using algorithm::find;
|
---|
| 322 | using algorithm::find_first;
|
---|
| 323 | using algorithm::ifind_first;
|
---|
| 324 | using algorithm::find_last;
|
---|
| 325 | using algorithm::ifind_last;
|
---|
| 326 | using algorithm::find_nth;
|
---|
| 327 | using algorithm::ifind_nth;
|
---|
| 328 | using algorithm::find_head;
|
---|
| 329 | using algorithm::find_tail;
|
---|
| 330 | using algorithm::find_token;
|
---|
| 331 |
|
---|
| 332 | } // namespace boost
|
---|
| 333 |
|
---|
| 334 |
|
---|
| 335 | #endif // BOOST_STRING_FIND_HPP
|
---|