diff --git a/es-core/src/utils/StringUtil.cpp b/es-core/src/utils/StringUtil.cpp index 3ec9a1728..4a36908f8 100644 --- a/es-core/src/utils/StringUtil.cpp +++ b/es-core/src/utils/StringUtil.cpp @@ -142,7 +142,7 @@ namespace Utils std::string string; for (size_t i = 0; i < _string.length(); ++i) - string += (char)tolower(_string[i]); + string += static_cast(tolower(_string[i])); return string; } @@ -152,7 +152,7 @@ namespace Utils std::string string; for (size_t i = 0; i < _string.length(); ++i) - string += (char)toupper(_string[i]); + string += static_cast(toupper(_string[i])); return string; } @@ -231,10 +231,10 @@ namespace Utils return trim(string); } - stringVector delimitedStringToVector(const std::string& _string, - const std::string& _delimiter, bool sort) + std::vector delimitedStringToVector(const std::string& _string, + const std::string& _delimiter, bool sort, bool caseInsensitive) { - stringVector vector; + std::vector vector; size_t start = 0; size_t comma = _string.find(_delimiter); @@ -245,24 +245,37 @@ namespace Utils } vector.push_back(_string.substr(start)); - if (sort) - std::sort(vector.begin(), vector.end()); + 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()); + } return vector; } - stringVector commaStringToVector(const std::string& _string, bool sort) + std::vector 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 _vector, bool caseInsensitive) { std::string string; - std::sort(_vector.begin(), _vector.end()); + 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()); - for (stringVector::const_iterator it = _vector.cbegin(); it != _vector.cend(); ++it) + for (std::vector::const_iterator it = _vector.cbegin(); + it != _vector.cend(); ++it) string += (string.length() ? "," : "") + (*it); return string; diff --git a/es-core/src/utils/StringUtil.h b/es-core/src/utils/StringUtil.h index 873d92c99..317a06c5a 100644 --- a/es-core/src/utils/StringUtil.h +++ b/es-core/src/utils/StringUtil.h @@ -17,10 +17,8 @@ namespace Utils { namespace String { - typedef std::vector stringVector; - 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 prevCursor(const std::string& _string, const size_t _cursor); size_t moveCursor(const std::string& _string, const size_t _cursor, const int _amount); @@ -34,15 +32,15 @@ namespace Utils bool startsWith(const std::string& _string, const std::string& _start); bool endsWith(const std::string& _string, const std::string& _end); std::string removeParenthesis(const std::string& _string); - stringVector delimitedStringToVector(const std::string& _string, - const std::string& _delimiter, bool sort = false); - stringVector commaStringToVector(const std::string& _string, bool sort = false); - std::string vectorToCommaString(stringVector _vector); + std::vector delimitedStringToVector(const std::string& _string, + const std::string& _delimiter, bool sort = false, bool caseInsensitive = false); + std::vector commaStringToVector(const std::string& _string, + bool sort = false, bool caseInsensitive = false); + std::string vectorToCommaString(std::vector _vector, + bool caseInsensitive = false); std::string format(const char* _string, ...); std::string scramble(const std::string& _input, const std::string& key); - - } // String:: - -} // Utils:: + } +} #endif // ES_CORE_UTILS_STRING_UTIL_H