Chris@16: // Boost string_algo library join.hpp header file ---------------------------// Chris@16: Chris@16: // Copyright Pavol Droba 2002-2006. 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_JOIN_HPP Chris@16: #define BOOST_STRING_JOIN_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: /*! \file Chris@16: Defines join algorithm. Chris@16: Chris@16: Join algorithm is a counterpart to split algorithms. Chris@16: It joins strings from a 'list' by adding user defined separator. Chris@16: Additionally there is a version that allows simple filtering Chris@16: by providing a predicate. Chris@16: */ Chris@16: Chris@16: namespace boost { Chris@16: namespace algorithm { Chris@16: Chris@16: // join --------------------------------------------------------------// Chris@16: Chris@16: //! Join algorithm Chris@16: /*! Chris@16: This algorithm joins all strings in a 'list' into one long string. Chris@16: Segments are concatenated by given separator. Chris@16: Chris@16: \param Input A container that holds the input strings. It must be a container-of-containers. Chris@16: \param Separator A string that will separate the joined segments. Chris@16: \return Concatenated string. Chris@16: Chris@16: \note This function provides the strong exception-safety guarantee Chris@16: */ Chris@16: template< typename SequenceSequenceT, typename Range1T> Chris@16: inline typename range_value::type Chris@16: join( Chris@16: const SequenceSequenceT& Input, Chris@16: const Range1T& Separator) Chris@16: { Chris@16: // Define working types Chris@16: typedef typename range_value::type ResultT; Chris@16: typedef typename range_const_iterator::type InputIteratorT; Chris@16: Chris@16: // Parse input Chris@16: InputIteratorT itBegin=::boost::begin(Input); Chris@16: InputIteratorT itEnd=::boost::end(Input); Chris@16: Chris@16: // Construct container to hold the result Chris@16: ResultT Result; Chris@16: Chris@16: // Append first element Chris@16: if(itBegin!=itEnd) Chris@16: { Chris@16: detail::insert(Result, ::boost::end(Result), *itBegin); Chris@16: ++itBegin; Chris@16: } Chris@16: Chris@16: for(;itBegin!=itEnd; ++itBegin) Chris@16: { Chris@16: // Add separator Chris@16: detail::insert(Result, ::boost::end(Result), ::boost::as_literal(Separator)); Chris@16: // Add element Chris@16: detail::insert(Result, ::boost::end(Result), *itBegin); Chris@16: } Chris@16: Chris@16: return Result; Chris@16: } Chris@16: Chris@16: // join_if ----------------------------------------------------------// Chris@16: Chris@16: //! Conditional join algorithm Chris@16: /*! Chris@16: This algorithm joins all strings in a 'list' into one long string. Chris@16: Segments are concatenated by given separator. Only segments that Chris@16: satisfy the predicate will be added to the result. Chris@16: Chris@16: \param Input A container that holds the input strings. It must be a container-of-containers. Chris@16: \param Separator A string that will separate the joined segments. Chris@16: \param Pred A segment selection predicate Chris@16: \return Concatenated string. Chris@16: Chris@16: \note This function provides the strong exception-safety guarantee Chris@16: */ Chris@16: template< typename SequenceSequenceT, typename Range1T, typename PredicateT> Chris@16: inline typename range_value::type Chris@16: join_if( Chris@16: const SequenceSequenceT& Input, Chris@16: const Range1T& Separator, Chris@16: PredicateT Pred) Chris@16: { Chris@16: // Define working types Chris@16: typedef typename range_value::type ResultT; Chris@16: typedef typename range_const_iterator::type InputIteratorT; Chris@16: Chris@16: // Parse input Chris@16: InputIteratorT itBegin=::boost::begin(Input); Chris@16: InputIteratorT itEnd=::boost::end(Input); Chris@16: Chris@16: // Construct container to hold the result Chris@16: ResultT Result; Chris@16: Chris@16: // Roll to the first element that will be added Chris@16: while(itBegin!=itEnd && !Pred(*itBegin)) ++itBegin; Chris@16: // Add this element Chris@16: if(itBegin!=itEnd) Chris@16: { Chris@16: detail::insert(Result, ::boost::end(Result), *itBegin); Chris@16: ++itBegin; Chris@16: } Chris@16: Chris@16: for(;itBegin!=itEnd; ++itBegin) Chris@16: { Chris@16: if(Pred(*itBegin)) Chris@16: { Chris@16: // Add separator Chris@16: detail::insert(Result, ::boost::end(Result), ::boost::as_literal(Separator)); Chris@16: // Add element Chris@16: detail::insert(Result, ::boost::end(Result), *itBegin); Chris@16: } Chris@16: } Chris@16: Chris@16: return Result; Chris@16: } Chris@16: Chris@16: } // namespace algorithm Chris@16: Chris@16: // pull names to the boost namespace Chris@16: using algorithm::join; Chris@16: using algorithm::join_if; Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: Chris@16: #endif // BOOST_STRING_JOIN_HPP Chris@16: