Changeset 11111 for code/trunk
- Timestamp:
- Feb 12, 2016, 1:05:41 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/libraries/util/StringUtils.cc
r11097 r11111 34 34 #include "StringUtils.h" 35 35 36 #include <algorithm> 36 37 #include <cctype> 37 38 #include <ctime> … … 54 55 void strip(std::string* str) 55 56 { 56 size_t pos; 57 while ((pos = str->find(' ')) < str->length()) 58 str->erase(pos, 1); 59 while ((pos = str->find('\t')) < str->length()) 60 str->erase(pos, 1); 61 while ((pos = str->find('\n')) < str->length()) 62 str->erase(pos, 1); 57 str->erase(std::remove_if(str->begin(), str->end(), [](char val) { return std::isspace(val) != 0; }), str->end()); 63 58 } 64 59 … … 74 69 std::string removeTrailingWhitespaces(const std::string& str) 75 70 { 76 size_t pos1 = 0; 77 int pos2 = static_cast<int>(str.size() - 1); 78 for (; pos1 < str.size() && (str[pos1] == ' ' || str[pos1] == '\t' || str[pos1] == '\n'); pos1++); 79 for (; pos2 > 0 && (str[pos2] == ' ' || str[pos2] == '\t' || str[pos2] == '\n'); pos2--); 80 return str.substr(pos1, pos2 - pos1 + 1); 71 auto pos1 = std::find_if(str.begin(), str.end(), [](char val) { return std::isspace(val) == 0; }); 72 auto pos2 = std::find_if(str.rbegin(), str.rend(), [](char val) { return std::isspace(val) == 0; }); 73 if (pos1 == str.end() && pos2 == str.rend()) 74 { 75 // String doesn't have non-whitespace characters 76 return ""; 77 } 78 return std::string(pos1, pos2.base()); 81 79 } 82 80 … … 162 160 /** 163 161 @brief Removes enclosing quotation marks if available (including whitespaces at the outside of the quotation marks). 164 @return The strip ed string without quotation marks162 @return The stripped string without quotation marks 165 163 */ 166 164 std::string stripEnclosingQuotes(const std::string& str) 167 165 { 168 size_t start = std::string::npos; 169 size_t end = 0; 170 171 for (size_t pos = 0; (pos < str.size()) && (pos < std::string::npos); pos++) 172 { 173 if (str[pos] == '"') 174 { 175 start = pos; 176 break; 177 } 178 179 if ((str[pos] != ' ') && (str[pos] != '\t') && (str[pos] != '\n')) 166 auto start = str.begin(); 167 auto end = str.rbegin(); 168 169 for (; start != str.end() && *start != '"'; ++start) 170 { 171 if (std::isspace(*start) == 0) 180 172 return str; 181 173 } 182 174 183 for (size_t pos = str.size() - 1; pos < std::string::npos; pos--) 184 { 185 if (str[pos] == '"') 186 { 187 end = pos; 188 break; 189 } 190 191 if ((str[pos] != ' ') && (str[pos] != '\t') && (str[pos] != '\n')) 175 for (; end != str.rend() && *end != '"'; ++end) 176 { 177 if (std::isspace(*end) == 0) 192 178 return str; 193 179 } 194 180 195 if ((start != std::string::npos) && (end != 0)) 196 return str.substr(start + 1, end - start - 1); 181 if (start != str.end() && end != str.rend()) 182 { 183 ++start; // Skip " 184 ++end; // Skip " 185 return std::string(start, end.base()); 186 } 197 187 else 198 188 return str; … … 201 191 /** 202 192 @brief Removes enclosing braces '{' and '}' (the braces must be exactly on the beginning and the end of the string). 203 @return The strip ed string without braces193 @return The stripped string without braces 204 194 */ 205 195 std::string stripEnclosingBraces(const std::string& str) 206 196 { 207 std::string output = str; 208 209 while (output.size() >= 2 && output[0] == '{' && output[output.size() - 1] == '}') 210 output = output.substr(1, output.size() - 2); 211 212 return output; 197 if (str.empty()) 198 { 199 return ""; 200 } 201 202 auto start = str.begin(); 203 auto end = str.rbegin(); 204 205 while (*start == '{' && *end == '}') 206 { 207 ++start; 208 ++end; 209 } 210 211 return std::string(start, end.base()); 213 212 } 214 213 … … 245 244 bool isEmpty(const std::string& str) 246 245 { 247 return getStripped(str).empty();246 return std::all_of(str.begin(), str.end(), [](char val) { return std::isspace(val) != 0; }); 248 247 } 249 248 … … 337 336 void lowercase(std::string* str) 338 337 { 339 for (char& character : *str) 340 { 341 character = static_cast<char>(tolower(character)); 342 } 338 std::transform(str->begin(), str->end(), str->begin(), [](char val) { return std::tolower(val); }); 343 339 } 344 340 … … 354 350 void uppercase(std::string* str) 355 351 { 356 for (char& character : *str) 357 { 358 character = static_cast<char>(toupper(character)); 359 } 352 std::transform(str->begin(), str->end(), str->begin(), [](char val) { return std::toupper(val); }); 360 353 } 361 354 … … 370 363 /** 371 364 @brief Compares two strings ignoring different casing. 372 @return s1 == s 1-> returns 0 / s1 < s2 -> returns -1 / s1 >= s2 -> returns 1365 @return s1 == s2 -> returns 0 / s1 < s2 -> returns -1 / s1 >= s2 -> returns 1 373 366 */ 374 367 int nocaseCmp(const std::string& s1, const std::string& s2) 375 368 { 376 std::string::const_iterator it1=s1.begin(); 377 std::string::const_iterator it2=s2.begin(); 378 379 //stop when either string's end has been reached 380 while ( (it1!=s1.end()) && (it2!=s2.end()) ) 381 { 382 if(::toupper(*it1) != ::toupper(*it2)) //letters differ? 383 // return -1 to indicate smaller than, 1 otherwise 384 return (::toupper(*it1) < ::toupper(*it2)) ? -1 : 1; 385 //proceed to the next character in each string 386 ++it1; 387 ++it2; 388 } 389 size_t size1=s1.size(), size2=s2.size();// cache lengths 369 size_t size1 = s1.size(), size2 = s2.size(); // cache lengths 370 371 int res = nocaseCmp(s1, s2, std::min(size1, size2)); 372 373 if (res != 0) 374 { 375 return res; 376 } 377 390 378 //return -1,0 or 1 according to strings' lengths 391 if (size1 ==size2)379 if (size1 == size2) 392 380 return 0; 393 return (size1<size2) ? -1 : 1; 381 return (size1 < size2) ? -1 : 1; 382 394 383 } 395 384 … … 405 394 if (len == 0) 406 395 return 0; 407 std::string::const_iterator it1 =s1.begin();408 std::string::const_iterator it2 =s2.begin();396 std::string::const_iterator it1 = s1.begin(); 397 std::string::const_iterator it2 = s2.begin(); 409 398 410 399 //stop when either string's end has been reached 411 while ( (it1 !=s1.end()) && (it2!=s2.end()) && len-- > 0)412 { 413 if( ::toupper(*it1) !=::toupper(*it2)) //letters differ?400 while ( (it1 != s1.end()) && (it2 != s2.end()) && len-- > 0) 401 { 402 if(std::toupper(*it1) != std::toupper(*it2)) //letters differ? 414 403 // return -1 to indicate smaller than, 1 otherwise 415 return ( ::toupper(*it1) <::toupper(*it2)) ? -1 : 1;404 return (std::toupper(*it1) < std::toupper(*it2)) ? -1 : 1; 416 405 //proceed to the next character in each string 417 406 ++it1; … … 508 497 /** 509 498 @brief 510 Get a timestamp for the cur ent time instant.499 Get a timestamp for the current time instant. 511 500 @return 512 501 Returns a string with the timestamp. … … 514 503 std::string getTimestamp(void) 515 504 { 516 struct tm *pTime;517 505 time_t ctTime; std::time(&ctTime); 518 pTime = std::localtime( &ctTime);506 tm* pTime = std::localtime(&ctTime); 519 507 std::ostringstream oss; 520 oss << std::setw(2) << std::setfill('0') << (pTime->tm_mon + 1) 521 << std::setw(2) << std::setfill('0') << pTime->tm_mday 522 << std::setw(2) << std::setfill('0') << (pTime->tm_year + 1900) 523 << "_" << std::setw(2) << std::setfill('0') << pTime->tm_hour 524 << std::setw(2) << std::setfill('0') << pTime->tm_min 525 << std::setw(2) << std::setfill('0') << pTime->tm_sec; 508 oss << std::put_time(pTime, "%m%d%Y_%H%M%S"); 526 509 return oss.str(); 527 510 }
Note: See TracChangeset
for help on using the changeset viewer.