From 4ea952927c4357c0569808e6a15a5817905e00e0 Mon Sep 17 00:00:00 2001 From: "bent.mozilla%gmail.com" Date: Wed, 17 Jan 2007 21:10:02 +0000 Subject: [PATCH] Bug 366592 - "Add comparison operators to external string API". r=bsmedberg. git-svn-id: svn://10.0.0.236/trunk@218542 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/xpcom/glue/nsStringAPI.cpp | 75 +++- mozilla/xpcom/glue/nsStringAPI.h | 103 +++++ .../xpcom/tests/external/TestMinStringAPI.cpp | 353 ++++++++++++++++++ 3 files changed, 530 insertions(+), 1 deletion(-) diff --git a/mozilla/xpcom/glue/nsStringAPI.cpp b/mozilla/xpcom/glue/nsStringAPI.cpp index 648b0ad0d05..192f26ca6a3 100644 --- a/mozilla/xpcom/glue/nsStringAPI.cpp +++ b/mozilla/xpcom/glue/nsStringAPI.cpp @@ -21,6 +21,7 @@ * Contributor(s): * Darin Fisher * Benjamin Smedberg + * Ben Turner * * 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 { diff --git a/mozilla/xpcom/glue/nsStringAPI.h b/mozilla/xpcom/glue/nsStringAPI.h index f30e09700a8..81d005d9602 100644 --- a/mozilla/xpcom/glue/nsStringAPI.h +++ b/mozilla/xpcom/glue/nsStringAPI.h @@ -21,6 +21,7 @@ * Contributor(s): * Darin Fisher * Benjamin Smedberg + * Ben Turner * * 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); diff --git a/mozilla/xpcom/tests/external/TestMinStringAPI.cpp b/mozilla/xpcom/tests/external/TestMinStringAPI.cpp index 35b011024ab..c91fe7f1d78 100644 --- a/mozilla/xpcom/tests/external/TestMinStringAPI.cpp +++ b/mozilla/xpcom/tests/external/TestMinStringAPI.cpp @@ -21,6 +21,7 @@ * Contributor(s): * Darin Fisher * Benjamin Smedberg + * Ben Turner * * 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 } };