Chris@16: // Boost string_algo library trim.hpp header file ---------------------------// Chris@16: Chris@16: // Copyright Pavol Droba 2002-2003. Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. Chris@16: // (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: // See http://www.boost.org/ for updates, documentation, and revision history. Chris@16: Chris@16: #ifndef BOOST_STRING_TRIM_ALL_HPP Chris@16: #define BOOST_STRING_TRIM_ALL_HPP Chris@16: Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: /*! \file Chris@16: Defines trim_all algorithms. Chris@16: Chris@16: Just like \c trim, \c trim_all removes all trailing and leading spaces from a Chris@16: sequence (string). In addition, spaces in the middle of the sequence are truncated Chris@16: to just one character. Space is recognized using given locales. Chris@16: Chris@16: \c trim_fill acts as trim_all, but the spaces in the middle are replaces with Chris@16: a user-define sequence of character. Chris@16: Chris@16: Parametric (\c _if) variants use a predicate (functor) to select which characters Chris@16: are to be trimmed.. Chris@16: Functions take a selection predicate as a parameter, which is used to determine Chris@16: whether a character is a space. Common predicates are provided in classification.hpp header. Chris@16: Chris@16: */ Chris@16: Chris@16: namespace boost { Chris@16: namespace algorithm { Chris@16: Chris@16: // multi line trim ----------------------------------------------- // Chris@16: Chris@16: //! Trim All - parametric Chris@16: /*! Chris@16: Remove all leading and trailing spaces from the input and Chris@16: compress all other spaces to a single character. Chris@16: The result is a trimmed copy of the input Chris@16: Chris@16: \param Input An input sequence Chris@16: \param IsSpace A unary predicate identifying spaces Chris@16: \return A trimmed copy of the input Chris@16: */ Chris@16: template Chris@16: inline SequenceT trim_all_copy_if(const SequenceT& Input, PredicateT IsSpace) Chris@16: { Chris@16: return Chris@16: ::boost::find_format_all_copy( Chris@16: ::boost::trim_copy_if(Input, IsSpace), Chris@16: ::boost::token_finder(IsSpace, ::boost::token_compress_on), Chris@16: ::boost::dissect_formatter(::boost::head_finder(1))); Chris@16: } Chris@16: Chris@16: Chris@16: //! Trim All Chris@16: /*! Chris@16: Remove all leading and trailing spaces from the input and Chris@16: compress all other spaces to a single character. Chris@16: The input sequence is modified in-place. Chris@16: Chris@16: \param Input An input sequence Chris@16: \param IsSpace A unary predicate identifying spaces Chris@16: */ Chris@16: template Chris@16: inline void trim_all_if(SequenceT& Input, PredicateT IsSpace) Chris@16: { Chris@16: ::boost::trim_if(Input, IsSpace); Chris@16: ::boost::find_format_all( Chris@16: Input, Chris@16: ::boost::token_finder(IsSpace, ::boost::token_compress_on), Chris@16: ::boost::dissect_formatter(::boost::head_finder(1))); Chris@16: } Chris@16: Chris@16: Chris@16: //! Trim All Chris@16: /*! Chris@16: Remove all leading and trailing spaces from the input and Chris@16: compress all other spaces to a single character. Chris@16: The result is a trimmed copy of the input Chris@16: Chris@16: \param Input An input sequence Chris@16: \param Loc A locale used for 'space' classification Chris@16: \return A trimmed copy of the input Chris@16: */ Chris@16: template Chris@16: inline SequenceT trim_all_copy(const SequenceT& Input, const std::locale& Loc =std::locale()) Chris@16: { Chris@16: return trim_all_copy_if(Input, ::boost::is_space(Loc)); Chris@16: } Chris@16: Chris@16: Chris@16: //! Trim All Chris@16: /*! Chris@16: Remove all leading and trailing spaces from the input and Chris@16: compress all other spaces to a single character. Chris@16: The input sequence is modified in-place. Chris@16: Chris@16: \param Input An input sequence Chris@16: \param Loc A locale used for 'space' classification Chris@16: \return A trimmed copy of the input Chris@16: */ Chris@16: template Chris@16: inline void trim_all(SequenceT& Input, const std::locale& Loc =std::locale()) Chris@16: { Chris@16: trim_all_if(Input, ::boost::is_space(Loc)); Chris@16: } Chris@16: Chris@16: Chris@16: //! Trim Fill - parametric Chris@16: /*! Chris@16: Remove all leading and trailing spaces from the input and Chris@16: replace all every block of consecutive spaces with a fill string Chris@16: defined by user. Chris@16: The result is a trimmed copy of the input Chris@16: Chris@16: \param Input An input sequence Chris@16: \param Fill A string used to fill the inner spaces Chris@16: \param IsSpace A unary predicate identifying spaces Chris@16: \return A trimmed copy of the input Chris@16: */ Chris@16: template Chris@16: inline SequenceT trim_fill_copy_if(const SequenceT& Input, const RangeT& Fill, PredicateT IsSpace) Chris@16: { Chris@16: return Chris@16: ::boost::find_format_all_copy( Chris@16: ::boost::trim_copy_if(Input, IsSpace), Chris@16: ::boost::token_finder(IsSpace, ::boost::token_compress_on), Chris@16: ::boost::const_formatter(::boost::as_literal(Fill))); Chris@16: } Chris@16: Chris@16: Chris@16: //! Trim Fill Chris@16: /*! Chris@16: Remove all leading and trailing spaces from the input and Chris@16: replace all every block of consecutive spaces with a fill string Chris@16: defined by user. Chris@16: The input sequence is modified in-place. Chris@16: Chris@16: \param Input An input sequence Chris@16: \param Fill A string used to fill the inner spaces Chris@16: \param IsSpace A unary predicate identifying spaces Chris@16: */ Chris@16: template Chris@16: inline void trim_fill_if(SequenceT& Input, const RangeT& Fill, PredicateT IsSpace) Chris@16: { Chris@16: ::boost::trim_if(Input, IsSpace); Chris@16: ::boost::find_format_all( Chris@16: Input, Chris@16: ::boost::token_finder(IsSpace, ::boost::token_compress_on), Chris@16: ::boost::const_formatter(::boost::as_literal(Fill))); Chris@16: } Chris@16: Chris@16: Chris@16: //! Trim Fill Chris@16: /*! Chris@16: Remove all leading and trailing spaces from the input and Chris@16: replace all every block of consecutive spaces with a fill string Chris@16: defined by user. Chris@16: The result is a trimmed copy of the input Chris@16: Chris@16: \param Input An input sequence Chris@16: \param Fill A string used to fill the inner spaces Chris@16: \param Loc A locale used for 'space' classification Chris@16: \return A trimmed copy of the input Chris@16: */ Chris@16: template Chris@16: inline SequenceT trim_fill_copy(const SequenceT& Input, const RangeT& Fill, const std::locale& Loc =std::locale()) Chris@16: { Chris@16: return trim_fill_copy_if(Input, Fill, ::boost::is_space(Loc)); Chris@16: } Chris@16: Chris@16: Chris@16: //! Trim Fill Chris@16: /*! Chris@16: Remove all leading and trailing spaces from the input and Chris@16: replace all every block of consecutive spaces with a fill string Chris@16: defined by user. Chris@16: The input sequence is modified in-place. Chris@16: Chris@16: \param Input An input sequence Chris@16: \param Fill A string used to fill the inner spaces Chris@16: \param Loc A locale used for 'space' classification Chris@16: \return A trimmed copy of the input Chris@16: */ Chris@16: template Chris@16: inline void trim_fill(SequenceT& Input, const RangeT& Fill, const std::locale& Loc =std::locale()) Chris@16: { Chris@16: trim_fill_if(Input, Fill, ::boost::is_space(Loc)); Chris@16: } Chris@16: Chris@16: Chris@16: } // namespace algorithm Chris@16: Chris@16: // pull names to the boost namespace Chris@16: using algorithm::trim_all; Chris@16: using algorithm::trim_all_if; Chris@16: using algorithm::trim_all_copy; Chris@16: using algorithm::trim_all_copy_if; Chris@16: using algorithm::trim_fill; Chris@16: using algorithm::trim_fill_if; Chris@16: using algorithm::trim_fill_copy; Chris@16: using algorithm::trim_fill_copy_if; Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #endif // BOOST_STRING_TRIM_ALL_HPP