From 81465e621b7ef3a9d043eb24255fd7b38eb44efc Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Mon, 13 Feb 2012 20:18:41 -0800 Subject: [PATCH] Add case conversion functionality --- Sources/Plasma/CoreLib/plString.cpp | 30 +++++++++++++++++++++++++++++ Sources/Plasma/CoreLib/plString.h | 5 +++++ 2 files changed, 35 insertions(+) diff --git a/Sources/Plasma/CoreLib/plString.cpp b/Sources/Plasma/CoreLib/plString.cpp index a8da244e..14331def 100644 --- a/Sources/Plasma/CoreLib/plString.cpp +++ b/Sources/Plasma/CoreLib/plString.cpp @@ -644,6 +644,36 @@ plString plString::Substr(int start, size_t size) const return str; } +plString plString::ToUpper() const +{ + // TODO: Unicode-aware case conversion + size_t size = GetSize(); + char *dupe = new char[size + 1]; + const char *self = c_str(); + for (size_t i = 0; i < size; ++i) + dupe[i] = toupper(self[i]); + + // Don't re-check UTF-8 on this + plString str; + str.fUtf8Buffer = plStringBuffer::Steal(dupe, size); + return str; +} + +plString plString::ToLower() const +{ + // TODO: Unicode-aware case conversion + size_t size = GetSize(); + char *dupe = new char[size + 1]; + const char *self = c_str(); + for (size_t i = 0; i < size; ++i) + dupe[i] = tolower(self[i]); + + // Don't re-check UTF-8 on this + plString str; + str.fUtf8Buffer = plStringBuffer::Steal(dupe, size); + return str; +} + plString &plString::operator+=(const plString &str) { size_t catsize = GetSize() + str.GetSize(); diff --git a/Sources/Plasma/CoreLib/plString.h b/Sources/Plasma/CoreLib/plString.h index 48f272c3..47835a1d 100644 --- a/Sources/Plasma/CoreLib/plString.h +++ b/Sources/Plasma/CoreLib/plString.h @@ -260,6 +260,11 @@ public: plString Left(size_t size) const { return Substr(0, size); } plString Right(size_t size) const { return Substr(GetSize() - size, size); } + // NOTE: Does ::Compare(blah, kCaseInsensitive) make more sense? If + // so, use that instead -- it's faster and more efficient! + plString ToUpper() const; + plString ToLower() const; + public: struct less : public std::binary_function {