1
+ /* ******************************************************************\
2
+
3
+ Module: String joiner
4
+
5
+
6
+
7
+ Purpose:
8
+ To join multiple objects that can be piped to a stream into a list separated
9
+ by a string provided by the user.
10
+
11
+ \*******************************************************************/
12
+
1
13
#ifndef CPROVER__util__string_join
2
14
#define CPROVER__util__string_join
3
15
4
-
5
16
#include < iterator>
6
17
#include < sstream>
7
18
8
- template <typename Str, typename It>
19
+
20
+ template <typename T>
21
+ struct string_traitst final
22
+ {
23
+ typedef typename T::value_type char_typet;
24
+ typedef typename T::traits_type traits_typet;
25
+ typedef typename T::allocator_type allocator_typet;
26
+ };
27
+
28
+ template <>
29
+ struct string_traitst <const char *> final
30
+ {
31
+ typedef char char_typet;
32
+ typedef typename std::char_traits<char > traits_typet;
33
+ typedef typename std::allocator<char > allocator_typet;
34
+ };
35
+
36
+
37
+ template <typename Str, typename It, typename Str_Traits = string_traitst<Str>>
9
38
class joined_strings
10
39
{
11
40
private:
12
41
const It begin, end;
13
42
Str sep;
14
43
15
44
public:
16
- typedef typename Str::value_type char_type;
17
- typedef typename Str::traits_type traits_type;
18
- typedef typename Str::allocator_type allocator_type;
45
+ typedef typename Str_Traits::char_typet char_typet;
46
+ typedef typename Str_Traits::traits_typet traits_typet;
47
+ typedef typename Str_Traits::allocator_typet allocator_typet;
48
+ typedef
49
+ typename std::basic_string<char_typet, traits_typet, allocator_typet>
50
+ string_typet;
19
51
20
52
private:
21
- typedef std::basic_ostringstream<char_type, traits_type, allocator_type >
22
- ostringstream_type ;
53
+ typedef std::basic_ostringstream<char_typet, traits_typet, allocator_typet >
54
+ ostringstream_typet ;
23
55
24
56
public:
25
- joined_strings (It begin, const It end, const Str &sep)
26
- : begin(begin), end(end), sep(sep)
57
+ template <typename It_In, typename Str_In>
58
+ joined_strings (It_In &&begin, It_In &&end, Str_In &&sep)
59
+ : begin(std::forward<It_In>(begin)),
60
+ end (std::forward<It_In>(end)),
61
+ sep(std::forward<Str_In>(sep))
27
62
{
28
63
}
29
64
30
- operator Str () const
65
+ operator string_typet () const
31
66
{
32
- ostringstream_type result;
67
+ ostringstream_typet result;
33
68
result << *this ;
34
69
return result.str ();
35
70
}
36
71
37
- template <typename ostream_type >
38
- friend ostream_type & operator <<(
39
- ostream_type &ostr, const joined_strings<Str, It> &joined)
72
+ template <typename ostream_typet >
73
+ friend ostream_typet & operator <<(
74
+ ostream_typet &ostr, const joined_strings<Str, It> &joined)
40
75
{
41
76
It it = joined.begin ;
42
77
if (it!=joined.end )
@@ -50,12 +85,24 @@ class joined_strings
50
85
}
51
86
};
52
87
88
+
53
89
template <typename Str, typename It>
54
- inline joined_strings<Str, It> join (It begin, const It end, const Str &sep)
90
+ inline joined_strings<Str, It> join (It begin, It end, const Str &sep)
55
91
{
56
- return joined_strings<Str, It>(begin, end, sep);
92
+ return joined_strings<Str, It>(std::move ( begin), std::move ( end) , sep);
57
93
}
58
94
95
+ template <typename It>
96
+ inline joined_strings<const char *, It> join (
97
+ It begin, It end, const char *sep)
98
+ {
99
+ return joined_strings<const char *, It>(
100
+ std::move (begin),
101
+ std::move (end),
102
+ sep);
103
+ }
104
+
105
+
59
106
template <typename Str, typename Container>
60
107
class joined_strings_container
61
108
: public joined_strings<Str, typename Container::const_iterator>
@@ -64,10 +111,11 @@ class joined_strings_container
64
111
Container container;
65
112
66
113
public:
67
- joined_strings_container (const Container &container, const Str &sep)
114
+ template <typename Container_In, typename Str_In>
115
+ joined_strings_container (Container_In &&container, Str_In &&sep)
68
116
: joined_strings<Str, typename Container::const_iterator>(
69
- container.begin(), container.end(), sep),
70
- container (container)
117
+ container.begin(), container.end(), std::forward<Str_In>( sep) ),
118
+ container (std::forward<Container_In>( container) )
71
119
{
72
120
}
73
121
};
@@ -79,5 +127,12 @@ inline joined_strings_container<Str, Container> join(
79
127
return joined_strings_container<Str, Container>(container, sep);
80
128
}
81
129
130
+ template <typename Container>
131
+ inline joined_strings_container<const char *, Container> join (
132
+ const Container &container, const char *sep)
133
+ {
134
+ return joined_strings_container<const char *, Container>(container, sep);
135
+ }
136
+
82
137
83
138
#endif
0 commit comments