Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/boost/algorithm/string/join.hpp @ 46

Last change on this file since 46 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

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