feat support vector<string> #7

Merged
tqcq merged 1 commits from feat/add_clamp into develop 2024-01-12 16:38:44 +08:00
3 changed files with 32 additions and 3 deletions
Showing only changes of commit cc133b70f5 - Show all commits

View File

@ -3,7 +3,7 @@
namespace ulib {
std::string
StrJoin(std::vector<nonstd::string_view> &vec,
StrJoin(const std::vector<nonstd::string_view> &vec,
nonstd::string_view delimiter,
bool ignore_empty_str)
{
@ -26,4 +26,13 @@ StrJoin(std::vector<nonstd::string_view> &vec,
return std::move(ss.str());
}
std::string
StrJoin(const std::vector<std::string> &vec,
nonstd::string_view delimiter,
bool ignore_empty_str)
{
return StrJoin(std::vector<nonstd::string_view>{vec.cbegin(), vec.cend()},
delimiter, ignore_empty_str);
}
}// namespace ulib

View File

@ -3,7 +3,6 @@
#include <functional>
#include <string_view.hpp>
#include <type_traits>
#include <vector>
namespace ulib {
@ -21,7 +20,11 @@ Clamp(const T &value, const T &low, const T &high)
return Clamp(value, low, high, std::less<T>{});
}
std::string StrJoin(std::vector<nonstd::string_view> &vec,
std::string StrJoin(const std::vector<nonstd::string_view> &vec,
nonstd::string_view delimiter = ",",
bool ignore_empty_str = true);
std::string StrJoin(const std::vector<std::string> &vec,
nonstd::string_view delimiter = ",",
bool ignore_empty_str = true);

View File

@ -1,5 +1,6 @@
#include <gtest/gtest.h>
#include <ulib/utils/utils.h>
#include <vector>
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<std::string> vec{"a", "b", "c"};
EXPECT_EQ(ulib::StrJoin(vec, ","), "a,b,c");
const std::vector<std::string> &const_vec = vec;
EXPECT_EQ(ulib::StrJoin(const_vec, ","), "a,b,c");
}
TEST(Utils, StrJoin_vector_nonstd_string_view)
{
std::vector<nonstd::string_view> vec{"a", "b", "c"};
EXPECT_EQ(ulib::StrJoin(vec, ","), "a,b,c");
const std::vector<nonstd::string_view> &const_vec = vec;
EXPECT_EQ(ulib::StrJoin(const_vec, ","), "a,b,c");
}