Skip to content

Commit d5d627d

Browse files
committed
Fixed #16 added new functions doPadLeft and doPadRight
1 parent d331c3d commit d5d627d

File tree

6 files changed

+81
-0
lines changed

6 files changed

+81
-0
lines changed

src/wsjcpp_core.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,28 @@ bool WsjcppCore::getFilePermissions(const std::string& sFilePath, WsjcppFilePerm
10511051
return true;
10521052
}
10531053

1054+
// ---------------------------------------------------------------------
1055+
1056+
std::string WsjcppCore::doPadLeft(const std::string& sIn, char cWhat, int nLength) {
1057+
std::string sRet;
1058+
int nPadLen = nLength - sIn.length();
1059+
for (int i = 0; i < nPadLen; i++) {
1060+
sRet += cWhat;
1061+
}
1062+
return sRet + sIn;
1063+
}
1064+
1065+
// ---------------------------------------------------------------------
1066+
1067+
std::string WsjcppCore::doPadRight(const std::string& sIn, char cWhat, int nLength) {
1068+
std::string sRet;
1069+
int nPadLen = nLength - sIn.length();
1070+
for (int i = 0; i < nPadLen; i++) {
1071+
sRet += cWhat;
1072+
}
1073+
return sIn + sRet;
1074+
}
1075+
10541076
// ---------------------------------------------------------------------
10551077
// WsjcppLog
10561078

src/wsjcpp_core.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ class WsjcppCore {
127127
static bool setFilePermissions(const std::string& sFilePath, const WsjcppFilePermissions &filePermissions, std::string& sError);
128128
static bool getFilePermissions(const std::string& sFilePath, WsjcppFilePermissions &filePermissions, std::string& sError);
129129

130+
static std::string doPadLeft(const std::string& sIn, char cWhat, int nLength);
131+
static std::string doPadRight(const std::string& sIn, char cWhat, int nLength);
132+
130133
};
131134

132135

unit-tests.wsjcpp/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_list_of_dirs.h")
6767
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_list_of_dirs.cpp")
6868
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_file_permissions.h")
6969
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_file_permissions.cpp")
70+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_string_padding.h")
71+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_string_padding.cpp")
7072

7173
include(${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.user-custom.txt)
7274

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
#include "unit_test_string_padding.h"
3+
#include <wsjcpp_core.h>
4+
5+
// ---------------------------------------------------------------------
6+
// UnitTestStringPadding
7+
8+
REGISTRY_WSJCPP_UNIT_TEST(UnitTestStringPadding)
9+
10+
UnitTestStringPadding::UnitTestStringPadding()
11+
: WsjcppUnitTestBase("UnitTestStringPadding") {
12+
}
13+
14+
// ---------------------------------------------------------------------
15+
16+
bool UnitTestStringPadding::doBeforeTest() {
17+
// nothing
18+
return true;
19+
}
20+
21+
// ---------------------------------------------------------------------
22+
23+
void UnitTestStringPadding::executeTest() {
24+
std::string sIn = "some";
25+
std::string sResult = WsjcppCore::doPadLeft(sIn, '_', 6);
26+
compare("pad left", sResult, "__some");
27+
std::string sResult2 = WsjcppCore::doPadRight(sIn, '_', 6);
28+
compare("pad left", sResult2, "some__");
29+
}
30+
31+
// ---------------------------------------------------------------------
32+
33+
bool UnitTestStringPadding::doAfterTest() {
34+
// nothing
35+
return true;
36+
}
37+
38+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef UNIT_TEST_STRING_PADDING_H
2+
#define UNIT_TEST_STRING_PADDING_H
3+
4+
#include <wsjcpp_unit_tests.h>
5+
6+
class UnitTestStringPadding : public WsjcppUnitTestBase {
7+
public:
8+
UnitTestStringPadding();
9+
virtual bool doBeforeTest() override;
10+
virtual void executeTest() override;
11+
virtual bool doAfterTest() override;
12+
};
13+
14+
#endif // UNIT_TEST_STRING_PADDING_H

wsjcpp.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,5 @@ unit-tests:
8484
description: "Check list of directories"
8585
- name: "FilePermissions"
8686
description: ""
87+
- name: "StringPadding"
88+
description: ""

0 commit comments

Comments
 (0)