From 0d949cc137bdf8c31607f9943eb814969937ff9a Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Sun, 6 Jan 2013 23:27:19 -0800 Subject: [PATCH] Add plString::Replace --- Sources/Plasma/CoreLib/plString.cpp | 23 +++++++++++++++++++++++ Sources/Plasma/CoreLib/plString.h | 2 ++ 2 files changed, 25 insertions(+) diff --git a/Sources/Plasma/CoreLib/plString.cpp b/Sources/Plasma/CoreLib/plString.cpp index 57c0c422..369f1182 100644 --- a/Sources/Plasma/CoreLib/plString.cpp +++ b/Sources/Plasma/CoreLib/plString.cpp @@ -674,6 +674,29 @@ plString plString::Substr(int start, size_t size) const return sub; } +plString plString::Replace(const char *from, const char *to) const +{ + if (IsEmpty() || !from || !from[0]) + return *this; + + if (!to) + to = ""; + + plStringStream out; + const char *pstart = c_str(); + const char *pnext; + size_t flen = strlen(from), tlen = strlen(to); + while (pnext = strstr(pstart, from)) { + out.append(pstart, pnext - pstart); + out.append(to, tlen); + pstart = pnext + flen; + } + + if (*pstart) + out << pstart; + return out.GetString(); +} + plString plString::ToUpper() const { // TODO: Unicode-aware case conversion diff --git a/Sources/Plasma/CoreLib/plString.h b/Sources/Plasma/CoreLib/plString.h index bfecb1de..598fd588 100644 --- a/Sources/Plasma/CoreLib/plString.h +++ b/Sources/Plasma/CoreLib/plString.h @@ -288,6 +288,8 @@ public: plString Left(size_t size) const { return Substr(0, size); } plString Right(size_t size) const { return Substr(GetSize() - size, size); } + plString Replace(const char *from, const char *to) const; + // NOTE: Does Compare(blah, kCaseInsensitive) make more sense? If // so, use that instead -- it's faster and more efficient! plString ToUpper() const;