Bug 1166598 (part 6) - Clean up nsStaticCaseInsensitiveNameTable. r=froydnj.

This patch simplifies nsStaticCaseInsensitiveNameTable greatly by taking
advantage of the following observations.

- |new| is infallible, so |new nsStaticCaseInsensitiveNameTable()| calls don't
  need their return value checked.

- nsStaticCaseInsensitiveNameTable::Init() checks if any of the added entries
  differ only in case, so the callers of that function don't need to do the
  same check.

- nsStaticCaseInsensitiveNameTable::Init() never returns false because
  moz_xmalloc() is infallible. (Its callers never check the return value
  anyway.) So it can be merged into the constructor. And
  ~nsStaticCaseInsensitiveNameTable() need not null-check |mNameArray|.

- PLDHashTable now has an initializing constructor and destructor, so these can
  be used in nsStaticCaseInsensitiveNameTable, thus avoiding manual
  PLD_HashTable{Init,Finish}() calls.
This commit is contained in:
Nicholas Nethercote 2015-05-05 21:13:53 -07:00
parent 6f0071f229
commit 16479c9560
5 changed files with 40 additions and 84 deletions

View File

@ -39,21 +39,8 @@ void nsColorNames::AddRefTable(void)
{
NS_ASSERTION(!gColorTable, "pre existing array!");
if (!gColorTable) {
gColorTable = new nsStaticCaseInsensitiveNameTable();
if (gColorTable) {
#ifdef DEBUG
{
// let's verify the table...
for (uint32_t index = 0; index < eColorName_COUNT; ++index) {
nsAutoCString temp1(kColorNames[index]);
nsAutoCString temp2(kColorNames[index]);
ToLowerCase(temp1);
NS_ASSERTION(temp1.Equals(temp2), "upper case char in table");
}
}
#endif
gColorTable->Init(kColorNames, eColorName_COUNT);
}
gColorTable =
new nsStaticCaseInsensitiveNameTable(kColorNames, eColorName_COUNT);
}
}

View File

