зеркало из https://github.com/mozilla/pjs.git
Bug 413477 - nsStringAPI needs RFind set of functions, patch by Prasad Sunkari (prasad@medhas.org), r=bsmedberg, a=beltzner
This commit is contained in:
Родитель
014dd9a52f
Коммит
67ce89ca20
|
@ -22,6 +22,7 @@
|
|||
* Darin Fisher <darin@meer.net>
|
||||
* Benjamin Smedberg <benjamin@smedbergs.us>
|
||||
* Ben Turner <mozilla@songbirdnest.com>
|
||||
* Prasad Sunkari <prasad@medhas.org>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
|
@ -403,6 +404,56 @@ nsAString::Find(const char *aStr, PRUint32 aOffset, PRBool aIgnoreCase) const
|
|||
return -1;
|
||||
}
|
||||
|
||||
PRInt32
|
||||
nsAString::RFind(const self_type& aStr, PRInt32 aOffset, ComparatorFunc c) const
|
||||
{
|
||||
const char_type *begin, *end;
|
||||
PRUint32 selflen = BeginReading(&begin, &end);
|
||||
|
||||
const char_type *other;
|
||||
PRUint32 otherlen = aStr.BeginReading(&other);
|
||||
|
||||
if (selflen < otherlen)
|
||||
return -1;
|
||||
|
||||
if (aOffset < 0 || aOffset > (selflen - otherlen))
|
||||
end -= otherlen;
|
||||
else
|
||||
end = begin + aOffset;
|
||||
|
||||
for (const char_type *cur = end; cur >= begin; --cur) {
|
||||
if (!c(cur, other, otherlen))
|
||||
return cur - begin;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
PRInt32
|
||||
nsAString::RFind(const char *aStr, PRInt32 aOffset, PRBool aIgnoreCase) const
|
||||
{
|
||||
PRBool (*match)(const PRUnichar*, const char*, PRUint32) =
|
||||
aIgnoreCase ? ns_strnimatch : ns_strnmatch;
|
||||
|
||||
const char_type *begin, *end;
|
||||
PRUint32 selflen = BeginReading(&begin, &end);
|
||||
PRUint32 otherlen = strlen(aStr);
|
||||
|
||||
if (selflen < otherlen)
|
||||
return -1;
|
||||
|
||||
if (aOffset < 0 || aOffset > (selflen - otherlen))
|
||||
end -= otherlen;
|
||||
else
|
||||
end = begin + aOffset;
|
||||
|
||||
for (const char_type *cur = end; cur >= begin; --cur) {
|
||||
if (match(cur, aStr, otherlen)) {
|
||||
return cur - begin;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
PRInt32
|
||||
nsAString::FindChar(char_type aChar, PRUint32 aOffset) const
|
||||
{
|
||||
|
@ -762,6 +813,60 @@ nsACString::Find(const char_type *aStr, PRUint32 aLen, ComparatorFunc c) const
|
|||
return -1;
|
||||
}
|
||||
|
||||
PRInt32
|
||||
nsACString::RFind(const self_type& aStr, PRInt32 aOffset, ComparatorFunc c) const
|
||||
{
|
||||
const char_type *begin, *end;
|
||||
PRUint32 selflen = BeginReading(&begin, &end);
|
||||
|
||||
const char_type *other;
|
||||
PRUint32 otherlen = aStr.BeginReading(&other);
|
||||
|
||||
if (selflen < otherlen)
|
||||
return -1;
|
||||
|
||||
if (aOffset < 0 || aOffset > (selflen - otherlen))
|
||||
end -= otherlen;
|
||||
else
|
||||
end = begin + aOffset;
|
||||
|
||||
for (const char_type *cur = end; cur >= begin; --cur) {
|
||||
if (!c(cur, other, otherlen))
|
||||
return cur - begin;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
PRInt32
|
||||
nsACString::RFind(const char_type *aStr, ComparatorFunc c) const
|
||||
{
|
||||
return RFind(aStr, strlen(aStr), c);
|
||||
}
|
||||
|
||||
PRInt32
|
||||
nsACString::RFind(const char_type *aStr, PRInt32 aLen, ComparatorFunc c) const
|
||||
{
|
||||
const char_type *begin, *end;
|
||||
PRUint32 selflen = BeginReading(&begin, &end);
|
||||
|
||||
if (aLen == 0) {
|
||||
NS_WARNING("Searching for zero-length string.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (aLen > selflen)
|
||||
return -1;
|
||||
|
||||
// We want to start searching otherlen characters before the end of the string
|
||||
end -= aLen;
|
||||
|
||||
for (const char_type *cur = end; cur >= begin; --cur) {
|
||||
if (!c(cur, aStr, aLen))
|
||||
return cur - begin;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
PRInt32
|
||||
nsACString::FindChar(char_type aChar, PRUint32 aOffset) const
|
||||
{
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
* Darin Fisher <darin@meer.net>
|
||||
* Benjamin Smedberg <benjamin@smedbergs.us>
|
||||
* Ben Turner <mozilla@songbirdnest.com>
|
||||
* Prasad Sunkari <prasad@medhas.org>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
|
@ -270,7 +271,7 @@ public:
|
|||
NS_HIDDEN_(PRBool) LowerCaseEqualsLiteral(const char *aASCIIString) const;
|
||||
|
||||
/**
|
||||
* Find the first occurence of aStr in this string.
|
||||
* Find the first occurrence of aStr in this string.
|
||||
*
|
||||
* @return the offset of aStr, or -1 if not found
|
||||
*/
|
||||
|
@ -279,7 +280,7 @@ public:
|
|||
{ return Find(aStr, 0, c); }
|
||||
|
||||
/**
|
||||
* Find the first occurence of aStr in this string, beginning at aOffset.
|
||||
* Find the first occurrence of aStr in this string, beginning at aOffset.
|
||||
*
|
||||
* @return the offset of aStr, or -1 if not found
|
||||
*/
|
||||
|
@ -296,6 +297,46 @@ public:
|
|||
|
||||
NS_HIDDEN_(PRInt32) Find(const char *aStr, PRUint32 aOffset, PRBool aIgnoreCase = PR_FALSE) const;
|
||||
|
||||
/**
|
||||
* Find the last occurrence of aStr in this string.
|
||||
*
|
||||
* @return The offset of aStr from the beginning of the string,
|
||||
* or -1 if not found.
|
||||
*/
|
||||
NS_HIDDEN_(PRInt32) RFind(const self_type& aStr,
|
||||
ComparatorFunc c = DefaultComparator) const
|
||||
{ return RFind(aStr, -1, c); }
|
||||
|
||||
/**
|
||||
* Find the last occurrence of aStr in this string, beginning at aOffset.
|
||||
*
|
||||
* @param aOffset the offset from the beginning of the string to begin
|
||||
* searching. If aOffset < 0, search from end of this string.
|
||||
* @return The offset of aStr from the beginning of the string,
|
||||
* or -1 if not found.
|
||||
*/
|
||||
NS_HIDDEN_(PRInt32) RFind(const self_type& aStr, PRInt32 aOffset,
|
||||
ComparatorFunc c = DefaultComparator) const;
|
||||
|
||||
/**
|
||||
* Find the last occurrence of an ASCII string within this string.
|
||||
*
|
||||
* @return The offset of aStr from the beginning of the string,
|
||||
* or -1 if not found.
|
||||
*/
|
||||
NS_HIDDEN_(PRInt32) RFind(const char *aStr, PRBool aIgnoreCase = PR_FALSE) const
|
||||
{ return RFind(aStr, -1, aIgnoreCase); }
|
||||
|
||||
/**
|
||||
* Find the last occurrence of an ASCII string beginning at aOffset.
|
||||
*
|
||||
* @param aOffset the offset from the beginning of the string to begin
|
||||
* searching. If aOffset < 0, search from end of this string.
|
||||
* @return The offset of aStr from the beginning of the string,
|
||||
* or -1 if not found.
|
||||
*/
|
||||
NS_HIDDEN_(PRInt32) RFind(const char *aStr, PRInt32 aOffset, PRBool aIgnoreCase) const;
|
||||
|
||||
/**
|
||||
* Search for the offset of the first occurrence of a character in a
|
||||
* string.
|
||||
|
@ -554,7 +595,7 @@ public:
|
|||
}
|
||||
|
||||
/**
|
||||
* Find the first occurence of aStr in this string.
|
||||
* Find the first occurrence of aStr in this string.
|
||||
*
|
||||
* @return the offset of aStr, or -1 if not found
|
||||
*/
|
||||
|
@ -563,7 +604,7 @@ public:
|
|||
{ return Find(aStr, 0, c); }
|
||||
|
||||
/**
|
||||
* Find the first occurence of aStr in this string, beginning at aOffset.
|
||||
* Find the first occurrence of aStr in this string, beginning at aOffset.
|
||||
*
|
||||
* @return the offset of aStr, or -1 if not found
|
||||
*/
|
||||
|
@ -571,7 +612,7 @@ public:
|
|||
ComparatorFunc c = DefaultComparator) const;
|
||||
|
||||
/**
|
||||
* Find the first occurence of aStr in this string.
|
||||
* Find the first occurrence of aStr in this string.
|
||||
*
|
||||
* @return the offset of aStr, or -1 if not found
|
||||
*/
|
||||
|
@ -581,6 +622,47 @@ public:
|
|||
NS_HIDDEN_(PRInt32) Find(const char_type *aStr, PRUint32 aLen,
|
||||
ComparatorFunc c = DefaultComparator) const;
|
||||
|
||||
/**
|
||||
* Find the last occurrence of aStr in this string.
|
||||
*
|
||||
* @return The offset of the character from the beginning of the string,
|
||||
* or -1 if not found.
|
||||
*/
|
||||
NS_HIDDEN_(PRInt32) RFind(const self_type& aStr,
|
||||
ComparatorFunc c = DefaultComparator) const
|
||||
{ return RFind(aStr, -1, c); }
|
||||
|
||||
/**
|
||||
* Find the last occurrence of aStr in this string, beginning at aOffset.
|
||||
*
|
||||
* @param aOffset the offset from the beginning of the string to begin
|
||||
* searching. If aOffset < 0, search from end of this string.
|
||||
* @return The offset of aStr from the beginning of the string,
|
||||
* or -1 if not found.
|
||||
*/
|
||||
NS_HIDDEN_(PRInt32) RFind(const self_type& aStr, PRInt32 aOffset,
|
||||
ComparatorFunc c = DefaultComparator) const;
|
||||
|
||||
/**
|
||||
* Find the last occurrence of aStr in this string.
|
||||
*
|
||||
* @return The offset of aStr from the beginning of the string,
|
||||
* or -1 if not found.
|
||||
*/
|
||||
NS_HIDDEN_(PRInt32) RFind(const char_type *aStr,
|
||||
ComparatorFunc c = DefaultComparator) const;
|
||||
|
||||
/**
|
||||
* Find the last occurrence of an ASCII string in this string,
|
||||
* beginning at aOffset.
|
||||
*
|
||||
* @param aLen is the length of aStr
|
||||
* @return The offset of aStr from the beginning of the string,
|
||||
* or -1 if not found.
|
||||
*/
|
||||
NS_HIDDEN_(PRInt32) RFind(const char_type *aStr, PRInt32 aLen,
|
||||
ComparatorFunc c = DefaultComparator) const;
|
||||
|
||||
/**
|
||||
* Search for the offset of the first occurrence of a character in a
|
||||
* string.
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Prasad Sunkari <prasad@medhas.org>
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
|
@ -155,6 +156,51 @@ int testVoid() {
|
|||
return ret;
|
||||
}
|
||||
|
||||
int testRFind() {
|
||||
PRInt32 ret = 0;
|
||||
|
||||
// nsString.RFind
|
||||
nsString str_haystack;
|
||||
nsString str_needle;
|
||||
str_needle.AssignLiteral("world");
|
||||
|
||||
ret += CHECK(-1 == str_haystack.RFind(str_needle));
|
||||
ret += CHECK(-1 == str_haystack.RFind("world"));
|
||||
|
||||
str_haystack.AssignLiteral("hello world hElLo woRlD");
|
||||
|
||||
ret += CHECK( 6 == str_haystack.RFind(str_needle));
|
||||
ret += CHECK( 6 == str_haystack.RFind(str_needle, -1));
|
||||
ret += CHECK( 6 == str_haystack.RFind(str_needle, 17));
|
||||
ret += CHECK( 6 == str_haystack.RFind("world", PR_FALSE));
|
||||
ret += CHECK(18 == str_haystack.RFind("world", PR_TRUE));
|
||||
ret += CHECK( 6 == str_haystack.RFind("world", -1, PR_FALSE));
|
||||
ret += CHECK(18 == str_haystack.RFind("world", -1, PR_TRUE));
|
||||
ret += CHECK( 6 == str_haystack.RFind("world", 17, PR_FALSE));
|
||||
ret += CHECK( 0 == str_haystack.RFind("hello", 0, PR_FALSE));
|
||||
ret += CHECK(18 == str_haystack.RFind("world", 19, PR_TRUE));
|
||||
ret += CHECK(18 == str_haystack.RFind("world", 22, PR_TRUE));
|
||||
ret += CHECK(18 == str_haystack.RFind("world", 23, PR_TRUE));
|
||||
|
||||
// nsCString.RFind
|
||||
nsCString cstr_haystack;
|
||||
nsCString cstr_needle;
|
||||
cstr_needle.AssignLiteral("world");
|
||||
|
||||
ret += CHECK(-1 == cstr_haystack.RFind(cstr_needle));
|
||||
ret += CHECK(-1 == cstr_haystack.RFind("world"));
|
||||
|
||||
cstr_haystack.AssignLiteral("hello world hElLo woRlD");
|
||||
|
||||
ret += CHECK( 6 == cstr_haystack.RFind(cstr_needle));
|
||||
ret += CHECK( 6 == cstr_haystack.RFind(cstr_needle, -1));
|
||||
ret += CHECK( 6 == cstr_haystack.RFind(cstr_needle, 17));
|
||||
ret += CHECK( 6 == cstr_haystack.RFind("world", 5));
|
||||
ret += CHECK( 0 == cstr_haystack.RFind(nsDependentCString("hello"), 0));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int rv = 0;
|
||||
rv += testEmpty();
|
||||
|
@ -162,6 +208,7 @@ int main() {
|
|||
rv += testWrite();
|
||||
rv += testFind();
|
||||
rv += testVoid();
|
||||
rv += testRFind();
|
||||
if (0 == rv) {
|
||||
fprintf(stderr, "PASS: StringAPI tests\n");
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче