diff --git a/src/ulib/utils/utils.cpp b/src/ulib/utils/utils.cpp index 3805fd7..948d97d 100644 --- a/src/ulib/utils/utils.cpp +++ b/src/ulib/utils/utils.cpp @@ -3,7 +3,7 @@ namespace ulib { std::string -StrJoin(std::vector &vec, +StrJoin(const std::vector &vec, nonstd::string_view delimiter, bool ignore_empty_str) { @@ -26,4 +26,13 @@ StrJoin(std::vector &vec, return std::move(ss.str()); } +std::string +StrJoin(const std::vector &vec, + nonstd::string_view delimiter, + bool ignore_empty_str) +{ + return StrJoin(std::vector{vec.cbegin(), vec.cend()}, + delimiter, ignore_empty_str); +} + }// namespace ulib diff --git a/src/ulib/utils/utils.h b/src/ulib/utils/utils.h index cdcf16c..5e90e99 100644 --- a/src/ulib/utils/utils.h +++ b/src/ulib/utils/utils.h @@ -3,7 +3,6 @@ #include #include -#include #include namespace ulib { @@ -21,7 +20,11 @@ Clamp(const T &value, const T &low, const T &high) return Clamp(value, low, high, std::less{}); } -std::string StrJoin(std::vector &vec, +std::string StrJoin(const std::vector &vec, + nonstd::string_view delimiter = ",", + bool ignore_empty_str = true); + +std::string StrJoin(const std::vector &vec, nonstd::string_view delimiter = ",", bool ignore_empty_str = true); diff --git a/tests/ulib/utils/utils_unittest.cpp b/tests/ulib/utils/utils_unittest.cpp index a54d7a5..a8c2891 100644 --- a/tests/ulib/utils/utils_unittest.cpp +++ b/tests/ulib/utils/utils_unittest.cpp @@ -1,5 +1,6 @@ #include #include +#include TEST(Utils, Clamp) { @@ -32,3 +33,19 @@ TEST(Utils, StrJoin) EXPECT_EQ(ulib::StrJoin(vec, ",", false), ",b,c"); EXPECT_EQ(ulib::StrJoin(vec, ",", true), "b,c"); } + +TEST(Utils, StrJoin_vector_string) +{ + std::vector vec{"a", "b", "c"}; + EXPECT_EQ(ulib::StrJoin(vec, ","), "a,b,c"); + const std::vector &const_vec = vec; + EXPECT_EQ(ulib::StrJoin(const_vec, ","), "a,b,c"); +} + +TEST(Utils, StrJoin_vector_nonstd_string_view) +{ + std::vector vec{"a", "b", "c"}; + EXPECT_EQ(ulib::StrJoin(vec, ","), "a,b,c"); + const std::vector &const_vec = vec; + EXPECT_EQ(ulib::StrJoin(const_vec, ","), "a,b,c"); +}