From dde3be140c7a546832b1fbf17cde228a9c3ce4cc Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Sun, 24 Feb 2013 19:38:12 -0800 Subject: [PATCH] Support negative substring indexes from the right side of the string --- Sources/Plasma/CoreLib/plString.cpp | 18 +++++++++++------- Sources/Plasma/CoreLib/plString.h | 19 ++++++++++++++----- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/Sources/Plasma/CoreLib/plString.cpp b/Sources/Plasma/CoreLib/plString.cpp index b7ec4263..3b90e06a 100644 --- a/Sources/Plasma/CoreLib/plString.cpp +++ b/Sources/Plasma/CoreLib/plString.cpp @@ -545,7 +545,7 @@ plString plString::Format(const char *fmt, ...) return str; } -int plString::Find(char ch, CaseSensitivity sense) const +ssize_t plString::Find(char ch, CaseSensitivity sense) const { if (sense == kCaseSensitive) { const char *cp = strchr(c_str(), ch); @@ -561,7 +561,7 @@ int plString::Find(char ch, CaseSensitivity sense) const } } -int plString::FindLast(char ch, CaseSensitivity sense) const +ssize_t plString::FindLast(char ch, CaseSensitivity sense) const { if (IsEmpty()) return -1; @@ -581,7 +581,7 @@ int plString::FindLast(char ch, CaseSensitivity sense) const } } -int plString::Find(const char *str, CaseSensitivity sense) const +ssize_t plString::Find(const char *str, CaseSensitivity sense) const { if (!str || !str[0]) return -1; @@ -718,14 +718,18 @@ plString plString::Trim(const char *charset) const return Substr(lp - c_str(), rp - lp + 1); } -plString plString::Substr(int start, size_t size) const +plString plString::Substr(ssize_t start, size_t size) const { size_t maxSize = GetSize(); - if (start > maxSize) + if (start < 0) { + // Handle negative indexes from the right of the string + start += maxSize; + if (start < 0) + start = 0; + } else if (static_cast(start) > maxSize) { return Null; - if (start < 0) - start = 0; + } if (start + size > maxSize) size = maxSize - start; diff --git a/Sources/Plasma/CoreLib/plString.h b/Sources/Plasma/CoreLib/plString.h index d5e39e4f..5358a836 100644 --- a/Sources/Plasma/CoreLib/plString.h +++ b/Sources/Plasma/CoreLib/plString.h @@ -71,6 +71,15 @@ typedef unsigned int UniChar; # define strlwr hsStrLower #endif +// ssize_t doesn't exist in MSVC2010 +#if _MSC_VER +# ifdef _WIN64 + typedef __int64 ssize_t; +# else + typedef int ssize_t; +# endif +#endif + /** Ref-counted string data buffer. * This is used to store actual string data in any (unchecked) encoding format, * including both the internal UTF-8 data of plString itself as well as the @@ -488,22 +497,22 @@ public: /** Find the index of the first instance of \a ch in this string. * \return -1 if the character was not found. */ - int Find(char ch, CaseSensitivity sense = kCaseSensitive) const; + ssize_t Find(char ch, CaseSensitivity sense = kCaseSensitive) const; /** Find the index of the last instance of \a ch in this string. * \return -1 if the character was not found. */ - int FindLast(char ch, CaseSensitivity sense = kCaseSensitive) const; + ssize_t FindLast(char ch, CaseSensitivity sense = kCaseSensitive) const; /** Find the index of the first instance of \a str in this string. * \return -1 if the substring was not found. */ - int Find(const char *str, CaseSensitivity sense = kCaseSensitive) const; + ssize_t Find(const char *str, CaseSensitivity sense = kCaseSensitive) const; /** Find the index of the first instance of \a str in this string. * \return -1 if the substring was not found. */ - int Find(const plString &str, CaseSensitivity sense = kCaseSensitive) const + ssize_t Find(const plString &str, CaseSensitivity sense = kCaseSensitive) const { return Find(str.c_str(), sense); } /** Check that this string matches the specified regular expression. @@ -541,7 +550,7 @@ public: * number of characters left in the string after \a start, Substr will * return the remainder of the string. */ - plString Substr(int start, size_t size = kSizeAuto) const; + plString Substr(ssize_t start, size_t size = kSizeAuto) const; /** Return a substring containing at most \a size characters from the left * of the string. Equivalent to Substr(0, size).