Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r68161 - in trunk: boost/algorithm/string libs/algorithm/string/doc libs/algorithm/string/test
From: droba_at_[hidden]
Date: 2011-01-14 18:06:17


Author: pavol_droba
Date: 2011-01-14 18:06:14 EST (Fri, 14 Jan 2011)
New Revision: 68161
URL: http://svn.boost.org/trac/boost/changeset/68161

Log:
trim_all algorithm added

Added:
   trunk/boost/algorithm/string/trim_all.hpp (contents, props changed)
Text files modified:
   trunk/libs/algorithm/string/doc/Jamfile.v2 | 1
   trunk/libs/algorithm/string/test/trim_test.cpp | 43 ++++++++++++++++++++++++++++++++++++++++
   2 files changed, 44 insertions(+), 0 deletions(-)

Added: trunk/boost/algorithm/string/trim_all.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/algorithm/string/trim_all.hpp 2011-01-14 18:06:14 EST (Fri, 14 Jan 2011)
@@ -0,0 +1,127 @@
+// Boost string_algo library trim.hpp header file ---------------------------//
+
+// Copyright Pavol Droba 2002-2003.
+//
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
+
+// See http://www.boost.org/ for updates, documentation, and revision history.
+
+#ifndef BOOST_STRING_TRIM_ALL_HPP
+#define BOOST_STRING_TRIM_ALL_HPP
+
+#include <boost/algorithm/string/config.hpp>
+
+#include <boost/algorithm/string/trim.hpp>
+#include <boost/algorithm/string/classification.hpp>
+#include <boost/algorithm/string/find_format.hpp>
+#include <boost/algorithm/string/formatter.hpp>
+#include <boost/algorithm/string/finder.hpp>
+#include <locale>
+
+/*! \file
+ Defines trim_all algorithms.
+
+ Just like \c trim, \c trim_all removes all trailing and leading spaces from a
+ sequence (string). In addition, spaces in the middle of the sequence are truncated
+ to just one character. Space is recognized using given locales.
+
+ 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
+ whether a character is a space. Common predicates are provided in classification.hpp header.
+
+*/
+
+namespace boost {
+ namespace algorithm {
+
+ // multi line trim ----------------------------------------------- //
+
+ //! Trim All - parametric
+ /*!
+ Remove all leading and trailing spaces from the input and
+ compress all other spaces to a single space.
+ The result is a trimmed copy of the input
+
+ \param Input An input sequence
+ \param IsSpace An unary predicate identifying spaces
+ \return A trimmed copy of the input
+ */
+ template<typename SequenceT, typename PredicateT>
+ inline SequenceT trim_all_copy_if(const SequenceT& Input, PredicateT IsSpace)
+ {
+ return
+ ::boost::find_format_all_copy(
+ ::boost::trim_copy_if(Input, IsSpace),
+ ::boost::token_finder(IsSpace, ::boost::token_compress_on),
+ ::boost::dissect_formatter(::boost::head_finder(1)));
+ }
+
+
+ //! Trim All
+ /*!
+ Remove all leading and trailing spaces from the input and
+ compress all other spaces to a single space.
+ The input sequence is modified in-place.
+
+ \param Input An input sequence
+ \param IsSpace An unary predicate identifying spaces
+ */
+ template<typename SequenceT, typename PredicateT>
+ inline void trim_all_if(SequenceT& Input, PredicateT IsSpace)
+ {
+ ::boost::trim_if(Input, IsSpace);
+ ::boost::find_format_all(
+ Input,
+ ::boost::token_finder(IsSpace, ::boost::token_compress_on),
+ ::boost::dissect_formatter(::boost::head_finder(1)));
+ }
+
+
+ //! Trim All
+ /*!
+ Remove all leading and trailing spaces from the input and
+ compress all other spaces to a single space.
+ The result is a trimmed copy of the input
+
+ \param Input An input sequence
+ \param Loc A locale used for 'space' classification
+ \return A trimmed copy of the input
+ */
+ template<typename SequenceT>
+ inline SequenceT trim_all_copy(const SequenceT& Input, const std::locale& Loc =std::locale())
+ {
+ return trim_all_copy_if(Input, ::boost::is_space(Loc));
+ }
+
+
+ //! Trim All
+ /*!
+ Remove all leading and trailing spaces from the input and
+ compress all other spaces to a single space.
+ The input sequence is modified in-place.
+
+ \param Input An input sequence
+ \param Loc A locale used for 'space' classification
+ \return A trimmed copy of the input
+ */
+ template<typename SequenceT>
+ inline void trim_all(SequenceT& Input, const std::locale& Loc =std::locale())
+ {
+ trim_all_if(Input, ::boost::is_space(Loc));
+ }
+
+
+ } // namespace algorithm
+
+ // pull names to the boost namespace
+ using algorithm::trim_all;
+ using algorithm::trim_all_if;
+ using algorithm::trim_all_copy;
+ using algorithm::trim_all_copy_if;
+
+} // namespace boost
+
+#endif // BOOST_STRING_TRIM_ALL_HPP

