From 9b626489702f78fa2832f3ee5a820cdca4764b19 Mon Sep 17 00:00:00 2001 From: Pavol Droba Date: Tue, 14 Feb 2006 09:37:26 +0000 Subject: [PATCH] Join algorithm implemented [SVN r32916] --- include/boost/algorithm/string/join.hpp | 144 +++++++++++++++++++++++ include/boost/algorithm/string/split.hpp | 8 +- 2 files changed, 148 insertions(+), 4 deletions(-) create mode 100644 include/boost/algorithm/string/join.hpp diff --git a/include/boost/algorithm/string/join.hpp b/include/boost/algorithm/string/join.hpp new file mode 100644 index 0000000..b4e0791 --- /dev/null +++ b/include/boost/algorithm/string/join.hpp @@ -0,0 +1,144 @@ +// Boost string_algo library join.hpp header file ---------------------------// + +// Copyright Pavol Droba 2002-2006. Use, modification and +// distribution is subject to 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) + +// See http://www.boost.org for updates, documentation, and revision history. + +#ifndef BOOST_STRING_JOIN_HPP +#define BOOST_STRING_JOIN_HPP + +#include +#include +#include + + +/*! \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::type + join( + const SequenceSequenceT& Input, + Range1T& Separator) + { + // Define working types + typedef typename range_value::type ResultT; + typedef typename range_const_iterator::type InputIteratorT; + + // Parse input + InputIteratorT itBegin=begin(Input); + InputIteratorT itEnd=end(Input); + + // Construct container to hold the result + ResultT Result; + + // Append first element + if(itBegin!=itEnd) + { + detail::insert(Result, end(Result), *itBegin); + ++itBegin; + } + + for(;itBegin!=itEnd; ++itBegin) + { + // Add separator + detail::insert(Result, end(Result), Separator); + // Add element + detail::insert(Result, end(Result), *itBegin); + } + + 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::type + join_if( + const SequenceSequenceT& Input, + Range1T& Separator, + PredicateT Pred) + { + // Define working types + typedef typename range_value::type ResultT; + typedef typename range_const_iterator::type InputIteratorT; + + // Parse input + InputIteratorT itBegin=begin(Input); + InputIteratorT itEnd=end(Input); + + // 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) + { + detail::insert(Result, end(Result), *itBegin); + ++itBegin; + } + + for(;itBegin!=itEnd; ++itBegin) + { + if(Pred(*itBegin)) + { + // Add separator + detail::insert(Result, end(Result), Separator); + // Add element + detail::insert(Result, end(Result), *itBegin); + } + } + + return Result; + } + + } // namespace algorithm + + // pull names to the boost namespace + using algorithm::join; + using algorithm::join_if; + +} // namespace boost + + +#endif // BOOST_STRING_JOIN_HPP + diff --git a/include/boost/algorithm/string/split.hpp b/include/boost/algorithm/string/split.hpp index b5c6ff9..71bf43f 100644 --- a/include/boost/algorithm/string/split.hpp +++ b/include/boost/algorithm/string/split.hpp @@ -1,6 +1,6 @@ -// Boost string_algo library find.hpp header file ---------------------------// +// Boost string_algo library split.hpp header file ---------------------------// -// Copyright Pavol Droba 2002-2003. Use, modification and +// Copyright Pavol Droba 2002-2006. Use, modification and // distribution is subject to 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) @@ -55,7 +55,7 @@ namespace boost { \note Prior content of the result will be overwritten. - \note This function provides the strong exception-safety guarantee + \note This function provides the strong exception-safety guarantee */ template< typename SequenceSequenceT, typename Range1T, typename Range2T > inline SequenceSequenceT& find_all( @@ -90,7 +90,7 @@ namespace boost { \note Prior content of the result will be overwritten. - \note This function provides the strong exception-safety guarantee + \note This function provides the strong exception-safety guarantee */ template< typename SequenceSequenceT, typename Range1T, typename Range2T > inline SequenceSequenceT& ifind_all(