feat add StrJoin #6

Merged
tqcq merged 3 commits from feat/add_clamp into develop 2024-01-12 16:31:22 +08:00
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 != vec.cend(); ++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");
}