Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r72338 - in trunk: boost/algorithm/string libs/algorithm/string/test
From: droba_at_[hidden]
Date: 2011-06-01 18:00:23


Author: pavol_droba
Date: 2011-06-01 18:00:22 EDT (Wed, 01 Jun 2011)
New Revision: 72338
URL: http://svn.boost.org/trac/boost/changeset/72338

Log:
trim_fill algorithm added

Text files modified:
   trunk/boost/algorithm/string/trim_all.hpp | 90 ++++++++++++++++++++++++++++++++++++++++
   trunk/libs/algorithm/string/test/trim_test.cpp | 45 +++++++++++++++++++
   2 files changed, 134 insertions(+), 1 deletions(-)

Modified: trunk/boost/algorithm/string/trim_all.hpp
==============================================================================
--- trunk/boost/algorithm/string/trim_all.hpp (original)
+++ trunk/boost/algorithm/string/trim_all.hpp 2011-06-01 18:00:22 EDT (Wed, 01 Jun 2011)
@@ -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 @@
         }
 
 
+ //! 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<typename SequenceT, typename RangeT, typename PredicateT>
+ 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<typename SequenceT, typename RangeT, typename PredicateT>
+ 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<typename SequenceT, typename RangeT>
+ 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<typename SequenceT, typename RangeT>
+ 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 @@
     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
 

Modified: trunk/libs/algorithm/string/test/trim_test.cpp
==============================================================================
--- trunk/libs/algorithm/string/test/trim_test.cpp (original)
+++ trunk/libs/algorithm/string/test/trim_test.cpp 2011-06-01 18:00:22 EDT (Wed, 01 Jun 2011)
@@ -151,11 +151,54 @@
     BOOST_CHECK( trim_all_copy_if( string("<>abc<>def<>"), is_any_of( "<<>>" ) )=="abc<def" );
 }
 
+void trim_fill_test()
+{
+ string str1(" 1x x x x1 ");
+ string str2("+---...2x+--x--+x-+-x2...---+");
+ string str3(" ");
+
+ // *** value passing tests *** //
+
+ // general string test
+ BOOST_CHECK( trim_fill_copy( str1, "-" )=="1x-x-x-x1" ) ;
+ BOOST_CHECK( trim_fill_copy_if( str2, " ", is_punct() )=="2x x x x2" ) ;
+
+ // spaces-only string test
+ BOOST_CHECK( trim_fill_copy( str3, " " )=="" );
+
+ // empty string check
+ BOOST_CHECK( trim_fill_copy( string(""), " " )=="" );
+
+ // general string test
+ trim_fill( str1, "-" );
+ BOOST_CHECK( str1=="1x-x-x-x1" ) ;
+ trim_fill_if( str2, "", is_punct() );
+ BOOST_CHECK( str2=="2xxxx2" ) ;
+
+ // spaces-only string test
+ str3 = " "; trim_fill( str3, "" );
+ BOOST_CHECK( str3=="" );
+
+ // empty string check
+ str3 = ""; trim_fill( str3, "" );
+ BOOST_CHECK( str3=="" );
+ BOOST_CHECK( str3=="" );
+
+ // *** non-standard predicate tests *** //
+ BOOST_CHECK(
+ trim_fill_copy_if(
+ string("123abc127deb456"),
+ "+",
+ is_classified(std::ctype_base::digit) )=="abc+deb" );
+ BOOST_CHECK( trim_fill_copy_if( string("<>abc<>def<>"), "-", is_any_of( "<<>>" ) )=="abc-def" );
+}
+
 // test main
 int test_main( int, char*[] )
 {
     trim_test();
     trim_all_test();
-
+ trim_fill_test();
+
     return 0;
 }


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk