Chris@16
|
1 // Boost string_algo library join.hpp header file ---------------------------//
|
Chris@16
|
2
|
Chris@16
|
3 // Copyright Pavol Droba 2002-2006.
|
Chris@16
|
4 //
|
Chris@16
|
5 // Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
6 // (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
7 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
8
|
Chris@16
|
9 // See http://www.boost.org/ for updates, documentation, and revision history.
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_STRING_JOIN_HPP
|
Chris@16
|
12 #define BOOST_STRING_JOIN_HPP
|
Chris@16
|
13
|
Chris@16
|
14 #include <boost/algorithm/string/config.hpp>
|
Chris@16
|
15 #include <boost/algorithm/string/detail/sequence.hpp>
|
Chris@16
|
16 #include <boost/range/value_type.hpp>
|
Chris@16
|
17 #include <boost/range/as_literal.hpp>
|
Chris@16
|
18
|
Chris@16
|
19 /*! \file
|
Chris@16
|
20 Defines join algorithm.
|
Chris@16
|
21
|
Chris@16
|
22 Join algorithm is a counterpart to split algorithms.
|
Chris@16
|
23 It joins strings from a 'list' by adding user defined separator.
|
Chris@16
|
24 Additionally there is a version that allows simple filtering
|
Chris@16
|
25 by providing a predicate.
|
Chris@16
|
26 */
|
Chris@16
|
27
|
Chris@16
|
28 namespace boost {
|
Chris@16
|
29 namespace algorithm {
|
Chris@16
|
30
|
Chris@16
|
31 // join --------------------------------------------------------------//
|
Chris@16
|
32
|
Chris@16
|
33 //! Join algorithm
|
Chris@16
|
34 /*!
|
Chris@16
|
35 This algorithm joins all strings in a 'list' into one long string.
|
Chris@16
|
36 Segments are concatenated by given separator.
|
Chris@16
|
37
|
Chris@16
|
38 \param Input A container that holds the input strings. It must be a container-of-containers.
|
Chris@16
|
39 \param Separator A string that will separate the joined segments.
|
Chris@16
|
40 \return Concatenated string.
|
Chris@16
|
41
|
Chris@16
|
42 \note This function provides the strong exception-safety guarantee
|
Chris@16
|
43 */
|
Chris@16
|
44 template< typename SequenceSequenceT, typename Range1T>
|
Chris@16
|
45 inline typename range_value<SequenceSequenceT>::type
|
Chris@16
|
46 join(
|
Chris@16
|
47 const SequenceSequenceT& Input,
|
Chris@16
|
48 const Range1T& Separator)
|
Chris@16
|
49 {
|
Chris@16
|
50 // Define working types
|
Chris@16
|
51 typedef typename range_value<SequenceSequenceT>::type ResultT;
|
Chris@16
|
52 typedef typename range_const_iterator<SequenceSequenceT>::type InputIteratorT;
|
Chris@16
|
53
|
Chris@16
|
54 // Parse input
|
Chris@16
|
55 InputIteratorT itBegin=::boost::begin(Input);
|
Chris@16
|
56 InputIteratorT itEnd=::boost::end(Input);
|
Chris@16
|
57
|
Chris@16
|
58 // Construct container to hold the result
|
Chris@16
|
59 ResultT Result;
|
Chris@16
|
60
|
Chris@16
|
61 // Append first element
|
Chris@16
|
62 if(itBegin!=itEnd)
|
Chris@16
|
63 {
|
Chris@16
|
64 detail::insert(Result, ::boost::end(Result), *itBegin);
|
Chris@16
|
65 ++itBegin;
|
Chris@16
|
66 }
|
Chris@16
|
67
|
Chris@16
|
68 for(;itBegin!=itEnd; ++itBegin)
|
Chris@16
|
69 {
|
Chris@16
|
70 // Add separator
|
Chris@16
|
71 detail::insert(Result, ::boost::end(Result), ::boost::as_literal(Separator));
|
Chris@16
|
72 // Add element
|
Chris@16
|
73 detail::insert(Result, ::boost::end(Result), *itBegin);
|
Chris@16
|
74 }
|
Chris@16
|
75
|
Chris@16
|
76 return Result;
|
Chris@16
|
77 }
|
Chris@16
|
78
|
Chris@16
|
79 // join_if ----------------------------------------------------------//
|
Chris@16
|
80
|
Chris@16
|
81 //! Conditional join algorithm
|
Chris@16
|
82 /*!
|
Chris@16
|
83 This algorithm joins all strings in a 'list' into one long string.
|
Chris@16
|
84 Segments are concatenated by given separator. Only segments that
|
Chris@16
|
85 satisfy the predicate will be added to the result.
|
Chris@16
|
86
|
Chris@16
|
87 \param Input A container that holds the input strings. It must be a container-of-containers.
|
Chris@16
|
88 \param Separator A string that will separate the joined segments.
|
Chris@16
|
89 \param Pred A segment selection predicate
|
Chris@16
|
90 \return Concatenated string.
|
Chris@16
|
91
|
Chris@16
|
92 \note This function provides the strong exception-safety guarantee
|
Chris@16
|
93 */
|
Chris@16
|
94 template< typename SequenceSequenceT, typename Range1T, typename PredicateT>
|
Chris@16
|
95 inline typename range_value<SequenceSequenceT>::type
|
Chris@16
|
96 join_if(
|
Chris@16
|
97 const SequenceSequenceT& Input,
|
Chris@16
|
98 const Range1T& Separator,
|
Chris@16
|
99 PredicateT Pred)
|
Chris@16
|
100 {
|
Chris@16
|
101 // Define working types
|
Chris@16
|
102 typedef typename range_value<SequenceSequenceT>::type ResultT;
|
Chris@16
|
103 typedef typename range_const_iterator<SequenceSequenceT>::type InputIteratorT;
|
Chris@16
|
104
|
Chris@16
|
105 // Parse input
|
Chris@16
|
106 InputIteratorT itBegin=::boost::begin(Input);
|
Chris@16
|
107 InputIteratorT itEnd=::boost::end(Input);
|
Chris@16
|
108
|
Chris@16
|
109 // Construct container to hold the result
|
Chris@16
|
110 ResultT Result;
|
Chris@16
|
111
|
Chris@16
|
112 // Roll to the first element that will be added
|
Chris@16
|
113 while(itBegin!=itEnd && !Pred(*itBegin)) ++itBegin;
|
Chris@16
|
114 // Add this element
|
Chris@16
|
115 if(itBegin!=itEnd)
|
Chris@16
|
116 {
|
Chris@16
|
117 detail::insert(Result, ::boost::end(Result), *itBegin);
|
Chris@16
|
118 ++itBegin;
|
Chris@16
|
119 }
|
Chris@16
|
120
|
Chris@16
|
121 for(;itBegin!=itEnd; ++itBegin)
|
Chris@16
|
122 {
|
Chris@16
|
123 if(Pred(*itBegin))
|
Chris@16
|
124 {
|
Chris@16
|
125 // Add separator
|
Chris@16
|
126 detail::insert(Result, ::boost::end(Result), ::boost::as_literal(Separator));
|
Chris@16
|
127 // Add element
|
Chris@16
|
128 detail::insert(Result, ::boost::end(Result), *itBegin);
|
Chris@16
|
129 }
|
Chris@16
|
130 }
|
Chris@16
|
131
|
Chris@16
|
132 return Result;
|
Chris@16
|
133 }
|
Chris@16
|
134
|
Chris@16
|
135 } // namespace algorithm
|
Chris@16
|
136
|
Chris@16
|
137 // pull names to the boost namespace
|
Chris@16
|
138 using algorithm::join;
|
Chris@16
|
139 using algorithm::join_if;
|
Chris@16
|
140
|
Chris@16
|
141 } // namespace boost
|
Chris@16
|
142
|
Chris@16
|
143
|
Chris@16
|
144 #endif // BOOST_STRING_JOIN_HPP
|
Chris@16
|
145
|