Modified: trunk/libs/algorithm/string/doc/Jamfile.v2
==============================================================================
--- trunk/libs/algorithm/string/doc/Jamfile.v2 (original)
+++ trunk/libs/algorithm/string/doc/Jamfile.v2 2011-01-14 18:06:14 EST (Fri, 14 Jan 2011)
@@ -43,6 +43,7 @@
     [ glob ../../../../boost/algorithm/string/formatter.hpp ]
     [ glob ../../../../boost/algorithm/string/regex.hpp ]
     [ glob ../../../../boost/algorithm/string/regex_find_format.hpp ]
+ [ glob ../../../../boost/algorithm/string/trim_all.hpp ]
     :
     <doxygen:param>HIDE_UNDOC_MEMBERS=YES
     <doxygen:param>EXTRACT_PRIVATE=NO

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-01-14 18:06:14 EST (Fri, 14 Jan 2011)
@@ -8,6 +8,7 @@
 // See http://www.boost.org for updates, documentation, and revision history.
 
 #include <boost/algorithm/string/trim.hpp>
+#include <boost/algorithm/string/trim_all.hpp>
 
 // Include unit test framework
 #include <boost/test/included/test_exec_monitor.hpp>
@@ -109,10 +110,52 @@
     BOOST_CHECK( trim_copy_if( string("<>abc<>"), is_any_of( "<<>>" ) )=="abc" );
 }
 
+void trim_all_test()
+{
+ string str1(" 1x x x x1 ");
+ string str2(" 2x x x x2 ");
+ string str3(" ");
+
+ // *** value passing tests *** //
+
+ // general string test
+ BOOST_CHECK( trim_all_copy( str1 )=="1x x x x1" ) ;
+ BOOST_CHECK( trim_all_copy( str2 )=="2x x x x2" ) ;
+
+ // spaces-only string test
+ BOOST_CHECK( trim_all_copy( str3 )=="" );
+
+ // empty string check
+ BOOST_CHECK( trim_all_copy( string("") )=="" );
+
+ // general string test
+ trim_all( str1 );
+ BOOST_CHECK( str1=="1x x x x1" ) ;
+ trim_all( str2 );
+ BOOST_CHECK( str2=="2x x x x2" ) ;
+
+ // spaces-only string test
+ str3 = " "; trim_all( str3 );
+ BOOST_CHECK( str3=="" );
+
+ // empty string check
+ str3 = ""; trim_all( str3 );
+ BOOST_CHECK( str3=="" );
+ BOOST_CHECK( str3=="" );
+
+ // *** non-standard predicate tests *** //
+ BOOST_CHECK(
+ trim_all_copy_if(
+ string("123abc127deb456"),
+ is_classified(std::ctype_base::digit) )=="abc1deb" );
+ BOOST_CHECK( trim_all_copy_if( string("<>abc<>def<>"), is_any_of( "<<>>" ) )=="abc<def" );
+}
+
 // test main
 int test_main( int, char*[] )
 {
     trim_test();
+ trim_all_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