From 2d49725be5e34c70c2d3e7a0f4249c554b019ae0 Mon Sep 17 00:00:00 2001 From: admiswalker Date: Thu, 24 Mar 2022 21:23:32 +0900 Subject: [PATCH] add sstd::stripAll() and stripAll_ow() --- sstd/src/string/strEdit.cpp | 62 +++++++++++++++++++++++++++++++++++++ sstd/src/string/strEdit.hpp | 8 +++++ test/string/strEdit.hpp | 59 +++++++++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+) diff --git a/sstd/src/string/strEdit.cpp b/sstd/src/string/strEdit.cpp index 73fb9b69..8af12a38 100755 --- a/sstd/src/string/strEdit.cpp +++ b/sstd/src/string/strEdit.cpp @@ -135,3 +135,65 @@ std::vector sstd::strip(const std::vector& vec){ } //-------------------------------------------------------------------------------------------------------- + +std::string stripAll_base(const char* str, const uint len, const char* stripList, const uint sLen){ + std::string ret('0', len); ret.clear(); + + if(len * sLen <= 256){ + for(uint i=0; i strip (const std::vector& vec); // -> strip(str) // removing head and tail spaces + + std::string stripAll (const char* str, const char* stripList); + std::string stripAll (const std::string& str, const char* stripList); + std::string stripAll (const char* str, const std::string& stripList); + std::string stripAll (const std::string& str, const std::string& stripList); + + void stripAll_ow( std::string& str, const char* stripList); + void stripAll_ow( std::string& str, const std::string& stripList); // todo: rm '\t' // todo: implementing memcopy version. Ref: https://postd.cc/how-quickly-can-you-remove-spaces-from-a-string/ } diff --git a/test/string/strEdit.hpp b/test/string/strEdit.hpp index fc0ecdc7..ff012320 100755 --- a/test/string/strEdit.hpp +++ b/test/string/strEdit.hpp @@ -78,3 +78,62 @@ TEST(strEdit, strip_vec){ } //----------------------------------------------------------------------------------------------------------------------------------------------- + +TEST(strEdit, stripAll_case01_01){ + std::string str_in = "0a0b0cx0d0xe0f0g0"; + std::string str_ans = "abcdefg"; + std::string ret = sstd::stripAll(str_in.c_str(), "x0"); + ASSERT_TRUE(ret == str_ans); +} +TEST(strEdit, stripAll_case01_02){ + std::string str_in = "\r\nabc\r\n\r\ndefg\r\n"; + std::string str_ans = "abcdefg"; + std::string ret = sstd::stripAll(str_in.c_str(), "\r\n"); + ASSERT_TRUE(ret == str_ans); +} +TEST(strEdit, stripAll_case02){ + std::string str_in = "0a0b0cx0d0xe0f0g00000000000000000000000000000xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx0000000x0x0x0x0x0x000x0x0xxx0000x0x0xx00000x00x0x0x00000000000000000000000000000xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx0000000x0x0x0x0x0x000x0x0xxx0000x0x0xx00000x00x0x0x00000000000000000000000000000xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx0000000x0x0x0x0x0x000x0x0xxx0000x0x0xx00000x00x0x0x"; + std::string str_ans = "abcdefg"; + std::string ret = sstd::stripAll(str_in.c_str(), "x0"); + ASSERT_TRUE(ret == str_ans); +} + +#define strEdit_stripAll_check_io(LHS, RHS) \ + std::string str_in = "0a0b0cx0d0xe0f0g0"; \ + std::string str_ans = "abcdefg"; \ + std::string ret = sstd::stripAll(LHS, RHS); \ + ASSERT_TRUE(ret == str_ans); +TEST(strEdit, stripAll_cc){ strEdit_stripAll_check_io(str_in.c_str(), "x0" ); } +TEST(strEdit, stripAll_sc){ strEdit_stripAll_check_io(str_in, "x0" ); } +TEST(strEdit, stripAll_cs){ strEdit_stripAll_check_io(str_in.c_str(), std::string("x0")); } +TEST(strEdit, stripAll_ss){ strEdit_stripAll_check_io(str_in, std::string("x0")); } +#undef strEdit_stripAll_check_io + +//----------------------------------------------------------------------------------------------------------------------------------------------- + +TEST(strEdit, stripAll_ow_case01_01){ + std::string str_in = "0a0b0cx0d0xe0f0g0"; + std::string str_ans = "abcdefg"; + sstd::stripAll_ow(str_in, "x0"); + ASSERT_STREQ(str_in.c_str(), str_ans.c_str()); +} +TEST(strEdit, stripAll_ow_case01_02){ + std::string str_in = ""; + std::string str_ans = ""; + sstd::stripAll_ow(str_in, "x0"); + ASSERT_STREQ(str_in.c_str(), str_ans.c_str()); +} +TEST(strEdit, stripAll_ow_case01_03){ + std::string str_in = ""; + std::string str_ans = ""; + sstd::stripAll_ow(str_in, ""); + ASSERT_STREQ(str_in.c_str(), str_ans.c_str()); +} +TEST(strEdit, stripAll_ow_case02){ + std::string str_in = "0a0b0cx0d0xe0f0g00000000000000000000000000000xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx0000000x0x0x0x0x0x000x0x0xxx0000x0x0xx00000x00x0x0x00000000000000000000000000000xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx0000000x0x0x0x0x0x000x0x0xxx0000x0x0xx00000x00x0x0x00000000000000000000000000000xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx0000000x0x0x0x0x0x000x0x0xxx0000x0x0xx00000x00x0x0x"; + std::string str_ans = "abcdefg"; + sstd::stripAll_ow(str_in, "x0"); + ASSERT_STREQ(str_in.c_str(), str_ans.c_str()); +} + +//-----------------------------------------------------------------------------------------------------------------------------------------------