feat add StrJoin
Some checks failed
rpcrypto-build / build (Debug, hisiv510.toolchain.cmake) (push) Successful in 1m3s
rpcrypto-build / build (Release, hisiv510.toolchain.cmake) (push) Successful in 1m8s
rpcrypto-build / build (Debug, himix200.toolchain.cmake) (push) Successful in 1m22s
rpcrypto-build / build (Release, himix200.toolchain.cmake) (push) Successful in 1m19s
linux-hisiv500-gcc / linux-gcc-hisiv500 (push) Successful in 1m18s
linux-x64-gcc / linux-gcc (push) Failing after 1m20s
linux-mips64-gcc / linux-gcc-mips64el (push) Failing after 1m37s
rpcrypto-build / build (Debug, hisiv510.toolchain.cmake) (pull_request) Successful in 58s
rpcrypto-build / build (Debug, himix200.toolchain.cmake) (pull_request) Successful in 1m0s
rpcrypto-build / build (Release, himix200.toolchain.cmake) (pull_request) Successful in 1m4s
rpcrypto-build / build (Release, hisiv510.toolchain.cmake) (pull_request) Successful in 1m9s
linux-hisiv500-gcc / linux-gcc-hisiv500 (pull_request) Successful in 1m15s
linux-x64-gcc / linux-gcc (pull_request) Failing after 1m16s
linux-mips64-gcc / linux-gcc-mips64el (pull_request) Failing after 1m33s

This commit is contained in:
tqcq 2024-01-12 16:24:06 +08:00
parent af72a33e68
commit f2c9e80a8a
5 changed files with 78 additions and 6 deletions

View File

@ -32,6 +32,7 @@ endif()
target_sources( target_sources(
${PROJECT_NAME} ${PROJECT_NAME}
PRIVATE 3party/mongoose/mongoose.c PRIVATE 3party/mongoose/mongoose.c
src/ulib/utils/utils.cpp
src/ulib/base/location.h src/ulib/base/location.h
src/ulib/base/location.cpp src/ulib/base/location.cpp
src/ulib/status.h src/ulib/status.h

29
src/ulib/utils/utils.cpp Normal file
View File

@ -0,0 +1,29 @@
#include "utils.h"
#include <sstream>
namespace ulib {
std::string
StrJoin(std::vector<nonstd::string_view> &vec,
nonstd::string_view delimiter,
bool ignore_empty_str)
{
if (vec.empty()) { return ""; }
auto iter = vec.cbegin();
if (ignore_empty_str) {
// find first non-empty string
while (iter != vec.cend() && iter->empty()) { ++iter; }
}
if (iter == vec.cend()) { return ""; }
std::stringstream ss;
ss << *iter;
for (++iter; iter != iter + 1; ++iter) {
if (ignore_empty_str && iter->empty()) { continue; }
ss << delimiter << *iter;
}
return std::move(ss.str());
}
}// namespace ulib

View File

@ -2,8 +2,18 @@
#define ULIB_SRC_ULIB_UTILS_UTILS_H_ #define ULIB_SRC_ULIB_UTILS_UTILS_H_
#include <functional> #include <functional>
#include <string_view.hpp>
#include <type_traits>
#include <vector>
namespace ulib { namespace ulib {
template<typename T, class Comp>
const T &
Clamp(const T &value, const T &low, const T &high, Comp comp)
{
return comp(value, low) ? low : (comp(high, value) ? high : value);
}
template<typename T> template<typename T>
const T & const T &
Clamp(const T &value, const T &low, const T &high) Clamp(const T &value, const T &low, const T &high)
@ -11,12 +21,9 @@ Clamp(const T &value, const T &low, const T &high)
return Clamp(value, low, high, std::less<T>{}); return Clamp(value, low, high, std::less<T>{});
} }
template<typename T, typename Comp> std::string StrJoin(std::vector<nonstd::string_view> &vec,
const T & nonstd::string_view delimiter = ",",
Clamp(const T &value, const T &low, const T &high, Comp comp) bool ignore_empty_str = true);
{
return comp(value, low) ? low : (comp(high, value) ? high : value);
}
}// namespace ulib }// namespace ulib
#endif// ULIB_SRC_ULIB_UTILS_UTILS_H_ #endif// ULIB_SRC_ULIB_UTILS_UTILS_H_

View File

@ -17,6 +17,7 @@ add_executable(
3party/optional/optional_unittest.cpp 3party/optional/optional_unittest.cpp
3party/sqlpp11/sqlpp11_unittest.cpp 3party/sqlpp11/sqlpp11_unittest.cpp
ulib/utils/defer_unittest.cpp ulib/utils/defer_unittest.cpp
ulib/utils/utils_unittest.cpp
ulib/status_or_unittest.cpp) ulib/status_or_unittest.cpp)
target_link_libraries(ulib_test PRIVATE ulib gtest gtest_main) target_link_libraries(ulib_test PRIVATE ulib gtest gtest_main)

View File

@ -0,0 +1,34 @@
#include <gtest/gtest.h>
#include <ulib/utils/utils.h>
TEST(Utils, Clamp)
{
EXPECT_EQ(ulib::Clamp(1, 2, 3), 2);
EXPECT_EQ(ulib::Clamp(2, 2, 3), 2);
EXPECT_EQ(ulib::Clamp(3, 2, 3), 3);
EXPECT_EQ(ulib::Clamp(4, 2, 3), 3);
}
TEST(Utils, StrJoin)
{
std::vector<nonstd::string_view> vec{"a", "b", "c"};
EXPECT_EQ(ulib::StrJoin(vec, ","), "a,b,c");
EXPECT_EQ(ulib::StrJoin(vec, ",", false), "a,b,c");
EXPECT_EQ(ulib::StrJoin(vec, ",", true), "a,b,c");
vec = {"a", "", "c"};
EXPECT_EQ(ulib::StrJoin(vec, ","), "a,c");
EXPECT_EQ(ulib::StrJoin(vec, ",", false), "a,,c");
EXPECT_EQ(ulib::StrJoin(vec, ",", true), "a,c");
vec = {"", "", ""};
EXPECT_EQ(ulib::StrJoin(vec, ","), "");
EXPECT_EQ(ulib::StrJoin(vec, ",", false), ",,");
EXPECT_EQ(ulib::StrJoin(vec, ",", true), "");
vec = {"a", "b", ""};
EXPECT_EQ(ulib::StrJoin(vec, ","), "a,b");
EXPECT_EQ(ulib::StrJoin(vec, ",", false), "a,b,");
EXPECT_EQ(ulib::StrJoin(vec, ",", true), "a,b");
vec = {"", "b", "c"};
EXPECT_EQ(ulib::StrJoin(vec, ","), "b,c");
EXPECT_EQ(ulib::StrJoin(vec, ",", false), ",b,c");
EXPECT_EQ(ulib::StrJoin(vec, ",", true), "b,c");
}