| 1 | // Boost string_algo library regex.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_REGEX_HPP |
|---|
| 11 | #define BOOST_STRING_REGEX_HPP |
|---|
| 12 | |
|---|
| 13 | #include <boost/algorithm/string/config.hpp> |
|---|
| 14 | #include <boost/regex.hpp> |
|---|
| 15 | |
|---|
| 16 | #include <boost/range/iterator_range.hpp> |
|---|
| 17 | #include <boost/range/begin.hpp> |
|---|
| 18 | #include <boost/range/end.hpp> |
|---|
| 19 | #include <boost/range/result_iterator.hpp> |
|---|
| 20 | |
|---|
| 21 | #include <boost/algorithm/string/find_format.hpp> |
|---|
| 22 | #include <boost/algorithm/string/regex_find_format.hpp> |
|---|
| 23 | #include <boost/algorithm/string/formatter.hpp> |
|---|
| 24 | #include <boost/algorithm/string/iter_find.hpp> |
|---|
| 25 | |
|---|
| 26 | /*! \file |
|---|
| 27 | Defines regex variants of the algorithms. |
|---|
| 28 | */ |
|---|
| 29 | |
|---|
| 30 | namespace boost { |
|---|
| 31 | namespace algorithm { |
|---|
| 32 | |
|---|
| 33 | // find_regex -----------------------------------------------// |
|---|
| 34 | |
|---|
| 35 | //! Find regex algorithm |
|---|
| 36 | /*! |
|---|
| 37 | Search for a substring matching the given regex in the input. |
|---|
| 38 | |
|---|
| 39 | \param Input A container which will be searched. |
|---|
| 40 | \param Rx A regular expression |
|---|
| 41 | \param Flags Regex options |
|---|
| 42 | \return |
|---|
| 43 | An \c iterator_range delimiting the match. |
|---|
| 44 | Returned iterator is either \c RangeT::iterator or |
|---|
| 45 | \c RangeT::const_iterator, depending on the constness of |
|---|
| 46 | the input parameter. |
|---|
| 47 | |
|---|
| 48 | \note This function provides the strong exception-safety guarantee |
|---|
| 49 | */ |
|---|
| 50 | template< |
|---|
| 51 | typename RangeT, |
|---|
| 52 | typename CharT, |
|---|
| 53 | typename RegexTraitsT> |
|---|
| 54 | inline iterator_range< |
|---|
| 55 | BOOST_STRING_TYPENAME range_result_iterator<RangeT>::type > |
|---|
| 56 | find_regex( |
|---|
| 57 | RangeT& Input, |
|---|
| 58 | const basic_regex<CharT, RegexTraitsT>& Rx, |
|---|
| 59 | match_flag_type Flags=match_default ) |
|---|
| 60 | { |
|---|
| 61 | return regex_finder(Rx,Flags)( |
|---|
| 62 | begin(Input), end(Input) ); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | // replace_regex --------------------------------------------------------------------// |
|---|
| 66 | |
|---|
| 67 | //! Replace regex algorithm |
|---|
| 68 | /*! |
|---|
| 69 | Search for a substring matching given regex and format it with |
|---|
| 70 | the specified format. |
|---|
| 71 | The result is a modified copy of the input. It is returned as a sequence |
|---|
| 72 | or copied to the output iterator. |
|---|
| 73 | |
|---|
| 74 | \param Output An output iterator to which the result will be copied |
|---|
| 75 | \param Input An input string |
|---|
| 76 | \param Rx A regular expression |
|---|
| 77 | \param Format Regex format definition |
|---|
| 78 | \param Flags Regex options |
|---|
| 79 | \return An output iterator pointing just after the last inserted character or |
|---|
| 80 | a modified copy of the input |
|---|
| 81 | |
|---|
| 82 | \note The second variant of this function provides the strong exception-safety guarantee |
|---|
| 83 | */ |
|---|
| 84 | template< |
|---|
| 85 | typename OutputIteratorT, |
|---|
| 86 | typename RangeT, |
|---|
| 87 | typename CharT, |
|---|
| 88 | typename RegexTraitsT, |
|---|
| 89 | typename FormatStringTraitsT, typename FormatStringAllocatorT > |
|---|
| 90 | inline OutputIteratorT replace_regex_copy( |
|---|
| 91 | OutputIteratorT Output, |
|---|
| 92 | const RangeT& Input, |
|---|
| 93 | const basic_regex<CharT, RegexTraitsT>& Rx, |
|---|
| 94 | const std::basic_string<CharT, FormatStringTraitsT, FormatStringAllocatorT>& Format, |
|---|
| 95 | match_flag_type Flags=match_default | format_default ) |
|---|
| 96 | { |
|---|
| 97 | return find_format_copy( |
|---|
| 98 | Output, |
|---|
| 99 | Input, |
|---|
| 100 | regex_finder( Rx, Flags ), |
|---|
| 101 | regex_formatter( Format, Flags ) ); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | //! Replace regex algorithm |
|---|
| 105 | /*! |
|---|
| 106 | \overload |
|---|
| 107 | */ |
|---|
| 108 | template< |
|---|
| 109 | typename SequenceT, |
|---|
| 110 | typename CharT, |
|---|
| 111 | typename RegexTraitsT, |
|---|
| 112 | typename FormatStringTraitsT, typename FormatStringAllocatorT > |
|---|
| 113 | inline SequenceT replace_regex_copy( |
|---|
| 114 | const SequenceT& Input, |
|---|
| 115 | const basic_regex<CharT, RegexTraitsT>& Rx, |
|---|
| 116 | const std::basic_string<CharT, FormatStringTraitsT, FormatStringAllocatorT>& Format, |
|---|
| 117 | match_flag_type Flags=match_default | format_default ) |
|---|
| 118 | { |
|---|
| 119 | return find_format_copy( |
|---|
| 120 | Input, |
|---|
| 121 | regex_finder( Rx, Flags ), |
|---|
| 122 | regex_formatter( Format, Flags ) ); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | //! Replace regex algorithm |
|---|
| 126 | /*! |
|---|
| 127 | Search for a substring matching given regex and format it with |
|---|
| 128 | the specified format. The input string is modified in-place. |
|---|
| 129 | |
|---|
| 130 | \param Input An input string |
|---|
| 131 | \param Rx A regular expression |
|---|
| 132 | \param Format Regex format definition |
|---|
| 133 | \param Flags Regex options |
|---|
| 134 | */ |
|---|
| 135 | template< |
|---|
| 136 | typename SequenceT, |
|---|
| 137 | typename CharT, |
|---|
| 138 | typename RegexTraitsT, |
|---|
| 139 | typename FormatStringTraitsT, typename FormatStringAllocatorT > |
|---|
| 140 | inline void replace_regex( |
|---|
| 141 | SequenceT& Input, |
|---|
| 142 | const basic_regex<CharT, RegexTraitsT>& Rx, |
|---|
| 143 | const std::basic_string<CharT, FormatStringTraitsT, FormatStringAllocatorT>& Format, |
|---|
| 144 | match_flag_type Flags=match_default | format_default ) |
|---|
| 145 | { |
|---|
| 146 | find_format( |
|---|
| 147 | Input, |
|---|
| 148 | regex_finder( Rx, Flags ), |
|---|
| 149 | regex_formatter( Format, Flags ) ); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | // replace_all_regex --------------------------------------------------------------------// |
|---|
| 153 | |
|---|
| 154 | //! Replace all regex algorithm |
|---|
| 155 | /*! |
|---|
| 156 | Format all substrings, matching given regex, with the specified format. |
|---|
| 157 | The result is a modified copy of the input. It is returned as a sequence |
|---|
| 158 | or copied to the output iterator. |
|---|
| 159 | |
|---|
| 160 | \param Output An output iterator to which the result will be copied |
|---|
| 161 | \param Input An input string |
|---|
| 162 | \param Rx A regular expression |
|---|
| 163 | \param Format Regex format definition |
|---|
| 164 | \param Flags Regex options |
|---|
| 165 | \return An output iterator pointing just after the last inserted character or |
|---|
| 166 | a modified copy of the input |
|---|
| 167 | |
|---|
| 168 | \note The second variant of this function provides the strong exception-safety guarantee |
|---|
| 169 | */ |
|---|
| 170 | template< |
|---|
| 171 | typename OutputIteratorT, |
|---|
| 172 | typename RangeT, |
|---|
| 173 | typename CharT, |
|---|
| 174 | typename RegexTraitsT, |
|---|
| 175 | typename FormatStringTraitsT, typename FormatStringAllocatorT > |
|---|
| 176 | inline OutputIteratorT replace_all_regex_copy( |
|---|
| 177 | OutputIteratorT Output, |
|---|
| 178 | const RangeT& Input, |
|---|
| 179 | const basic_regex<CharT, RegexTraitsT>& Rx, |
|---|
| 180 | const std::basic_string<CharT, FormatStringTraitsT, FormatStringAllocatorT>& Format, |
|---|
| 181 | match_flag_type Flags=match_default | format_default ) |
|---|
| 182 | { |
|---|
| 183 | return find_format_all_copy( |
|---|
| 184 | Output, |
|---|
| 185 | Input, |
|---|
| 186 | regex_finder( Rx, Flags ), |
|---|
| 187 | regex_formatter( Format, Flags ) ); |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | //! Replace all regex algorithm |
|---|
| 191 | /*! |
|---|
| 192 | \overload |
|---|
| 193 | */ |
|---|
| 194 | template< |
|---|
| 195 | typename SequenceT, |
|---|
| 196 | typename CharT, |
|---|
| 197 | typename RegexTraitsT, |
|---|
| 198 | typename FormatStringTraitsT, typename FormatStringAllocatorT > |
|---|
| 199 | inline SequenceT replace_all_regex_copy( |
|---|
| 200 | const SequenceT& Input, |
|---|
| 201 | const basic_regex<CharT, RegexTraitsT>& Rx, |
|---|
| 202 | const std::basic_string<CharT, FormatStringTraitsT, FormatStringAllocatorT>& Format, |
|---|
| 203 | match_flag_type Flags=match_default | format_default ) |
|---|
| 204 | { |
|---|
| 205 | return find_format_all_copy( |
|---|
| 206 | Input, |
|---|
| 207 | regex_finder( Rx, Flags ), |
|---|
| 208 | regex_formatter( Format, Flags ) ); |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | //! Replace all regex algorithm |
|---|
| 212 | /*! |
|---|
| 213 | Format all substrings, matching given regex, with the specified format. |
|---|
| 214 | The input string is modified in-place. |
|---|
| 215 | |
|---|
| 216 | \param Input An input string |
|---|
| 217 | \param Rx A regular expression |
|---|
| 218 | \param Format Regex format definition |
|---|
| 219 | \param Flags Regex options |
|---|
| 220 | */ |
|---|
| 221 | template< |
|---|
| 222 | typename SequenceT, |
|---|
| 223 | typename CharT, |
|---|
| 224 | typename RegexTraitsT, |
|---|
| 225 | typename FormatStringTraitsT, typename FormatStringAllocatorT > |
|---|
| 226 | inline void replace_all_regex( |
|---|
| 227 | SequenceT& Input, |
|---|
| 228 | const basic_regex<CharT, RegexTraitsT>& Rx, |
|---|
| 229 | const std::basic_string<CharT, FormatStringTraitsT, FormatStringAllocatorT>& Format, |
|---|
| 230 | match_flag_type Flags=match_default | format_default ) |
|---|
| 231 | { |
|---|
| 232 | find_format_all( |
|---|
| 233 | Input, |
|---|
| 234 | regex_finder( Rx, Flags ), |
|---|
| 235 | regex_formatter( Format, Flags ) ); |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | // erase_regex --------------------------------------------------------------------// |
|---|
| 239 | |
|---|
| 240 | //! Erase regex algorithm |
|---|
| 241 | /*! |
|---|
| 242 | Remove a substring matching given regex from the input. |
|---|
| 243 | The result is a modified copy of the input. It is returned as a sequence |
|---|
| 244 | or copied to the output iterator. |
|---|
| 245 | |
|---|
| 246 | \param Output An output iterator to which the result will be copied |
|---|
| 247 | \param Input An input string |
|---|
| 248 | \param Rx A regular expression |
|---|
| 249 | \param Flags Regex options |
|---|
| 250 | \return An output iterator pointing just after the last inserted character or |
|---|
| 251 | a modified copy of the input |
|---|
| 252 | |
|---|
| 253 | \note The second variant of this function provides the strong exception-safety guarantee |
|---|
| 254 | */ |
|---|
| 255 | template< |
|---|
| 256 | typename OutputIteratorT, |
|---|
| 257 | typename RangeT, |
|---|
| 258 | typename CharT, |
|---|
| 259 | typename RegexTraitsT > |
|---|
| 260 | inline OutputIteratorT erase_regex_copy( |
|---|
| 261 | OutputIteratorT Output, |
|---|
| 262 | const RangeT& Input, |
|---|
| 263 | const basic_regex<CharT, RegexTraitsT>& Rx, |
|---|
| 264 | match_flag_type Flags=match_default ) |
|---|
| 265 | { |
|---|
| 266 | return find_format_copy( |
|---|
| 267 | Output, |
|---|
| 268 | Input, |
|---|
| 269 | regex_finder( Rx, Flags ), |
|---|
| 270 | empty_formatter( Input ) ); |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | //! Erase regex algorithm |
|---|
| 274 | /*! |
|---|
| 275 | \overload |
|---|
| 276 | */ |
|---|
| 277 | template< |
|---|
| 278 | typename SequenceT, |
|---|
| 279 | typename CharT, |
|---|
| 280 | typename RegexTraitsT > |
|---|
| 281 | inline SequenceT erase_regex_copy( |
|---|
| 282 | const SequenceT& Input, |
|---|
| 283 | const basic_regex<CharT, RegexTraitsT>& Rx, |
|---|
| 284 | match_flag_type Flags=match_default ) |
|---|
| 285 | { |
|---|
| 286 | return find_format_copy( |
|---|
| 287 | Input, |
|---|
| 288 | regex_finder( Rx, Flags ), |
|---|
| 289 | empty_formatter( Input ) ); |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 292 | //! Erase regex algorithm |
|---|
| 293 | /*! |
|---|
| 294 | Remove a substring matching given regex from the input. |
|---|
| 295 | The input string is modified in-place. |
|---|
| 296 | |
|---|
| 297 | \param Input An input string |
|---|
| 298 | \param Rx A regular expression |
|---|
| 299 | \param Flags Regex options |
|---|
| 300 | */ |
|---|
| 301 | template< |
|---|
| 302 | typename SequenceT, |
|---|
| 303 | typename CharT, |
|---|
| 304 | typename RegexTraitsT > |
|---|
| 305 | inline void erase_regex( |
|---|
| 306 | SequenceT& Input, |
|---|
| 307 | const basic_regex<CharT, RegexTraitsT>& Rx, |
|---|
| 308 | match_flag_type Flags=match_default ) |
|---|
| 309 | { |
|---|
| 310 | find_format( |
|---|
| 311 | Input, |
|---|
| 312 | regex_finder( Rx, Flags ), |
|---|
| 313 | empty_formatter( Input ) ); |
|---|
| 314 | } |
|---|
| 315 | |
|---|
| 316 | // erase_all_regex --------------------------------------------------------------------// |
|---|
| 317 | |
|---|
| 318 | //! Erase all regex algorithm |
|---|
| 319 | /*! |
|---|
| 320 | Erase all substrings, matching given regex, from the input. |
|---|
| 321 | The result is a modified copy of the input. It is returned as a sequence |
|---|
| 322 | or copied to the output iterator. |
|---|
| 323 | |
|---|
| 324 | |
|---|
| 325 | \param Output An output iterator to which the result will be copied |
|---|
| 326 | \param Input An input string |
|---|
| 327 | \param Rx A regular expression |
|---|
| 328 | \param Flags Regex options |
|---|
| 329 | \return An output iterator pointing just after the last inserted character or |
|---|
| 330 | a modified copy of the input |
|---|
| 331 | |
|---|
| 332 | \note The second variant of this function provides the strong exception-safety guarantee |
|---|
| 333 | */ |
|---|
| 334 | template< |
|---|
| 335 | typename OutputIteratorT, |
|---|
| 336 | typename RangeT, |
|---|
| 337 | typename CharT, |
|---|
| 338 | typename RegexTraitsT > |
|---|
| 339 | inline OutputIteratorT erase_all_regex_copy( |
|---|
| 340 | OutputIteratorT Output, |
|---|
| 341 | const RangeT& Input, |
|---|
| 342 | const basic_regex<CharT, RegexTraitsT>& Rx, |
|---|
| 343 | match_flag_type Flags=match_default ) |
|---|
| 344 | { |
|---|
| 345 | return find_format_all_copy( |
|---|
| 346 | Output, |
|---|
| 347 | Input, |
|---|
| 348 | regex_finder( Rx, Flags ), |
|---|
| 349 | empty_formatter( Input ) ); |
|---|
| 350 | } |
|---|
| 351 | |
|---|
| 352 | //! Erase all regex algorithm |
|---|
| 353 | /*! |
|---|
| 354 | \overload |
|---|
| 355 | */ |
|---|
| 356 | template< |
|---|
| 357 | typename SequenceT, |
|---|
| 358 | typename CharT, |
|---|
| 359 | typename RegexTraitsT > |
|---|
| 360 | inline SequenceT erase_all_regex_copy( |
|---|
| 361 | const SequenceT& Input, |
|---|
| 362 | const basic_regex<CharT, RegexTraitsT>& Rx, |
|---|
| 363 | match_flag_type Flags=match_default ) |
|---|
| 364 | { |
|---|
| 365 | return find_format_all_copy( |
|---|
| 366 | Input, |
|---|
| 367 | regex_finder( Rx, Flags ), |
|---|
| 368 | empty_formatter( Input ) ); |
|---|
| 369 | } |
|---|
| 370 | |
|---|
| 371 | //! Erase all regex algorithm |
|---|
| 372 | /*! |
|---|
| 373 | Erase all substrings, matching given regex, from the input. |
|---|
| 374 | The input string is modified in-place. |
|---|
| 375 | |
|---|
| 376 | \param Input An input string |
|---|
| 377 | \param Rx A regular expression |
|---|
| 378 | \param Flags Regex options |
|---|
| 379 | */ |
|---|
| 380 | template< |
|---|
| 381 | typename SequenceT, |
|---|
| 382 | typename CharT, |
|---|
| 383 | typename RegexTraitsT> |
|---|
| 384 | inline void erase_all_regex( |
|---|
| 385 | SequenceT& Input, |
|---|
| 386 | const basic_regex<CharT, RegexTraitsT>& Rx, |
|---|
| 387 | match_flag_type Flags=match_default ) |
|---|
| 388 | { |
|---|
| 389 | find_format_all( |
|---|
| 390 | Input, |
|---|
| 391 | regex_finder( Rx, Flags ), |
|---|
| 392 | empty_formatter( Input ) ); |
|---|
| 393 | } |
|---|
| 394 | |
|---|
| 395 | // find_all_regex ------------------------------------------------------------------// |
|---|
| 396 | |
|---|
| 397 | //! Find all regex algorithm |
|---|
| 398 | /*! |
|---|
| 399 | This algorithm finds all substrings matching the give regex |
|---|
| 400 | in the input. |
|---|
| 401 | |
|---|
| 402 | Each part is copied and added as a new element to the output container. |
|---|
| 403 | Thus the result container must be able to hold copies |
|---|
| 404 | of the matches (in a compatible structure like std::string) or |
|---|
| 405 | a reference to it (e.g. using the iterator range class). |
|---|
| 406 | Examples of such a container are \c std::vector<std::string> |
|---|
| 407 | or \c std::list<boost::iterator_range<std::string::iterator>> |
|---|
| 408 | |
|---|
| 409 | \param Result A container that can hold copies of references to the substrings. |
|---|
| 410 | \param Input A container which will be searched. |
|---|
| 411 | \param Rx A regular expression |
|---|
| 412 | \param Flags Regex options |
|---|
| 413 | \return A reference to the result |
|---|
| 414 | |
|---|
| 415 | \note Prior content of the result will be overwritten. |
|---|
| 416 | |
|---|
| 417 | \note This function provides the strong exception-safety guarantee |
|---|
| 418 | */ |
|---|
| 419 | template< |
|---|
| 420 | typename SequenceSequenceT, |
|---|
| 421 | typename RangeT, |
|---|
| 422 | typename CharT, |
|---|
| 423 | typename RegexTraitsT > |
|---|
| 424 | inline SequenceSequenceT& find_all_regex( |
|---|
| 425 | SequenceSequenceT& Result, |
|---|
| 426 | const RangeT& Input, |
|---|
| 427 | const basic_regex<CharT, RegexTraitsT>& Rx, |
|---|
| 428 | match_flag_type Flags=match_default ) |
|---|
| 429 | { |
|---|
| 430 | return iter_find( |
|---|
| 431 | Result, |
|---|
| 432 | Input, |
|---|
| 433 | regex_finder(Rx,Flags) ); |
|---|
| 434 | } |
|---|
| 435 | |
|---|
| 436 | // split_regex ------------------------------------------------------------------// |
|---|
| 437 | |
|---|
| 438 | //! Split regex algorithm |
|---|
| 439 | /*! |
|---|
| 440 | Tokenize expression. This function is equivalent to C strtok. Input |
|---|
| 441 | sequence is split into tokens, separated by separators. Separator |
|---|
| 442 | is an every match of the given regex. |
|---|
| 443 | Each part is copied and added as a new element to the output container. |
|---|
| 444 | Thus the result container must be able to hold copies |
|---|
| 445 | of the matches (in a compatible structure like std::string) or |
|---|
| 446 | a reference to it (e.g. using the iterator range class). |
|---|
| 447 | Examples of such a container are \c std::vector<std::string> |
|---|
| 448 | or \c std::list<boost::iterator_range<std::string::iterator>> |
|---|
| 449 | |
|---|
| 450 | \param Result A container that can hold copies of references to the substrings. |
|---|
| 451 | \param Input A container which will be searched. |
|---|
| 452 | \param Rx A regular expression |
|---|
| 453 | \param Flags Regex options |
|---|
| 454 | \return A reference to the result |
|---|
| 455 | |
|---|
| 456 | \note Prior content of the result will be overwritten. |
|---|
| 457 | |
|---|
| 458 | \note This function provides the strong exception-safety guarantee |
|---|
| 459 | */ |
|---|
| 460 | template< |
|---|
| 461 | typename SequenceSequenceT, |
|---|
| 462 | typename RangeT, |
|---|
| 463 | typename CharT, |
|---|
| 464 | typename RegexTraitsT > |
|---|
| 465 | inline SequenceSequenceT& split_regex( |
|---|
| 466 | SequenceSequenceT& Result, |
|---|
| 467 | const RangeT& Input, |
|---|
| 468 | const basic_regex<CharT, RegexTraitsT>& Rx, |
|---|
| 469 | match_flag_type Flags=match_default ) |
|---|
| 470 | { |
|---|
| 471 | return iter_split( |
|---|
| 472 | Result, |
|---|
| 473 | Input, |
|---|
| 474 | regex_finder(Rx,Flags) ); |
|---|
| 475 | } |
|---|
| 476 | |
|---|
| 477 | // join_if ------------------------------------------------------------------// |
|---|
| 478 | |
|---|
| 479 | #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING |
|---|
| 480 | |
|---|
| 481 | //! Conditional join algorithm |
|---|
| 482 | /*! |
|---|
| 483 | This algorithm joins all strings in a 'list' into one long string. |
|---|
| 484 | Segments are concatenated by given separator. Only segments that |
|---|
| 485 | match the given regular expression will be added to the result |
|---|
| 486 | |
|---|
| 487 | This is a specialization of join_if algorithm. |
|---|
| 488 | |
|---|
| 489 | \param Input A container that holds the input strings. It must be a container-of-containers. |
|---|
| 490 | \param Separator A string that will separate the joined segments. |
|---|
| 491 | \param Rx A regular expression |
|---|
| 492 | \param Flags Regex options |
|---|
| 493 | \return Concatenated string. |
|---|
| 494 | |
|---|
| 495 | \note This function provides the strong exception-safety guarantee |
|---|
| 496 | */ |
|---|
| 497 | template< |
|---|
| 498 | typename SequenceSequenceT, |
|---|
| 499 | typename Range1T, |
|---|
| 500 | typename CharT, |
|---|
| 501 | typename RegexTraitsT > |
|---|
| 502 | inline typename range_value<SequenceSequenceT>::type |
|---|
| 503 | join_if( |
|---|
| 504 | const SequenceSequenceT& Input, |
|---|
| 505 | Range1T& Separator, |
|---|
| 506 | const basic_regex<CharT, RegexTraitsT>& Rx, |
|---|
| 507 | match_flag_type Flags=match_default ) |
|---|
| 508 | { |
|---|
| 509 | // Define working types |
|---|
| 510 | typedef typename range_value<SequenceSequenceT>::type ResultT; |
|---|
| 511 | typedef typename range_const_iterator<SequenceSequenceT>::type InputIteratorT; |
|---|
| 512 | |
|---|
| 513 | // Parse input |
|---|
| 514 | InputIteratorT itBegin=begin(Input); |
|---|
| 515 | InputIteratorT itEnd=end(Input); |
|---|
| 516 | |
|---|
| 517 | // Construct container to hold the result |
|---|
| 518 | ResultT Result; |
|---|
| 519 | |
|---|
| 520 | |
|---|
| 521 | // Roll to the first element that will be added |
|---|
| 522 | while( |
|---|
| 523 | itBegin!=itEnd && |
|---|
| 524 | !regex_match(begin(*itBegin), end(*itBegin), Rx, Flags)) ++itBegin; |
|---|
| 525 | |
|---|
| 526 | // Add this element |
|---|
| 527 | if(itBegin!=itEnd) |
|---|
| 528 | { |
|---|
| 529 | detail::insert(Result, end(Result), *itBegin); |
|---|
| 530 | ++itBegin; |
|---|
| 531 | } |
|---|
| 532 | |
|---|
| 533 | for(;itBegin!=itEnd; ++itBegin) |
|---|
| 534 | { |
|---|
| 535 | if(regex_match(begin(*itBegin), end(*itBegin), Rx, Flags)) |
|---|
| 536 | { |
|---|
| 537 | // Add separator |
|---|
| 538 | detail::insert(Result, end(Result), Separator); |
|---|
| 539 | // Add element |
|---|
| 540 | detail::insert(Result, end(Result), *itBegin); |
|---|
| 541 | } |
|---|
| 542 | } |
|---|
| 543 | |
|---|
| 544 | return Result; |
|---|
| 545 | } |
|---|
| 546 | |
|---|
| 547 | #else // BOOST_NO_FUNCTION_TEMPLATE_ORDERING |
|---|
| 548 | |
|---|
| 549 | //! Conditional join algorithm |
|---|
| 550 | /*! |
|---|
| 551 | This algorithm joins all strings in a 'list' into one long string. |
|---|
| 552 | Segments are concatenated by given separator. Only segments that |
|---|
| 553 | match the given regular expression will be added to the result |
|---|
| 554 | |
|---|
| 555 | This is a specialization of join_if algorithm. |
|---|
| 556 | |
|---|
| 557 | \param Input A container that holds the input strings. It must be a container-of-containers. |
|---|
| 558 | \param Separator A string that will separate the joined segments. |
|---|
| 559 | \param Rx A regular expression |
|---|
| 560 | \param Flags Regex options |
|---|
| 561 | \return Concatenated string. |
|---|
| 562 | |
|---|
| 563 | \note This function provides the strong exception-safety guarantee |
|---|
| 564 | */ |
|---|
| 565 | template< |
|---|
| 566 | typename SequenceSequenceT, |
|---|
| 567 | typename Range1T, |
|---|
| 568 | typename CharT, |
|---|
| 569 | typename RegexTraitsT > |
|---|
| 570 | inline typename range_value<SequenceSequenceT>::type |
|---|
| 571 | join_if_regex( |
|---|
| 572 | const SequenceSequenceT& Input, |
|---|
| 573 | Range1T& Separator, |
|---|
| 574 | const basic_regex<CharT, RegexTraitsT>& Rx, |
|---|
| 575 | match_flag_type Flags=match_default ) |
|---|
| 576 | { |
|---|
| 577 | // Define working types |
|---|
| 578 | typedef typename range_value<SequenceSequenceT>::type ResultT; |
|---|
| 579 | typedef typename range_const_iterator<SequenceSequenceT>::type InputIteratorT; |
|---|
| 580 | |
|---|
| 581 | // Parse input |
|---|
| 582 | InputIteratorT itBegin=begin(Input); |
|---|
| 583 | InputIteratorT itEnd=end(Input); |
|---|
| 584 | |
|---|
| 585 | // Construct container to hold the result |
|---|
| 586 | ResultT Result; |
|---|
| 587 | |
|---|
| 588 | |
|---|
| 589 | // Roll to the first element that will be added |
|---|
| 590 | while( |
|---|
| 591 | itBegin!=itEnd && |
|---|
| 592 | !regex_match(begin(*itBegin), end(*itBegin), Rx, Flags)) ++itBegin; |
|---|
| 593 | |
|---|
| 594 | // Add this element |
|---|
| 595 | if(itBegin!=itEnd) |
|---|
| 596 | { |
|---|
| 597 | detail::insert(Result, end(Result), *itBegin); |
|---|
| 598 | ++itBegin; |
|---|
| 599 | } |
|---|
| 600 | |
|---|
| 601 | for(;itBegin!=itEnd; ++itBegin) |
|---|
| 602 | { |
|---|
| 603 | if(regex_match(begin(*itBegin), end(*itBegin), Rx, Flags)) |
|---|
| 604 | { |
|---|
| 605 | // Add separator |
|---|
| 606 | detail::insert(Result, end(Result), Separator); |
|---|
| 607 | // Add element |
|---|
| 608 | detail::insert(Result, end(Result), *itBegin); |
|---|
| 609 | } |
|---|
| 610 | } |
|---|
| 611 | |
|---|
| 612 | return Result; |
|---|
| 613 | } |
|---|
| 614 | |
|---|
| 615 | |
|---|
| 616 | #endif // BOOST_NO_FUNCTION_TEMPLATE_ORDERING |
|---|
| 617 | |
|---|
| 618 | } // namespace algorithm |
|---|
| 619 | |
|---|
| 620 | // pull names into the boost namespace |
|---|
| 621 | using algorithm::find_regex; |
|---|
| 622 | using algorithm::replace_regex; |
|---|
| 623 | using algorithm::replace_regex_copy; |
|---|
| 624 | using algorithm::replace_all_regex; |
|---|
| 625 | using algorithm::replace_all_regex_copy; |
|---|
| 626 | using algorithm::erase_regex; |
|---|
| 627 | using algorithm::erase_regex_copy; |
|---|
| 628 | using algorithm::erase_all_regex; |
|---|
| 629 | using algorithm::erase_all_regex_copy; |
|---|
| 630 | using algorithm::find_all_regex; |
|---|
| 631 | using algorithm::split_regex; |
|---|
| 632 | |
|---|
| 633 | #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING |
|---|
| 634 | using algorithm::join_if; |
|---|
| 635 | #else // BOOST_NO_FUNCTION_TEMPLATE_ORDERING |
|---|
| 636 | using algorithm::join_if_regex; |
|---|
| 637 | #endif // BOOST_NO_FUNCTION_TEMPLATE_ORDERING |
|---|
| 638 | |
|---|
| 639 | |
|---|
| 640 | |
|---|
| 641 | } // namespace boost |
|---|
| 642 | |
|---|
| 643 | |
|---|
| 644 | #endif // BOOST_STRING_REGEX_HPP |
|---|