Added case insensitive sorting option to some StringUtil functions.

This commit is contained in:
Leon Styhre 2020-10-22 21:14:15 +02:00
parent effc0f31dc
commit ab87063770
2 changed files with 34 additions and 23 deletions

View file

@ -142,7 +142,7 @@ namespace Utils
std::string string; std::string string;
for (size_t i = 0; i < _string.length(); ++i) for (size_t i = 0; i < _string.length(); ++i)
string += (char)tolower(_string[i]); string += static_cast<char>(tolower(_string[i]));
return string; return string;
} }
@ -152,7 +152,7 @@ namespace Utils
std::string string; std::string string;
for (size_t i = 0; i < _string.length(); ++i) for (size_t i = 0; i < _string.length(); ++i)
string += (char)toupper(_string[i]); string += static_cast<char>(toupper(_string[i]));
return string; return string;
} }
@ -231,10 +231,10 @@ namespace Utils
return trim(string); return trim(string);
} }
stringVector delimitedStringToVector(const std::string& _string, std::vector<std::string> delimitedStringToVector(const std::string& _string,
const std::string& _delimiter, bool sort) const std::string& _delimiter, bool sort, bool caseInsensitive)
{ {
stringVector vector; std::vector<std::string> vector;
size_t start = 0; size_t start = 0;
size_t comma = _string.find(_delimiter); size_t comma = _string.find(_delimiter);
@ -245,24 +245,37 @@ namespace Utils
} }
vector.push_back(_string.substr(start)); vector.push_back(_string.substr(start));
if (sort) if (sort) {
if (caseInsensitive)
std::sort(std::begin(vector), std::end(vector),
[](std::string a, std::string b) {
return std::toupper(a.front()) < std::toupper(b.front()); });
else
std::sort(vector.begin(), vector.end()); std::sort(vector.begin(), vector.end());
}
return vector; return vector;
} }
stringVector commaStringToVector(const std::string& _string, bool sort) std::vector<std::string> commaStringToVector(const std::string& _string,
bool sort, bool caseInsensitive)
{ {
return delimitedStringToVector(_string, ",", sort); return delimitedStringToVector(_string, ",", sort, caseInsensitive);
} }
std::string vectorToCommaString(stringVector _vector) std::string vectorToCommaString(std::vector<std::string> _vector, bool caseInsensitive)
{ {
std::string string; std::string string;
if (caseInsensitive)
std::sort(std::begin(_vector), std::end(_vector),
[](std::string a, std::string b) {
return std::toupper(a.front()) < std::toupper(b.front()); });
else
std::sort(_vector.begin(), _vector.end()); std::sort(_vector.begin(), _vector.end());
for (stringVector::const_iterator it = _vector.cbegin(); it != _vector.cend(); ++it) for (std::vector<std::string>::const_iterator it = _vector.cbegin();
it != _vector.cend(); ++it)
string += (string.length() ? "," : "") + (*it); string += (string.length() ? "," : "") + (*it);
return string; return string;

View file

@ -17,8 +17,6 @@ namespace Utils
{ {
namespace String namespace String
{ {
typedef std::vector<std::string> stringVector;
unsigned int chars2Unicode(const std::string& _string, size_t& _cursor); unsigned int chars2Unicode(const std::string& _string, size_t& _cursor);
std::string unicode2Chars(const unsigned int _unicode); std::string unicode2Chars(const unsigned int _unicode);
size_t nextCursor(const std::string& _string, const size_t _cursor); size_t nextCursor(const std::string& _string, const size_t _cursor);
@ -34,15 +32,15 @@ namespace Utils
bool startsWith(const std::string& _string, const std::string& _start); bool startsWith(const std::string& _string, const std::string& _start);
bool endsWith(const std::string& _string, const std::string& _end); bool endsWith(const std::string& _string, const std::string& _end);
std::string removeParenthesis(const std::string& _string); std::string removeParenthesis(const std::string& _string);
stringVector delimitedStringToVector(const std::string& _string, std::vector<std::string> delimitedStringToVector(const std::string& _string,
const std::string& _delimiter, bool sort = false); const std::string& _delimiter, bool sort = false, bool caseInsensitive = false);
stringVector commaStringToVector(const std::string& _string, bool sort = false); std::vector<std::string> commaStringToVector(const std::string& _string,
std::string vectorToCommaString(stringVector _vector); bool sort = false, bool caseInsensitive = false);
std::string vectorToCommaString(std::vector<std::string> _vector,
bool caseInsensitive = false);
std::string format(const char* _string, ...); std::string format(const char* _string, ...);
std::string scramble(const std::string& _input, const std::string& key); std::string scramble(const std::string& _input, const std::string& key);
}
} // String:: }
} // Utils::
#endif // ES_CORE_UTILS_STRING_UTIL_H #endif // ES_CORE_UTILS_STRING_UTIL_H