Merge pull request 'feat add StrJoin' (#6) from feat/add_clamp into develop
All checks were successful
rpcrypto-build / build (Debug, hisiv510.toolchain.cmake) (push) Successful in 1m11s
rpcrypto-build / build (Debug, himix200.toolchain.cmake) (push) Successful in 1m24s
rpcrypto-build / build (Release, himix200.toolchain.cmake) (push) Successful in 1m22s
linux-hisiv500-gcc / linux-gcc-hisiv500 (push) Successful in 1m9s
rpcrypto-build / build (Release, hisiv510.toolchain.cmake) (push) Successful in 1m11s
linux-mips64-gcc / linux-gcc-mips64el (push) Successful in 1m35s
linux-x64-gcc / linux-gcc (push) Successful in 1m48s
All checks were successful
rpcrypto-build / build (Debug, hisiv510.toolchain.cmake) (push) Successful in 1m11s
rpcrypto-build / build (Debug, himix200.toolchain.cmake) (push) Successful in 1m24s
rpcrypto-build / build (Release, himix200.toolchain.cmake) (push) Successful in 1m22s
linux-hisiv500-gcc / linux-gcc-hisiv500 (push) Successful in 1m9s
rpcrypto-build / build (Release, hisiv510.toolchain.cmake) (push) Successful in 1m11s
linux-mips64-gcc / linux-gcc-mips64el (push) Successful in 1m35s
linux-x64-gcc / linux-gcc (push) Successful in 1m48s
Reviewed-on: #6
This commit is contained in:
commit
6742d9a059
@ -32,6 +32,7 @@ endif()
|
||||
target_sources(
|
||||
${PROJECT_NAME}
|
||||
PRIVATE 3party/mongoose/mongoose.c
|
||||
src/ulib/utils/utils.cpp
|
||||
src/ulib/base/location.h
|
||||
src/ulib/base/location.cpp
|
||||
src/ulib/status.h
|
||||
|
29
src/ulib/utils/utils.cpp
Normal file
29
src/ulib/utils/utils.cpp
Normal 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
|
@ -2,8 +2,18 @@
|
||||
#define ULIB_SRC_ULIB_UTILS_UTILS_H_
|
||||
|
||||
#include <functional>
|
||||
#include <string_view.hpp>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
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>
|
||||
const T &
|
||||
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>{});
|
||||
}
|
||||
|
||||
template<typename T, typename 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);
|
||||
}
|
||||
std::string StrJoin(std::vector<nonstd::string_view> &vec,
|
||||
nonstd::string_view delimiter = ",",
|
||||
bool ignore_empty_str = true);
|
||||
|
||||
}// namespace ulib
|
||||
#endif// ULIB_SRC_ULIB_UTILS_UTILS_H_
|
||||
|
@ -17,6 +17,7 @@ add_executable(
|
||||
3party/optional/optional_unittest.cpp
|
||||
3party/sqlpp11/sqlpp11_unittest.cpp
|
||||
ulib/utils/defer_unittest.cpp
|
||||
ulib/utils/utils_unittest.cpp
|
||||
ulib/status_or_unittest.cpp)
|
||||
target_link_libraries(ulib_test PRIVATE ulib gtest gtest_main)
|
||||
|
||||
|
34
tests/ulib/utils/utils_unittest.cpp
Normal file
34
tests/ulib/utils/utils_unittest.cpp
Normal 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");
|
||||
}
|
Loading…
Reference in New Issue
Block a user