| 
									
										
										
										
											2006-02-14 09:37:26 +00:00
										 |  |  | //  Boost string_algo library join.hpp header file  ---------------------------//
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-07-27 10:27:37 +00:00
										 |  |  | //  Copyright Pavol Droba 2002-2006.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // 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)
 | 
					
						
							| 
									
										
										
										
											2006-02-14 09:37:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-07-27 10:27:37 +00:00
										 |  |  | //  See http://www.boost.org/ for updates, documentation, and revision history.
 | 
					
						
							| 
									
										
										
										
											2006-02-14 09:37:26 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifndef BOOST_STRING_JOIN_HPP
 | 
					
						
							|  |  |  | #define BOOST_STRING_JOIN_HPP
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <boost/algorithm/string/config.hpp>
 | 
					
						
							|  |  |  | #include <boost/algorithm/string/detail/sequence.hpp>
 | 
					
						
							|  |  |  | #include <boost/range/value_type.hpp>
 | 
					
						
							| 
									
										
										
										
											2007-09-28 07:19:29 +00:00
										 |  |  | #include <boost/range/as_literal.hpp>
 | 
					
						
							| 
									
										
										
										
											2006-02-14 09:37:26 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*! \file
 | 
					
						
							|  |  |  |     Defines join algorithm.  | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Join algorithm is a counterpart to split algorithms. | 
					
						
							|  |  |  |     It joins strings from a 'list' by adding user defined separator. | 
					
						
							|  |  |  |     Additionally there is a version that allows simple filtering | 
					
						
							|  |  |  |     by providing a predicate. | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace boost { | 
					
						
							|  |  |  |     namespace algorithm { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //  join --------------------------------------------------------------//
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         //! Join algorithm
 | 
					
						
							|  |  |  |         /*!
 | 
					
						
							|  |  |  |             This algorithm joins all strings in a 'list' into one long string. | 
					
						
							|  |  |  |             Segments are concatenated by given separator. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             \param Input A container that holds the input strings. It must be a container-of-containers. | 
					
						
							|  |  |  |             \param Separator A string that will separate the joined segments. | 
					
						
							|  |  |  |             \return Concatenated string. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             \note This function provides the strong exception-safety guarantee | 
					
						
							|  |  |  |         */ | 
					
						
							|  |  |  |         template< typename SequenceSequenceT, typename Range1T> | 
					
						
							|  |  |  |         inline typename range_value<SequenceSequenceT>::type  | 
					
						
							|  |  |  |         join( | 
					
						
							|  |  |  |             const SequenceSequenceT& Input, | 
					
						
							| 
									
										
										
										
											2007-07-01 22:23:55 +00:00
										 |  |  |             const Range1T& Separator) | 
					
						
							| 
									
										
										
										
											2006-02-14 09:37:26 +00:00
										 |  |  |         { | 
					
						
							|  |  |  |             // Define working types
 | 
					
						
							|  |  |  |             typedef typename range_value<SequenceSequenceT>::type ResultT; | 
					
						
							|  |  |  |             typedef typename range_const_iterator<SequenceSequenceT>::type InputIteratorT; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // Parse input
 | 
					
						
							| 
									
										
										
										
											2008-06-17 21:04:00 +00:00
										 |  |  |             InputIteratorT itBegin=::boost::begin(Input); | 
					
						
							|  |  |  |             InputIteratorT itEnd=::boost::end(Input); | 
					
						
							| 
									
										
										
										
											2006-02-14 09:37:26 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // Construct container to hold the result
 | 
					
						
							|  |  |  |             ResultT Result; | 
					
						
							|  |  |  |              | 
					
						
							|  |  |  |             // Append first element
 | 
					
						
							|  |  |  |             if(itBegin!=itEnd) | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2008-06-17 21:04:00 +00:00
										 |  |  |                 detail::insert(Result, ::boost::end(Result), *itBegin); | 
					
						
							| 
									
										
										
										
											2006-02-14 09:37:26 +00:00
										 |  |  |                 ++itBegin; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             for(;itBegin!=itEnd; ++itBegin) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 // Add separator
 | 
					
						
							| 
									
										
										
										
											2009-08-05 20:01:10 +00:00
										 |  |  |                 detail::insert(Result, ::boost::end(Result), ::boost::as_literal(Separator)); | 
					
						
							| 
									
										
										
										
											2006-02-14 09:37:26 +00:00
										 |  |  |                 // Add element
 | 
					
						
							| 
									
										
										
										
											2008-06-17 21:04:00 +00:00
										 |  |  |                 detail::insert(Result, ::boost::end(Result), *itBegin); | 
					
						
							| 
									
										
										
										
											2006-02-14 09:37:26 +00:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return Result; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // join_if ----------------------------------------------------------//
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         //! Conditional join algorithm
 | 
					
						
							|  |  |  |         /*!
 | 
					
						
							|  |  |  |             This algorithm joins all strings in a 'list' into one long string. | 
					
						
							|  |  |  |             Segments are concatenated by given separator. Only segments that | 
					
						
							|  |  |  |             satisfy the predicate will be added to the result. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             \param Input A container that holds the input strings. It must be a container-of-containers. | 
					
						
							|  |  |  |             \param Separator A string that will separate the joined segments. | 
					
						
							|  |  |  |             \param Pred A segment selection predicate | 
					
						
							|  |  |  |             \return Concatenated string. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             \note This function provides the strong exception-safety guarantee | 
					
						
							|  |  |  |         */ | 
					
						
							|  |  |  |         template< typename SequenceSequenceT, typename Range1T, typename PredicateT> | 
					
						
							|  |  |  |         inline typename range_value<SequenceSequenceT>::type  | 
					
						
							|  |  |  |         join_if( | 
					
						
							|  |  |  |             const SequenceSequenceT& Input, | 
					
						
							| 
									
										
										
										
											2007-07-01 22:23:55 +00:00
										 |  |  |             const Range1T& Separator, | 
					
						
							| 
									
										
										
										
											2006-02-14 09:37:26 +00:00
										 |  |  |             PredicateT Pred) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             // Define working types
 | 
					
						
							|  |  |  |             typedef typename range_value<SequenceSequenceT>::type ResultT; | 
					
						
							|  |  |  |             typedef typename range_const_iterator<SequenceSequenceT>::type InputIteratorT; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // Parse input
 | 
					
						
							| 
									
										
										
										
											2008-06-17 21:04:00 +00:00
										 |  |  |             InputIteratorT itBegin=::boost::begin(Input); | 
					
						
							|  |  |  |             InputIteratorT itEnd=::boost::end(Input); | 
					
						
							| 
									
										
										
										
											2006-02-14 09:37:26 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // Construct container to hold the result
 | 
					
						
							|  |  |  |             ResultT Result; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // Roll to the first element that will be added
 | 
					
						
							|  |  |  |             while(itBegin!=itEnd && !Pred(*itBegin)) ++itBegin; | 
					
						
							|  |  |  |             // Add this element
 | 
					
						
							|  |  |  |             if(itBegin!=itEnd) | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2008-06-17 21:04:00 +00:00
										 |  |  |                 detail::insert(Result, ::boost::end(Result), *itBegin); | 
					
						
							| 
									
										
										
										
											2006-02-14 09:37:26 +00:00
										 |  |  |                 ++itBegin; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             for(;itBegin!=itEnd; ++itBegin) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 if(Pred(*itBegin)) | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     // Add separator
 | 
					
						
							| 
									
										
										
										
											2009-08-05 20:01:10 +00:00
										 |  |  |                     detail::insert(Result, ::boost::end(Result), ::boost::as_literal(Separator)); | 
					
						
							| 
									
										
										
										
											2006-02-14 09:37:26 +00:00
										 |  |  |                     // Add element
 | 
					
						
							| 
									
										
										
										
											2008-06-17 21:04:00 +00:00
										 |  |  |                     detail::insert(Result, ::boost::end(Result), *itBegin); | 
					
						
							| 
									
										
										
										
											2006-02-14 09:37:26 +00:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return Result; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } // namespace algorithm
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // pull names to the boost namespace
 | 
					
						
							|  |  |  |     using algorithm::join; | 
					
						
							|  |  |  |     using algorithm::join_if; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } // namespace boost
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif  // BOOST_STRING_JOIN_HPP
 | 
					
						
							|  |  |  | 
 |