Common: Simplify uppercase/lowercase string functions.

Also changed it to not modify the given string, but rather return a copy of it.
This commit is contained in:
Lioncash 2014-09-08 14:38:26 -04:00
parent 335082e74e
commit 2ff6c6fbf8
2 changed files with 9 additions and 12 deletions

View File

@ -3,6 +3,7 @@
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <algorithm> #include <algorithm>
#include <cctype>
#include <cstdlib> #include <cstdlib>
#include <cstdio> #include <cstdio>
@ -18,19 +19,15 @@
#endif #endif
/// Make a string lowercase /// Make a string lowercase
void LowerStr(char* str) { std::string ToLowerCaseString(std::string input) {
for (int i = 0; str[i]; i++) { std::transform(input.begin(), input.end(), input.begin(), ::tolower);
str[i] = tolower(str[ i ]); return input;
}
} }
/// Make a string uppercase /// Make a string uppercase
void UpperStr(char* str) { std::string ToUpperCaseString(std::string input) {
for (int i=0; i < strlen(str); i++) { std::transform(input.begin(), input.end(), input.begin(), ::toupper);
if(str[i] >= 'a' && str[i] <= 'z') { return input;
str[i] &= 0xDF;
}
}
} }
// faster than sscanf // faster than sscanf

View File

@ -13,10 +13,10 @@
#include "common/common.h" #include "common/common.h"
/// Make a string lowercase /// Make a string lowercase
void LowerStr(char* str); std::string ToLowerCaseString(std::string input);
/// Make a string uppercase /// Make a string uppercase
void UpperStr(char* str); std::string ToUpperCaseString(std::string input);
std::string StringFromFormat(const char* format, ...); std::string StringFromFormat(const char* format, ...);
// Cheap! // Cheap!