From 2ff6c6fbf85932c2a690350c35998075deaf4623 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 8 Sep 2014 14:38:26 -0400 Subject: [PATCH] Common: Simplify uppercase/lowercase string functions. Also changed it to not modify the given string, but rather return a copy of it. --- src/common/string_util.cpp | 17 +++++++---------- src/common/string_util.h | 4 ++-- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/common/string_util.cpp b/src/common/string_util.cpp index c1f22bda3..82c679190 100644 --- a/src/common/string_util.cpp +++ b/src/common/string_util.cpp @@ -3,6 +3,7 @@ // Refer to the license.txt file included. #include +#include #include #include @@ -18,19 +19,15 @@ #endif /// Make a string lowercase -void LowerStr(char* str) { - for (int i = 0; str[i]; i++) { - str[i] = tolower(str[ i ]); - } +std::string ToLowerCaseString(std::string input) { + std::transform(input.begin(), input.end(), input.begin(), ::tolower); + return input; } /// Make a string uppercase -void UpperStr(char* str) { - for (int i=0; i < strlen(str); i++) { - if(str[i] >= 'a' && str[i] <= 'z') { - str[i] &= 0xDF; - } - } +std::string ToUpperCaseString(std::string input) { + std::transform(input.begin(), input.end(), input.begin(), ::toupper); + return input; } // faster than sscanf diff --git a/src/common/string_util.h b/src/common/string_util.h index ba4cd363e..29d6a5b8c 100644 --- a/src/common/string_util.h +++ b/src/common/string_util.h @@ -13,10 +13,10 @@ #include "common/common.h" /// Make a string lowercase -void LowerStr(char* str); +std::string ToLowerCaseString(std::string input); /// Make a string uppercase -void UpperStr(char* str); +std::string ToUpperCaseString(std::string input); std::string StringFromFormat(const char* format, ...); // Cheap!