From 34c49f856cfeb45d6d5dd76e0a01c96d65cc1b57 Mon Sep 17 00:00:00 2001 From: Pavol Droba Date: Wed, 1 Jun 2011 22:00:22 +0000 Subject: [PATCH 1/2] trim_fill algorithm added [SVN r72338] --- include/boost/algorithm/string/trim_all.hpp | 90 +++++++++++++++++++++ string/test/trim_test.cpp | 45 ++++++++++- 2 files changed, 134 insertions(+), 1 deletion(-) diff --git a/include/boost/algorithm/string/trim_all.hpp b/include/boost/algorithm/string/trim_all.hpp index e6c97c5..17aba2d 100644 --- a/include/boost/algorithm/string/trim_all.hpp +++ b/include/boost/algorithm/string/trim_all.hpp @@ -27,6 +27,9 @@ sequence (string). In addition, spaces in the middle of the sequence are truncated to just one character. Space is recognized using given locales. + \c trim_fill acts as trim_all, but the spaces in the middle are replaces with + a user-define sequence of character. + Parametric (\c _if) variants use a predicate (functor) to select which characters are to be trimmed.. Functions take a selection predicate as a parameter, which is used to determine @@ -114,6 +117,89 @@ namespace boost { } + //! Trim Fill - parametric + /*! + Remove all leading and trailing spaces from the input and + replace all every block of consecutive spaces with a fill string + defined by user. + The result is a trimmed copy of the input + + \param Input An input sequence + \param Fill A string used to fill the inner spaces + \param IsSpace An unary predicate identifying spaces + \return A trimmed copy of the input + */ + template + inline SequenceT trim_fill_copy_if(const SequenceT& Input, const RangeT& Fill, PredicateT IsSpace) + { + return + ::boost::find_format_all_copy( + ::boost::trim_copy_if(Input, IsSpace), + ::boost::token_finder(IsSpace, ::boost::token_compress_on), + ::boost::const_formatter(::boost::as_literal(Fill))); + } + + + //! Trim Fill + /*! + Remove all leading and trailing spaces from the input and + replace all every block of consecutive spaces with a fill string + defined by user. + The input sequence is modified in-place. + + \param Input An input sequence + \param Fill A string used to fill the inner spaces + \param IsSpace An unary predicate identifying spaces + */ + template + inline void trim_fill_if(SequenceT& Input, const RangeT& Fill, PredicateT IsSpace) + { + ::boost::trim_if(Input, IsSpace); + ::boost::find_format_all( + Input, + ::boost::token_finder(IsSpace, ::boost::token_compress_on), + ::boost::const_formatter(::boost::as_literal(Fill))); + } + + + //! Trim Fill + /*! + Remove all leading and trailing spaces from the input and + replace all every block of consecutive spaces with a fill string + defined by user. + The result is a trimmed copy of the input + + \param Input An input sequence + \param Fill A string used to fill the inner spaces + \param Loc A locale used for 'space' classification + \return A trimmed copy of the input + */ + template + inline SequenceT trim_fill_copy(const SequenceT& Input, const RangeT& Fill, const std::locale& Loc =std::locale()) + { + return trim_fill_copy_if(Input, Fill, ::boost::is_space(Loc)); + } + + + //! Trim Fill + /*! + Remove all leading and trailing spaces from the input and + replace all every block of consecutive spaces with a fill string + defined by user. + The input sequence is modified in-place. + + \param Input An input sequence + \param Fill A string used to fill the inner spaces + \param Loc A locale used for 'space' classification + \return A trimmed copy of the input + */ + template + inline void trim_fill(SequenceT& Input, const RangeT& Fill, const std::locale& Loc =std::locale()) + { + trim_fill_if(Input, Fill, ::boost::is_space(Loc)); + } + + } // namespace algorithm // pull names to the boost namespace @@ -121,6 +207,10 @@ namespace boost { using algorithm::trim_all_if; using algorithm::trim_all_copy; using algorithm::trim_all_copy_if; + using algorithm::trim_fill; + using algorithm::trim_fill_if; + using algorithm::trim_fill_copy; + using algorithm::trim_fill_copy_if; } // namespace boost diff --git a/string/test/trim_test.cpp b/string/test/trim_test.cpp index a19b1b7..8dcce28 100644 --- a/string/test/trim_test.cpp +++ b/string/test/trim_test.cpp @@ -151,11 +151,54 @@ void trim_all_test() BOOST_CHECK( trim_all_copy_if( string("<>abc<>def<>"), is_any_of( "<<>>" ) )=="abcabc<>def<>"), "-", is_any_of( "<<>>" ) )=="abc-def" ); +} + // test main int test_main( int, char*[] ) { trim_test(); trim_all_test(); - + trim_fill_test(); + return 0; } From e92d471817f65e06739cf287bf3e9e652c58edb9 Mon Sep 17 00:00:00 2001 From: Pavol Droba Date: Thu, 2 Jun 2011 20:08:16 +0000 Subject: [PATCH 2/2] Comment updated [SVN r72350] --- include/boost/algorithm/string/trim_all.hpp | 46 ++++++++++----------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/include/boost/algorithm/string/trim_all.hpp b/include/boost/algorithm/string/trim_all.hpp index 17aba2d..70fd041 100644 --- a/include/boost/algorithm/string/trim_all.hpp +++ b/include/boost/algorithm/string/trim_all.hpp @@ -27,8 +27,8 @@ sequence (string). In addition, spaces in the middle of the sequence are truncated to just one character. Space is recognized using given locales. - \c trim_fill acts as trim_all, but the spaces in the middle are replaces with - a user-define sequence of character. + \c trim_fill acts as trim_all, but the spaces in the middle are replaces with + a user-define sequence of character. Parametric (\c _if) variants use a predicate (functor) to select which characters are to be trimmed.. @@ -45,7 +45,7 @@ namespace boost { //! Trim All - parametric /*! Remove all leading and trailing spaces from the input and - compress all other spaces to a single space. + compress all other spaces to a single character. The result is a trimmed copy of the input \param Input An input sequence @@ -66,7 +66,7 @@ namespace boost { //! Trim All /*! Remove all leading and trailing spaces from the input and - compress all other spaces to a single space. + compress all other spaces to a single character. The input sequence is modified in-place. \param Input An input sequence @@ -86,7 +86,7 @@ namespace boost { //! Trim All /*! Remove all leading and trailing spaces from the input and - compress all other spaces to a single space. + compress all other spaces to a single character. The result is a trimmed copy of the input \param Input An input sequence @@ -103,7 +103,7 @@ namespace boost { //! Trim All /*! Remove all leading and trailing spaces from the input and - compress all other spaces to a single space. + compress all other spaces to a single character. The input sequence is modified in-place. \param Input An input sequence @@ -121,11 +121,11 @@ namespace boost { /*! Remove all leading and trailing spaces from the input and replace all every block of consecutive spaces with a fill string - defined by user. + defined by user. The result is a trimmed copy of the input \param Input An input sequence - \param Fill A string used to fill the inner spaces + \param Fill A string used to fill the inner spaces \param IsSpace An unary predicate identifying spaces \return A trimmed copy of the input */ @@ -142,13 +142,13 @@ namespace boost { //! Trim Fill /*! - Remove all leading and trailing spaces from the input and - replace all every block of consecutive spaces with a fill string - defined by user. + Remove all leading and trailing spaces from the input and + replace all every block of consecutive spaces with a fill string + defined by user. The input sequence is modified in-place. \param Input An input sequence - \param Fill A string used to fill the inner spaces + \param Fill A string used to fill the inner spaces \param IsSpace An unary predicate identifying spaces */ template @@ -164,9 +164,9 @@ namespace boost { //! Trim Fill /*! - Remove all leading and trailing spaces from the input and - replace all every block of consecutive spaces with a fill string - defined by user. + Remove all leading and trailing spaces from the input and + replace all every block of consecutive spaces with a fill string + defined by user. The result is a trimmed copy of the input \param Input An input sequence @@ -183,13 +183,13 @@ namespace boost { //! Trim Fill /*! - Remove all leading and trailing spaces from the input and - replace all every block of consecutive spaces with a fill string - defined by user. + Remove all leading and trailing spaces from the input and + replace all every block of consecutive spaces with a fill string + defined by user. The input sequence is modified in-place. \param Input An input sequence - \param Fill A string used to fill the inner spaces + \param Fill A string used to fill the inner spaces \param Loc A locale used for 'space' classification \return A trimmed copy of the input */ @@ -207,10 +207,10 @@ namespace boost { using algorithm::trim_all_if; using algorithm::trim_all_copy; using algorithm::trim_all_copy_if; - using algorithm::trim_fill; - using algorithm::trim_fill_if; - using algorithm::trim_fill_copy; - using algorithm::trim_fill_copy_if; + using algorithm::trim_fill; + using algorithm::trim_fill_if; + using algorithm::trim_fill_copy; + using algorithm::trim_fill_copy_if; } // namespace boost