1 // Boost string_algo library join.hpp header file ---------------------------//
3 // Copyright Pavol Droba 2002-2006.
5 // Distributed under the Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 // See http://www.boost.org/ for updates, documentation, and revision history.
11 #ifndef BOOST_STRING_JOIN_HPP
12 #define BOOST_STRING_JOIN_HPP
14 #include <boost/algorithm/string/config.hpp>
15 #include <boost/algorithm/string/detail/sequence.hpp>
16 #include <boost/range/value_type.hpp>
17 #include <boost/range/as_literal.hpp>
20 Defines join algorithm.
22 Join algorithm is a counterpart to split algorithms.
23 It joins strings from a 'list' by adding user defined separator.
24 Additionally there is a version that allows simple filtering
25 by providing a predicate.
31 // join --------------------------------------------------------------//
35 This algorithm joins all strings in a 'list' into one long string.
36 Segments are concatenated by given separator.
38 \param Input A container that holds the input strings. It must be a container-of-containers.
39 \param Separator A string that will separate the joined segments.
40 \return Concatenated string.
42 \note This function provides the strong exception-safety guarantee
44 template< typename SequenceSequenceT, typename Range1T>
45 inline typename range_value<SequenceSequenceT>::type
47 const SequenceSequenceT& Input,
48 const Range1T& Separator)
50 // Define working types
51 typedef typename range_value<SequenceSequenceT>::type ResultT;
52 typedef typename range_const_iterator<SequenceSequenceT>::type InputIteratorT;
55 InputIteratorT itBegin=::boost::begin(Input);
56 InputIteratorT itEnd=::boost::end(Input);
58 // Construct container to hold the result
61 // Append first element
64 detail::insert(Result, ::boost::end(Result), *itBegin);
68 for(;itBegin!=itEnd; ++itBegin)
71 detail::insert(Result, ::boost::end(Result), ::boost::as_literal(Separator));
73 detail::insert(Result, ::boost::end(Result), *itBegin);
79 // join_if ----------------------------------------------------------//
81 //! Conditional join algorithm
83 This algorithm joins all strings in a 'list' into one long string.
84 Segments are concatenated by given separator. Only segments that
85 satisfy the predicate will be added to the result.
87 \param Input A container that holds the input strings. It must be a container-of-containers.
88 \param Separator A string that will separate the joined segments.
89 \param Pred A segment selection predicate
90 \return Concatenated string.
92 \note This function provides the strong exception-safety guarantee
94 template< typename SequenceSequenceT, typename Range1T, typename PredicateT>
95 inline typename range_value<SequenceSequenceT>::type
97 const SequenceSequenceT& Input,
98 const Range1T& Separator,
101 // Define working types
102 typedef typename range_value<SequenceSequenceT>::type ResultT;
103 typedef typename range_const_iterator<SequenceSequenceT>::type InputIteratorT;
106 InputIteratorT itBegin=::boost::begin(Input);
107 InputIteratorT itEnd=::boost::end(Input);
109 // Construct container to hold the result
112 // Roll to the first element that will be added
113 while(itBegin!=itEnd && !Pred(*itBegin)) ++itBegin;
117 detail::insert(Result, ::boost::end(Result), *itBegin);
121 for(;itBegin!=itEnd; ++itBegin)
126 detail::insert(Result, ::boost::end(Result), ::boost::as_literal(Separator));
128 detail::insert(Result, ::boost::end(Result), *itBegin);
135 } // namespace algorithm
137 // pull names to the boost namespace
138 using algorithm::join;
139 using algorithm::join_if;
144 #endif // BOOST_STRING_JOIN_HPP