Skip to content

Commit 6964a38

Browse files
committed
Added WsjcppCore::startsWith and WsjcppCore::endsWith
1 parent bbf19db commit 6964a38

File tree

6 files changed

+164
-21
lines changed

6 files changed

+164
-21
lines changed

src/wsjcpp_core.cpp

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -837,8 +837,6 @@ std::string WsjcppCore::extractURLProtocol(const std::string& sValue) {
837837
return sRet;
838838
}
839839

840-
// ---------------------------------------------------------------------
841-
842840
bool WsjcppCore::getEnv(const std::string& sName, std::string& sValue) {
843841
if (const char* env_p = std::getenv(sName.c_str())) {
844842
sValue = std::string(env_p);
@@ -847,8 +845,6 @@ bool WsjcppCore::getEnv(const std::string& sName, std::string& sValue) {
847845
return false;
848846
}
849847

850-
// ---------------------------------------------------------------------
851-
852848
std::string WsjcppCore::encodeUriComponent(const std::string& sValue) {
853849
std::stringstream ssRet;
854850
for (int i = 0; i < sValue.length(); i++) {
@@ -867,8 +863,6 @@ std::string WsjcppCore::encodeUriComponent(const std::string& sValue) {
867863
return ssRet.str();
868864
}
869865

870-
// ---------------------------------------------------------------------
871-
872866
std::string WsjcppCore::decodeUriComponent(const std::string& sValue) {
873867
std::string sRet = "";
874868
std::string sHex = "";
@@ -892,8 +886,6 @@ std::string WsjcppCore::decodeUriComponent(const std::string& sValue) {
892886
return sRet;
893887
}
894888

895-
// ---------------------------------------------------------------------
896-
897889
std::string WsjcppCore::getHumanSizeBytes(long nBytes) {
898890
if (nBytes == 0) {
899891
return "0B";
@@ -917,8 +909,6 @@ std::string WsjcppCore::getHumanSizeBytes(long nBytes) {
917909
return std::to_string(nBytes) + "PB";
918910
}
919911

920-
// ---------------------------------------------------------------------
921-
922912
bool WsjcppCore::recoursiveCopyFiles(const std::string& sSourceDir, const std::string& sTargetDir) {
923913
if (!WsjcppCore::dirExists(sSourceDir)) {
924914
WsjcppLog::err("recoursiveCopyFiles", "Source Dir '" + sSourceDir + "' did not exists");
@@ -959,8 +949,6 @@ bool WsjcppCore::recoursiveCopyFiles(const std::string& sSourceDir, const std::s
959949
return true;
960950
}
961951

962-
// ---------------------------------------------------------------------
963-
964952
bool WsjcppCore::recoursiveRemoveDir(const std::string& sDir) {
965953
if (!WsjcppCore::dirExists(sDir)) {
966954
WsjcppLog::err("recoursiveCopyFiles", "Dir '" + sDir + "' did not exists");
@@ -989,8 +977,6 @@ bool WsjcppCore::recoursiveRemoveDir(const std::string& sDir) {
989977
return true;
990978
}
991979

992-
// ---------------------------------------------------------------------
993-
994980
bool WsjcppCore::setFilePermissions(const std::string& sFilePath, const WsjcppFilePermissions &filePermissions, std::string& sError) {
995981

996982
mode_t m = 0x0;
@@ -1017,8 +1003,6 @@ bool WsjcppCore::setFilePermissions(const std::string& sFilePath, const WsjcppFi
10171003
return true;
10181004
}
10191005

1020-
// ---------------------------------------------------------------------
1021-
10221006
bool WsjcppCore::getFilePermissions(const std::string& sFilePath, WsjcppFilePermissions &filePermissions, std::string& sError) {
10231007
if (!WsjcppCore::fileExists(sFilePath)) {
10241008
sError = "File '" + sFilePath + "' - not found";
@@ -1054,8 +1038,6 @@ bool WsjcppCore::getFilePermissions(const std::string& sFilePath, WsjcppFilePerm
10541038
return true;
10551039
}
10561040

1057-
// ---------------------------------------------------------------------
1058-
10591041
std::string WsjcppCore::doPadLeft(const std::string& sIn, char cWhat, size_t nLength) {
10601042
std::string sRet;
10611043
size_t nPadLen = nLength - sIn.length();
@@ -1065,8 +1047,6 @@ std::string WsjcppCore::doPadLeft(const std::string& sIn, char cWhat, size_t nLe
10651047
return sRet + sIn;
10661048
}
10671049

1068-
// ---------------------------------------------------------------------
1069-
10701050
std::string WsjcppCore::doPadRight(const std::string& sIn, char cWhat, size_t nLength) {
10711051
std::string sRet;
10721052
size_t nPadLen = nLength - sIn.length();
@@ -1076,6 +1056,18 @@ std::string WsjcppCore::doPadRight(const std::string& sIn, char cWhat, size_t nL
10761056
return sIn + sRet;
10771057
}
10781058

1059+
bool WsjcppCore::startsWith(const std::string& sLine, const std::string& sStart) {
1060+
return sLine.rfind(sStart, 0) == 0;
1061+
}
1062+
1063+
bool WsjcppCore::endsWith(const std::string& sLine, const std::string& sEnd) {
1064+
// https://www.techiedelight.com/check-if-a-string-ends-with-another-string-in-cpp/
1065+
if (sLine.length() < sEnd.length()) {
1066+
return false;
1067+
}
1068+
return std::equal(sEnd.rbegin(), sEnd.rend(), sLine.rbegin());
1069+
}
1070+
10791071
// ---------------------------------------------------------------------
10801072
// WsjcppLog
10811073

@@ -1269,7 +1261,6 @@ const std::vector<WsjcppResourceFile*> &WsjcppResourcesManager::list() {
12691261
return *g_pWsjcppResourceFiles;
12701262
}
12711263

1272-
// ---------------------------------------------------------------------
12731264

12741265
/*
12751266
bool WsjcppResourcesManager::make(const std::string &sWorkspace) {

src/wsjcpp_core.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ class WsjcppCore {
132132
static std::string doPadLeft(const std::string& sIn, char cWhat, size_t nLength);
133133
static std::string doPadRight(const std::string& sIn, char cWhat, size_t nLength);
134134

135+
static bool startsWith(const std::string& sLine, const std::string& sStart);
136+
static bool endsWith(const std::string& sLine, const std::string& sEnd);
137+
135138
};
136139

137140

unit-tests.wsjcpp/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_date_time_format
5252
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_make_dirs_path.cpp")
5353
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_extract_dirpath.cpp")
5454
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_extract_filepath.cpp")
55+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_starts_with.cpp")
56+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_ends_with.cpp")
5557

5658
include(${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.user-custom.txt)
5759

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
2+
#include <wsjcpp_core.h>
3+
#include <wsjcpp_unit_tests.h>
4+
5+
// ---------------------------------------------------------------------
6+
// UnitTestEndsWith
7+
8+
class UnitTestEndsWith : public WsjcppUnitTestBase {
9+
public:
10+
UnitTestEndsWith();
11+
virtual bool doBeforeTest() override;
12+
virtual void executeTest() override;
13+
virtual bool doAfterTest() override;
14+
};
15+
16+
REGISTRY_WSJCPP_UNIT_TEST(UnitTestEndsWith)
17+
18+
UnitTestEndsWith::UnitTestEndsWith()
19+
: WsjcppUnitTestBase("UnitTestEndsWith") {
20+
}
21+
22+
// ---------------------------------------------------------------------
23+
24+
bool UnitTestEndsWith::doBeforeTest() {
25+
// do something before test
26+
return true;
27+
}
28+
29+
// ---------------------------------------------------------------------
30+
31+
void UnitTestEndsWith::executeTest() {
32+
33+
struct LTest {
34+
LTest(
35+
const std::string &sStr,
36+
const std::string &sStart,
37+
bool bExpected
38+
) {
39+
this->sStr = sStr;
40+
this->sStart = sStart;
41+
this->bExpected = bExpected;
42+
};
43+
std::string sStr;
44+
std::string sStart;
45+
bool bExpected;
46+
};
47+
48+
std::vector<LTest> tests;
49+
tests.push_back(LTest("123456789", "789", true));
50+
tests.push_back(LTest("122456779", "789", false));
51+
tests.push_back(LTest("12", "789", false));
52+
tests.push_back(LTest("12", "abc", false));
53+
tests.push_back(LTest("^1234567", "789", false));
54+
tests.push_back(LTest(" 12345", "789", false));
55+
tests.push_back(LTest(" 12345", "345", true));
56+
57+
for (int i = 0; i < tests.size(); i++) {
58+
LTest test = tests[i];
59+
std::string sPrefix = "test" + std::to_string(i) + "(\"" + test.sStr + "\", \"" + test.sStart + "\")";
60+
compare(sPrefix + ": size", WsjcppCore::endsWith(test.sStr, test.sStart), test.bExpected);
61+
}
62+
}
63+
64+
// ---------------------------------------------------------------------
65+
66+
bool UnitTestEndsWith::doAfterTest() {
67+
// do somethig after test
68+
return true;
69+
}
70+
71+
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
2+
#include <wsjcpp_core.h>
3+
#include <wsjcpp_unit_tests.h>
4+
5+
// ---------------------------------------------------------------------
6+
// UnitTestStartsWith
7+
8+
class UnitTestStartsWith : public WsjcppUnitTestBase {
9+
public:
10+
UnitTestStartsWith();
11+
virtual bool doBeforeTest() override;
12+
virtual void executeTest() override;
13+
virtual bool doAfterTest() override;
14+
};
15+
16+
REGISTRY_WSJCPP_UNIT_TEST(UnitTestStartsWith)
17+
18+
UnitTestStartsWith::UnitTestStartsWith()
19+
: WsjcppUnitTestBase("UnitTestStartsWith") {
20+
}
21+
22+
// ---------------------------------------------------------------------
23+
24+
bool UnitTestStartsWith::doBeforeTest() {
25+
// do something before test
26+
return true;
27+
}
28+
29+
// ---------------------------------------------------------------------
30+
31+
void UnitTestStartsWith::executeTest() {
32+
33+
struct LTest {
34+
LTest(
35+
const std::string &sStr,
36+
const std::string &sStart,
37+
bool bExpected
38+
) {
39+
this->sStr = sStr;
40+
this->sStart = sStart;
41+
this->bExpected = bExpected;
42+
};
43+
std::string sStr;
44+
std::string sStart;
45+
bool bExpected;
46+
};
47+
48+
std::vector<LTest> tests;
49+
tests.push_back(LTest("123456789", "123", true));
50+
tests.push_back(LTest("122456789", "123", false));
51+
tests.push_back(LTest("12", "123", false));
52+
tests.push_back(LTest("12", "abc", false));
53+
tests.push_back(LTest("^1234567", "123", false));
54+
tests.push_back(LTest(" 12345", "123", false));
55+
56+
for (int i = 0; i < tests.size(); i++) {
57+
LTest test = tests[i];
58+
std::string sPrefix = "test" + std::to_string(i) + "(\"" + test.sStr + "\", \"" + test.sStart + "\")";
59+
compare(sPrefix + ": size", WsjcppCore::startsWith(test.sStr, test.sStart), test.bExpected);
60+
}
61+
}
62+
63+
// ---------------------------------------------------------------------
64+
65+
bool UnitTestStartsWith::doAfterTest() {
66+
// do somethig after test
67+
return true;
68+
}
69+
70+

wsjcpp.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,9 @@ unit-tests:
8888
description: ""
8989
- name: "ExtractFilepath"
9090
description: ""
91+
- name: "StartsWith"
92+
description: ""
93+
94+
- name: "EndsWith"
95+
description: ""
96+

0 commit comments

Comments
 (0)