зеркало из https://github.com/mozilla/gecko-dev.git
BUg 103222: Spiff up nsICollation::CompareStrings string-fu, r=dbaron, sr=brendan
This commit is contained in:
Родитель
19436c0ab5
Коммит
536285092a
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 <nsIAtom> 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;
|
||||
|
|
|
@ -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 <nsICaseConversion> mCaseConversion;
|
||||
nsCOMPtr <nsIUnicodeEncoder> mEncoder;
|
||||
nsCOMPtr <nsIAtom> mEncoderCharsetAtom;
|
||||
nsCOMPtr <nsICharsetConverterManager2> mCharsetConverterManager;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
|
|
Загрузка…
Ссылка в новой задаче