@@ -21,16 +21,16 @@ namespace Utils
2121
2222 void trim (std::string &str)
2323 {
24- static const char whitespace[] = { " \t\n\v\f\r " };
24+ static const std::array< char , 7 > whitespace { " \t\n\v\f\r " };
2525
26- const size_t last = str.find_last_not_of (whitespace);
26+ const size_t last = str.find_last_not_of (whitespace. data () );
2727
2828 if (std::string::npos == last)
2929 {
3030 return str.clear ();
3131 }
3232
33- str.assign (str.cbegin () + str.find_first_not_of (whitespace), str.cbegin () + last + 1 );
33+ str.assign (str.cbegin () + str.find_first_not_of (whitespace. data () ), str.cbegin () + last + 1 );
3434 }
3535
3636 std::vector<std::string> explode (const std::string &str, const char sep)
@@ -84,12 +84,12 @@ namespace Utils
8484
8585 const uint8_t *bin = reinterpret_cast <const uint8_t *>(binData);
8686
87- const char hexDigits[] = { " 0123456789abcdef" };
87+ static const std::array< char , 17 > hexDigits { " 0123456789abcdef" };
8888
8989 for (size_t i = dataSize - 1 ; std::numeric_limits<size_t >::max () != i; --i)
9090 {
91- str[(i << 1 ) + 0 ] = hexDigits[bin[i] >> 4 ];
92- str[(i << 1 ) + 1 ] = hexDigits[bin[i] & 0x0F ];
91+ str[i * 2 + 0 ] = hexDigits[bin[i] >> 4 ];
92+ str[i * 2 + 1 ] = hexDigits[bin[i] & 0x0F ];
9393 }
9494
9595 return str;
@@ -119,8 +119,8 @@ namespace Utils
119119
120120 for (size_t i = 0 ; i < bin.length (); ++i)
121121 {
122- const char a = hexStr[(i << 1 ) + 0 ];
123- const char b = hexStr[(i << 1 ) + 1 ];
122+ const char a = hexStr[i * 2 + 0 ];
123+ const char b = hexStr[i * 2 + 1 ];
124124
125125 bin[i] = (
126126 (hexStringToBinEncodeSymbol (a) << 4 ) | hexStringToBinEncodeSymbol (b)
@@ -387,81 +387,71 @@ namespace Utils
387387 return true ;
388388 }
389389
390- inline bool isUrlAllowed (const char c)
390+ static inline bool isCharUrlAllowed (const char c)
391391 {
392- static const std::string special (" -_.~" );
393-
394- return std::string::npos != special.find (c);
392+ return c == ' -' || c == ' _' || c == ' .' || c == ' ~' ;
395393 }
396394
397395 std::string urlEncode (const std::string &str)
398396 {
399- std::ostringstream encoded;
400- encoded. fill ( ' 0 ' );
401- encoded << std::hex ;
397+ std::string encoded;
398+
399+ static const std::array< char , 17 > hexDigits { " 0123456789abcdef " } ;
402400
403- for (auto it = str. cbegin (); str.cend () != it ; ++it )
401+ for (size_t i = 0 ; i < str.length () ; ++i )
404402 {
405- const char & c = *it ;
403+ const unsigned char c = str[i] ;
406404
407- if (' ' == c )
405+ if (std::isalnum (c) || isCharUrlAllowed (c) )
408406 {
409- encoded << ' + ' ;
407+ encoded. push_back (c) ;
410408 }
411- else if (std::isalnum (c) || isUrlAllowed (c) )
409+ else if (' ' == c )
412410 {
413- encoded << c ;
411+ encoded. push_back ( ' + ' ) ;
414412 }
415413 else
416414 {
417- encoded << ' %' << std::setw (2 ) << (int ) ( (unsigned char ) c);
415+ const uint8_t a = c >> 4 ;
416+ const uint8_t b = c & 0x0F ;
417+
418+ encoded.push_back (' %' );
419+ encoded.push_back (hexDigits[a]);
420+ encoded.push_back (hexDigits[b]);
418421 }
419422 }
420423
421- return encoded. str () ;
424+ return encoded;
422425 }
423426
424427 std::string urlDecode (const std::string &str)
425428 {
426429 std::string decoded;
427430
428- // ch length must be >= 3
429- std::array<char , sizeof (size_t )> ch;
430-
431- for (auto it = str.cbegin (); str.cend () != it; ++it)
431+ for (size_t i = 0 ; i < str.length (); ++i)
432432 {
433- const char & c = *it ;
433+ unsigned char c = str[i] ;
434434
435435 if (' %' == c)
436436 {
437- ++it;
438-
439- if (str.cend () == it)
437+ if (i + 2 < str.length () )
440438 {
441- break ;
442- }
443-
444- ch[0 ] = *it;
439+ const char a = str[i + 1 ];
440+ const char b = str[i + 2 ];
445441
446- ++it;
442+ c = (
443+ (hexStringToBinEncodeSymbol (a) << 4 ) | hexStringToBinEncodeSymbol (b)
444+ );
447445
448- if (str.cend () == it)
449- {
450- break ;
446+ i += 2 ;
451447 }
452-
453- ch[1 ] = *it;
454-
455- decoded.push_back (strtoul (ch.data (), nullptr , 16 ) );
456448 }
457449 else if (' +' == c)
458450 {
459- decoded.push_back (' ' );
460- }
461- else
462- {
463- decoded.push_back (c);
451+ c = ' ' ;
464452 }
453+
454+ decoded.push_back (c);
465455 }
466456
467457 return decoded;
0 commit comments