1111
1212namespace Utils
1313{
14+ void toLower (std::string &str, const std::locale &loc)
15+ {
16+ for (auto &c : str)
17+ {
18+ c = std::tolower (c, loc);
19+ }
20+ }
21+
1422 void trim (std::string &str)
1523 {
16- const size_t last = str.find_last_not_of (" \t\n\v\f\r " );
24+ static const char whitespace[] = {" \t\n\v\f\r " };
25+
26+ const size_t last = str.find_last_not_of (whitespace);
1727
1828 if (std::string::npos == last)
1929 {
2030 return str.clear ();
2131 }
2232
23- str.assign (str.cbegin () + str.find_first_not_of (" \t\n\v\f\r " ), str.cbegin () + last + 1 );
33+ str.assign (str.cbegin () + str.find_first_not_of (whitespace ), str.cbegin () + last + 1 );
2434 }
2535
2636 std::vector<std::string> explode (const std::string &str, const char sep)
@@ -68,7 +78,7 @@ namespace Utils
6878 return buf;
6979 }
7080
71- std::string binToHexString (const char *binData, const size_t dataSize)
81+ std::string binToHexString (const void *binData, const size_t dataSize)
7282 {
7383 std::string str (dataSize * 2 , 0 );
7484
@@ -109,8 +119,8 @@ namespace Utils
109119
110120 for (size_t i = 0 ; i < bin.length (); ++i)
111121 {
112- char a = hexStr[(i << 1 ) + 0 ];
113- char b = hexStr[(i << 1 ) + 1 ];
122+ const char a = hexStr[(i << 1 ) + 0 ];
123+ const char b = hexStr[(i << 1 ) + 1 ];
114124
115125 bin[i] = (
116126 (hexStringToBinEncodeSymbol (a) << 4 ) | hexStringToBinEncodeSymbol (b)
@@ -164,7 +174,7 @@ namespace Utils
164174
165175 time = htonll (time);
166176
167- return binToHexString (reinterpret_cast < const char *>( &time) , sizeof (time) );
177+ return binToHexString (&time, sizeof (time) );
168178 }
169179
170180 char *stlStringToPChar (const std::string &str)
@@ -254,24 +264,19 @@ namespace Utils
254264
255265 time_t stringTimeToTimestamp (const std::string &strTime)
256266 {
257- /* static const std::unordered_map<std::string, int> map_days {
258- {"Sun", 0}, {"Mon", 1}, {"Tue", 2}, {"Wed", 3}, {"Thu", 4}, {"Fri", 5}, {"Sat", 6}
259- };*/
260-
261267 static const std::unordered_map<std::string, int > map_months {
262268 {" Jan" , 0 }, {" Feb" , 1 }, {" Mar" , 2 }, {" Apr" , 3 }, {" May" , 4 }, {" Jun" , 5 }, {" Jul" , 6 }, {" Aug" , 7 }, {" Sep" , 8 }, {" Oct" , 9 }, {" Nov" , 10 }, {" Dec" , 11 }
263269 };
264270
265271 if (strTime.length () > 64 )
266272 {
267- return ( time_t ) ~0 ;
273+ return ~0 ;
268274 }
269275
270276 const size_t str_mon_length = 64 ;
271277 std::vector<char > s_mon (str_mon_length);
272278
273- struct ::tm tc;
274- memset (&tc, 0 , sizeof (tc) );
279+ struct ::tm tc = {};
275280
276281 // Parse RFC 822
277282 #ifdef WIN32
@@ -301,27 +306,24 @@ namespace Utils
301306
302307 ::time_t cur_time = tTime;
303308
304- if ( ( time_t )~ 0 == tTime )
309+ if (tTime == ~ 0 )
305310 {
306311 ::time (&cur_time);
307312 }
308313
309314 #ifdef WIN32
310- struct ::tm stm = {0 };
315+ struct ::tm stm = {};
311316
312- if (isGmtTime)
313- {
314- ::localtime_s (&stm, &cur_time);
315- }
316- else
317- {
317+ isGmtTime ?
318+ ::localtime_s (&stm, &cur_time) :
318319 ::gmtime_s(&stm, &cur_time);
319- }
320320
321321 // RFC 822
322322 ::strftime (buf.data(), buf.size(), "%a, %d %b %Y %H:%M:%S GMT", &stm);
323323 #else
324- struct ::tm *ptm = isGmtTime ? localtime (&cur_time) : gmtime (&cur_time);
324+ struct ::tm *ptm = isGmtTime ?
325+ ::localtime (&cur_time) :
326+ ::gmtime(&cur_time);
325327
326328 // RFC 822
327329 ::strftime (buf.data(), buf.size(), "%a, %d %b %G %H:%M:%S GMT", ptm);
@@ -350,7 +352,7 @@ namespace Utils
350352 {
351353 if (cookieHeader.empty () )
352354 {
353- return false ;
355+ return true ;
354356 }
355357
356358 for (size_t cur_pos = 0 , next_value; std::string::npos != cur_pos; cur_pos = next_value)
@@ -385,7 +387,7 @@ namespace Utils
385387 return true ;
386388 }
387389
388- inline bool isUrlAllowed (const std::string::value_type c)
390+ inline bool isUrlAllowed (const char c)
389391 {
390392 static const std::string special (" -_.~" );
391393
@@ -400,7 +402,7 @@ namespace Utils
400402
401403 for (auto it = str.cbegin (); str.cend () != it; ++it)
402404 {
403- const std::string::value_type &c = *it;
405+ const char &c = *it;
404406
405407 if (' ' == c)
406408 {
@@ -423,11 +425,12 @@ namespace Utils
423425 {
424426 std::string decoded;
425427
426- std::string::value_type ch[3 ] = {0 };
428+ // ch length must be >= 3
429+ std::array<char , sizeof (size_t )> ch;
427430
428431 for (auto it = str.cbegin (); str.cend () != it; ++it)
429432 {
430- const std::string::value_type &c = *it;
433+ const char &c = *it;
431434
432435 if (' %' == c)
433436 {
@@ -449,7 +452,7 @@ namespace Utils
449452
450453 ch[1 ] = *it;
451454
452- decoded.push_back (strtoul (ch, nullptr , 16 ) );
455+ decoded.push_back (strtoul (ch. data () , nullptr , 16 ) );
453456 }
454457 else if (' +' == c)
455458 {
0 commit comments