Merge pull request 'feat support vector<string>' (#7) from feat/add_clamp into develop
All checks were successful
rpcrypto-build / build (Debug, himix200.toolchain.cmake) (push) Successful in 1m3s
rpcrypto-build / build (Debug, hisiv510.toolchain.cmake) (push) Successful in 1m10s
rpcrypto-build / build (Release, himix200.toolchain.cmake) (push) Successful in 1m12s
rpcrypto-build / build (Release, hisiv510.toolchain.cmake) (push) Successful in 1m2s
linux-hisiv500-gcc / linux-gcc-hisiv500 (push) Successful in 1m8s
linux-mips64-gcc / linux-gcc-mips64el (push) Successful in 1m31s
linux-x64-gcc / linux-gcc (push) Successful in 1m49s

Reviewed-on: #7
This commit is contained in:
tqcq 2024-01-12 08:38:41 +00:00
commit 4efbba1f96
3 changed files with 32 additions and 3 deletions

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");
}