Make the string fu in nsStaticCaseInsensitiveNameTable a little smarter; use

that for nsCSSProps.  Bug 162243, r=dbaron, sr=dveditz.
This commit is contained in:
bzbarsky%mit.edu 2003-01-17 04:55:10 +00:00
Родитель 224a502604
Коммит 5113e3dec2
3 изменённых файлов: 61 добавлений и 31 удалений

Просмотреть файл

@ -105,8 +105,14 @@ nsCSSProps::LookupProperty(const nsACString& aProperty)
nsCSSProperty nsCSSProperty
nsCSSProps::LookupProperty(const nsAString& aProperty) { nsCSSProps::LookupProperty(const nsAString& aProperty) {
nsCAutoString theProp; theProp.AssignWithConversion(aProperty); // This is faster than converting and calling
return LookupProperty(theProp); // LookupProperty(nsACString&). The table will do its own
// converting and avoid a PromiseFlatCString() call.
NS_ASSERTION(gPropertyTable, "no lookup table, needs addref");
if (gPropertyTable) {
return nsCSSProperty(gPropertyTable->Lookup(aProperty));
}
return eCSSProperty_UNKNOWN;
} }
const nsAFlatCString& const nsAFlatCString&

Просмотреть файл

@ -105,8 +105,14 @@ nsCSSProps::LookupProperty(const nsACString& aProperty)
nsCSSProperty nsCSSProperty
nsCSSProps::LookupProperty(const nsAString& aProperty) { nsCSSProps::LookupProperty(const nsAString& aProperty) {
nsCAutoString theProp; theProp.AssignWithConversion(aProperty); // This is faster than converting and calling
return LookupProperty(theProp); // LookupProperty(nsACString&). The table will do its own
// converting and avoid a PromiseFlatCString() call.
NS_ASSERTION(gPropertyTable, "no lookup table, needs addref");
if (gPropertyTable) {
return nsCSSProperty(gPropertyTable->Lookup(aProperty));
}
return eCSSProperty_UNKNOWN;
} }
const nsAFlatCString& const nsAFlatCString&

Просмотреть файл

