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

Implement stringutils::join_arguments. #1524

Merged
merged 3 commits into from
Oct 21, 2024
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
11 changes: 11 additions & 0 deletions src/utils/string_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,16 @@ std::string to_string(double value, const std::string& format_spec) {
return fmt::format(format_spec, value);
}

std::string join_arguments(const std::string& lhs, const std::string& rhs) {
if (lhs.empty()) {
return rhs;
} else if (rhs.empty()) {
return lhs;
} else {
return fmt::format("{}", fmt::join({lhs, rhs}, ", "));
}
}


} // namespace stringutils
} // namespace nmodl
6 changes: 6 additions & 0 deletions src/utils/string_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@ static inline bool starts_with(const std::string& haystack, const std::string& n
*/
std::string to_string(double value, const std::string& format_spec = "{:.16g}");

/** Joint two (list of) arguments.
*
* The tricks is to not add a ',' when either side is empty.
*/
std::string join_arguments(const std::string& lhs, const std::string& rhs);

/** \} */ // end of utils

} // namespace stringutils
Expand Down
18 changes: 18 additions & 0 deletions test/unit/utils/string_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,21 @@ TEST_CASE("starts_with") {
REQUIRE(!stringutils::starts_with("abcde", "abcde++"));
}
}

TEST_CASE("join_arguments") {
SECTION("both empty") {
REQUIRE(stringutils::join_arguments("", "") == "");
}

SECTION("lhs emtpy") {
REQUIRE(stringutils::join_arguments("", "foo, bar") == "foo, bar");
}

SECTION("rhs empty") {
REQUIRE(stringutils::join_arguments("foo", "") == "foo");
}

SECTION("neither empty") {
REQUIRE(stringutils::join_arguments("foo", "bar") == "foo, bar");
}
}
Loading