diff --git a/content/xul/templates/src/nsXULSortService.cpp b/content/xul/templates/src/nsXULSortService.cpp index 1c6f1b7da3e5..6c1d857ec1f0 100644 --- a/content/xul/templates/src/nsXULSortService.cpp +++ b/content/xul/templates/src/nsXULSortService.cpp @@ -929,17 +929,19 @@ XULSortServiceImpl::CompareNodes(nsIRDFNode *cellNode1, PRBool isCollationKey1, nsresult rv = NS_ERROR_FAILURE; bothValid = PR_TRUE; sortOrder = 0; - if(collationService) + nsDependentString uni1Str(uni1); + nsDependentString uni2Str(uni2); + if (collationService) { - nsAutoString v1(uni1); - nsAutoString v2(uni2); rv = collationService->CompareString( kCollationCaseInSensitive, - v1,v2,&sortOrder); + uni1Str, + uni2Str, + &sortOrder); } if (NS_FAILED(rv)) { - sortOrder = Compare(nsDependentString(uni1), - nsDependentString(uni2), + sortOrder = Compare(uni1Str, + uni2Str, nsCaseInsensitiveStringComparator()); } } diff --git a/intl/locale/public/nsICollation.h b/intl/locale/public/nsICollation.h index 11506ccb28af..b055795a5a9d 100644 --- a/intl/locale/public/nsICollation.h +++ b/intl/locale/public/nsICollation.h @@ -70,17 +70,17 @@ public: // compare two strings // result is same as strcmp NS_IMETHOD CompareString(const nsCollationStrength strength, - const nsString& string1, const nsString& string2, PRInt32* result) = 0; + const nsAString& string1, const nsAString& string2, PRInt32* result) = 0; // get a length (of character) of a sort key to be generated by an input string // length is a byte length NS_IMETHOD GetSortKeyLen(const nsCollationStrength strength, - const nsString& stringIn, PRUint32* outLen) = 0; + const nsAString& stringIn, PRUint32* outLen) = 0; // create sort key from input string // length is a byte length, caller should allocate a memory for a key NS_IMETHOD CreateRawSortKey(const nsCollationStrength strength, - const nsString& stringIn, PRUint8* key, PRUint32 *outLen) = 0; + const nsAString& stringIn, PRUint8* key, PRUint32 *outLen) = 0; // compare two sort keys // length is a byte length, result is same as strcmp diff --git a/intl/locale/src/mac/nsCollationMac.cpp b/intl/locale/src/mac/nsCollationMac.cpp index 7ec85ba35da0..50a41dec3588 100644 --- a/intl/locale/src/mac/nsCollationMac.cpp +++ b/intl/locale/src/mac/nsCollationMac.cpp @@ -207,21 +207,24 @@ nsresult nsCollationMac::Initialize(nsILocale* locale) nsresult nsCollationMac::GetSortKeyLen(const nsCollationStrength strength, - const nsString& stringIn, PRUint32* outLen) + const nsAString& stringIn, PRUint32* outLen) { *outLen = stringIn.Length() * sizeof(PRUnichar); return NS_OK; } nsresult nsCollationMac::CreateRawSortKey(const nsCollationStrength strength, - const nsString& stringIn, PRUint8* key, PRUint32* outLen) + const nsAString& stringIn, PRUint8* key, PRUint32* outLen) { nsresult res = NS_OK; - nsAutoString stringNormalized(stringIn); + nsAutoString stringNormalized; if (strength != kCollationCaseSensitive) { - res = mCollation->NormalizeString(stringNormalized); + res = mCollation->NormalizeString(stringIn, stringNormalized); + } else { + stringNormalized = stringIn; } + // convert unicode to charset char *str; int str_len; diff --git a/intl/locale/src/mac/nsCollationMac.h b/intl/locale/src/mac/nsCollationMac.h index 4812e6718802..b739880f4fc2 100644 --- a/intl/locale/src/mac/nsCollationMac.h +++ b/intl/locale/src/mac/nsCollationMac.h @@ -44,18 +44,18 @@ public: // compare two strings // result is same as strcmp NS_IMETHOD CompareString(const nsCollationStrength strength, - const nsString& string1, const nsString& string2, PRInt32* result) + const nsAString& string1, const nsAString& string2, PRInt32* result) {return mCollation->CompareString(this, strength, string1, string2, result);} // get a length (of character) of a sort key to be generated by an input string // length is character length not byte length NS_IMETHOD GetSortKeyLen(const nsCollationStrength strength, - const nsString& stringIn, PRUint32* outLen); + const nsAString& stringIn, PRUint32* outLen); // create sort key from input string // length is character length not byte length, caller to allocate a memory for a key NS_IMETHOD CreateRawSortKey(const nsCollationStrength strength, - const nsString& stringIn, PRUint8* key, PRUint32* outLen); + const nsAString& stringIn, PRUint8* key, PRUint32* outLen); // compare two sort keys // length is character length not byte length, result is same as strcmp diff --git a/intl/locale/src/nsCollation.cpp b/intl/locale/src/nsCollation.cpp index 073cdd9ae932..185a9f446552 100644 --- a/intl/locale/src/nsCollation.cpp +++ b/intl/locale/src/nsCollation.cpp @@ -46,12 +46,11 @@ #include "nsCollationCID.h" #include "nsUnicharUtilCIID.h" #include "prmem.h" +#include "nsReadableUtils.h" //////////////////////////////////////////////////////////////////////////////// -NS_DEFINE_IID(kICollationFactoryIID, NS_ICOLLATIONFACTORY_IID); NS_DEFINE_CID(kCollationCID, NS_COLLATION_CID); -static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID); NS_IMPL_ISUPPORTS1(nsCollationFactory, nsICollationFactory); @@ -62,7 +61,7 @@ nsresult nsCollationFactory::CreateCollation(nsILocale* locale, nsICollation** i nsICollation *inst; nsresult res; - res = nsComponentManager::CreateInstance(kCollationCID, NULL, NS_GET_IID(nsICollation), (void**) &inst); + res = CallCreateInstance(kCollationCID, &inst); if (NS_FAILED(res)) { return res; } @@ -75,24 +74,19 @@ nsresult nsCollationFactory::CreateCollation(nsILocale* locale, nsICollation** i //////////////////////////////////////////////////////////////////////////////// -NS_DEFINE_CID(kUnicharUtilCID, NS_UNICHARUTIL_CID); -NS_DEFINE_IID(kCaseConversionIID, NS_ICASECONVERSION_IID); - nsCollation::nsCollation() { - mCaseConversion = NULL; - nsresult res = nsComponentManager::CreateInstance(kUnicharUtilCID, NULL, kCaseConversionIID, (void**) &mCaseConversion); + nsresult res; + mCaseConversion = do_CreateInstance(NS_UNICHARUTIL_CONTRACTID, &res); NS_ASSERTION(NS_SUCCEEDED(res), "CreateInstance failed for kCaseConversionIID"); } nsCollation::~nsCollation() { - if (mCaseConversion != NULL) - mCaseConversion->Release(); } nsresult nsCollation::CompareString(nsICollation *inst, const nsCollationStrength strength, - const nsString& string1, const nsString& string2, PRInt32* result) + const nsAString& string1, const nsAString& string2, PRInt32* result) { PRUint32 aLength1, aLength2; PRUint8 *aKey1, *aKey2; @@ -166,34 +160,35 @@ PRInt32 nsCollation::CompareRawSortKey(const PRUint8* key1, const PRUint32 len1, return result; } -nsresult nsCollation::NormalizeString(nsString& stringInOut) +nsresult nsCollation::NormalizeString(const nsAString& stringIn, nsAString& stringOut) { - if (mCaseConversion == NULL) { - stringInOut.ToLowerCase(); + if (!mCaseConversion) { + stringOut = stringIn; + ToLowerCase(stringOut); // XXXjag Can this ever happen in a normal situation? } else { - PRInt32 aLength = stringInOut.Length(); + PRInt32 aLength = stringIn.Length(); if (aLength <= 64) { - PRUnichar conversionBuf[64]; - mCaseConversion->ToLower(stringInOut.get(), conversionBuf, aLength); - stringInOut.Assign(conversionBuf, aLength); + PRUnichar conversionBuffer[64]; + mCaseConversion->ToLower(PromiseFlatString(stringIn).get(), conversionBuffer, aLength); + stringOut.Assign(conversionBuffer, aLength); } else { - PRUnichar *aBuffer; - aBuffer = new PRUnichar[aLength]; - if (aBuffer == NULL) { + PRUnichar* conversionBuffer; + conversionBuffer = new PRUnichar[aLength]; + if (!conversionBuffer) { return NS_ERROR_OUT_OF_MEMORY; } - mCaseConversion->ToLower(stringInOut.get(), aBuffer, aLength); - stringInOut.Assign(aBuffer, aLength); - delete [] aBuffer; + mCaseConversion->ToLower(PromiseFlatString(stringIn).get(), conversionBuffer, aLength); + stringOut.Assign(conversionBuffer, aLength); + delete [] conversionBuffer; } } return NS_OK; } -nsresult nsCollation::UnicodeToChar(const nsString& src, char** dst, const nsString& aCharset) +nsresult nsCollation::UnicodeToChar(const nsAString& aSrc, char** dst, const nsAString& aCharset) { NS_ENSURE_ARG_POINTER(dst); @@ -204,13 +199,14 @@ nsresult nsCollation::UnicodeToChar(const nsString& src, char** dst, const nsStr if (NS_SUCCEEDED(res)) { nsCOMPtr charsetAtom; - res = mCharsetConverterManager->GetCharsetAtom(aCharset.get(), getter_AddRefs(charsetAtom)); + res = mCharsetConverterManager->GetCharsetAtom(PromiseFlatString(aCharset).get(), getter_AddRefs(charsetAtom)); if (NS_SUCCEEDED(res)) { if (charsetAtom != mEncoderCharsetAtom) { mEncoderCharsetAtom = charsetAtom; res = mCharsetConverterManager->GetUnicodeEncoder(mEncoderCharsetAtom, getter_AddRefs(mEncoder)); } if (NS_SUCCEEDED(res)) { + const nsPromiseFlatString& src = PromiseFlatString(aSrc); const PRUnichar *unichars = src.get(); PRInt32 unicharLength = src.Length(); PRInt32 dstLength; diff --git a/intl/locale/src/nsCollation.h b/intl/locale/src/nsCollation.h index a87c05e2bf72..e3a094ef7459 100644 --- a/intl/locale/src/nsCollation.h +++ b/intl/locale/src/nsCollation.h @@ -55,7 +55,7 @@ public: // compare two strings // result is same as strcmp nsresult CompareString(nsICollation *inst, const nsCollationStrength strength, - const nsString& string1, const nsString& string2, PRInt32* result); + const nsAString& string1, const nsAString& string2, PRInt32* result); // compare two sort keys // length is a byte length, result is same as strcmp @@ -63,14 +63,14 @@ public: const PRUint8* key2, const PRUint32 len2); // normalize string before collation key generation - nsresult NormalizeString(nsString& stringInOut); + nsresult NormalizeString(const nsAString& stringIn, nsAString& stringOut); // charset conversion util, C string buffer is allocate by PR_Malloc, caller should call PR_Free - nsresult UnicodeToChar(const nsString& src, char** dst, const nsString& aCharset); + nsresult UnicodeToChar(const nsAString& aSrc, char** dst, const nsAString& aCharset); protected: - nsICaseConversion* mCaseConversion; + nsCOMPtr mCaseConversion; nsCOMPtr mEncoder; nsCOMPtr mEncoderCharsetAtom; nsCOMPtr mCharsetConverterManager; diff --git a/intl/locale/src/os2/nsCollationOS2.cpp b/intl/locale/src/os2/nsCollationOS2.cpp index a64de1a1ab80..bdeed4aa9850 100644 --- a/intl/locale/src/os2/nsCollationOS2.cpp +++ b/intl/locale/src/os2/nsCollationOS2.cpp @@ -156,14 +156,16 @@ nsresult nsCollationOS2::Initialize(nsILocale *locale) nsresult nsCollationOS2::GetSortKeyLen(const nsCollationStrength strength, - const nsString& stringIn, PRUint32* outLen) + const nsAString& stringIn, PRUint32* outLen) { // this may not necessary because collation key length // probably will not change by this normalization nsresult res = NS_OK; - nsString stringNormalized = stringIn; + nsAutoString stringNormalized; if (strength != kCollationCaseSensitive) { - res = mCollation->NormalizeString(stringNormalized); + res = mCollation->NormalizeString(stringIn, stringNormalized); + } else { + stringNormalized = stringIn; } LocaleObject locObj = NULL; @@ -186,13 +188,15 @@ nsresult nsCollationOS2::GetSortKeyLen(const nsCollationStrength strength, nsresult nsCollationOS2::CreateRawSortKey(const nsCollationStrength strength, - const nsString& stringIn, PRUint8* key, PRUint32* outLen) + const nsAString& stringIn, PRUint8* key, PRUint32* outLen) { nsresult res = NS_OK; - nsString stringNormalized = stringIn; + nsAutoString stringNormalized; if (strength != kCollationCaseSensitive) { - res = mCollation->NormalizeString(stringNormalized); + res = mCollation->NormalizeString(stringIn, stringNormalized); + } else { + stringNormalized = stringIn; } LocaleObject locObj = NULL; diff --git a/intl/locale/src/os2/nsCollationOS2.h b/intl/locale/src/os2/nsCollationOS2.h index 2e735b744627..4022ab4686a6 100644 --- a/intl/locale/src/os2/nsCollationOS2.h +++ b/intl/locale/src/os2/nsCollationOS2.h @@ -40,18 +40,18 @@ public: // compare two strings // result is same as strcmp NS_IMETHOD CompareString(const nsCollationStrength strength, - const nsString& string1, const nsString& string2, PRInt32* result) + const nsAString& string1, const nsAString& string2, PRInt32* result) {return mCollation->CompareString(this, strength, string1, string2, result);} // get a length (of character) of a sort key to be generated by an input string // length is a byte length NS_IMETHOD GetSortKeyLen(const nsCollationStrength strength, - const nsString& stringIn, PRUint32* outLen); + const nsAString& stringIn, PRUint32* outLen); // create sort key from input string // length is character length not byte length, caller to allocate a memory for a key NS_IMETHOD CreateRawSortKey(const nsCollationStrength strength, - const nsString& stringIn, PRUint8* key, PRUint32* outLen); + const nsAString& stringIn, PRUint8* key, PRUint32* outLen); // compare two sort keys // length is character length not byte length, result is same as strcmp diff --git a/intl/locale/src/unix/nsCollationUnix.cpp b/intl/locale/src/unix/nsCollationUnix.cpp index f458e64ad9c2..dda46ec240c7 100644 --- a/intl/locale/src/unix/nsCollationUnix.cpp +++ b/intl/locale/src/unix/nsCollationUnix.cpp @@ -180,15 +180,18 @@ nsresult nsCollationUnix::Initialize(nsILocale* locale) nsresult nsCollationUnix::GetSortKeyLen(const nsCollationStrength strength, - const nsString& stringIn, PRUint32* outLen) + const nsAString& stringIn, + PRUint32* outLen) { nsresult res = NS_OK; // this may not necessary because collation key length // probably will not change by this normalization - nsString stringNormalized = stringIn; + nsAutoString stringNormalized; if (strength != kCollationCaseSensitive) { - res = mCollation->NormalizeString(stringNormalized); + res = mCollation->NormalizeString(stringIn, stringNormalized); + } else { + stringNormalized = stringIn; } // convert unicode to charset @@ -213,13 +216,16 @@ nsresult nsCollationUnix::GetSortKeyLen(const nsCollationStrength strength, } nsresult nsCollationUnix::CreateRawSortKey(const nsCollationStrength strength, - const nsString& stringIn, PRUint8* key, PRUint32* outLen) + const nsAString& stringIn, + PRUint8* key, PRUint32* outLen) { nsresult res = NS_OK; - nsString stringNormalized = stringIn; + nsAutoString stringNormalized; if (strength != kCollationCaseSensitive) { - res = mCollation->NormalizeString(stringNormalized); + res = mCollation->NormalizeString(stringIn, stringNormalized); + } else { + stringNormalized = stringIn; } // convert unicode to charset char *str; diff --git a/intl/locale/src/unix/nsCollationUnix.h b/intl/locale/src/unix/nsCollationUnix.h index f1fa4b9b9762..c5b349ccce1d 100644 --- a/intl/locale/src/unix/nsCollationUnix.h +++ b/intl/locale/src/unix/nsCollationUnix.h @@ -51,18 +51,18 @@ public: // compare two strings // result is same as strcmp NS_IMETHOD CompareString(const nsCollationStrength strength, - const nsString& string1, const nsString& string2, PRInt32* result) + const nsAString& string1, const nsAString& string2, PRInt32* result) {return mCollation->CompareString(this, strength, string1, string2, result);} // get a length (of character) of a sort key to be generated by an input string // length is character length not byte length NS_IMETHOD GetSortKeyLen(const nsCollationStrength strength, - const nsString& stringIn, PRUint32* outLen); + const nsAString& stringIn, PRUint32* outLen); // create sort key from input string // length is character length not byte length, caller to allocate a memory for a key NS_IMETHOD CreateRawSortKey(const nsCollationStrength strength, - const nsString& stringIn, PRUint8* key, PRUint32* outLen); + const nsAString& stringIn, PRUint8* key, PRUint32* outLen); // compare two sort keys // length is character length not byte length, result is same as strcmp diff --git a/intl/locale/src/windows/nsCollationWin.cpp b/intl/locale/src/windows/nsCollationWin.cpp index c3949631c4f6..5bb28a982115 100644 --- a/intl/locale/src/windows/nsCollationWin.cpp +++ b/intl/locale/src/windows/nsCollationWin.cpp @@ -147,14 +147,15 @@ nsresult nsCollationWin::Initialize(nsILocale* locale) nsresult nsCollationWin::GetSortKeyLen(const nsCollationStrength strength, - const nsString& stringIn, PRUint32* outLen) + const nsAString& stringIn, PRUint32* outLen) { nsresult res = NS_OK; // Currently, no length change by the normalization. // API returns number of bytes when LCMAP_SORTKEY is specified if (mW_API) { *outLen = LCMapStringW(mLCID, LCMAP_SORTKEY, - (LPCWSTR) stringIn.get(), (int) stringIn.Length(), NULL, 0); + (LPCWSTR) PromiseFlatString(stringIn).get(), + (int) stringIn.Length(), NULL, 0); } else { char *Cstr = nsnull; @@ -169,14 +170,16 @@ nsresult nsCollationWin::GetSortKeyLen(const nsCollationStrength strength, } nsresult nsCollationWin::CreateRawSortKey(const nsCollationStrength strength, - const nsString& stringIn, PRUint8* key, PRUint32* outLen) + const nsAString& stringIn, PRUint8* key, PRUint32* outLen) { int byteLen; nsresult res = NS_OK; - nsAutoString stringNormalized; stringNormalized.Assign(stringIn); + nsAutoString stringNormalized; if (mCollation != NULL && strength == kCollationCaseInSensitive) { - mCollation->NormalizeString(stringNormalized); + mCollation->NormalizeString(stringIn, stringNormalized); + } else { + stringNormalized = stringIn; } if (mW_API) { diff --git a/intl/locale/src/windows/nsCollationWin.h b/intl/locale/src/windows/nsCollationWin.h index 7aa1d50d5c13..03aa48f026d2 100644 --- a/intl/locale/src/windows/nsCollationWin.h +++ b/intl/locale/src/windows/nsCollationWin.h @@ -43,18 +43,18 @@ public: // compare two strings // result is same as strcmp NS_IMETHOD CompareString(const nsCollationStrength strength, - const nsString& string1, const nsString& string2, PRInt32* result) + const nsAString& string1, const nsAString& string2, PRInt32* result) {return mCollation->CompareString(this, strength, string1, string2, result);} // get a length (of character) of a sort key to be generated by an input string // length is a byte length NS_IMETHOD GetSortKeyLen(const nsCollationStrength strength, - const nsString& stringIn, PRUint32* outLen); + const nsAString& stringIn, PRUint32* outLen); // create sort key from input string // length is a byte length, caller should allocate a memory for a key NS_IMETHOD CreateRawSortKey(const nsCollationStrength strength, - const nsString& stringIn, PRUint8* key, PRUint32* outLen); + const nsAString& stringIn, PRUint8* key, PRUint32* outLen); // compare two sort keys // length is a byte length, result is same as strcmp