From 0aa9780658a1899e753a9e472a0b5e4d59b8e65e Mon Sep 17 00:00:00 2001 From: "nhotta%netscape.com" Date: Fri, 19 Mar 1999 23:21:43 +0000 Subject: [PATCH] Added two functions which use nsString as a sort key. --- intl/locale/public/nsICollation.h | 15 +++++-- intl/locale/src/mac/nsCollationMac.cpp | 2 +- intl/locale/src/mac/nsCollationMac.h | 22 ++++++---- intl/locale/src/nsCollation.cpp | 48 +++++++++++++++++++--- intl/locale/src/nsCollation.h | 11 ++++- intl/locale/src/unix/nsCollationUnix.cpp | 2 +- intl/locale/src/unix/nsCollationUnix.h | 21 +++++++--- intl/locale/src/windows/nsCollationWin.cpp | 6 +-- intl/locale/src/windows/nsCollationWin.h | 22 +++++++--- intl/locale/tests/LocaleSelfTest.cpp | 41 ++++++++++++------ 10 files changed, 143 insertions(+), 47 deletions(-) diff --git a/intl/locale/public/nsICollation.h b/intl/locale/public/nsICollation.h index 7392390b1092..28da73f13593 100644 --- a/intl/locale/public/nsICollation.h +++ b/intl/locale/public/nsICollation.h @@ -73,14 +73,21 @@ public: // 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; + + // create a sort key (nsString) NS_IMETHOD CreateSortKey(const nsCollationStrength strength, - const nsString& stringIn, PRUint8* key, PRUint32 *outLen) = 0; + const nsString& stringIn, nsString& key) = 0; // compare two sort keys // length is a byte length, result is same as strcmp - NS_IMETHOD CompareSortKey(const PRUint8* key1, const PRUint32 len1, - const PRUint8* key2, const PRUint32 len2, - PRInt32* result) = 0; + NS_IMETHOD CompareRawSortKey(const PRUint8* key1, const PRUint32 len1, + const PRUint8* key2, const PRUint32 len2, + PRInt32* result) = 0; + + // compare two sort keys (nsString) + NS_IMETHOD CompareSortKey(const nsString& key1, const nsString& key2, PRInt32* result) = 0; // init this interface to a specified locale (should only be called by collation factory) // diff --git a/intl/locale/src/mac/nsCollationMac.cpp b/intl/locale/src/mac/nsCollationMac.cpp index 747ad66328c5..8a86ea3760b0 100644 --- a/intl/locale/src/mac/nsCollationMac.cpp +++ b/intl/locale/src/mac/nsCollationMac.cpp @@ -154,7 +154,7 @@ nsresult nsCollationMac::GetSortKeyLen(const nsCollationStrength strength, return NS_OK; } -nsresult nsCollationMac::CreateSortKey(const nsCollationStrength strength, +nsresult nsCollationMac::CreateRawSortKey(const nsCollationStrength strength, const nsString& stringIn, PRUint8* key, PRUint32* outLen) { nsresult res = NS_OK; diff --git a/intl/locale/src/mac/nsCollationMac.h b/intl/locale/src/mac/nsCollationMac.h index ddb84eb16e2b..4a5c128534f9 100644 --- a/intl/locale/src/mac/nsCollationMac.h +++ b/intl/locale/src/mac/nsCollationMac.h @@ -41,7 +41,7 @@ public: // result is same as strcmp NS_IMETHOD CompareString(const nsCollationStrength strength, const nsString& string1, const nsString& string2, PRInt32* result) - {return mCollation->CompareString(this, strength, string1, string2, 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 @@ -50,15 +50,23 @@ public: // create sort key from input string // length is character length not byte length, caller to allocate a memory for a key - NS_IMETHOD CreateSortKey(const nsCollationStrength strength, - const nsString& stringIn, PRUint8* key, PRUint32* outLen); + NS_IMETHOD CreateRawSortKey(const nsCollationStrength strength, + const nsString& stringIn, PRUint8* key, PRUint32* outLen); + // create a sort key (nsString) + NS_IMETHOD CreateSortKey(const nsCollationStrength strength, + const nsString& stringIn, nsString& key) + {return mCollation->CreateSortKey(this, strength, stringIn, key);} // compare two sort keys // length is character length not byte length, result is same as strcmp - NS_IMETHOD CompareSortKey(const PRUint8* key1, const PRUint32 len1, - const PRUint8* key2, const PRUint32 len2, - PRInt32* result) - {*result = mCollation->CompareSortKey(key1, len1, key2, len2);return NS_OK;}; + NS_IMETHOD CompareRawSortKey(const PRUint8* key1, const PRUint32 len1, + const PRUint8* key2, const PRUint32 len2, + PRInt32* result) + {*result = mCollation->CompareRawSortKey(key1, len1, key2, len2);return NS_OK;} + + // compare two sort keys (nsString) + NS_IMETHOD CompareSortKey(const nsString& key1, const nsString& key2, PRInt32* result) + {*result = mCollation->CompareSortKey(key1, key2);return NS_OK;} // init this interface to a specified locale (should only be called by collation factory) // diff --git a/intl/locale/src/nsCollation.cpp b/intl/locale/src/nsCollation.cpp index 8e1dc90e3e58..2902b40be8a1 100644 --- a/intl/locale/src/nsCollation.cpp +++ b/intl/locale/src/nsCollation.cpp @@ -83,7 +83,7 @@ nsresult nsCollation::CompareString(nsICollation *inst, const nsCollationStrengt aKey1 = new PRUint8[aLength1]; if (NULL == aKey1) return NS_ERROR_OUT_OF_MEMORY; - res = inst->CreateSortKey(strength, string1, aKey1, &aLength1); + res = inst->CreateRawSortKey(strength, string1, aKey1, &aLength1); if (NS_FAILED(res)) { delete [] aKey1; return res; @@ -100,7 +100,7 @@ nsresult nsCollation::CompareString(nsICollation *inst, const nsCollationStrengt delete [] aKey1; return NS_ERROR_OUT_OF_MEMORY; } - res = inst->CreateSortKey(strength, string2, aKey2, &aLength2); + res = inst->CreateRawSortKey(strength, string2, aKey2, &aLength2); if (NS_FAILED(res)) { delete [] aKey1; delete [] aKey2; @@ -108,7 +108,7 @@ nsresult nsCollation::CompareString(nsICollation *inst, const nsCollationStrengt } // Compare keys - *result = CompareSortKey(aKey1, aLength1, aKey2, aLength2); + *result = CompareRawSortKey(aKey1, aLength1, aKey2, aLength2); // delete keys delete [] aKey1; @@ -117,8 +117,33 @@ nsresult nsCollation::CompareString(nsICollation *inst, const nsCollationStrengt return res; } -PRInt32 nsCollation::CompareSortKey(const PRUint8* key1, const PRUint32 len1, - const PRUint8* key2, const PRUint32 len2) +nsresult nsCollation::CreateSortKey(nsICollation *inst, const nsCollationStrength strength, + const nsString& stringIn, nsString& key) +{ + PRUint32 aLength; + PRUint8 *aKey; + nsresult res; + + res = inst->GetSortKeyLen(strength, stringIn, &aLength); + if (NS_SUCCEEDED(res)) { + aKey = new PRUint8[aLength]; + if (nsnull != aKey) { + res = inst->CreateRawSortKey(strength, stringIn, aKey, &aLength); + if (NS_SUCCEEDED(res)) { + key.SetString((PRUnichar *) aKey, aLength / sizeof(PRUnichar)); + } + delete [] aKey; + } + else { + res = NS_ERROR_OUT_OF_MEMORY; + } + } + + return res; +} + +PRInt32 nsCollation::CompareRawSortKey(const PRUint8* key1, const PRUint32 len1, + const PRUint8* key2, const PRUint32 len2) { PRUint32 len = (len1 < len2) ? len1 : len2; PRInt32 result; @@ -131,6 +156,19 @@ PRInt32 nsCollation::CompareSortKey(const PRUint8* key1, const PRUint32 len1, return result; } +PRInt32 nsCollation::CompareSortKey(const nsString& key1, const nsString& key2) +{ + PRUint8 *rawKey1, *rawKey2; + PRUint32 len1, len2; + + rawKey1 = (PRUint8 *) key1.GetUnicode(); + len1 = key1.Length() * sizeof(PRUnichar); + rawKey2 = (PRUint8 *) key2.GetUnicode(); + len2 = key2.Length() * sizeof(PRUnichar); + + return CompareRawSortKey(rawKey1, len1, rawKey2, len2); +} + nsresult nsCollation::NormalizeString(nsAutoString& stringInOut) { if (mCaseConversion == NULL) { diff --git a/intl/locale/src/nsCollation.h b/intl/locale/src/nsCollation.h index f8c23dbf0a9b..18db150abdba 100644 --- a/intl/locale/src/nsCollation.h +++ b/intl/locale/src/nsCollation.h @@ -50,10 +50,17 @@ public: nsresult CompareString(nsICollation *inst, const nsCollationStrength strength, const nsString& string1, const nsString& string2, PRInt32* result); + // create a sort key (nsString) + nsresult CreateSortKey(nsICollation *inst, const nsCollationStrength strength, + const nsString& stringIn, nsString& key); + // compare two sort keys // length is a byte length, result is same as strcmp - PRInt32 CompareSortKey(const PRUint8* key1, const PRUint32 len1, - const PRUint8* key2, const PRUint32 len2); + PRInt32 CompareRawSortKey(const PRUint8* key1, const PRUint32 len1, + const PRUint8* key2, const PRUint32 len2); + + // compare two sort keys (nsString) + PRInt32 CompareSortKey(const nsString& key1, const nsString& key2); // normalize string before collation key generation nsresult NormalizeString(nsAutoString& stringInOut); diff --git a/intl/locale/src/unix/nsCollationUnix.cpp b/intl/locale/src/unix/nsCollationUnix.cpp index 9464ac5039f1..d973a297d517 100644 --- a/intl/locale/src/unix/nsCollationUnix.cpp +++ b/intl/locale/src/unix/nsCollationUnix.cpp @@ -100,7 +100,7 @@ nsresult nsCollationUnix::GetSortKeyLen(const nsCollationStrength strength, return res; } -nsresult nsCollationUnix::CreateSortKey(const nsCollationStrength strength, +nsresult nsCollationUnix::CreateRawSortKey(const nsCollationStrength strength, const nsString& stringIn, PRUint8* key, PRUint32* outLen) { nsresult res = NS_OK; diff --git a/intl/locale/src/unix/nsCollationUnix.h b/intl/locale/src/unix/nsCollationUnix.h index 5d25678c257e..84e6adda75ae 100644 --- a/intl/locale/src/unix/nsCollationUnix.h +++ b/intl/locale/src/unix/nsCollationUnix.h @@ -39,7 +39,7 @@ public: // result is same as strcmp NS_IMETHOD CompareString(const nsCollationStrength strength, const nsString& string1, const nsString& string2, PRInt32* result) - {return mCollation->CompareString(this, strength, string1, string2, 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 @@ -48,15 +48,24 @@ public: // 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); + + // create a sort key (nsString) NS_IMETHOD CreateSortKey(const nsCollationStrength strength, - const nsString& stringIn, PRUint8* key, PRUint32* outLen); + const nsString& stringIn, nsString& key) + {return mCollation->CreateSortKey(this, strength, stringIn, key);} // compare two sort keys // length is character length not byte length, result is same as strcmp - NS_IMETHOD CompareSortKey(const PRUint8* key1, const PRUint32 len1, - const PRUint8* key2, const PRUint32 len2, - PRInt32* result) - {*result = mCollation->CompareSortKey(key1, len1, key2, len2);return NS_OK;}; + NS_IMETHOD CompareRawSortKey(const PRUint8* key1, const PRUint32 len1, + const PRUint8* key2, const PRUint32 len2, + PRInt32* result) + {*result = mCollation->CompareRawSortKey(key1, len1, key2, len2);return NS_OK;} + + // compare two sort keys (nsString) + NS_IMETHOD CompareSortKey(const nsString& key1, const nsString& key2, PRInt32* result) + {*result = mCollation->CompareSortKey(key1, key2);return NS_OK;} // init this interface to a specified locale (should only be called by collation factory) // diff --git a/intl/locale/src/windows/nsCollationWin.cpp b/intl/locale/src/windows/nsCollationWin.cpp index 936b7d6ba87f..023eac2ebefb 100644 --- a/intl/locale/src/windows/nsCollationWin.cpp +++ b/intl/locale/src/windows/nsCollationWin.cpp @@ -75,7 +75,7 @@ nsresult nsCollationWin::Initialize(nsILocale* locale) nsresult nsCollationWin::GetSortKeyLen(const nsCollationStrength strength, - const nsString& stringIn, PRUint32* outLen) + const nsString& stringIn, PRUint32* outLen) { // Currently, no length change by the normalization. // API returns number of bytes when LCMAP_SORTKEY is specified @@ -85,8 +85,8 @@ nsresult nsCollationWin::GetSortKeyLen(const nsCollationStrength strength, return NS_OK; } -nsresult nsCollationWin::CreateSortKey(const nsCollationStrength strength, - const nsString& stringIn, PRUint8* key, PRUint32* outLen) +nsresult nsCollationWin::CreateRawSortKey(const nsCollationStrength strength, + const nsString& stringIn, PRUint8* key, PRUint32* outLen) { int byteLen; nsAutoString stringNormalized(stringIn); diff --git a/intl/locale/src/windows/nsCollationWin.h b/intl/locale/src/windows/nsCollationWin.h index 3d6e7b2e0ebf..e65d2b7ec167 100644 --- a/intl/locale/src/windows/nsCollationWin.h +++ b/intl/locale/src/windows/nsCollationWin.h @@ -38,7 +38,7 @@ public: // result is same as strcmp NS_IMETHOD CompareString(const nsCollationStrength strength, const nsString& string1, const nsString& string2, PRInt32* result) - {return mCollation->CompareString(this, strength, string1, string2, 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 @@ -47,15 +47,25 @@ public: // 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); + + // create a sort key (nsString) NS_IMETHOD CreateSortKey(const nsCollationStrength strength, - const nsString& stringIn, PRUint8* key, PRUint32* outLen); + const nsString& stringIn, nsString& key) + {return mCollation->CreateSortKey(this, strength, stringIn, key);} // compare two sort keys // length is a byte length, result is same as strcmp - NS_IMETHOD CompareSortKey(const PRUint8* key1, const PRUint32 len1, - const PRUint8* key2, const PRUint32 len2, - PRInt32* result) - {*result = mCollation->CompareSortKey(key1, len1, key2, len2);return NS_OK;}; + NS_IMETHOD CompareRawSortKey(const PRUint8* key1, const PRUint32 len1, + const PRUint8* key2, const PRUint32 len2, + PRInt32* result) + {*result = mCollation->CompareRawSortKey(key1, len1, key2, len2);return NS_OK;} + + // compare two sort keys (nsString) + NS_IMETHOD CompareSortKey(const nsString& key1, const nsString& key2, PRInt32* result) + {*result = mCollation->CompareSortKey(key1, key2);return NS_OK;} + // init this interface to a specified locale (should only be called by collation factory) // diff --git a/intl/locale/tests/LocaleSelfTest.cpp b/intl/locale/tests/LocaleSelfTest.cpp index 82bc41a9d386..d6181c3165a4 100644 --- a/intl/locale/tests/LocaleSelfTest.cpp +++ b/intl/locale/tests/LocaleSelfTest.cpp @@ -84,6 +84,7 @@ static nsresult CreateCollationKey(nsICollation *t, nsCollationStrength strength { nsresult res; + // create a raw collation key res = t->GetSortKeyLen(strength, stringIn, keyLength); if(NS_FAILED(res)) { cout << "\tFailed!! return value != NS_OK\n"; @@ -92,11 +93,23 @@ static nsresult CreateCollationKey(nsICollation *t, nsCollationStrength strength if (NULL == *aKey) { cout << "\tFailed!! memory allocation failed.\n"; } - res = t->CreateSortKey(strength, stringIn, *aKey, keyLength); + res = t->CreateRawSortKey(strength, stringIn, *aKey, keyLength); if(NS_FAILED(res)) { cout << "\tFailed!! return value != NS_OK\n"; } + // create a key in nsString + nsString aKeyString; + res = t->CreateSortKey(strength, stringIn, aKeyString); + if(NS_FAILED(res)) { + cout << "\tFailed!! return value != NS_OK\n"; + } + + // compare the generated key + nsString tempString; + tempString.SetString((PRUnichar *) *aKey, *keyLength / sizeof(PRUnichar)); + NS_ASSERTION(aKeyString == tempString, "created key mismatch"); + return res; } @@ -237,12 +250,12 @@ static void TestCollation(nsILocale *locale) } cout << "keyLength: " << keyLength1 << "\n"; - cout << "Test 4 - CreateSortKey():\n"; + cout << "Test 4 - CreateRawSortKey():\n"; aKey1 = (PRUint8 *) new PRUint8[keyLength1]; if (NULL == aKey1) { cout << "\tFailed!! memory allocation failed.\n"; } - res = t->CreateSortKey(kCollationCaseSensitive, string2, aKey1, &keyLength1); + res = t->CreateRawSortKey(kCollationCaseSensitive, string2, aKey1, &keyLength1); if(NS_FAILED(res)) { cout << "\tFailed!! return value != NS_OK\n"; } @@ -272,24 +285,24 @@ static void TestCollation(nsILocale *locale) } cout << "\n"; - cout << "Test 5 - CompareSortKey():\n"; + cout << "Test 5 - CompareRawSortKey():\n"; res = CreateCollationKey(t, kCollationCaseSensitive, string1, &aKey3, &keyLength3); if(NS_FAILED(res)) { cout << "\tFailed!! return value != NS_OK\n"; } - res = t->CompareSortKey(aKey1, keyLength1, aKey3, keyLength3, &result); + res = t->CompareRawSortKey(aKey1, keyLength1, aKey3, keyLength3, &result); if(NS_FAILED(res)) { cout << "\tFailed!! return value != NS_OK\n"; } cout << "case sensitive comparison (string1 vs string2): " << result << "\n"; - res = t->CompareSortKey(aKey3, keyLength3, aKey1, keyLength1, &result); + res = t->CompareRawSortKey(aKey3, keyLength3, aKey1, keyLength1, &result); if(NS_FAILED(res)) { cout << "\tFailed!! return value != NS_OK\n"; } cout << "case sensitive comparison (string2 vs string1): " << result << "\n"; - res = t->CompareSortKey(aKey2, keyLength2, aKey3, keyLength3, &result); + res = t->CompareRawSortKey(aKey2, keyLength2, aKey3, keyLength3, &result); if(NS_FAILED(res)) { cout << "\tFailed!! return value != NS_OK\n"; } @@ -314,12 +327,12 @@ static void TestCollation(nsILocale *locale) if(NS_FAILED(res)) { cout << "\tFailed!! return value != NS_OK\n"; } - res = t->CompareSortKey(aKey1, keyLength1, aKey2, keyLength2, &result); + res = t->CompareRawSortKey(aKey1, keyLength1, aKey2, keyLength2, &result); if(NS_FAILED(res)) { cout << "\tFailed!! return value != NS_OK\n"; } cout << "case sensitive comparison (string1 vs string3): " << result << "\n"; - res = t->CompareSortKey(aKey1, keyLength1, aKey3, keyLength3, &result); + res = t->CompareRawSortKey(aKey1, keyLength1, aKey3, keyLength3, &result); if(NS_FAILED(res)) { cout << "\tFailed!! return value != NS_OK\n"; } @@ -387,7 +400,7 @@ static int compare2( const void *arg1, const void *arg2 ) keyrec1 = (collation_rec *) arg1; keyrec2 = (collation_rec *) arg2; - res = g_collationInst->CompareSortKey(keyrec1->aKey, keyrec1->aLength, + res = g_collationInst->CompareRawSortKey(keyrec1->aKey, keyrec1->aLength, keyrec2->aKey, keyrec2->aLength, &result); return (int) result; @@ -567,6 +580,10 @@ static void TestSort(nsILocale *locale, nsCollationStrength collationStrength, F res = factoryInst->Release(); + for (int i = 0; i < 5; i++) { + delete [] key_array[i].aKey; + } + cout << "==============================\n"; cout << "Finish sort Test \n"; cout << "==============================\n"; @@ -718,7 +735,7 @@ static char* find_option(int argc, char** argv, char* arg) int main(int argc, char** argv) { nsresult res; -#if !XP_PC +#ifdef XP_MAC res = nsComponentManager::RegisterComponent(kCollationFactoryCID, NULL, NULL, LOCALE_DLL_NAME, PR_FALSE, PR_FALSE); if (NS_FAILED(res)) cout << "RegisterComponent failed\n"; @@ -751,7 +768,7 @@ int main(int argc, char** argv) { res = nsComponentManager::FindFactory(kLocaleFactoryCID, (nsIFactory**)&localeFactory); if (NS_FAILED(res) || localeFactory == nsnull) cout << "FindFactory nsILocaleFactory failed\n"; - res = localeFactory->GetSystemLocale(&locale); + res = localeFactory->GetApplicationLocale(&locale); if (NS_FAILED(res) || locale == nsnull) cout << "GetSystemLocale failed\n"; localeFactory->Release();