Skip to content

Commit 835a1ab

Browse files
committed
Added decodeUriComponent, encodeUriComponent, unit2HexString, replaceAll
1 parent f367175 commit 835a1ab

16 files changed

+443
-7
lines changed

.travis.yml

+5-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ addons:
1616

1717
# Build steps
1818
script:
19+
- mkdir -p tmp
20+
- cd tmp
21+
- make
22+
- cd ../unit-tests.wsjcpp
1923
- ./build_simple.sh
20-
- cd unit-tests.wsjcpp
21-
- ./build_simple.sh
22-
- ./run_unit-tests.sh
24+
- ./run_unit-tests.sh

README.md

+43-2
Original file line numberDiff line numberDiff line change
@@ -203,18 +203,28 @@ static std::string& rtrim(std::string& str, const std::string& chars = "\t\n\v\f
203203
static std::string& trim(std::string& str, const std::string& chars = "\t\n\v\f\r ");
204204
```
205205

206-
### to_lower
206+
### toLower
207207

208208
```
209-
static std::string& to_lower(std::string& str);
209+
static std::string toLower(const std::string& str);
210210
```
211211

212+
Convert text to lower charaters, like "ABc" -> "abc". Worked only with latin alphabet.
213+
212214
### toUpper
213215

214216
```
215217
static std::string toUpper(const std::string& str);
216218
```
217219

220+
Convert text to upper charaters like "abC" -> "ABC". Worked only with latin alphabet.
221+
222+
### replaceAll
223+
224+
```
225+
WSJCppCore::replaceAll(std::string& str, const std::string& from, const std::string& to);
226+
```
227+
218228
### createUuid
219229

220230
Generate uuid, but you need to call `WSJCppCore::initRandom();` before it (for example in main() function)
@@ -231,6 +241,21 @@ Example output:
231241
b49d92ae-f11c-f8bc-3a94-e7519e341927
232242
```
233243

244+
### uint2hexString
245+
`unsigned int` to hex string (lowercase)
246+
247+
```
248+
std::cout << WSJCppCore::uint2hexString(1) << std::endl;
249+
std::cout << WSJCppCore::uint2hexString(3000) << std::endl;
250+
std::cout << WSJCppCore::uint2hexString(4123123123) << std::endl;
251+
```
252+
253+
Example output
254+
```
255+
00000001
256+
00000bb8
257+
f5c1ddb3
258+
```
234259
### convertVoidToULong
235260

236261
```
@@ -278,3 +303,19 @@ Example output:
278303
```
279304
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
280305
```
306+
307+
### encodeUriComponent
308+
309+
Simular a js function `encodeURIComponent`
310+
311+
```
312+
static std::string encodeUriComponent(const std::string& sValue);
313+
```
314+
315+
### decodeUriComponent
316+
317+
Simular a js function `decodeURIComponent`
318+
319+
```
320+
static std::string decodeUriComponent(const std::string& sValue);
321+
```

src/wsjcpp_core.cpp

+79
Original file line numberDiff line numberDiff line change
@@ -385,13 +385,23 @@ std::string& WSJCppCore::trim(std::string& str, const std::string& chars) {
385385
// ---------------------------------------------------------------------
386386

387387
std::string& WSJCppCore::to_lower(std::string& str) {
388+
WSJCppLog::warn("WSJCppCore::to_lower", "Deprecated function, please use ");
388389
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
389390
return str;
390391
}
391392

392393
// ---------------------------------------------------------------------
393394
// will worked only with latin
394395

396+
std::string WSJCppCore::toLower(const std::string& str) {
397+
std::string sRet = str;
398+
std::transform(sRet.begin(), sRet.end(), sRet.begin(), ::tolower);
399+
return sRet;
400+
}
401+
402+
// ---------------------------------------------------------------------
403+
// will worked only with latin
404+
395405
std::string WSJCppCore::toUpper(const std::string& str) {
396406
std::string sRet = str;
397407
std::transform(sRet.begin(), sRet.end(), sRet.begin(), ::toupper);
@@ -400,6 +410,19 @@ std::string WSJCppCore::toUpper(const std::string& str) {
400410

401411
// ---------------------------------------------------------------------
402412

413+
void WSJCppCore::replaceAll(std::string& str, const std::string& sFrom, const std::string& sTo) {
414+
if (sFrom.empty()) {
415+
return;
416+
}
417+
size_t start_pos = 0;
418+
while ((start_pos = str.find(sFrom, start_pos)) != std::string::npos) {
419+
str.replace(start_pos, sFrom.length(), sTo);
420+
start_pos += sTo.length(); // In case 'to' contains 'sFrom', like replacing 'x' with 'yx'
421+
}
422+
}
423+
424+
// ---------------------------------------------------------------------
425+
403426
void WSJCppCore::initRandom() {
404427
std::srand(std::rand() + std::time(0));
405428
}
@@ -421,6 +444,17 @@ std::string WSJCppCore::createUuid() {
421444

422445
// ---------------------------------------------------------------------
423446

447+
std::string WSJCppCore::uint2hexString(unsigned int n) {
448+
std::string sRet;
449+
for (int i = 0; i < 8; i++) {
450+
sRet += "0123456789abcdef"[n % 16];
451+
n >>= 4;
452+
}
453+
return std::string(sRet.rbegin(), sRet.rend());
454+
}
455+
456+
// ---------------------------------------------------------------------
457+
424458
unsigned long WSJCppCore::convertVoidToULong(void *p) {
425459
std::uintptr_t ret = reinterpret_cast<std::uintptr_t>(p);
426460
return (unsigned long)ret;
@@ -457,6 +491,51 @@ bool WSJCppCore::getEnv(const std::string& sName, std::string& sValue) {
457491
return false;
458492
}
459493

494+
// ---------------------------------------------------------------------
495+
496+
std::string WSJCppCore::encodeUriComponent(const std::string& sValue) {
497+
std::stringstream ssRet;
498+
for (int i = 0; i < sValue.length(); i++) {
499+
char c = sValue[i];
500+
if (
501+
c == '-' || c == '_' || c == '.' || c == '!'
502+
|| c == '~' || c == '*' || c == '\''
503+
|| c == '(' || c == ')' || (c >= '0' && c <= '9')
504+
|| (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
505+
) {
506+
ssRet << c;
507+
} else {
508+
ssRet << '%' << std::uppercase << std::hex << (int)c;
509+
}
510+
}
511+
return ssRet.str();
512+
}
513+
514+
// ---------------------------------------------------------------------
515+
516+
std::string WSJCppCore::decodeUriComponent(const std::string& sValue) {
517+
std::string sRet = "";
518+
std::string sHex = "";
519+
int nLen = sValue.length();
520+
for (int i = 0; i < sValue.length(); i++) {
521+
char c = sValue[i];
522+
if (c == '%') {
523+
if (i+2 >= nLen) {
524+
WSJCppLog::throw_err("WSJCppCore::decodeUriElement", "Wrong format of string");
525+
}
526+
sHex = "0x";
527+
sHex += sValue[i+1];
528+
sHex += sValue[i+2];
529+
i = i + 2;
530+
char c1 = std::stoul(sHex, nullptr, 16);
531+
sRet += c1;
532+
} else {
533+
sRet += c;
534+
}
535+
}
536+
return sRet;
537+
}
538+
460539
// ---------------------------------------------------------------------
461540
// WSJCppLog
462541

src/wsjcpp_core.h

+7-2
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,21 @@ 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);
47+
static std::string& to_lower(std::string& str); // deprecated
48+
static std::string toLower(const std::string &str);
4849
static std::string toUpper(const std::string& str);
50+
static void replaceAll(std::string& str, const std::string& from, const std::string& to);
4951

5052
static void initRandom();
5153
static std::string createUuid();
52-
54+
static std::string uint2hexString(unsigned int n);
5355
static unsigned long convertVoidToULong(void *p);
5456
static std::string getPointerAsHex(void *p);
5557
static std::string extractURLProtocol(const std::string& sValue);
5658
static bool getEnv(const std::string& sName, std::string& sValue);
59+
60+
static std::string encodeUriComponent(const std::string& sValue);
61+
static std::string decodeUriComponent(const std::string& sValue);
5762
};
5863

5964

unit-tests.wsjcpp/CMakeLists.txt

+10
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_create_uuid.h")
4141
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_create_uuid.cpp")
4242
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_get_env.h")
4343
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_get_env.cpp")
44+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_to_lower.h")
45+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_to_lower.cpp")
46+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_replace_all.h")
47+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_replace_all.cpp")
48+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_decode_uri_component.h")
49+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_decode_uri_component.cpp")
50+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_encode_uri_component.h")
51+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_encode_uri_component.cpp")
52+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_uint2_hex_string.h")
53+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_uint2_hex_string.cpp")
4454

4555
include(${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.user-custom.txt)
4656

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include "unit_test_decode_uri_component.h"
2+
#include <vector>
3+
#include <wsjcpp_core.h>
4+
5+
REGISTRY_UNIT_TEST(UnitTestDecodeUriComponent)
6+
7+
UnitTestDecodeUriComponent::UnitTestDecodeUriComponent()
8+
: WSJCppUnitTestBase("UnitTestDecodeUriComponent") {
9+
}
10+
11+
// ---------------------------------------------------------------------
12+
13+
void UnitTestDecodeUriComponent::init() {
14+
// nothing
15+
}
16+
17+
// ---------------------------------------------------------------------
18+
19+
bool UnitTestDecodeUriComponent::run() {
20+
bool bTestSuccess = true;
21+
22+
struct LTest {
23+
LTest(
24+
const std::string &sStr,
25+
const std::string &sExpectedStr
26+
) {
27+
this->sStr = sStr;
28+
this->sExpectedStr = sExpectedStr;
29+
};
30+
std::string sStr;
31+
std::string sExpectedStr;
32+
};
33+
std::vector<LTest> tests;
34+
tests.push_back(LTest("Some%20long%20string", "Some long string"));
35+
tests.push_back(LTest("-_.!~*'()%20abc123", "-_.!~*'() abc123"));
36+
tests.push_back(LTest("%3B%2C%2F%3F%3A%40%26%3D%2B%24%22%23", ";,/?:@&=+$\"#"));
37+
38+
for (int i = 0; i < tests.size(); i++) {
39+
LTest test = tests[i];
40+
std::string sStr = WSJCppCore::decodeUriComponent(test.sStr);
41+
compareS(bTestSuccess, "test" + std::to_string(i), sStr, test.sExpectedStr);
42+
}
43+
44+
return bTestSuccess;
45+
}
46+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef UNIT_TEST_DECODE_URI_COMPONENT_H
2+
#define UNIT_TEST_DECODE_URI_COMPONENT_H
3+
4+
#include <wsjcpp_unit_tests.h>
5+
6+
// Description: TODO
7+
class UnitTestDecodeUriComponent : public WSJCppUnitTestBase {
8+
public:
9+
UnitTestDecodeUriComponent();
10+
virtual void init();
11+
virtual bool run();
12+
};
13+
14+
#endif // UNIT_TEST_DECODE_URI_COMPONENT_H
15+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include "unit_test_encode_uri_component.h"
2+
#include <vector>
3+
#include <wsjcpp_core.h>
4+
5+
REGISTRY_UNIT_TEST(UnitTestEncodeUriComponent)
6+
7+
UnitTestEncodeUriComponent::UnitTestEncodeUriComponent()
8+
: WSJCppUnitTestBase("UnitTestEncodeUriComponent") {
9+
}
10+
11+
// ---------------------------------------------------------------------
12+
13+
void UnitTestEncodeUriComponent::init() {
14+
// nothing
15+
}
16+
17+
// ---------------------------------------------------------------------
18+
19+
bool UnitTestEncodeUriComponent::run() {
20+
bool bTestSuccess = true;
21+
22+
struct LTest {
23+
LTest(
24+
const std::string &sStr,
25+
const std::string &sExpectedStr
26+
) {
27+
this->sStr = sStr;
28+
this->sExpectedStr = sExpectedStr;
29+
};
30+
std::string sStr;
31+
std::string sExpectedStr;
32+
};
33+
std::vector<LTest> tests;
34+
tests.push_back(LTest("Some long string", "Some%20long%20string"));
35+
tests.push_back(LTest("-_.!~*'() abc123", "-_.!~*'()%20abc123"));
36+
tests.push_back(LTest(";,/?:@&=+$\"#", "%3B%2C%2F%3F%3A%40%26%3D%2B%24%22%23"));
37+
38+
for (int i = 0; i < tests.size(); i++) {
39+
LTest test = tests[i];
40+
std::string sStr = WSJCppCore::encodeUriComponent(test.sStr);
41+
compareS(bTestSuccess, "test" + std::to_string(i), sStr, test.sExpectedStr);
42+
}
43+
44+
return bTestSuccess;
45+
}
46+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef UNIT_TEST_ENCODE_URI_COMPONENT_H
2+
#define UNIT_TEST_ENCODE_URI_COMPONENT_H
3+
4+
#include <wsjcpp_unit_tests.h>
5+
6+
// Description: TODO
7+
class UnitTestEncodeUriComponent : public WSJCppUnitTestBase {
8+
public:
9+
UnitTestEncodeUriComponent();
10+
virtual void init();
11+
virtual bool run();
12+
};
13+
14+
#endif // UNIT_TEST_ENCODE_URI_COMPONENT_H
15+

0 commit comments

Comments
 (0)