diff --git a/include/boost/algorithm/string/trim_all.hpp b/include/boost/algorithm/string/trim_all.hpp new file mode 100644 index 0000000..0fce694 --- /dev/null +++ b/include/boost/algorithm/string/trim_all.hpp @@ -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 + +#include +#include +#include +#include +#include +#include + +/*! \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 + 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 + 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 + 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 + 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 diff --git a/string/doc/Jamfile.v2 b/string/doc/Jamfile.v2 index fcdc99e..9ddebb8 100644 --- a/string/doc/Jamfile.v2 +++ b/string/doc/Jamfile.v2 @@ -43,6 +43,7 @@ doxygen autodoc [ 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 ] : HIDE_UNDOC_MEMBERS=YES EXTRACT_PRIVATE=NO diff --git a/string/test/trim_test.cpp b/string/test/trim_test.cpp index ed80c28..55c7b83 100644 --- a/string/test/trim_test.cpp +++ b/string/test/trim_test.cpp @@ -8,6 +8,7 @@ // See http://www.boost.org for updates, documentation, and revision history. #include +#include // Include unit test framework #include @@ -109,10 +110,52 @@ void trim_test() 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