mirror of
https://github.com/mozilla/gecko-dev.git
synced 2025-03-01 13:57:32 +00:00
Added two functions which use nsString as a sort key.
This commit is contained in:
parent
2da15c6b9f
commit
0aa9780658
@ -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)
|
||||
//
|
||||
|
@ -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;
|
||||
|
@ -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)
|
||||
//
|
||||
|
@ -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) {
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
|
@ -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)
|
||||
//
|
||||
|
@ -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);
|
||||
|
@ -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)
|
||||
//
|
||||
|
@ -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();
|
||||
|
Loading…
x
Reference in New Issue
Block a user