Thrill  0.1
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 //! \name Split and Join
22 //! \{
23 
24 /*!
25  * Join a sequence of strings by some glue string between each pair from the
26  * sequence. The sequence in given as a range between two iterators.
27  *
28  * \param glue string to glue
29  * \param first the beginning iterator of the range to join
30  * \param last the ending iterator of the range to join
31  * \return string constructed from the range with the glue between two strings.
32  */
33 template <typename Glue, typename Iterator>
34 static inline
35 std::string join(Glue glue, Iterator first, Iterator last) {
36  std::ostringstream out;
37  if (first == last) return out.str();
38 
39  out << *first;
40  ++first;
41 
42  while (first != last)
43  {
44  out << glue;
45  out << *first;
46  ++first;
47  }
48 
49  return out.str();
50 }
51 
52 /*!
53  * Join a Container of strings by some glue character between each pair from the
54  * sequence.
55  *
56  * \param glue character for glue
57  * \param parts the vector of strings to join
58  * \return string constructed from the vector with the glue between two strings
59  */
60 template <typename Container>
61 static inline
62 std::string join(char glue, const Container& parts) {
63  return join(glue, std::begin(parts), std::end(parts));
64 }
65 
66 /*!
67  * Join a Container of strings by some glue string between each pair from the
68  * sequence.
69  *
70  * \param glue string to glue
71  * \param parts the vector of strings to join
72  * \return string constructed from the vector with the glue between two strings
73  */
74 template <typename Container>
75 static inline
76 std::string join(const char* glue, const Container& parts) {
77  return join(glue, std::begin(parts), std::end(parts));
78 }
79 
80 /*!
81  * Join a Container of strings by some glue string between each pair from the
82  * sequence.
83  *
84  * \param glue string to glue
85  * \param parts the vector of strings to join
86  * \return string constructed from the vector with the glue between two strings
87  */
88 template <typename Container>
89 static inline
90 std::string join(const std::string& glue, const Container& parts) {
91  return join(glue, std::begin(parts), std::end(parts));
92 }
93 
94 //! \}
95 //! \}
96 
97 } // namespace tlx
98 
99 #endif // !TLX_STRING_JOIN_GENERIC_HEADER
100 
101 /******************************************************************************/
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