[Orxonox-commit 6456] r11111 - code/trunk/src/libraries/util
muemart at orxonox.net
muemart at orxonox.net
Fri Feb 12 13:05:42 CET 2016
Author: muemart
Date: 2016-02-12 13:05:41 +0100 (Fri, 12 Feb 2016)
New Revision: 11111
Modified:
code/trunk/src/libraries/util/StringUtils.cc
Log:
Some simplifications in StringUtils.cc
Modified: code/trunk/src/libraries/util/StringUtils.cc
===================================================================
--- code/trunk/src/libraries/util/StringUtils.cc 2016-02-07 17:47:15 UTC (rev 11110)
+++ code/trunk/src/libraries/util/StringUtils.cc 2016-02-12 12:05:41 UTC (rev 11111)
@@ -33,6 +33,7 @@
#include "StringUtils.h"
+#include <algorithm>
#include <cctype>
#include <ctime>
#include <memory>
@@ -53,13 +54,7 @@
/// Removes all whitespaces from a string.
void strip(std::string* str)
{
- size_t pos;
- while ((pos = str->find(' ')) < str->length())
- str->erase(pos, 1);
- while ((pos = str->find('\t')) < str->length())
- str->erase(pos, 1);
- while ((pos = str->find('\n')) < str->length())
- str->erase(pos, 1);
+ str->erase(std::remove_if(str->begin(), str->end(), [](char val) { return std::isspace(val) != 0; }), str->end());
}
/// Returns a copy of a string without whitespaces.
@@ -73,11 +68,14 @@
/// Returns a copy of a string without trailing whitespaces.
std::string removeTrailingWhitespaces(const std::string& str)
{
- size_t pos1 = 0;
- int pos2 = static_cast<int>(str.size() - 1);
- for (; pos1 < str.size() && (str[pos1] == ' ' || str[pos1] == '\t' || str[pos1] == '\n'); pos1++);
- for (; pos2 > 0 && (str[pos2] == ' ' || str[pos2] == '\t' || str[pos2] == '\n'); pos2--);
- return str.substr(pos1, pos2 - pos1 + 1);
+ auto pos1 = std::find_if(str.begin(), str.end(), [](char val) { return std::isspace(val) == 0; });
+ auto pos2 = std::find_if(str.rbegin(), str.rend(), [](char val) { return std::isspace(val) == 0; });
+ if (pos1 == str.end() && pos2 == str.rend())
+ {
+ // String doesn't have non-whitespace characters
+ return "";
+ }
+ return std::string(pos1, pos2.base());
}
/// Splits a given string by a delimiter and stores it in an output vector. See @ref SubString for a more sophisticated implementation.
@@ -161,55 +159,56 @@
/**
@brief Removes enclosing quotation marks if available (including whitespaces at the outside of the quotation marks).
- @return The striped string without quotation marks
+ @return The stripped string without quotation marks
*/
std::string stripEnclosingQuotes(const std::string& str)
{
- size_t start = std::string::npos;
- size_t end = 0;
+ auto start = str.begin();
+ auto end = str.rbegin();
- for (size_t pos = 0; (pos < str.size()) && (pos < std::string::npos); pos++)
+ for (; start != str.end() && *start != '"'; ++start)
{
- if (str[pos] == '"')
- {
- start = pos;
- break;
- }
-
- if ((str[pos] != ' ') && (str[pos] != '\t') && (str[pos] != '\n'))
+ if (std::isspace(*start) == 0)
return str;
}
- for (size_t pos = str.size() - 1; pos < std::string::npos; pos--)
+ for (; end != str.rend() && *end != '"'; ++end)
{
- if (str[pos] == '"')
- {
- end = pos;
- break;
- }
-
- if ((str[pos] != ' ') && (str[pos] != '\t') && (str[pos] != '\n'))
+ if (std::isspace(*end) == 0)
return str;
}
- if ((start != std::string::npos) && (end != 0))
- return str.substr(start + 1, end - start - 1);
+ if (start != str.end() && end != str.rend())
+ {
+ ++start; // Skip "
+ ++end; // Skip "
+ return std::string(start, end.base());
+ }
else
return str;
}
/**
@brief Removes enclosing braces '{' and '}' (the braces must be exactly on the beginning and the end of the string).
- @return The striped string without braces
+ @return The stripped string without braces
*/
std::string stripEnclosingBraces(const std::string& str)
{
- std::string output = str;
+ if (str.empty())
+ {
+ return "";
+ }
- while (output.size() >= 2 && output[0] == '{' && output[output.size() - 1] == '}')
- output = output.substr(1, output.size() - 2);
+ auto start = str.begin();
+ auto end = str.rbegin();
- return output;
+ while (*start == '{' && *end == '}')
+ {
+ ++start;
+ ++end;
+ }
+
+ return std::string(start, end.base());
}
/**
@@ -244,7 +243,7 @@
/// Determines if a string is empty (contains only whitespaces).
bool isEmpty(const std::string& str)
{
- return getStripped(str).empty();
+ return std::all_of(str.begin(), str.end(), [](char val) { return std::isspace(val) != 0; });
}
/**
@@ -336,10 +335,7 @@
/// Replaces each char between A and Z with its lowercase equivalent.
void lowercase(std::string* str)
{
- for (char& character : *str)
- {
- character = static_cast<char>(tolower(character));
- }
+ std::transform(str->begin(), str->end(), str->begin(), [](char val) { return std::tolower(val); });
}
/// Returns a copy of the given string where all chars are converted to lowercase.
@@ -353,10 +349,7 @@
/// Replaces each char between a and z with its uppercase equivalent.
void uppercase(std::string* str)
{
- for (char& character : *str)
- {
- character = static_cast<char>(toupper(character));
- }
+ std::transform(str->begin(), str->end(), str->begin(), [](char val) { return std::toupper(val); });
}
/// Returns a copy of the given string where all chars are converted to uppercase.
@@ -369,28 +362,24 @@
/**
@brief Compares two strings ignoring different casing.
- @return s1 == s1 -> returns 0 / s1 < s2 -> returns -1 / s1 >= s2 -> returns 1
+ @return s1 == s2 -> returns 0 / s1 < s2 -> returns -1 / s1 >= s2 -> returns 1
*/
int nocaseCmp(const std::string& s1, const std::string& s2)
{
- std::string::const_iterator it1=s1.begin();
- std::string::const_iterator it2=s2.begin();
+ size_t size1 = s1.size(), size2 = s2.size(); // cache lengths
- //stop when either string's end has been reached
- while ( (it1!=s1.end()) && (it2!=s2.end()) )
+ int res = nocaseCmp(s1, s2, std::min(size1, size2));
+
+ if (res != 0)
{
- if(::toupper(*it1) != ::toupper(*it2)) //letters differ?
- // return -1 to indicate smaller than, 1 otherwise
- return (::toupper(*it1) < ::toupper(*it2)) ? -1 : 1;
- //proceed to the next character in each string
- ++it1;
- ++it2;
+ return res;
}
- size_t size1=s1.size(), size2=s2.size();// cache lengths
+
//return -1,0 or 1 according to strings' lengths
- if (size1==size2)
+ if (size1 == size2)
return 0;
- return (size1<size2) ? -1 : 1;
+ return (size1 < size2) ? -1 : 1;
+
}
@@ -404,15 +393,15 @@
{
if (len == 0)
return 0;
- std::string::const_iterator it1=s1.begin();
- std::string::const_iterator it2=s2.begin();
+ std::string::const_iterator it1 = s1.begin();
+ std::string::const_iterator it2 = s2.begin();
//stop when either string's end has been reached
- while ( (it1!=s1.end()) && (it2!=s2.end()) && len-- > 0)
+ while ( (it1 != s1.end()) && (it2 != s2.end()) && len-- > 0)
{
- if(::toupper(*it1) != ::toupper(*it2)) //letters differ?
+ if(std::toupper(*it1) != std::toupper(*it2)) //letters differ?
// return -1 to indicate smaller than, 1 otherwise
- return (::toupper(*it1) < ::toupper(*it2)) ? -1 : 1;
+ return (std::toupper(*it1) < std::toupper(*it2)) ? -1 : 1;
//proceed to the next character in each string
++it1;
++it2;
@@ -507,22 +496,16 @@
/**
@brief
- Get a timestamp for the curent time instant.
+ Get a timestamp for the current time instant.
@return
Returns a string with the timestamp.
*/
std::string getTimestamp(void)
{
- struct tm *pTime;
time_t ctTime; std::time(&ctTime);
- pTime = std::localtime( &ctTime );
+ tm* pTime = std::localtime(&ctTime);
std::ostringstream oss;
- oss << std::setw(2) << std::setfill('0') << (pTime->tm_mon + 1)
- << std::setw(2) << std::setfill('0') << pTime->tm_mday
- << std::setw(2) << std::setfill('0') << (pTime->tm_year + 1900)
- << "_" << std::setw(2) << std::setfill('0') << pTime->tm_hour
- << std::setw(2) << std::setfill('0') << pTime->tm_min
- << std::setw(2) << std::setfill('0') << pTime->tm_sec;
+ oss << std::put_time(pTime, "%m%d%Y_%H%M%S");
return oss.str();
}
}
More information about the Orxonox-commit
mailing list