@@ -385,13 +385,23 @@ std::string& WSJCppCore::trim(std::string& str, const std::string& chars) {
385
385
// ---------------------------------------------------------------------
386
386
387
387
std::string& WSJCppCore::to_lower (std::string& str) {
388
+ WSJCppLog::warn (" WSJCppCore::to_lower" , " Deprecated function, please use " );
388
389
std::transform (str.begin (), str.end (), str.begin (), ::tolower);
389
390
return str;
390
391
}
391
392
392
393
// ---------------------------------------------------------------------
393
394
// will worked only with latin
394
395
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
+
395
405
std::string WSJCppCore::toUpper (const std::string& str) {
396
406
std::string sRet = str;
397
407
std::transform (sRet .begin (), sRet .end (), sRet .begin (), ::toupper);
@@ -400,6 +410,19 @@ std::string WSJCppCore::toUpper(const std::string& str) {
400
410
401
411
// ---------------------------------------------------------------------
402
412
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
+
403
426
void WSJCppCore::initRandom () {
404
427
std::srand (std::rand () + std::time (0 ));
405
428
}
@@ -421,6 +444,17 @@ std::string WSJCppCore::createUuid() {
421
444
422
445
// ---------------------------------------------------------------------
423
446
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
+
424
458
unsigned long WSJCppCore::convertVoidToULong (void *p) {
425
459
std::uintptr_t ret = reinterpret_cast <std::uintptr_t >(p);
426
460
return (unsigned long )ret;
@@ -457,6 +491,51 @@ bool WSJCppCore::getEnv(const std::string& sName, std::string& sValue) {
457
491
return false ;
458
492
}
459
493
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
+
460
539
// ---------------------------------------------------------------------
461
540
// WSJCppLog
462
541
0 commit comments