From 27f814b57036f1e5950c73b1e4d9f56469603bbc Mon Sep 17 00:00:00 2001 From: Luc Grosheintz Date: Fri, 18 Oct 2024 14:54:03 +0200 Subject: [PATCH 1/3] Implement `stringutils::join_arguments`. --- src/utils/string_utils.cpp | 11 +++++++++++ src/utils/string_utils.hpp | 6 ++++++ test/unit/utils/string_utils.cpp | 18 ++++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/src/utils/string_utils.cpp b/src/utils/string_utils.cpp index 51947eebd..64631d8e3 100644 --- a/src/utils/string_utils.cpp +++ b/src/utils/string_utils.cpp @@ -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 == "") { + return rhs; + } else if (rhs == "") { + return lhs; + } else { + return fmt::format("{}", fmt::join({lhs, rhs}, ", ")); + } +} + + } // namespace stringutils } // namespace nmodl diff --git a/src/utils/string_utils.hpp b/src/utils/string_utils.hpp index 2b0d81d12..5099550d6 100644 --- a/src/utils/string_utils.hpp +++ b/src/utils/string_utils.hpp @@ -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 diff --git a/test/unit/utils/string_utils.cpp b/test/unit/utils/string_utils.cpp index e55631fc3..1d4bf3b62 100644 --- a/test/unit/utils/string_utils.cpp +++ b/test/unit/utils/string_utils.cpp @@ -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"); + } +} From 9281cca1b5d3e7cab98b9aad9d59d4139aa6427e Mon Sep 17 00:00:00 2001 From: Luc Grosheintz Date: Fri, 18 Oct 2024 14:11:11 +0000 Subject: [PATCH 2/3] Update src/utils/string_utils.cpp Co-authored-by: Nicolas Cornu --- src/utils/string_utils.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/string_utils.cpp b/src/utils/string_utils.cpp index 64631d8e3..068e8f932 100644 --- a/src/utils/string_utils.cpp +++ b/src/utils/string_utils.cpp @@ -28,7 +28,7 @@ std::string to_string(double value, const std::string& format_spec) { } std::string join_arguments(const std::string& lhs, const std::string& rhs) { - if (lhs == "") { + if (lhs.empty()) { return rhs; } else if (rhs == "") { return lhs; From 182a4b12dde0b1e937c0d206a3f10edb58692edd Mon Sep 17 00:00:00 2001 From: Luc Grosheintz Date: Fri, 18 Oct 2024 14:11:16 +0000 Subject: [PATCH 3/3] Update src/utils/string_utils.cpp Co-authored-by: Nicolas Cornu --- src/utils/string_utils.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/string_utils.cpp b/src/utils/string_utils.cpp index 068e8f932..e804817a8 100644 --- a/src/utils/string_utils.cpp +++ b/src/utils/string_utils.cpp @@ -30,7 +30,7 @@ std::string to_string(double value, const std::string& format_spec) { std::string join_arguments(const std::string& lhs, const std::string& rhs) { if (lhs.empty()) { return rhs; - } else if (rhs == "") { + } else if (rhs.empty()) { return lhs; } else { return fmt::format("{}", fmt::join({lhs, rhs}, ", "));