Skip to content

Commit d8cd119

Browse files
committed
Fixed #5 add function WSJCppCore::split
1 parent 835a1ab commit d8cd119

File tree

9 files changed

+126
-13
lines changed

9 files changed

+126
-13
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ addons:
1818
script:
1919
- mkdir -p tmp
2020
- cd tmp
21+
- cmake ..
2122
- make
2223
- cd ../unit-tests.wsjcpp
2324
- ./build_simple.sh

README.md

+20
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,26 @@ Convert text to upper charaters like "abC" -> "ABC". Worked only with latin alph
225225
WSJCppCore::replaceAll(std::string& str, const std::string& from, const std::string& to);
226226
```
227227

228+
### replaceAll
229+
230+
```
231+
std::string sWhat = "|1a|2b|3c|4d|";
232+
std::vector<std::string> vSplitted = WSJCppCore::split(sWhat, "|");
233+
for (int i = 0; i < vSplitted.size(); i++) {
234+
std::cout << vSplitted[i] << std::endl;
235+
}
236+
```
237+
238+
Example output:
239+
```
240+
241+
1a
242+
2b
243+
3c
244+
4d
245+
246+
```
247+
228248
### createUuid
229249

230250
Generate uuid, but you need to call `WSJCppCore::initRandom();` before it (for example in main() function)

src.wsjcpp/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Automaticly generated by [email protected]
22
cmake_minimum_required(VERSION 3.0)
33

4-
add_definitions(-DWSJCPP_VERSION="v0.0.6")
4+
add_definitions(-DWSJCPP_VERSION="v0.0.7")
55
add_definitions(-DWSJCPP_NAME="wsjcpp-core")
66

77
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")

src/wsjcpp_core.cpp

+25-8
Original file line numberDiff line numberDiff line change
@@ -382,14 +382,6 @@ std::string& WSJCppCore::trim(std::string& str, const std::string& chars) {
382382
return WSJCppCore::ltrim(WSJCppCore::rtrim(str, chars), chars);
383383
}
384384

385-
// ---------------------------------------------------------------------
386-
387-
std::string& WSJCppCore::to_lower(std::string& str) {
388-
WSJCppLog::warn("WSJCppCore::to_lower", "Deprecated function, please use ");
389-
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
390-
return str;
391-
}
392-
393385
// ---------------------------------------------------------------------
394386
// will worked only with latin
395387

@@ -423,6 +415,31 @@ void WSJCppCore::replaceAll(std::string& str, const std::string& sFrom, const st
423415

424416
// ---------------------------------------------------------------------
425417

418+
std::vector<std::string> WSJCppCore::split(const std::string& sWhat, const std::string& sDelim) {
419+
std::vector<std::string> vRet;
420+
int nPos = 0;
421+
int nLen = sWhat.length();
422+
int nDelimLen = sDelim.length();
423+
while (nPos < nLen) {
424+
std::size_t nFoundPos = sWhat.find(sDelim, nPos);
425+
if (nFoundPos != std::string::npos) {
426+
std::string sToken = sWhat.substr(nPos, nFoundPos - nPos);
427+
vRet.push_back(sToken);
428+
nPos = nFoundPos + nDelimLen;
429+
if (nFoundPos + nDelimLen == nLen) { // last delimiter
430+
vRet.push_back("");
431+
}
432+
} else {
433+
std::string sToken = sWhat.substr(nPos, nLen - nPos);
434+
vRet.push_back(sToken);
435+
break;
436+
}
437+
}
438+
return vRet;
439+
}
440+
441+
// ---------------------------------------------------------------------
442+
426443
void WSJCppCore::initRandom() {
427444
std::srand(std::rand() + std::time(0));
428445
}

src/wsjcpp_core.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ class WSJCppCore {
4444
static std::string& ltrim(std::string& str, const std::string& chars = "\t\n\v\f\r ");
4545
static std::string& rtrim(std::string& str, const std::string& chars = "\t\n\v\f\r ");
4646
static std::string& trim(std::string& str, const std::string& chars = "\t\n\v\f\r ");
47-
static std::string& to_lower(std::string& str); // deprecated
4847
static std::string toLower(const std::string &str);
4948
static std::string toUpper(const std::string& str);
5049
static void replaceAll(std::string& str, const std::string& from, const std::string& to);
50+
static std::vector<std::string> split(const std::string& sWhat, const std::string& sDelim);
5151

5252
static void initRandom();
5353
static std::string createUuid();

unit-tests.wsjcpp/CMakeLists.txt

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
cmake_minimum_required(VERSION 3.0)
33

44
project(unit-tests C CXX)
5-
add_definitions(-DWSJCPP_VERSION="ut-v0.0.6")
5+
add_definitions(-DWSJCPP_VERSION="ut-v0.0.7")
66
add_definitions(-DWSJCPP_NAME="unit-tests-wsjcpp-core")
77

88
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
@@ -19,7 +19,7 @@ set (WSJCPP_SOURCES "")
1919
find_package(Threads REQUIRED)
2020
list (APPEND WSJCPP_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
2121

22-
# wsjcpp-core:v0.0.6
22+
# wsjcpp-core:v0.0.7
2323
list (APPEND WSJCPP_INCLUDE_DIRS "../src")
2424
list (APPEND WSJCPP_SOURCES "../src/wsjcpp_core.cpp")
2525
list (APPEND WSJCPP_SOURCES "../src/wsjcpp_core.h")
@@ -51,6 +51,8 @@ list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_encode_uri_compo
5151
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_encode_uri_component.cpp")
5252
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_uint2_hex_string.h")
5353
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_uint2_hex_string.cpp")
54+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_split.h")
55+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_split.cpp")
5456

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

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include "unit_test_split.h"
2+
#include <vector>
3+
#include <wsjcpp_core.h>
4+
5+
REGISTRY_UNIT_TEST(UnitTestSplit)
6+
7+
UnitTestSplit::UnitTestSplit()
8+
: WSJCppUnitTestBase("UnitTestSplit") {
9+
}
10+
11+
// ---------------------------------------------------------------------
12+
13+
void UnitTestSplit::init() {
14+
// nothing
15+
}
16+
17+
// ---------------------------------------------------------------------
18+
19+
bool UnitTestSplit::run() {
20+
bool bTestSuccess = true;
21+
22+
struct LTest {
23+
LTest(
24+
const std::string &sStr,
25+
const std::string &sDelim,
26+
const std::vector<std::string> &vExpectedVector
27+
) {
28+
this->sStr = sStr;
29+
this->sDelim = sDelim;
30+
this->vExpectedVector = vExpectedVector;
31+
};
32+
std::string sStr;
33+
std::string sDelim;
34+
std::vector<std::string> vExpectedVector;
35+
};
36+
std::vector<LTest> tests;
37+
tests.push_back(LTest("1 2 3 4 5", " ", {"1", "2", "3", "4", "5"}));
38+
tests.push_back(LTest("|1f|2п|3%^|44354|5kdasjfdre|2", "|", {"", "1f", "2п", "3%^", "44354", "5kdasjfdre", "2"}));
39+
tests.push_back(LTest("|1f|2п|3%^|44354|5kdasjfdre|", "|", {"", "1f", "2п", "3%^", "44354", "5kdasjfdre", ""}));
40+
tests.push_back(LTest("some1 => some2 => some3", "=>", {"some1 ", " some2 ", " some3"}));
41+
tests.push_back(LTest("some1 => some2 => some3 =>", "=>", {"some1 ", " some2 ", " some3 ", ""}));
42+
43+
for (int i = 0; i < tests.size(); i++) {
44+
LTest test = tests[i];
45+
std::string sPrefix = "test" + std::to_string(i) + "(\"" + test.sStr + "\")";
46+
std::vector<std::string> vSplitted = WSJCppCore::split(test.sStr, test.sDelim);
47+
compareN(bTestSuccess, sPrefix + ": size", vSplitted.size(), test.vExpectedVector.size());
48+
int nMin = std::min(vSplitted.size(), test.vExpectedVector.size());
49+
for (int n = 0; n < nMin; n++) {
50+
compareS(bTestSuccess, sPrefix + ", element: " + std::to_string(n), vSplitted[n], test.vExpectedVector[n]);
51+
}
52+
}
53+
54+
return bTestSuccess;
55+
}
56+
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef UNIT_TEST_SPLIT_H
2+
#define UNIT_TEST_SPLIT_H
3+
4+
#include <wsjcpp_unit_tests.h>
5+
6+
// Description: TODO
7+
class UnitTestSplit : public WSJCppUnitTestBase {
8+
public:
9+
UnitTestSplit();
10+
virtual void init();
11+
virtual bool run();
12+
};
13+
14+
#endif // UNIT_TEST_SPLIT_H
15+

wsjcpp.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ cmake_cxx_standard: 11
33
cmake_minimum_required: 3.0
44

55
name: wsjcpp-core
6-
version: v0.0.6
6+
version: v0.0.7
77
description: Basic Utils for wsjcpp
88
issues: https://github.com/wsjcpp/wsjcpp-core/issues
99
repositories:
@@ -58,3 +58,5 @@ unit-tests:
5858
description: "Check encoding"
5959
- name: "Uint2HexString"
6060
description: "Test convert unsigned int to hex string"
61+
- name: "Split"
62+
description: "Test split function"

0 commit comments

Comments
 (0)