Skip to content

Commit 18c79a2

Browse files
committed
Implemented strSplit() and strJoin() with tests.
1 parent 5f29e1e commit 18c79a2

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

src/bin2cpp/common.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <stdio.h>
2828
#include <stdlib.h>
2929
#include <string>
30+
#include <sstream>
3031

3132
#include "rapidassist/strings.h"
3233
#include "rapidassist/filesystem.h"
@@ -322,4 +323,45 @@ namespace bin2cpp
322323
return tmp;
323324
}
324325

326+
void strSplit(const std::string& value, char separator, std::vector<std::string>& values)
327+
{
328+
values.clear();
329+
size_t start = 0;
330+
size_t end = std::string::npos;
331+
size_t length = 0;
332+
333+
// find first separator
334+
end = value.find(separator, start);
335+
while ( end != std::string::npos )
336+
{
337+
length = end - start;
338+
std::string item = value.substr(start, length);
339+
values.push_back(item);
340+
341+
// find next separator
342+
start = end + 1;
343+
end = value.find(separator, start);
344+
}
345+
346+
// Capture last token
347+
values.push_back(value.substr(start));
348+
}
349+
350+
std::string strJoin(const std::vector<std::string>& values, char separator)
351+
{
352+
std::string output;
353+
354+
for ( size_t i = 0; i < values.size(); i++ )
355+
{
356+
const std::string& element = values[i];
357+
output += element;
358+
359+
bool is_last = (i == (values.size() - 1));
360+
if ( !is_last )
361+
output.append(1, separator);
362+
}
363+
364+
return output;
365+
}
366+
325367
}; //bin2cpp

src/bin2cpp/common.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <string>
3030
#include <stdio.h>
3131
#include <set>
32+
#include <vector>
3233

3334
namespace bin2cpp
3435
{
@@ -124,6 +125,22 @@ namespace bin2cpp
124125
///<return>Returns the path matching all given components.<return>
125126
std::string pathJoin(const std::string & directory, const std::string & file_name, const std::string & file_extension);
126127

128+
///<summary>
129+
///Split a string into a list of values based on a specified separator character.
130+
///</summary>
131+
///<param name="value">The input string that contains values to split.</param>
132+
///<param name="separator">The character that separate the values in the string.</param>
133+
///<param name="values">The output individual values in the input string.</param>
134+
void strSplit(const std::string & value, char separator, std::vector<std::string> & values);
135+
136+
///<summary>
137+
///"Combine a list of values into a single string, using a specified separator character.
138+
///</summary>
139+
///<param name="values">The list of values to join to a single string.</param>
140+
///<param name="separator">The character that separate the values in the string.</param>
141+
///<return>Returns the combined string.<return>
142+
std::string strJoin(const std::vector<std::string>& values, char separator);
143+
127144
}; //bin2cpp
128145

129146
#endif //BIN2CPP_COMMON_H

test/bin2cpp_unittest/TestCommon.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,53 @@ TEST_F(TestCommon, testPathSplitJoin)
253253
"file_ext=\"" << file_ext << "\"\n";
254254
}
255255
}
256+
257+
TEST_F(TestCommon, testStringSplitJoin)
258+
{
259+
struct TEST_HELPER
260+
{
261+
char separator;
262+
std::string value;
263+
std::vector<std::string> expected_values;
264+
};
265+
static const char SEPARATOR = ':';
266+
267+
// arrange
268+
static const TEST_HELPER test_values[] = {
269+
{SEPARATOR, "", {""}}, // empty string
270+
{SEPARATOR, "abc", {"abc"}}, // no separator
271+
{SEPARATOR, "a:b:c", {"a", "b", "c"}}, // multiple
272+
{SEPARATOR, ":b:c", {"", "b", "c"}}, // start with separator
273+
{SEPARATOR, "a:b:", {"a", "b", ""}}, // end with separator
274+
{SEPARATOR, "a::", {"a", "", ""}}, // 2 consecutive separators
275+
{SEPARATOR, "::", {"", "", ""}}, // only consecutive separators
276+
};
277+
static const size_t num_test_values = sizeof(test_values) / sizeof(test_values[0]);
278+
279+
// act (strSplit)
280+
for ( size_t i = 0; i < num_test_values; i++ )
281+
{
282+
const TEST_HELPER& t = test_values[i];
283+
284+
std::vector<std::string> actual_values;
285+
bin2cpp::strSplit(t.value, t.separator, actual_values);
286+
287+
// assert
288+
ASSERT_EQ(actual_values, t.expected_values) << "Test fail for strSplit() with test_values[" << i << "]. Splitting value `" << t.value << "` with separator '" << t.separator << "' should result in " << t.expected_values.size() << " elements.";
289+
}
290+
291+
// act (strJoin)
292+
for ( size_t i = 0; i < num_test_values; i++ )
293+
{
294+
const TEST_HELPER& t = test_values[i];
295+
296+
std::vector<std::string> tmp_values;
297+
bin2cpp::strSplit(t.value, t.separator, tmp_values);
298+
299+
std::string expected_value = t.value;
300+
std::string actual_value = bin2cpp::strJoin(tmp_values, t.separator);
301+
302+
// assert
303+
ASSERT_EQ(actual_value, expected_value) << "Test fail for strJoin() with test_values[" << i << "]. Joining values with separator '" << t.separator << "' should result in `" << expected_value << "` but we got `" << actual_value << "`.";
304+
}
305+
}

0 commit comments

Comments
 (0)