diff --git a/intl/locale/src/nsCollation.cpp b/intl/locale/src/nsCollation.cpp index 611cf4d5441..ec4db6abb0d 100644 --- a/intl/locale/src/nsCollation.cpp +++ b/intl/locale/src/nsCollation.cpp @@ -83,10 +83,28 @@ nsresult nsCollation::CompareString(nsICollation *inst, const nsCollationStrengt PRUint8 *aKey1, *aKey2; nsresult res; - // Create a key for string1 res = inst->GetSortKeyLen(strength, string1, &aLength1); if (NS_FAILED(res)) return res; + res = inst->GetSortKeyLen(strength, string2, &aLength2); + if (NS_FAILED(res)) { + return res; + } + + // if input string is small then use local buffer for keys + if (aLength1 <= 128 && aLength2 <= 128) { + PRUint8 aKeyBuf1[128], aKeyBuf2[128]; + res = inst->CreateRawSortKey(strength, string1, aKeyBuf1, &aLength1); + if (NS_SUCCEEDED(res)) { + res = inst->CreateRawSortKey(strength, string2, aKeyBuf2, &aLength2); + if (NS_SUCCEEDED(res)) { + *result = CompareRawSortKey(aKeyBuf1, aLength1, aKeyBuf2, aLength2); + } + } + return res; + } + + // Create a key for string1 aKey1 = new PRUint8[aLength1]; if (NULL == aKey1) return NS_ERROR_OUT_OF_MEMORY; @@ -97,11 +115,6 @@ nsresult nsCollation::CompareString(nsICollation *inst, const nsCollationStrengt } // Create a key for string2 - res = inst->GetSortKeyLen(strength, string2, &aLength2); - if (NS_FAILED(res)) { - delete [] aKey1; - return res; - } aKey2 = new PRUint8[aLength2]; if (NULL == aKey2) { delete [] aKey1; @@ -189,22 +202,29 @@ PRInt32 nsCollation::CompareSortKey(const nsString& key1, const nsString& key2) return CompareRawSortKey(rawKey1, len1, rawKey2, len2); } -nsresult nsCollation::NormalizeString(nsAutoString& stringInOut) +nsresult nsCollation::NormalizeString(nsString& stringInOut) { if (mCaseConversion == NULL) { stringInOut.ToLowerCase(); } else { - PRUnichar *aBuffer; PRInt32 aLength = stringInOut.Length(); - aBuffer = new PRUnichar[aLength]; - if (aBuffer == NULL) { - return NS_ERROR_OUT_OF_MEMORY; + if (aLength <= 64) { + PRUnichar conversionBuf[64]; + mCaseConversion->ToLower(stringInOut.GetUnicode(), conversionBuf, aLength); + stringInOut.Assign(conversionBuf, aLength); + } + else { + PRUnichar *aBuffer; + aBuffer = new PRUnichar[aLength]; + if (aBuffer == NULL) { + return NS_ERROR_OUT_OF_MEMORY; + } + mCaseConversion->ToLower(stringInOut.GetUnicode(), aBuffer, aLength); + stringInOut.Assign(aBuffer, aLength); + delete [] aBuffer; } - mCaseConversion->ToLower(stringInOut.GetUnicode(), aBuffer, aLength); - stringInOut.Assign(aBuffer, aLength); - delete [] aBuffer; } return NS_OK; } diff --git a/intl/locale/src/nsCollation.h b/intl/locale/src/nsCollation.h index 3cfb30b8488..4ad905c22d1 100644 --- a/intl/locale/src/nsCollation.h +++ b/intl/locale/src/nsCollation.h @@ -67,7 +67,7 @@ public: PRInt32 CompareSortKey(const nsString& key1, const nsString& key2); // normalize string before collation key generation - nsresult NormalizeString(nsAutoString& stringInOut); + nsresult NormalizeString(nsString& stringInOut); // 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);