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_HPP Chris@16: #define BOOST_STRING_TRIM_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: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: /*! \file Chris@16: Defines trim algorithms. Chris@16: Trim algorithms are used to remove trailing and leading spaces from a Chris@16: sequence (string). Space is recognized using given locales. 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: // left trim -----------------------------------------------// Chris@16: Chris@16: Chris@16: //! Left trim - parametric Chris@16: /*! Chris@16: Remove all leading spaces from the input. Chris@16: The supplied predicate is used to determine which characters are considered spaces. Chris@16: The result is a trimmed copy of the input. It is returned as a sequence Chris@16: or copied to the output iterator Chris@16: Chris@16: \param Output An output iterator to which the result will be copied Chris@16: \param Input An input range Chris@16: \param IsSpace A unary predicate identifying spaces Chris@16: \return Chris@16: An output iterator pointing just after the last inserted character or Chris@16: a copy of the input Chris@16: Chris@16: \note The second variant of this function provides the strong exception-safety guarantee Chris@16: */ Chris@16: template Chris@16: inline OutputIteratorT trim_left_copy_if( Chris@16: OutputIteratorT Output, Chris@16: const RangeT& Input, Chris@16: PredicateT IsSpace) Chris@16: { Chris@16: iterator_range::type> lit_range(::boost::as_literal(Input)); Chris@16: Chris@16: std::copy( Chris@16: ::boost::algorithm::detail::trim_begin( Chris@16: ::boost::begin(lit_range), Chris@16: ::boost::end(lit_range), Chris@16: IsSpace ), Chris@16: ::boost::end(lit_range), Chris@16: Output); Chris@16: Chris@16: return Output; Chris@16: } Chris@16: Chris@16: //! Left trim - parametric Chris@16: /*! Chris@16: \overload Chris@16: */ Chris@16: template Chris@16: inline SequenceT trim_left_copy_if(const SequenceT& Input, PredicateT IsSpace) Chris@16: { Chris@16: return SequenceT( Chris@16: ::boost::algorithm::detail::trim_begin( Chris@16: ::boost::begin(Input), Chris@16: ::boost::end(Input), Chris@16: IsSpace ), Chris@16: ::boost::end(Input)); Chris@16: } Chris@16: Chris@16: //! Left trim - parametric Chris@16: /*! Chris@16: Remove all leading spaces from the input. 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: \note This function provides the strong exception-safety guarantee Chris@16: */ Chris@16: template Chris@16: inline SequenceT trim_left_copy(const SequenceT& Input, const std::locale& Loc=std::locale()) Chris@16: { Chris@16: return Chris@16: ::boost::algorithm::trim_left_copy_if( Chris@16: Input, Chris@16: is_space(Loc)); Chris@16: } Chris@16: Chris@16: //! Left trim Chris@16: /*! Chris@16: Remove all leading spaces from the input. The supplied predicate is Chris@16: used to determine which characters are considered spaces. 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_left_if(SequenceT& Input, PredicateT IsSpace) Chris@16: { Chris@16: Input.erase( Chris@16: ::boost::begin(Input), Chris@16: ::boost::algorithm::detail::trim_begin( Chris@16: ::boost::begin(Input), Chris@16: ::boost::end(Input), Chris@16: IsSpace)); Chris@16: } Chris@16: Chris@16: //! Left trim Chris@16: /*! Chris@16: Remove all leading spaces from the input. 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: */ Chris@16: template Chris@16: inline void trim_left(SequenceT& Input, const std::locale& Loc=std::locale()) Chris@16: { Chris@16: ::boost::algorithm::trim_left_if( Chris@16: Input, Chris@16: is_space(Loc)); Chris@16: } Chris@16: Chris@16: // right trim -----------------------------------------------// Chris@16: Chris@16: //! Right trim - parametric Chris@16: /*! Chris@16: Remove all trailing spaces from the input. Chris@16: The supplied predicate is used to determine which characters are considered spaces. Chris@16: The result is a trimmed copy of the input. It is returned as a sequence Chris@16: or copied to the output iterator Chris@16: Chris@16: \param Output An output iterator to which the result will be copied Chris@16: \param Input An input range Chris@16: \param IsSpace A unary predicate identifying spaces Chris@16: \return Chris@16: An output iterator pointing just after the last inserted character or Chris@16: a copy of the input Chris@16: Chris@16: \note The second variant of this function provides the strong exception-safety guarantee Chris@16: */ Chris@16: template Chris@16: inline OutputIteratorT trim_right_copy_if( Chris@16: OutputIteratorT Output, Chris@16: const RangeT& Input, Chris@16: PredicateT IsSpace ) Chris@16: { Chris@16: iterator_range::type> lit_range(::boost::as_literal(Input)); Chris@16: Chris@16: std::copy( Chris@16: ::boost::begin(lit_range), Chris@16: ::boost::algorithm::detail::trim_end( Chris@16: ::boost::begin(lit_range), Chris@16: ::boost::end(lit_range), Chris@16: IsSpace ), Chris@16: Output ); Chris@16: Chris@16: return Output; Chris@16: } Chris@16: Chris@16: //! Right trim - parametric Chris@16: /*! Chris@16: \overload Chris@16: */ Chris@16: template Chris@16: inline SequenceT trim_right_copy_if(const SequenceT& Input, PredicateT IsSpace) Chris@16: { Chris@16: return SequenceT( Chris@16: ::boost::begin(Input), Chris@16: ::boost::algorithm::detail::trim_end( Chris@16: ::boost::begin(Input), Chris@16: ::boost::end(Input), Chris@16: IsSpace) Chris@16: ); Chris@16: } Chris@16: Chris@16: //! Right trim Chris@16: /*! Chris@16: Remove all trailing spaces from the input. 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: \note This function provides the strong exception-safety guarantee Chris@16: */ Chris@16: template Chris@16: inline SequenceT trim_right_copy(const SequenceT& Input, const std::locale& Loc=std::locale()) Chris@16: { Chris@16: return Chris@16: ::boost::algorithm::trim_right_copy_if( Chris@16: Input, Chris@16: is_space(Loc)); Chris@16: } Chris@16: Chris@16: Chris@16: //! Right trim - parametric Chris@16: /*! Chris@16: Remove all trailing spaces from the input. Chris@16: The supplied predicate is used to determine which characters are considered spaces. 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_right_if(SequenceT& Input, PredicateT IsSpace) Chris@16: { Chris@16: Input.erase( Chris@16: ::boost::algorithm::detail::trim_end( Chris@16: ::boost::begin(Input), Chris@16: ::boost::end(Input), Chris@16: IsSpace ), Chris@16: ::boost::end(Input) Chris@16: ); Chris@16: } Chris@16: Chris@16: Chris@16: //! Right trim Chris@16: /*! Chris@16: Remove all trailing spaces from the input. 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: */ Chris@16: template Chris@16: inline void trim_right(SequenceT& Input, const std::locale& Loc=std::locale()) Chris@16: { Chris@16: ::boost::algorithm::trim_right_if( Chris@16: Input, Chris@16: is_space(Loc) ); Chris@16: } Chris@16: Chris@16: // both side trim -----------------------------------------------// Chris@16: Chris@16: //! Trim - parametric Chris@16: /*! Chris@16: Remove all trailing and leading spaces from the input. Chris@16: The supplied predicate is used to determine which characters are considered spaces. Chris@16: The result is a trimmed copy of the input. It is returned as a sequence Chris@16: or copied to the output iterator Chris@16: Chris@16: \param Output An output iterator to which the result will be copied Chris@16: \param Input An input range Chris@16: \param IsSpace A unary predicate identifying spaces Chris@16: \return Chris@16: An output iterator pointing just after the last inserted character or Chris@16: a copy of the input Chris@16: Chris@16: \note The second variant of this function provides the strong exception-safety guarantee Chris@16: */ Chris@16: template Chris@16: inline OutputIteratorT trim_copy_if( Chris@16: OutputIteratorT Output, Chris@16: const RangeT& Input, Chris@16: PredicateT IsSpace) Chris@16: { Chris@16: iterator_range::type> lit_range(::boost::as_literal(Input)); Chris@16: Chris@16: BOOST_STRING_TYPENAME Chris@16: range_const_iterator::type TrimEnd= Chris@16: ::boost::algorithm::detail::trim_end( Chris@16: ::boost::begin(lit_range), Chris@16: ::boost::end(lit_range), Chris@16: IsSpace); Chris@16: Chris@16: std::copy( Chris@16: detail::trim_begin( Chris@16: ::boost::begin(lit_range), TrimEnd, IsSpace), Chris@16: TrimEnd, Chris@16: Output Chris@16: ); Chris@16: Chris@16: return Output; Chris@16: } Chris@16: Chris@16: //! Trim - parametric Chris@16: /*! Chris@16: \overload Chris@16: */ Chris@16: template Chris@16: inline SequenceT trim_copy_if(const SequenceT& Input, PredicateT IsSpace) Chris@16: { Chris@16: BOOST_STRING_TYPENAME Chris@16: range_const_iterator::type TrimEnd= Chris@16: ::boost::algorithm::detail::trim_end( Chris@16: ::boost::begin(Input), Chris@16: ::boost::end(Input), Chris@16: IsSpace); Chris@16: Chris@16: return SequenceT( Chris@16: detail::trim_begin( Chris@16: ::boost::begin(Input), Chris@16: TrimEnd, Chris@16: IsSpace), Chris@16: TrimEnd Chris@16: ); Chris@16: } Chris@16: Chris@16: //! Trim Chris@16: /*! Chris@16: Remove all leading and trailing spaces from the input. 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: \note This function provides the strong exception-safety guarantee Chris@16: */ Chris@16: template Chris@16: inline SequenceT trim_copy( const SequenceT& Input, const std::locale& Loc=std::locale() ) Chris@16: { Chris@16: return Chris@16: ::boost::algorithm::trim_copy_if( Chris@16: Input, Chris@16: is_space(Loc) ); Chris@16: } Chris@16: Chris@16: //! Trim Chris@16: /*! Chris@16: Remove all leading and trailing spaces from the input. Chris@16: The supplied predicate is used to determine which characters are considered spaces. 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_if(SequenceT& Input, PredicateT IsSpace) Chris@16: { Chris@16: ::boost::algorithm::trim_right_if( Input, IsSpace ); Chris@16: ::boost::algorithm::trim_left_if( Input, IsSpace ); Chris@16: } Chris@16: Chris@16: //! Trim Chris@16: /*! Chris@16: Remove all leading and trailing spaces from the input. 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: */ Chris@16: template Chris@16: inline void trim(SequenceT& Input, const std::locale& Loc=std::locale()) Chris@16: { Chris@16: ::boost::algorithm::trim_if( Chris@16: Input, Chris@16: is_space( Loc ) ); Chris@16: } Chris@16: Chris@16: } // namespace algorithm Chris@16: Chris@16: // pull names to the boost namespace Chris@16: using algorithm::trim_left; Chris@16: using algorithm::trim_left_if; Chris@16: using algorithm::trim_left_copy; Chris@16: using algorithm::trim_left_copy_if; Chris@16: using algorithm::trim_right; Chris@16: using algorithm::trim_right_if; Chris@16: using algorithm::trim_right_copy; Chris@16: using algorithm::trim_right_copy_if; Chris@16: using algorithm::trim; Chris@16: using algorithm::trim_if; Chris@16: using algorithm::trim_copy; Chris@16: using algorithm::trim_copy_if; Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #endif // BOOST_STRING_TRIM_HPP