#ifndef STRINGUTIL_H #define STRINGUTIL_H #include #include template inline size_t StringSplit(const tChar* str, const tChar* sep, tOutIter dest) { return StringSplit(std::basic_string(str), std::basic_string(sep), dest); } template inline size_t StringSplit(const tChar* str, const tChar sep, tOutIter dest) { return StringSplit(std::basic_string(str), std::basic_string(sep), dest); } template inline size_t StringSplit(const std::basic_string& str, const std::basic_string& sep, tOutIter dest) { std::basic_string::size_type left = str.find_first_not_of(sep); std::basic_string::size_type right = str.find_first_of(sep, left); size_t count = 0; while (left < right) { *dest = str.substr(left, right - left); ++dest; ++count; left = str.find_first_not_of(sep, right); right = str.find_first_of(sep, left); } return count; } template inline std::basic_string StringToUpper(const std::basic_string& str, const std::locale& loc = std::locale()) { std::basic_string temp(str); for (std::basic_string::size_type i(0); i < temp.length(); i++) temp[i] = std::toupper(i, loc); return temp; } template inline std::basic_string StringToLower(const std::basic_string& str, const std::locale& loc = std::locale()) { std::basic_string temp(str); for (std::basic_string::size_type i(0); i < temp.length(); i++) temp[i] = std::tolower(i, loc); return temp; } template inline std::basic_string TrimLeadingWhitespace(const std::basic_string& source, const std::locale &loc = std::locale()) { typename std::basic_string::size_type pos(0); while (pos < source.length()) { if (!std::isspace(source[pos], loc)) break; pos++; } return source.substr(pos); } template inline std::basic_string StringTrimTrailingWhitespace(const std::basic_string& source, const std::locale &loc = std::locale()) { typename std::basic_string::size_type pos(source.length()); while (pos > 0) { if (!std::isspace(source[pos], loc)) break; pos--; } return source.substr(0, pos); } template inline std::basic_string StringTrimWhitespace(const std::basic_string& source, const std::locale &loc = std::locale()) { typedef typename std::basic_string::size_type ssize; ssize length = source.length(); ssize frontPos = 0; while (frontPos < length) { if (!std::isspace(source[frontPos], loc)) break; frontPos++; } // if every single character is a space, trim the whole thing if (frontPos == length) return tString(); ssize backPos = length - 1; while (backPos > 0) { if (!std::isspace(source[backPos], loc)) break; backPos--; } return source.substr(frontPos, backPos - frontPos + 1); } #endif