Bug 366592 - "Add comparison operators to external string API". r=bsmedberg.

This commit is contained in:
bent.mozilla@gmail.com
2007-01-17 21:10:02 +00:00
parent 420226fc02
commit 25e9c36c1b
3 changed files with 530 additions and 1 deletions

View File

@@ -21,6 +21,7 @@
* Contributor(s):
* Darin Fisher <darin@meer.net>
* Benjamin Smedberg <benjamin@smedbergs.us>
* Ben Turner <mozilla@songbirdnest.com>
*
* 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
@@ -230,12 +231,48 @@ nsAString::DefaultComparator(const char_type *a, const char_type *b,
if (*a == *b)
continue;
return a < b ? -1 : 1;
return *a < *b ? -1 : 1;
}
return 0;
}
PRInt32
nsAString::Compare(const char_type *other, ComparatorFunc c) const
{
const char_type *cself;
PRUint32 selflen = NS_StringGetData(*this, &cself);
PRUint32 otherlen = NS_strlen(other);
PRUint32 comparelen = selflen <= otherlen ? selflen : otherlen;
PRInt32 result = c(cself, other, comparelen);
if (result == 0) {
if (selflen < otherlen)
return -1;
else if (selflen > otherlen)
return 1;
}
return result;
}
PRInt32
nsAString::Compare(const self_type &other, ComparatorFunc c) const
{
const char_type *cself, *cother;
PRUint32 selflen = NS_StringGetData(*this, &cself);
PRUint32 otherlen = NS_StringGetData(other, &cother);
PRUint32 comparelen = selflen <= otherlen ? selflen : otherlen;
PRInt32 result = c(cself, cother, comparelen);
if (result == 0) {
if (selflen < otherlen)
return -1;
else if (selflen > otherlen)
return 1;
}
return result;
}
PRBool
nsAString::Equals(const char_type *other, ComparatorFunc c) const
{
@@ -617,6 +654,42 @@ nsACString::DefaultComparator(const char_type *a, const char_type *b,
return memcmp(a, b, len);
}
PRInt32
nsACString::Compare(const char_type *other, ComparatorFunc c) const
{
const char_type *cself;
PRUint32 selflen = NS_CStringGetData(*this, &cself);
PRUint32 otherlen = strlen(other);
PRUint32 comparelen = selflen <= otherlen ? selflen : otherlen;
PRInt32 result = c(cself, other, comparelen);
if (result == 0) {
if (selflen < otherlen)
return -1;
else if (selflen > otherlen)
return 1;
}
return result;
}
PRInt32
nsACString::Compare(const self_type &other, ComparatorFunc c) const
{
const char_type *cself, *cother;
PRUint32 selflen = NS_CStringGetData(*this, &cself);
PRUint32 otherlen = NS_CStringGetData(other, &cother);
PRUint32 comparelen = selflen <= otherlen ? selflen : otherlen;
PRInt32 result = c(cself, cother, comparelen);
if (result == 0) {
if (selflen < otherlen)
return -1;
else if (selflen > otherlen)
return 1;
}
return result;
}
PRBool
nsACString::Equals(const char_type *other, ComparatorFunc c) const
{

View File

@@ -21,6 +21,7 @@
* Contributor(s):
* Darin Fisher <darin@meer.net>
* Benjamin Smedberg <benjamin@smedbergs.us>
* Ben Turner <mozilla@songbirdnest.com>
*
* 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
@@ -185,12 +186,36 @@ public:
const char_type *b,
PRUint32 length);
NS_HIDDEN_(PRInt32) Compare( const char_type *other,
ComparatorFunc c = DefaultComparator ) const;
NS_HIDDEN_(PRInt32) Compare( const self_type &other,
ComparatorFunc c = DefaultComparator ) const;
NS_HIDDEN_(PRBool) Equals( const char_type *other,
ComparatorFunc c = DefaultComparator ) const;
NS_HIDDEN_(PRBool) Equals( const self_type &other,
ComparatorFunc c = DefaultComparator ) const;
NS_HIDDEN_(PRBool) operator < (const self_type &other) const
{
return Compare(other) < 0;
}
NS_HIDDEN_(PRBool) operator < (const char_type *other) const
{
return Compare(other) < 0;
}
NS_HIDDEN_(PRBool) operator <= (const self_type &other) const
{
return Compare(other) <= 0;
}
NS_HIDDEN_(PRBool) operator <= (const char_type *other) const
{
return Compare(other) <= 0;
}
NS_HIDDEN_(PRBool) operator == (const self_type &other) const
{
return Equals(other);
@@ -200,6 +225,33 @@ public:
return Equals(other);
}
NS_HIDDEN_(PRBool) operator >= (const self_type &other) const
{
return Compare(other) >= 0;
}
NS_HIDDEN_(PRBool) operator >= (const char_type *other) const
{
return Compare(other) >= 0;
}
NS_HIDDEN_(PRBool) operator > (const self_type &other) const
{
return Compare(other) > 0;
}
NS_HIDDEN_(PRBool) operator > (const char_type *other) const
{
return Compare(other) > 0;
}
NS_HIDDEN_(PRBool) operator != (const self_type &other) const
{
return !Equals(other);
}
NS_HIDDEN_(PRBool) operator != (const char_type *other) const
{
return !Equals(other);
}
NS_HIDDEN_(PRBool) EqualsLiteral(const char *aASCIIString) const;
/**
@@ -411,12 +463,36 @@ public:
const char_type *b,
PRUint32 length);
NS_HIDDEN_(PRInt32) Compare( const char_type *other,
ComparatorFunc c = DefaultComparator ) const;
NS_HIDDEN_(PRInt32) Compare( const self_type &other,
ComparatorFunc c = DefaultComparator ) const;
NS_HIDDEN_(PRBool) Equals( const char_type *other,
ComparatorFunc c = DefaultComparator ) const;
NS_HIDDEN_(PRBool) Equals( const self_type &other,
ComparatorFunc c = DefaultComparator ) const;
NS_HIDDEN_(PRBool) operator < (const self_type &other) const
{
return Compare(other) < 0;
}
NS_HIDDEN_(PRBool) operator < (const char_type *other) const
{
return Compare(other) < 0;
}
NS_HIDDEN_(PRBool) operator <= (const self_type &other) const
{
return Compare(other) <= 0;
}
NS_HIDDEN_(PRBool) operator <= (const char_type *other) const
{
return Compare(other) <= 0;
}
NS_HIDDEN_(PRBool) operator == (const self_type &other) const
{
return Equals(other);
@@ -426,6 +502,33 @@ public:
return Equals(other);
}
NS_HIDDEN_(PRBool) operator >= (const self_type &other) const
{
return Compare(other) >= 0;
}
NS_HIDDEN_(PRBool) operator >= (const char_type *other) const
{
return Compare(other) >= 0;
}
NS_HIDDEN_(PRBool) operator > (const self_type &other) const
{
return Compare(other) > 0;
}
NS_HIDDEN_(PRBool) operator > (const char_type *other) const
{
return Compare(other) > 0;
}
NS_HIDDEN_(PRBool) operator != (const self_type &other) const
{
return !Equals(other);
}
NS_HIDDEN_(PRBool) operator != (const char_type *other) const
{
return !Equals(other);
}
NS_HIDDEN_(PRBool) EqualsLiteral( const char_type *other ) const
{
return Equals(other);

View File

@@ -21,6 +21,7 @@
* Contributor(s):
* Darin Fisher <darin@meer.net>
* Benjamin Smedberg <benjamin@smedbergs.us>
* Ben Turner <mozilla@songbirdnest.com>
*
* 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
@@ -547,6 +548,357 @@ static PRBool test_compressws()
return check.Equals(NS_LITERAL_STRING("Testing 1 2 3"));
}
static PRBool test_comparisons()
{
PRBool result;
// nsString
NS_NAMED_LITERAL_STRING(shortString1, "Foo");
NS_NAMED_LITERAL_STRING(shortString2, "Bar");
NS_NAMED_LITERAL_STRING(shortString3, "Bar");
NS_NAMED_LITERAL_STRING(shortString4, "bar");
NS_NAMED_LITERAL_STRING(longString, "FooBar");
// ==
result = (shortString1 == shortString2);
if (result)
return PR_FALSE;
result = (shortString2 == shortString3);
if (!result)
return PR_FALSE;
result = (shortString3 == shortString4);
if (result)
return PR_FALSE;
result = (shortString1 == longString);
if (result)
return PR_FALSE;
result = (longString == shortString1);
if (result)
return PR_FALSE;
// !=
result = (shortString1 != shortString2);
if (!result)
return PR_FALSE;
result = (shortString2 != shortString3);
if (result)
return PR_FALSE;
result = (shortString3 != shortString4);
if (!result)
return PR_FALSE;
result = (shortString1 != longString);
if (!result)
return PR_FALSE;
result = (longString != shortString1);
if (!result)
return PR_FALSE;
// <
result = (shortString1 < shortString2);
if (result)
return PR_FALSE;
result = (shortString2 < shortString1);
if (!result)
return PR_FALSE;
result = (shortString1 < longString);
if (!result)
return PR_FALSE;
result = (longString < shortString1);
if (result)
return PR_FALSE;
result = (shortString2 < shortString3);
if (result)
return PR_FALSE;
result = (shortString3 < shortString4);
if (!result)
return PR_FALSE;
result = (shortString4 < shortString3);
if (result)
return PR_FALSE;
// <=
result = (shortString1 <= shortString2);
if (result)
return PR_FALSE;
result = (shortString2 <= shortString1);
if (!result)
return PR_FALSE;
result = (shortString1 <= longString);
if (!result)
return PR_FALSE;
result = (longString <= shortString1);
if (result)
return PR_FALSE;
result = (shortString2 <= shortString3);
if (!result)
return PR_FALSE;
result = (shortString3 <= shortString4);
if (!result)
return PR_FALSE;
result = (shortString4 <= shortString3);
if (result)
return PR_FALSE;
// >
result = (shortString1 > shortString2);
if (!result)
return PR_FALSE;
result = (shortString2 > shortString1);
if (result)
return PR_FALSE;
result = (shortString1 > longString);
if (result)
return PR_FALSE;
result = (longString > shortString1);
if (!result)
return PR_FALSE;
result = (shortString2 > shortString3);
if (result)
return PR_FALSE;
result = (shortString3 > shortString4);
if (result)
return PR_FALSE;
result = (shortString4 > shortString3);
if (!result)
return PR_FALSE;
// >=
result = (shortString1 >= shortString2);
if (!result)
return PR_FALSE;
result = (shortString2 >= shortString1);
if (result)
return PR_FALSE;
result = (shortString1 >= longString);
if (result)
return PR_FALSE;
result = (longString >= shortString1);
if (!result)
return PR_FALSE;
result = (shortString2 >= shortString3);
if (!result)
return PR_FALSE;
result = (shortString3 >= shortString4);
if (result)
return PR_FALSE;
result = (shortString4 >= shortString3);
if (!result)
return PR_FALSE;
// nsCString
NS_NAMED_LITERAL_CSTRING(shortCString1, "Foo");
NS_NAMED_LITERAL_CSTRING(shortCString2, "Bar");
NS_NAMED_LITERAL_CSTRING(shortCString3, "Bar");
NS_NAMED_LITERAL_CSTRING(shortCString4, "bar");
NS_NAMED_LITERAL_CSTRING(longCString, "FooBar");
// ==
result = (shortCString1 == shortCString2);
if (result)
return PR_FALSE;
result = (shortCString2 == shortCString3);
if (!result)
return PR_FALSE;
result = (shortCString3 == shortCString4);
if (result)
return PR_FALSE;
result = (shortCString1 == longCString);
if (result)
return PR_FALSE;
result = (longCString == shortCString1);
if (result)
return PR_FALSE;
// !=
result = (shortCString1 != shortCString2);
if (!result)
return PR_FALSE;
result = (shortCString2 != shortCString3);
if (result)
return PR_FALSE;
result = (shortCString3 != shortCString4);
if (!result)
return PR_FALSE;
result = (shortCString1 != longCString);
if (!result)
return PR_FALSE;
result = (longCString != shortCString1);
if (!result)
return PR_FALSE;
// <
result = (shortCString1 < shortCString2);
if (result)
return PR_FALSE;
result = (shortCString2 < shortCString1);
if (!result)
return PR_FALSE;
result = (shortCString1 < longCString);
if (!result)
return PR_FALSE;
result = (longCString < shortCString1);
if (result)
return PR_FALSE;
result = (shortCString2 < shortCString3);
if (result)
return PR_FALSE;
result = (shortCString3 < shortCString4);
if (!result)
return PR_FALSE;
result = (shortCString4 < shortCString3);
if (result)
return PR_FALSE;
// <=
result = (shortCString1 <= shortCString2);
if (result)
return PR_FALSE;
result = (shortCString2 <= shortCString1);
if (!result)
return PR_FALSE;
result = (shortCString1 <= longCString);
if (!result)
return PR_FALSE;
result = (longCString <= shortCString1);
if (result)
return PR_FALSE;
result = (shortCString2 <= shortCString3);
if (!result)
return PR_FALSE;
result = (shortCString3 <= shortCString4);
if (!result)
return PR_FALSE;
result = (shortCString4 <= shortCString3);
if (result)
return PR_FALSE;
// >
result = (shortCString1 > shortCString2);
if (!result)
return PR_FALSE;
result = (shortCString2 > shortCString1);
if (result)
return PR_FALSE;
result = (shortCString1 > longCString);
if (result)
return PR_FALSE;
result = (longCString > shortCString1);
if (!result)
return PR_FALSE;
result = (shortCString2 > shortCString3);
if (result)
return PR_FALSE;
result = (shortCString3 > shortCString4);
if (result)
return PR_FALSE;
result = (shortCString4 > shortCString3);
if (!result)
return PR_FALSE;
// >=
result = (shortCString1 >= shortCString2);
if (!result)
return PR_FALSE;
result = (shortCString2 >= shortCString1);
if (result)
return PR_FALSE;
result = (shortCString1 >= longCString);
if (result)
return PR_FALSE;
result = (longCString >= shortCString1);
if (!result)
return PR_FALSE;
result = (shortCString2 >= shortCString3);
if (!result)
return PR_FALSE;
result = (shortCString3 >= shortCString4);
if (result)
return PR_FALSE;
result = (shortCString4 >= shortCString3);
if (!result)
return PR_FALSE;
return PR_TRUE;
}
//----
typedef PRBool (*TestFunc)();
@@ -573,6 +925,7 @@ tests[] =
{ "test_trim", test_trim },
{ "test_find", test_find },
{ "test_compressws", test_compressws },
{ "test_comparisons", test_comparisons },
{ nsnull, nsnull }
};