join.hh

Go to the documentation of this file.
00001 /* vim: set sw=4 sts=4 et foldmethod=syntax : */
00002 
00003 /*
00004  * Copyright (c) 2005, 2006, 2007, 2009 Ciaran McCreesh
00005  *
00006  * This file is part of the Paludis package manager. Paludis is free software;
00007  * you can redistribute it and/or modify it under the terms of the GNU General
00008  * Public License version 2, as published by the Free Software Foundation.
00009  *
00010  * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
00011  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00012  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00013  * details.
00014  *
00015  * You should have received a copy of the GNU General Public License along with
00016  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00017  * Place, Suite 330, Boston, MA  02111-1307  USA
00018  */
00019 
00020 #ifndef PALUDIS_GUARD_PALUDIS_JOIN_HH
00021 #define PALUDIS_GUARD_PALUDIS_JOIN_HH 1
00022 
00023 #include <paludis/util/stringify.hh>
00024 #include <iterator>
00025 #include <string>
00026 
00027 /** \file
00028  * Declarations for the join function.
00029  *
00030  * \ingroup g_strings
00031  *
00032  * \section Examples
00033  *
00034  * - None at this time.
00035  */
00036 
00037 namespace paludis
00038 {
00039     /**
00040      * Join together the items from i to end using joiner.
00041      *
00042      * \ingroup g_strings
00043      */
00044     template <typename I_, typename T_>
00045     T_ join(I_ i, I_ end, const T_ & joiner)
00046     {
00047         T_ result;
00048         if (i != end)
00049             while (true)
00050             {
00051                 result += stringify(*i);
00052                 if (++i == end)
00053                     break;
00054                 result += joiner;
00055             }
00056         return result;
00057     }
00058 
00059     /**
00060      * Join together the items from i to end using joiner, using
00061      * a function other than stringify.
00062      *
00063      * \ingroup g_strings
00064      */
00065     template <typename I_, typename T_, typename F_>
00066     T_ join(I_ i, I_ end, const T_ & joiner, const F_ & f)
00067     {
00068         T_ result;
00069         if (i != end)
00070             while (true)
00071             {
00072                 result += (f)(*i);
00073                 if (++i == end)
00074                     break;
00075                 result += joiner;
00076             }
00077         return result;
00078     }
00079 
00080     /**
00081      * Convenience alternative join allowing a char * to be used for a
00082      * string.
00083      *
00084      * \ingroup g_strings
00085      */
00086     template <typename I_>
00087     std::string join(I_ begin, const I_ end, const char * const t)
00088     {
00089         return join(begin, end, std::string(t));
00090     }
00091 
00092     /**
00093      * Convenience alternative join allowing a char * to be used for a
00094      * string, using a function other than stringify.
00095      *
00096      * \ingroup g_strings
00097      */
00098     template <typename I_, typename F_>
00099     std::string join(I_ begin, const I_ end, const char * const t, const F_ & f)
00100     {
00101         return join(begin, end, std::string(t), f);
00102     }
00103 }
00104 
00105 #endif

Generated on Mon Sep 21 10:36:08 2009 for paludis by  doxygen 1.5.4