Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new StringJoin() utility function #203

Merged
merged 1 commit into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions libutils/string_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -1781,3 +1781,23 @@ bool StringMatchesOption(
}
return StringEqualN_IgnoreCase(supplied, longopt, length);
}

char *StringJoin(const Seq *const seq, const char *sep)
{
assert(seq != NULL);

Writer *const writer = StringWriter();
const size_t len = SeqLength(seq);
for (size_t i = 0; i < len; i++)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd do i < (len -1) writing the separator after every item and then writing the last item after the for loop without a separator. But YMMV.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then the program would crash if the sequence was empty, right ? 🤔

Copy link
Contributor

@vpodzime vpodzime Dec 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, but that's easy to check upfront. Which is better than having an unnecessary if inside the loop.

{
if (i != 0 && sep != NULL)
{
WriterWrite(writer, sep);
}
const char *const str = SeqAt(seq, i);
WriterWrite(writer, str);
}

char *const data = StringWriterClose(writer);
return data;
}
10 changes: 10 additions & 0 deletions libutils/string_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -322,4 +322,14 @@ void CanonifyNameInPlace(char *str);
bool StringMatchesOption(
const char *supplied, const char *longopt, const char *shortopt);

/**
* @brief Join elements in sequence into a string
* @param[in] seq Sequence of strings to join
* @param[in] sep Separator between elements (can be NULL)
* @return The concatenation of the elements in sequence
* @note Sequence must contain only NUL-terminated strings, otherwise behavior
* is undefined
*/
char *StringJoin(const Seq *seq, const char *sep);

#endif
38 changes: 38 additions & 0 deletions tests/unit/string_lib_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1470,6 +1470,43 @@ static void test_StringMatchesOption(void)
assert_true(StringMatchesOption("--host", "--hosts", "-H"));
}

static void test_StringJoin(void)
{
const char *argv[] = { "one", "two", "three" };
const int argc = sizeof(argv) / sizeof(argv[0]);

Seq *seq = SeqFromArgv(argc, argv);

char *actual = StringJoin(seq, NULL);
assert_string_equal(actual, "onetwothree");
free(actual);

actual = StringJoin(seq, "");
assert_string_equal(actual, "onetwothree");
free(actual);

actual = StringJoin(seq, ", ");
assert_string_equal(actual, "one, two, three");
free(actual);

SeqDestroy(seq);
seq = SeqNew(0, NULL);

actual = StringJoin(seq, NULL);
assert_string_equal(actual, "");
free(actual);

actual = StringJoin(seq, "");
assert_string_equal(actual, "");
free(actual);

actual = StringJoin(seq, ", ");
assert_string_equal(actual, "");
free(actual);

SeqDestroy(seq);
}

int main()
{
PRINT_TEST_BANNER();
Expand Down Expand Up @@ -1564,6 +1601,7 @@ int main()
unit_test(test_StrCatDelim),

unit_test(test_StringMatchesOption),
unit_test(test_StringJoin),
};

return run_tests(tests);
Expand Down
Loading