diff --git a/src/kits/support/String.cpp b/src/kits/support/String.cpp index 982a0db010..b3fdc7de30 100644 --- a/src/kits/support/String.cpp +++ b/src/kits/support/String.cpp @@ -36,6 +36,8 @@ // System Includes ------------------------------------------------------------- #include +// Temporary Includes +#include "string_helper.h" /*---- Construction --------------------------------------------------------*/ BString::BString() @@ -628,6 +630,36 @@ BString::FindFirst(char c, int32 fromOffset) const } +int32 +BString::FindLast(const BString &string) const +{ + return _FindBefore(string.String(), Length(), -1); +} + + +int32 +BString::FindLast(const char *str) const +{ + return _FindBefore(str, Length(), -1); +} + + +int32 +BString::FindLast(const BString &string, int32 beforeOffset) const +{ + return _FindBefore(string.String(), beforeOffset, -1); +} + + +int32 +BString::FindLast(char c) const +{ + char tmp[2] = { c, '\0' }; + + return _FindBefore(tmp, Length(), -1); +} + + int32 BString::IFindFirst(const BString &string) const { @@ -656,6 +688,34 @@ BString::IFindFirst(const char *string, int32 fromOffset) const } +int32 +BString::IFindLast(const BString &string) const +{ + return _IFindBefore(string.String(), Length(), -1); +} + + +int32 +BString::IFindLast(const char *string) const +{ + return _IFindBefore(string, Length(), -1); +} + + +int32 +BString::IFindLast(const BString &string, int32 beforeOffset) const +{ + return _IFindBefore(string.String(), beforeOffset, -1); +} + + +int32 +BString::IFindLast(const char *string, int32 beforeOffset) const +{ + return _IFindBefore(string, beforeOffset, -1); +} + + /*---- Replacing -----------------------------------------------------------*/ BString& BString::ReplaceFirst(char replaceThis, char withThis) @@ -1163,23 +1223,53 @@ BString::_ShortFindAfter(const char *str, int32 ) const int32 -BString::_FindBefore(const char *str, int32 offset, int32 ) const +BString::_FindBefore(const char *str, int32 offset, int32) const { - assert(str != NULL); + if (!str) + return 0; + if (offset <= 0) return B_ERROR; - //TODO: Implement + int len2 = strlen(str); + + if (len2 == 0) + return 0; + + char *ptr1 = _privateData + offset - len2; - return B_ERROR; + while (ptr1 >= _privateData) { + if (!memcmp(ptr1, str, len2)) + return ptr1 - _privateData; + ptr1--; + } + + return B_ERROR; } int32 -BString::_IFindBefore(const char *str, int32 , int32 ) const +BString::_IFindBefore(const char *str, int32 offset, int32) const { - assert(str != NULL); - //TODO: Implement + if (!str) + return 0; + + if (offset <= 0) + return B_ERROR; + + int len2 = strlen(str); + + if (len2 == 0) + return 0; + + char *ptr1 = _privateData + offset - len2; + + while (ptr1 >= _privateData) { + if (!strncasecmp(ptr1, str, len2)) + return ptr1 - _privateData; + ptr1--; + } + return B_ERROR; } @@ -1193,6 +1283,7 @@ BString::_SetLength(int32 length) #if DEBUG // TODO: Implement? +// AFAIK, these are not implemented in BeOS R5 BString::_SetUsingAsCString(bool) {} BString::_AssertNotUsingAsCString() {} #endif diff --git a/src/kits/support/string_helper.cpp b/src/kits/support/string_helper.cpp new file mode 100644 index 0000000000..c94b58e5bb --- /dev/null +++ b/src/kits/support/string_helper.cpp @@ -0,0 +1,26 @@ +//Those functions are here just to compile BString. +//They will be removed as soon as our libc compiles. + +#include +#include + +char * +strcasestr(const char *s, const char *find) +{ + char c, sc; + size_t len; + + if ((c = *find++) != 0) { + c = tolower((unsigned char)c); + len = strlen(find); + do { + do { + if ((sc = *s++) == 0) + return NULL; + } while ((char)tolower((unsigned char)sc) != c); + } while (strncasecmp(s, find, len) != 0); + s--; + } + return (char *)s; +} + diff --git a/src/kits/support/string_helper.h b/src/kits/support/string_helper.h new file mode 100644 index 0000000000..252ad95521 --- /dev/null +++ b/src/kits/support/string_helper.h @@ -0,0 +1,9 @@ +//Those functions are here just to compile BString. +//They will be removed as soon as our libc compiles. + +#ifndef __stringhelper_h +#define __stringhelper_h + +extern char *strcasestr(const char *s, const char *find); + +#endif //__stringhelper_h \ No newline at end of file diff --git a/src/kits/support/support.src b/src/kits/support/support.src index cfe9cfe911..23dd01e671 100644 --- a/src/kits/support/support.src +++ b/src/kits/support/support.src @@ -7,4 +7,5 @@ SUPPORT_KIT_SOURCE = Locker.cpp StopWatch.cpp #String.cpp + #string_helper.cpp ;