Thrill  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
join_generic.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * tlx/string/join_generic.hpp
3  *
4  * Part of tlx - http://panthema.net/tlx
5  *
6  * Copyright (C) 2007-2017 Timo Bingmann <[email protected]>
7  *
8  * All rights reserved. Published under the Boost Software License, Version 1.0
9  ******************************************************************************/
10 
11 #ifndef TLX_STRING_JOIN_GENERIC_HEADER
12 #define TLX_STRING_JOIN_GENERIC_HEADER
13 
14 #include <sstream>
15 #include <string>
16 
17 namespace tlx {
18 
19 //! \addtogroup tlx_string
20 //! \{
21 
22 /*!
23  * Join a sequence of strings by some glue string between each pair from the
24  * sequence. The sequence in given as a range between two iterators.
25  *
26  * \param glue string to glue
27  * \param first the beginning iterator of the range to join
28  * \param last the ending iterator of the range to join
29  * \return string constructed from the range with the glue between two strings.
30  */
31 template <typename Glue, typename Iterator>
32 static inline
33 std::string join(Glue glue, Iterator first, Iterator last) {
34  std::ostringstream out;
35  if (first == last) return out.str();
36 
37  out << *first;
38  ++first;
39 
40  while (first != last)
41  {
42  out << glue;
43  out << *first;
44  ++first;
45  }
46 
47  return out.str();
48 }
49 
50 /*!
51  * Join a Container of strings by some glue character between each pair from the
52  * sequence.
53  *
54  * \param glue character for glue
55  * \param parts the vector of strings to join
56  * \return string constructed from the vector with the glue between two strings
57  */
58 template <typename Container>
59 static inline
60 std::string join(char glue, const Container& parts) {
61  return join(glue, parts.begin(), parts.end());
62 }
63 
64 /*!
65  * Join a Container of strings by some glue string between each pair from the
66  * sequence.
67  *
68  * \param glue string to glue
69  * \param parts the vector of strings to join
70  * \return string constructed from the vector with the glue between two strings
71  */
72 template <typename Container>
73 static inline
74 std::string join(const char* glue, const Container& parts) {
75  return join(glue, parts.begin(), parts.end());
76 }
77 
78 /*!
79  * Join a Container of strings by some glue string between each pair from the
80  * sequence.
81  *
82  * \param glue string to glue
83  * \param parts the vector of strings to join
84  * \return string constructed from the vector with the glue between two strings
85  */
86 template <typename Container>
87 static inline
88 std::string join(const std::string& glue, const Container& parts) {
89  return join(glue, parts.begin(), parts.end());
90 }
91 
92 //! \}
93 
94 } // namespace tlx
95 
96 #endif // !TLX_STRING_JOIN_GENERIC_HEADER
97 
98 /******************************************************************************/
std::basic_string< char, std::char_traits< char >, Allocator< char > > string
string with Manager tracking
Definition: allocator.hpp:220
std::string join(char glue, const std::vector< std::string > &parts)
Join a vector of strings by some glue character between each pair from the sequence.
Definition: join.cpp:16