From 296e44e509e759c7a65efeff62b9446fb048ab06 Mon Sep 17 00:00:00 2001 From: cxl Date: Thu, 3 Jun 2010 17:21:30 +0000 Subject: [PATCH] Core: [W]String::ReverseFind git-svn-id: svn://ultimatepp.org/upp/trunk@2453 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- uppsrc/Core/AString.hpp | 23 +++++++++++++++++++++++ uppsrc/Core/String.h | 6 +++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/uppsrc/Core/AString.hpp b/uppsrc/Core/AString.hpp index 8161f78f0..dd03d5d06 100644 --- a/uppsrc/Core/AString.hpp +++ b/uppsrc/Core/AString.hpp @@ -60,6 +60,29 @@ int AString::ReverseFind(int chr, int from) const return -1; } +template +int AString::ReverseFind(int len, const tchar *s, int from) const +{ + ASSERT(from >= 0 && from <= GetLength()); + if(from < GetLength()) { + const tchar *ptr = B::Begin(); + const tchar *p = ptr + from - len + 1; + len *= sizeof(tchar); + while(p >= ptr) { + if(memcmp(s, p, len) == 0) + return (int)(p - ptr); + p--; + } + } + return -1; +} + +template +int AString::ReverseFind(const tchar *s, int from) const +{ + return ReverseFind(strlen__(s), s, from); +} + template int AString::ReverseFind(int chr) const { diff --git a/uppsrc/Core/String.h b/uppsrc/Core/String.h index ff8b4b4aa..4918e4a0e 100644 --- a/uppsrc/Core/String.h +++ b/uppsrc/Core/String.h @@ -87,7 +87,11 @@ public: int Find(int len, const tchar *s, int from) const; int Find(const tchar *s, int from = 0) const; int Find(const String& s, int from = 0) const { return Find(s.GetCount(), ~s, from); } - + + int ReverseFind(int len, const tchar *s, int from) const; + int ReverseFind(const tchar *s, int from = 0) const; + int ReverseFind(const String& s, int from = 0) const { return ReverseFind(s.GetCount(), ~s, from); } + 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()); }