From 379131d97d1bda4b50a0a384c36abd239783cc83 Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Mon, 8 Apr 2013 15:44:31 +0200 Subject: [PATCH] BString: Add StartsWith() and EndsWith() methods --- headers/os/support/String.h | 8 ++++++ src/kits/support/String.cpp | 49 +++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/headers/os/support/String.h b/headers/os/support/String.h index bbe5354d51..a487a040e3 100644 --- a/headers/os/support/String.h +++ b/headers/os/support/String.h @@ -221,6 +221,14 @@ public: int32 IFindLast(const char* string, int32 beforeOffset) const; + bool StartsWith(const BString& string) const; + bool StartsWith(const char* string) const; + bool StartsWith(const char* string, int32 length) const; + + bool EndsWith(const BString& string) const; + bool EndsWith(const char* string) const; + bool EndsWith(const char* string, int32 length) const; + // Replacing BString& ReplaceFirst(char replaceThis, char withThis); BString& ReplaceLast(char replaceThis, char withThis); diff --git a/src/kits/support/String.cpp b/src/kits/support/String.cpp index 990100f7cc..36936cf9f1 100644 --- a/src/kits/support/String.cpp +++ b/src/kits/support/String.cpp @@ -1362,6 +1362,55 @@ BString::IFindLast(const char* string, int32 beforeOffset) const } +bool +BString::StartsWith(const BString& string) const +{ + return StartsWith(string.String(), string.Length()); +} + + +bool +BString::StartsWith(const char* string) const +{ + return StartsWith(string, strlen(string)); +} + + +bool +BString::StartsWith(const char* string, int32 length) const +{ + if (length > Length()) + return false; + + return memcmp(String(), string, length) == 0; +} + + +bool +BString::EndsWith(const BString& string) const +{ + return EndsWith(string.String(), string.Length()); +} + + +bool +BString::EndsWith(const char* string) const +{ + return EndsWith(string, strlen(string)); +} + + +bool +BString::EndsWith(const char* string, int32 length) const +{ + int32 offset = Length() - length; + if (offset < 0) + return false; + + return memcmp(String() + offset, string, length) == 0; +} + + // #pragma mark - Replacing