Skip to content

Commit 827b774

Browse files
committed
Add new join_strings function that applies a function to the elements of the container it flattens
and add tests for it.
1 parent 3a8b34d commit 827b774

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed

src/util/string_utils.h

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,36 @@ std::string trim_from_last_delimiter(
4848
/// \param e: Iterator pointing past last item to print
4949
/// \param delimiter: Object to print between each item in the iterator range
5050
/// \return A reference to the ostream that was passed in
51-
template<typename Stream, typename It, typename Delimiter>
51+
template <typename Stream, typename It, typename Delimiter>
52+
Stream &
53+
join_strings(Stream &&os, const It b, const It e, const Delimiter &delimiter)
54+
{
55+
using value_type = decltype(*b);
56+
// Call auxiliary function with identity function
57+
return join_strings(
58+
os, b, e, delimiter, [](const value_type &x) { return x; });
59+
}
60+
61+
template <
62+
typename Stream,
63+
typename It,
64+
typename Delimiter,
65+
typename ToStringFunc>
5266
Stream &join_strings(
53-
Stream &os,
67+
Stream &&os,
5468
const It b,
5569
const It e,
56-
const Delimiter &delimiter)
70+
const Delimiter &delimiter,
71+
ToStringFunc &&to_string_func)
5772
{
5873
if(b==e)
5974
{
6075
return os;
6176
}
62-
os << *b;
77+
os << to_string_func(*b);
6378
for(auto it=std::next(b); it!=e; ++it)
6479
{
65-
os << delimiter << *it;
80+
os << delimiter << to_string_func(*it);
6681
}
6782
return os;
6883
}

unit/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ SRC += analyses/ai/ai.cpp \
6767
util/small_map.cpp \
6868
util/small_shared_two_way_ptr.cpp \
6969
util/std_expr.cpp \
70+
util/string_utils/join_string.cpp \
7071
util/string_utils/split_string.cpp \
7172
util/string_utils/strip_string.cpp \
7273
util/symbol_table.cpp \
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*******************************************************************\
2+
3+
Module: Unit tests of join_string
4+
5+
Author: Diffblue Ltd.
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// join_string Unit Tests
11+
12+
#include <sstream>
13+
#include <string>
14+
#include <vector>
15+
16+
#include <testing-utils/use_catch.h>
17+
#include <util/string_utils.h>
18+
19+
TEST_CASE(
20+
"join_strings() should apply the function argument its passed to the "
21+
"elements of the container",
22+
"[core][utils][string_utils][join_strings]")
23+
{
24+
std::vector<int> vec{1, 2, 3};
25+
auto result = join_strings(
26+
std::ostringstream(),
27+
vec.begin(),
28+
vec.end(),
29+
"-",
30+
[](int x) { return std::to_string(x + 1); })
31+
.str();
32+
REQUIRE(result == "2-3-4");
33+
}
34+
35+
TEST_CASE(
36+
"join_strings() when passed no function argument should apply the default "
37+
"identity function to the elements of the container",
38+
"[core][utils][string_utils][join_strings]")
39+
{
40+
std::vector<int> vec{1, 2, 3};
41+
auto result =
42+
join_strings(std::ostringstream(), vec.begin(), vec.end(), ",").str();
43+
REQUIRE(result == "1,2,3");
44+
}

0 commit comments

Comments
 (0)