Skip to content

Commit

Permalink
add sstd::stripAll() and stripAll_ow()
Browse files Browse the repository at this point in the history
  • Loading branch information
admiswalker committed Mar 24, 2022
1 parent fc85eb1 commit 2d49725
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
62 changes: 62 additions & 0 deletions sstd/src/string/strEdit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,65 @@ std::vector<std::string> sstd::strip(const std::vector<std::string>& 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<len; ++i){
bool TF_continue=false;
for(uint si=0; si<sLen; ++si){
if(str[i]==stripList[si]){ TF_continue=true; break; }
}
if(TF_continue){ continue; }

ret += str[i];
}
}else{
bool sTbl[256]={false};
for(uint si=0; si<sLen; ++si){ sTbl[ (uchar)stripList[si] ] = true; }

for(uint i=0; i<len; ++i){
if( sTbl[ (uchar)str[i] ] ){ continue; }
ret += str[i];
}
}

return ret;
}
std::string sstd::stripAll (const char* str, const char* stripList){ return stripAll_base(str, strlen(str), stripList, strlen(stripList)); }
std::string sstd::stripAll (const std::string& str, const char* stripList){ return stripAll_base(str.c_str(), str.size(), stripList, strlen(stripList)); }
std::string sstd::stripAll (const char* str, const std::string& stripList){ return stripAll_base(str, strlen(str), stripList.c_str(), stripList.size()); }
std::string sstd::stripAll (const std::string& str, const std::string& stripList){ return stripAll_base(str.c_str(), str.size(), stripList.c_str(), stripList.size()); }

void stripAll_ow_base(std::string& str, const char* stripList, const uint sLen){
const uint len = str.size();
uint r=0;

if(len * sLen <= 256){
for(uint i=0; i<len; ++i){
bool TF_continue=false;
for(uint si=0; si<sLen; ++si){
if(str[i]==stripList[si]){ TF_continue=true; break; }
}
if(TF_continue){ continue; }

str[r] = str[i]; ++r;
}
}else{
bool sTbl[256]={false};
for(uint si=0; si<sLen; ++si){ sTbl[ (uchar)stripList[si] ] = true; }

for(uint i=0; i<len; ++i){
if( sTbl[ (uchar)str[i] ] ){ continue; }
str[r] = str[i]; ++r;
}
}

str.resize(r);
return;
}
void sstd::stripAll_ow( std::string& str, const char* stripList){ stripAll_ow_base(str, stripList, strlen(stripList)); return; }
void sstd::stripAll_ow( std::string& str, const std::string& stripList){ stripAll_ow_base(str, stripList.c_str(), stripList.size()); return; }

//--------------------------------------------------------------------------------------------------------
8 changes: 8 additions & 0 deletions sstd/src/string/strEdit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ namespace sstd{
std::string strip (const std::string& str); // removing head and tail tab and spaces
void strip_ow( std::string& str); // removing head and tail tab and spaces. ow: overwrite
std::vector<std::string> strip (const std::vector<std::string>& 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/
}
Expand Down
59 changes: 59 additions & 0 deletions test/string/strEdit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

//-----------------------------------------------------------------------------------------------------------------------------------------------

0 comments on commit 2d49725

Please sign in to comment.