@ -27,24 +27,17 @@ nsCSSKeywords::AddRefTable(void)
{
if (0 == gKeywordTableRefCount++) {
NS_ASSERTION(!gKeywordTable, "pre existing array!");
gKeywordTable = new nsStaticCaseInsensitiveNameTable();
if (gKeywordTable) {
gKeywordTable =
new nsStaticCaseInsensitiveNameTable(kCSSRawKeywords, eCSSKeyword_COUNT);
#ifdef DEBUG
{
// let's verify the table...
int32_t index = 0;
for (; index < eCSSKeyword_COUNT && kCSSRawKeywords[index]; ++index) {
nsAutoCString temp1(kCSSRawKeywords[index]);
nsAutoCString temp2(kCSSRawKeywords[index]);
ToLowerCase(temp1);
NS_ASSERTION(temp1.Equals(temp2), "upper case char in table");
NS_ASSERTION(-1 == temp1.FindChar('_'), "underscore char in table");
}
NS_ASSERTION(index == eCSSKeyword_COUNT, "kCSSRawKeywords and eCSSKeyword_COUNT are out of sync");
// Partially verify the entries.
int32_t index = 0;
for (; index < eCSSKeyword_COUNT && kCSSRawKeywords[index]; ++index) {
nsAutoCString temp(kCSSRawKeywords[index]);
NS_ASSERTION(-1 == temp.FindChar('_'), "underscore char in table");
}
NS_ASSERTION(index == eCSSKeyword_COUNT, "kCSSRawKeywords and eCSSKeyword_COUNT are out of sync");
#endif
gKeywordTable->Init(kCSSRawKeywords, eCSSKeyword_COUNT);
}
}
}

View File

@ -134,22 +134,15 @@ static nsCSSProperty gAliases[eCSSAliasCount != 0 ? eCSSAliasCount : 1] = {
nsStaticCaseInsensitiveNameTable*
CreateStaticTable(const char* const aRawTable[], int32_t aLength)
{
auto table = new nsStaticCaseInsensitiveNameTable();
if (table) {
auto table = new nsStaticCaseInsensitiveNameTable(aRawTable, aLength);
#ifdef DEBUG
// let's verify the table...
for (int32_t index = 0; index < aLength; ++index) {
nsAutoCString temp1(aRawTable[index]);
nsAutoCString temp2(aRawTable[index]);
ToLowerCase(temp1);
MOZ_ASSERT(temp1.Equals(temp2),
"upper case char in case insensitive name table");
MOZ_ASSERT(-1 == temp1.FindChar('_'),
"underscore char in case insensitive name table");
}
#endif
table->Init(aRawTable, aLength);
// Partially verify the entries.
for (int32_t index = 0; index < aLength; ++index) {
nsAutoCString temp(aRawTable[index]);
MOZ_ASSERT(-1 == temp.FindChar('_'),
"underscore char in case insensitive name table");
}
#endif
return table;
}

View File

@ -101,45 +101,20 @@ static const struct PLDHashTableOps nametable_CaseInsensitiveHashTableOps = {
nullptr,
};
nsStaticCaseInsensitiveNameTable::nsStaticCaseInsensitiveNameTable()
nsStaticCaseInsensitiveNameTable::nsStaticCaseInsensitiveNameTable(
const char* const aNames[], int32_t aLength)
: mNameArray(nullptr)
, mNameTable(&nametable_CaseInsensitiveHashTableOps,
sizeof(NameTableEntry), aLength)
, mNullStr("")
{
MOZ_COUNT_CTOR(nsStaticCaseInsensitiveNameTable);
}
nsStaticCaseInsensitiveNameTable::~nsStaticCaseInsensitiveNameTable()
{
if (mNameArray) {
// manually call the destructor on placement-new'ed objects
for (uint32_t index = 0; index < mNameTable.EntryCount(); index++) {
mNameArray[index].~nsDependentCString();
}
free((void*)mNameArray);
}
if (mNameTable.IsInitialized()) {
PL_DHashTableFinish(&mNameTable);
}
MOZ_COUNT_DTOR(nsStaticCaseInsensitiveNameTable);
}
bool
nsStaticCaseInsensitiveNameTable::Init(const char* const aNames[],
int32_t aLength)
{
NS_ASSERTION(!mNameArray, "double Init");
NS_ASSERTION(!mNameTable.IsInitialized(), "double Init");
NS_ASSERTION(aNames, "null name table");
NS_ASSERTION(aLength, "0 length");
MOZ_ASSERT(aNames, "null name table");
MOZ_ASSERT(aLength, "0 length");
mNameArray = (nsDependentCString*)
moz_xmalloc(aLength * sizeof(nsDependentCString));
if (!mNameArray) {
return false;
}
PL_DHashTableInit(&mNameTable, &nametable_CaseInsensitiveHashTableOps,
sizeof(NameTableEntry), aLength);
for (int32_t index = 0; index < aLength; ++index) {
const char* raw = aNames[index];
@ -149,10 +124,10 @@ nsStaticCaseInsensitiveNameTable::Init(const char* const aNames[],
nsAutoCString temp1(raw);
nsDependentCString temp2(raw);
ToLowerCase(temp1);
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");
MOZ_ASSERT(temp1.Equals(temp2), "upper case char in table");
MOZ_ASSERT(nsCRT::IsAscii(raw),
"non-ascii string in table -- "
"case-insensitive matching won't work right");
}
#endif
// use placement-new to initialize the string object
@ -175,7 +150,16 @@ nsStaticCaseInsensitiveNameTable::Init(const char* const aNames[],
#ifdef DEBUG
PL_DHashMarkTableImmutable(&mNameTable);
#endif
return true;
}
nsStaticCaseInsensitiveNameTable::~nsStaticCaseInsensitiveNameTable()
{
// manually call the destructor on placement-new'ed objects
for (uint32_t index = 0; index < mNameTable.EntryCount(); index++) {
mNameArray[index].~nsDependentCString();
}
free((void*)mNameArray);
MOZ_COUNT_DTOR(nsStaticCaseInsensitiveNameTable);
}
int32_t

View File

@ -33,17 +33,16 @@ class nsStaticCaseInsensitiveNameTable
public:
enum { NOT_FOUND = -1 };
bool Init(const char* const aNames[], int32_t aLength);
int32_t Lookup(const nsACString& aName);
int32_t Lookup(const nsAString& aName);
const nsAFlatCString& GetStringValue(int32_t aIndex);
nsStaticCaseInsensitiveNameTable();
nsStaticCaseInsensitiveNameTable(const char* const aNames[], int32_t aLength);
~nsStaticCaseInsensitiveNameTable();
private:
nsDependentCString* mNameArray;
PLDHashTable mNameTable;
PLDHashTable2 mNameTable;
nsDependentCString mNullStr;
};