From 54df285bf3a45136a2c87eac8ca57e766e5e2f35 Mon Sep 17 00:00:00 2001 From: cxl Date: Sun, 10 Oct 2010 17:58:58 +0000 Subject: [PATCH] Core: String::Replace git-svn-id: svn://ultimatepp.org/upp/trunk@2765 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- uppsrc/Core/AString.hpp | 18 ++++++++++++++++++ uppsrc/Core/String.h | 4 ++++ 2 files changed, 22 insertions(+) diff --git a/uppsrc/Core/AString.hpp b/uppsrc/Core/AString.hpp index dd03d5d06..bb4e15547 100644 --- a/uppsrc/Core/AString.hpp +++ b/uppsrc/Core/AString.hpp @@ -77,6 +77,24 @@ int AString::ReverseFind(int len, const tchar *s, int from) const return -1; } +template +void AString::Replace(const tchar *find, int findlen, const tchar *replace, int replacelen) +{ + String r; + int i = 0; + const tchar *p = Begin(); + for(;;) { + int j = Find(findlen, find, i); + if(j < 0) + break; + r.Cat(p + i, j - i); + r.Cat(replace, replacelen); + i = j + findlen; + } + r.Cat(p + i, GetCount() - i); + *this = r; +} + template int AString::ReverseFind(const tchar *s, int from) const { diff --git a/uppsrc/Core/String.h b/uppsrc/Core/String.h index 0f97e39f3..8883848d2 100644 --- a/uppsrc/Core/String.h +++ b/uppsrc/Core/String.h @@ -94,6 +94,10 @@ public: int ReverseFind(const tchar *s) const { return ReverseFind(s, GetLength()-1);} int ReverseFind(const String& s) const { return ReverseFind(s, GetLength()-1);} + void Replace(const tchar *find, int findlen, const tchar *replace, int replacelen); + void Replace(const String& find, const String& replace) { Replace(~find, find.GetCount(), ~replace, replace.GetCount()); } + void Replace(const tchar *find, const tchar *replace) { Replace(find, strlen(find), replace, strlen(replace)); } + bool StartsWith(const tchar *s, int len) const; bool StartsWith(const tchar *s) const { return StartsWith(s, strlen__(s)); } bool StartsWith(const String& s) const { return StartsWith(~s, s.GetLength()); }