Ticket #1152: find_format.patch

File find_format.patch, 15.5 KB (added by Steven Watanabe, 13 years ago)

patch and test case

  • boost/algorithm/string/detail/find_format_all.hpp

     
    2929                typename InputT,
    3030                typename FinderT,
    3131                typename FormatterT,
    32                 typename FindResultT >
    33             inline OutputIteratorT find_format_all_copy_impl(
    34                 OutputIteratorT Output,
    35                 const InputT& Input,
    36                 FinderT Finder,
    37                 FormatterT Formatter,
    38                 const FindResultT& FindResult )
    39             {       
    40                 return find_format_all_copy_impl2(
    41                     Output,
    42                     Input,
    43                     Finder,
    44                     Formatter,
    45                     FindResult,
    46                     Formatter(FindResult) );
    47             }
    48 
    49             template<
    50                 typename OutputIteratorT,
    51                 typename InputT,
    52                 typename FinderT,
    53                 typename FormatterT,
    5432                typename FindResultT,
    5533                typename FormatResultT >
    5634            inline OutputIteratorT find_format_all_copy_impl2(
     
    7957                while( M )
    8058                {
    8159                    // Copy the beginning of the sequence
    82                     std::copy( LastMatch, M.begin(), Output );
     60                    Output = std::copy( LastMatch, M.begin(), Output );
    8361                    // Copy formated result
    84                     std::copy( ::boost::begin(M.format_result()), ::boost::end(M.format_result()), Output );
     62                    Output = std::copy( ::boost::begin(M.format_result()), ::boost::end(M.format_result()), Output );
    8563
    8664                    // Proceed to the next match
    8765                    LastMatch=M.end();
     
    8967                }
    9068
    9169                // Copy the rest of the sequence
    92                 std::copy( LastMatch, ::boost::end(Input), Output );
     70                Output = std::copy( LastMatch, ::boost::end(Input), Output );
    9371
    9472                return Output;
    9573            }
    9674
    97 // find_format_all_copy implementation ----------------------------------------------//
    98 
    9975            template<
    100                 typename InputT,
     76                typename OutputIteratorT,
     77                typename InputT,
    10178                typename FinderT,
    10279                typename FormatterT,
    10380                typename FindResultT >
    104             inline InputT find_format_all_copy_impl(
     81            inline OutputIteratorT find_format_all_copy_impl(
     82                OutputIteratorT Output,
    10583                const InputT& Input,
    10684                FinderT Finder,
    10785                FormatterT Formatter,
    108                 const FindResultT& FindResult)
    109             {
    110                 return find_format_all_copy_impl2(
     86                const FindResultT& FindResult )
     87            {       
     88                return find_format_all_copy_impl2(
     89                    Output,
    11190                    Input,
    11291                    Finder,
    11392                    Formatter,
     
    11594                    Formatter(FindResult) );
    11695            }
    11796
     97// find_format_all_copy implementation ----------------------------------------------//
     98
    11899            template<
    119100                typename InputT,
    120101                typename FinderT,
     
    164145                return Output;
    165146            }
    166147
    167 // find_format_all implementation ------------------------------------------------//
    168        
    169             template<
    170                 typename InputT,
     148            template<
     149                typename InputT,
    171150                typename FinderT,
    172151                typename FormatterT,
    173152                typename FindResultT >
    174             inline void find_format_all_impl(
    175                 InputT& Input,
     153            inline InputT find_format_all_copy_impl(
     154                const InputT& Input,
    176155                FinderT Finder,
    177156                FormatterT Formatter,
    178                 FindResultT FindResult)
     157                const FindResultT& FindResult)
    179158            {
    180                 find_format_all_impl2(
     159                return find_format_all_copy_impl2(
    181160                    Input,
    182161                    Finder,
    183162                    Formatter,
     
    185164                    Formatter(FindResult) );
    186165            }
    187166
     167// find_format_all implementation ------------------------------------------------//
     168
    188169            template<
    189170                typename InputT,
    190171                typename FinderT,
     
    255236                    insert( Input, ::boost::end(Input), Storage.begin(), Storage.end() );
    256237                }
    257238            }
     239       
     240            template<
     241                typename InputT,
     242                typename FinderT,
     243                typename FormatterT,
     244                typename FindResultT >
     245            inline void find_format_all_impl(
     246                InputT& Input,
     247                FinderT Finder,
     248                FormatterT Formatter,
     249                FindResultT FindResult)
     250            {
     251                find_format_all_impl2(
     252                    Input,
     253                    Finder,
     254                    Formatter,
     255                    FindResult,
     256                    Formatter(FindResult) );
     257            }
    258258
    259259        } // namespace detail
    260260    } // namespace algorithm
  • boost/algorithm/string/detail/find_format.hpp

     
    2828                typename OutputIteratorT,
    2929                typename InputT,
    3030                typename FormatterT,
    31                 typename FindResultT >
    32             inline OutputIteratorT find_format_copy_impl(
    33                 OutputIteratorT Output,
    34                 const InputT& Input,
    35                 FormatterT Formatter,
    36                 const FindResultT& FindResult )
    37             {       
    38                 return find_format_copy_impl2(
    39                     Output,
    40                     Input,
    41                     Formatter,
    42                     FindResult,
    43                     Formatter(FindResult) );
    44             }
    45 
    46             template<
    47                 typename OutputIteratorT,
    48                 typename InputT,
    49                 typename FormatterT,
    5031                typename FindResultT,
    5132                typename FormatResultT >
    5233            inline OutputIteratorT find_format_copy_impl2(
     
    6849                if ( !M )
    6950                {
    7051                    // Match not found - return original sequence
    71                     std::copy( ::boost::begin(Input), ::boost::end(Input), Output );
     52                    Output = std::copy( ::boost::begin(Input), ::boost::end(Input), Output );
    7253                    return Output;
    7354                }
    7455
    7556                // Copy the beginning of the sequence
    76                 std::copy( ::boost::begin(Input), ::boost::begin(M), Output );
     57                Output = std::copy( ::boost::begin(Input), ::boost::begin(M), Output );
    7758                // Format find result
    7859                // Copy formated result
    79                 std::copy( ::boost::begin(M.format_result()), ::boost::end(M.format_result()), Output );
     60                Output = std::copy( ::boost::begin(M.format_result()), ::boost::end(M.format_result()), Output );
    8061                // Copy the rest of the sequence
    81                 std::copy( M.end(), ::boost::end(Input), Output );
     62                Output = std::copy( M.end(), ::boost::end(Input), Output );
    8263
    8364                return Output;
    8465            }
    8566
    86 // find_format_copy implementation --------------------------------------------------//
    87 
    8867            template<
    89                 typename InputT,
     68                typename OutputIteratorT,
     69                typename InputT,
    9070                typename FormatterT,
    9171                typename FindResultT >
    92             inline InputT find_format_copy_impl(
     72            inline OutputIteratorT find_format_copy_impl(
     73                OutputIteratorT Output,
    9374                const InputT& Input,
    9475                FormatterT Formatter,
    95                 const FindResultT& FindResult)
    96             {
    97                 return find_format_copy_impl2(
     76                const FindResultT& FindResult )
     77            {       
     78                return find_format_copy_impl2(
     79                    Output,
    9880                    Input,
    9981                    Formatter,
    10082                    FindResult,
    10183                    Formatter(FindResult) );
    10284            }
    10385
     86// find_format_copy implementation --------------------------------------------------//
     87
    10488            template<
    10589                typename InputT,
    10690                typename FormatterT,
     
    138122                return Output;
    139123            }
    140124
    141 // replace implementation ----------------------------------------------------//
    142        
    143             template<
    144                 typename InputT,
     125            template<
     126                typename InputT,
    145127                typename FormatterT,
    146128                typename FindResultT >
    147             inline void find_format_impl(
    148                 InputT& Input,
     129            inline InputT find_format_copy_impl(
     130                const InputT& Input,
    149131                FormatterT Formatter,
    150132                const FindResultT& FindResult)
    151133            {
    152                 find_format_impl2(
     134                return find_format_copy_impl2(
    153135                    Input,
    154136                    Formatter,
    155137                    FindResult,
    156138                    Formatter(FindResult) );
    157139            }
    158140
     141// replace implementation ----------------------------------------------------//
     142
    159143            template<
    160144                typename InputT,
    161145                typename FormatterT,
     
    185169                // Replace match
    186170                replace( Input, M.begin(), M.end(), M.format_result() );
    187171            }
     172       
     173            template<
     174                typename InputT,
     175                typename FormatterT,
     176                typename FindResultT >
     177            inline void find_format_impl(
     178                InputT& Input,
     179                FormatterT Formatter,
     180                const FindResultT& FindResult)
     181            {
     182                find_format_impl2(
     183                    Input,
     184                    Formatter,
     185                    FindResult,
     186                    Formatter(FindResult) );
     187            }
    188188
    189189        } // namespace detail
    190190    } // namespace algorithm
  • libs/algorithm/string/test/find_format_test.cpp

     
     1//  Boost string_algo library find_format_test.cpp file  ------------------//
     2
     3//  Copyright (c) 2009 Steven Watanabe
     4//  Distributed under the Boost Software License, Version 1.0. (See
     5//  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#include <boost/algorithm/string/find_format.hpp>
     11#include <boost/algorithm/string/finder.hpp>
     12#include <boost/algorithm/string/formatter.hpp>
     13
     14// Include unit test framework
     15#include <boost/test/included/test_exec_monitor.hpp>
     16
     17#include <boost/test/test_tools.hpp>
     18
     19void find_format_test()
     20{
     21    const std::string source = "$replace $replace";
     22    std::string expected = "ok $replace";
     23    std::string output(80, '\0');
     24
     25    std::string::iterator pos =
     26        boost::find_format_copy(output.begin(),
     27                                source,
     28                                boost::first_finder("$replace"),
     29                                boost::const_formatter("ok"));
     30    BOOST_CHECK(pos == output.begin() + expected.size());
     31    output.erase(std::remove(output.begin(), output.end(), '\0'), output.end());
     32    BOOST_CHECK_EQUAL(output, expected);
     33
     34    output = boost::find_format_copy(source, boost::first_finder("$replace"), boost::const_formatter("ok"));
     35    BOOST_CHECK_EQUAL(output, expected);
     36
     37    // now try finding a string that doesn't exist
     38    output.resize(80);
     39    pos = boost::find_format_copy(output.begin(),
     40                                source,
     41                                boost::first_finder("$noreplace"),
     42                                boost::const_formatter("bad"));
     43    BOOST_CHECK(pos == output.begin() + source.size());
     44    output.erase(std::remove(output.begin(), output.end(), '\0'), output.end());
     45    BOOST_CHECK_EQUAL(output, source);
     46
     47    output = boost::find_format_copy(source, boost::first_finder("$noreplace"), boost::const_formatter("bad"));
     48    BOOST_CHECK_EQUAL(output, source);
     49
     50    // in place version
     51    output = source;
     52    boost::find_format(output, boost::first_finder("$replace"), boost::const_formatter("ok"));
     53    BOOST_CHECK_EQUAL(output, expected);
     54    output = source;
     55    boost::find_format(output, boost::first_finder("$noreplace"), boost::const_formatter("bad"));
     56    BOOST_CHECK_EQUAL(output, source);
     57}
     58
     59void find_format_all_test()
     60{
     61    const std::string source = "$replace $replace";
     62    std::string expected = "ok ok";
     63    std::string output(80, '\0');
     64
     65    std::string::iterator pos =
     66        boost::find_format_all_copy(output.begin(),
     67                                source,
     68                                boost::first_finder("$replace"),
     69                                boost::const_formatter("ok"));
     70    BOOST_CHECK(pos == output.begin() + expected.size());
     71    output.erase(std::remove(output.begin(), output.end(), '\0'), output.end());
     72    BOOST_CHECK_EQUAL(output, expected);
     73
     74    output = boost::find_format_all_copy(source, boost::first_finder("$replace"), boost::const_formatter("ok"));
     75    BOOST_CHECK_EQUAL(output, expected);
     76
     77    // now try finding a string that doesn't exist
     78    output.resize(80);
     79    pos = boost::find_format_all_copy(output.begin(),
     80                                source,
     81                                boost::first_finder("$noreplace"),
     82                                boost::const_formatter("bad"));
     83    BOOST_CHECK(pos == output.begin() + source.size());
     84    output.erase(std::remove(output.begin(), output.end(), '\0'), output.end());
     85    BOOST_CHECK_EQUAL(output, source);
     86
     87    output = boost::find_format_all_copy(source, boost::first_finder("$noreplace"), boost::const_formatter("bad"));
     88    BOOST_CHECK_EQUAL(output, source);
     89
     90    // in place version
     91    output = source;
     92    boost::find_format_all(output, boost::first_finder("$replace"), boost::const_formatter("ok"));
     93    BOOST_CHECK_EQUAL(output, expected);
     94    output = source;
     95    boost::find_format_all(output, boost::first_finder("$noreplace"), boost::const_formatter("bad"));
     96    BOOST_CHECK_EQUAL(output, source);
     97}
     98
     99int test_main( int, char*[] )
     100{
     101    find_format_test();
     102    find_format_all_test();
     103
     104    return 0;
     105}
  • libs/algorithm/string/test/Jamfile.v2

    Property changes on: libs\algorithm\string\test\find_format_test.cpp
    ___________________________________________________________________
    Name: svn:mime-type
       + text/plain
    Name: svn:keywords
       + Id
    Name: svn:eol-style
       + native
    
     
    5959            :   
    6060            : regex
    6161        ]
     62      [ run
     63            find_format_test.cpp
     64          : :
     65            :   
     66            : find_format
     67        ]
    6268    ;
    6369