@ -34,6 +34,8 @@
/* Class to manage lookup of static names in a table. */ /* Class to manage lookup of static names in a table. */
#include "nsCRT.h"
#include "nscore.h" #include "nscore.h"
#include "nsString.h" #include "nsString.h"
#include "nsReadableUtils.h" #include "nsReadableUtils.h"
@ -49,17 +51,35 @@ struct nameTableEntry : public PLDHashEntryHdr
}; };
PR_STATIC_CALLBACK(PRBool) PR_STATIC_CALLBACK(PRBool)
matchNameKeys(PLDHashTable*, const PLDHashEntryHdr* aHdr, matchNameKeysCaseInsensitive(PLDHashTable*, const PLDHashEntryHdr* aHdr,
const void* key) const void* key)
{ {
const nameTableEntry* entry = const nameTableEntry* entry =
NS_STATIC_CAST(const nameTableEntry *, aHdr); NS_STATIC_CAST(const nameTableEntry *, aHdr);
const char *keyValue = NS_STATIC_CAST(const char*, key); const char *keyValue = NS_STATIC_CAST(const char*, key);
return (strcmp(entry->mKey, keyValue)==0); return (nsCRT::strcasecmp(entry->mKey, keyValue)==0);
} }
/*
* caseInsensitiveHashKey is just like PL_DHashStringKey except it
* uses (*s & ~0x20) instead of simply *s. This means that "aFOO" and
* "afoo" and "aFoo" will all hash to the same thing. It also means
* that some strings that aren't case-insensensitively equal will hash
* to the same value, but it's just a hash function so it doesn't
* matter.
*/
PR_STATIC_CALLBACK(PLDHashNumber)
caseInsensitiveStringHashKey(PLDHashTable *table, const void *key)
{
PLDHashNumber h = 0;
for (const unsigned char* s =
NS_STATIC_CAST(const unsigned char*, key); *s != '\0'; s++)
h = (h >> (PL_DHASH_BITS - 4)) ^ (h << 4) ^ (*s & ~0x20);
return h;
}
PR_STATIC_CALLBACK(const void*) PR_STATIC_CALLBACK(const void*)
getNameKey(PLDHashTable*, PLDHashEntryHdr* aHdr) getNameKey(PLDHashTable*, PLDHashEntryHdr* aHdr)
{ {
@ -69,12 +89,12 @@ getNameKey(PLDHashTable*, PLDHashEntryHdr* aHdr)
return entry->mKey; return entry->mKey;
} }
struct PLDHashTableOps nametable_HashTableOps = { struct PLDHashTableOps nametable_CaseInsensitiveHashTableOps = {
PL_DHashAllocTable, PL_DHashAllocTable,
PL_DHashFreeTable, PL_DHashFreeTable,
getNameKey, getNameKey,
PL_DHashStringKey, caseInsensitiveStringHashKey,
matchNameKeys, matchNameKeysCaseInsensitive,
PL_DHashMoveEntryStub, PL_DHashMoveEntryStub,
PL_DHashClearEntryStub, PL_DHashClearEntryStub,
PL_DHashFinalizeStub, PL_DHashFinalizeStub,
@ -92,7 +112,7 @@ nsStaticCaseInsensitiveNameTable::nsStaticCaseInsensitiveNameTable()
nsStaticCaseInsensitiveNameTable::~nsStaticCaseInsensitiveNameTable() nsStaticCaseInsensitiveNameTable::~nsStaticCaseInsensitiveNameTable()
{ {
// manually call the destructor on placement-new'ed objects // manually call the destructor on placement-new'ed objects
for (PRInt32 index = 0; index < mNameTable.entryCount; index++) { for (PRUint32 index = 0; index < mNameTable.entryCount; index++) {
mNameArray[index].~nsDependentCString(); mNameArray[index].~nsDependentCString();
} }
nsMemory::Free((void*)mNameArray); nsMemory::Free((void*)mNameArray);
@ -109,22 +129,24 @@ nsStaticCaseInsensitiveNameTable::Init(const char* Names[], PRInt32 Count)
NS_ASSERTION(Count, "0 count"); NS_ASSERTION(Count, "0 count");
mNameArray = (nsDependentCString*)nsMemory::Alloc(Count * sizeof(nsDependentCString)); mNameArray = (nsDependentCString*)nsMemory::Alloc(Count * sizeof(nsDependentCString));
PL_DHashTableInit(&mNameTable, &nametable_HashTableOps, nsnull, PL_DHashTableInit(&mNameTable, &nametable_CaseInsensitiveHashTableOps,
sizeof(nameTableEntry), Count); nsnull, sizeof(nameTableEntry), Count);
if (!mNameArray || !mNameTable.ops) { if (!mNameArray || !mNameTable.ops) {
return PR_FALSE; return PR_FALSE;
} }
for (PRInt32 index = 0; index < Count; ++index) { for (PRInt32 index = 0; index < Count; ++index) {
char* raw = (char*) Names[index]; char* raw = (char*) Names[index];
PRUint32 len = strlen(raw);
#ifdef DEBUG #ifdef DEBUG
{ {
// verify invarients of contents // verify invariants of contents
nsCAutoString temp1(raw); nsCAutoString temp1(raw);
nsDependentCString temp2(raw); nsDependentCString temp2(raw);
ToLowerCase(temp1); ToLowerCase(temp1);
NS_ASSERTION(temp1.Equals(temp2), "upper case char in table"); NS_ASSERTION(temp1.Equals(temp2), "upper case char in table");
NS_ASSERTION(nsCRT::IsAscii(raw),
"non-ascii string in table -- "
"case-insensitive matching won't work right");
} }
#endif #endif
// use placement-new to initialize the string object // use placement-new to initialize the string object
@ -145,13 +167,12 @@ nsStaticCaseInsensitiveNameTable::Init(const char* Names[], PRInt32 Count)
} }
inline PRInt32 inline PRInt32
LookupLowercasedKeyword(const nsACString& aLowercasedKeyword, LookupFlatKeyword(const nsAFlatCString& aKeyword,
PLDHashTable& aTable) PLDHashTable& aTable)
{ {
const nsPromiseFlatCString& flatString = PromiseFlatCString(aLowercasedKeyword);
nameTableEntry *entry = nameTableEntry *entry =
NS_STATIC_CAST(nameTableEntry*, NS_STATIC_CAST(nameTableEntry*,
PL_DHashTableOperate(&aTable, flatString.get(), PL_DHASH_LOOKUP)); PL_DHashTableOperate(&aTable, aKeyword.get(), PL_DHASH_LOOKUP));
if (!entry || PL_DHASH_ENTRY_IS_FREE(entry)) if (!entry || PL_DHASH_ENTRY_IS_FREE(entry))
return nsStaticCaseInsensitiveNameTable::NOT_FOUND; return nsStaticCaseInsensitiveNameTable::NOT_FOUND;
@ -165,9 +186,7 @@ nsStaticCaseInsensitiveNameTable::Lookup(const nsACString& aName)
NS_ASSERTION(mNameArray, "not inited"); NS_ASSERTION(mNameArray, "not inited");
NS_ASSERTION(mNameTable.ops, "not inited"); NS_ASSERTION(mNameTable.ops, "not inited");
nsCAutoString strLower(aName); return LookupFlatKeyword(PromiseFlatCString(aName), mNameTable);
ToLowerCase(strLower);
return LookupLowercasedKeyword(strLower, mNameTable);
} }
PRInt32 PRInt32
@ -176,10 +195,9 @@ nsStaticCaseInsensitiveNameTable::Lookup(const nsAString& aName)
NS_ASSERTION(mNameArray, "not inited"); NS_ASSERTION(mNameArray, "not inited");
NS_ASSERTION(mNameTable.ops, "not inited"); NS_ASSERTION(mNameTable.ops, "not inited");
nsCAutoString strLower; nsCAutoString cstring;
strLower.AssignWithConversion(aName); cstring.AssignWithConversion(aName);
ToLowerCase(strLower); return LookupFlatKeyword(cstring, mNameTable);
return LookupLowercasedKeyword(strLower, mNameTable);
} }
const nsAFlatCString& const nsAFlatCString&
@ -188,7 +206,7 @@ nsStaticCaseInsensitiveNameTable::GetStringValue(PRInt32 index)
NS_ASSERTION(mNameArray, "not inited"); NS_ASSERTION(mNameArray, "not inited");
NS_ASSERTION(mNameTable.ops, "not inited"); NS_ASSERTION(mNameTable.ops, "not inited");
if ((NOT_FOUND < index) && (index < mNameTable.entryCount)) { if ((NOT_FOUND < index) && ((PRUint32)index < mNameTable.entryCount)) {
return mNameArray[index]; return mNameArray[index];
} else { } else {
return mNullStr; return mNullStr;