Skip to content

Commit 8461c97

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 8461c97

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

src/util/string_utils.h

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,34 @@ std::string trim_from_last_delimiter(
5050
/// \return A reference to the ostream that was passed in
5151
template<typename Stream, typename It, typename Delimiter>
5252
Stream &join_strings(
53-
Stream &os,
53+
Stream &&os,
5454
const It b,
5555
const It e,
5656
const Delimiter &delimiter)
57+
{
58+
using value_type = decltype(*b);
59+
// Call auxiliary function with identity function
60+
return join_strings(os, b, e, delimiter, [](const value_type &x) {
61+
return x;
62+
});
63+
}
64+
65+
template<typename Stream, typename It, typename Delimiter, typename ToStringFunc>
66+
Stream &join_strings(
67+
Stream &&os,
68+
const It b,
69+
const It e,
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
@@ -69,6 +69,7 @@ SRC += analyses/ai/ai.cpp \
6969
util/std_expr.cpp \
7070
util/string_utils/split_string.cpp \
7171
util/string_utils/strip_string.cpp \
72+
util/string_utils/join_string.cpp \
7273
util/symbol_table.cpp \
7374
util/symbol.cpp \
7475
util/unicode.cpp \
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 <vector>
13+
#include <sstream>
14+
#include <string>
15+
16+
#include <testing-utils/use_catch.h>
17+
#include <util/string_utils.h>
18+
19+
TEST_CASE("join_strings() should apply the function argument its passed to the elements of the container")
20+
{
21+
std::vector<int> vec{1, 2, 3};
22+
auto result = join_strings(std::ostringstream(), vec.begin(), vec.end(), "-", [](int x){
23+
return std::to_string(x);
24+
}).str();
25+
REQUIRE(result == "1-2-3");
26+
}
27+
28+
TEST_CASE("join_strings() when passed no function argument should apply the default identity function to the elements of the container")
29+
{
30+
std::vector<int> vec{1, 2, 3};
31+
auto result = join_strings(std::ostringstream(), vec.begin(), vec.end(), ",").str();
32+
REQUIRE(result == "1,2,3");
33+
}

0 commit comments

Comments
 (0)