Bug 339105 - nsUnicharPtrHashKey, r=darin

This commit is contained in:
benjamin%smedbergs.us 2006-06-15 13:48:29 +00:00
Родитель 52365fd4b0
Коммит 0ce991da3a
3 изменённых файлов: 51 добавлений и 5 удалений

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

@ -46,6 +46,7 @@
#include NEW_H
#include "nsStringGlue.h"
#include "nsCRTGlue.h"
#include <stdlib.h>
#include <string.h>
@ -65,7 +66,8 @@
NS_COM_GLUE PRUint32 HashString(const nsAString& aStr);
NS_COM_GLUE PRUint32 HashString(const nsACString& aStr);
NS_COM_GLUE PRUint32 HashCString(const char* aKey);
NS_COM_GLUE PRUint32 HashString(const char* aKey);
NS_COM_GLUE PRUint32 HashString(const PRUnichar* aKey);
/**
* hashkey wrapper using nsAString KeyType
@ -280,7 +282,7 @@ public:
}
static const char* KeyToPointer(const char* aKey) { return aKey; }
static PLDHashNumber HashKey(const char* aKey) { return HashCString(aKey); }
static PLDHashNumber HashKey(const char* aKey) { return HashString(aKey); }
enum { ALLOW_MEMMOVE = PR_TRUE };
private:
@ -310,7 +312,7 @@ public:
}
static KeyTypePointer KeyToPointer(KeyType aKey) { return aKey; }
static PLDHashNumber HashKey(KeyTypePointer aKey) { return HashCString(aKey); }
static PLDHashNumber HashKey(KeyTypePointer aKey) { return HashString(aKey); }
enum { ALLOW_MEMMOVE = PR_TRUE };
@ -318,6 +320,37 @@ private:
const char* mKey;
};
/**
* hashkey wrapper for const PRUnichar*; at construction, this class duplicates
* a string pointed to by the pointer so that it doesn't matter whether or not
* the string lives longer than the hash table.
*/
class nsUnicharPtrHashKey : public PLDHashEntryHdr
{
public:
typedef const PRUnichar* KeyType;
typedef const PRUnichar* KeyTypePointer;
nsUnicharPtrHashKey(const PRUnichar* aKey) : mKey(NS_strdup(aKey)) { }
nsUnicharPtrHashKey(const nsUnicharPtrHashKey& toCopy) : mKey(NS_strdup(toCopy.mKey)) { }
~nsUnicharPtrHashKey() { if (mKey) NS_Free(NS_CONST_CAST(PRUnichar *, mKey)); }
const PRUnichar* GetKey() const { return mKey; }
const PRUnichar* GetKeyPointer() const { return mKey; }
PRBool KeyEquals(KeyTypePointer aKey) const
{
return !NS_strcmp(mKey, aKey);
}
static KeyTypePointer KeyToPointer(KeyType aKey) { return aKey; }
static PLDHashNumber HashKey(KeyTypePointer aKey) { return HashString(aKey); }
enum { ALLOW_MEMMOVE = PR_TRUE };
private:
const PRUnichar* mKey;
};
/**
* Hashtable key class to use with objects that support nsIHashable
*/

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

@ -85,7 +85,20 @@ HashString( const nsACString& aStr )
}
PRUint32
HashCString(const char *str)
HashString(const char *str)
{
PRUint32 code = 0;
while (*str) {
code = (code>>28) ^ (code<<4) ^ PRUint32(*str);
++str;
}
return code;
}
PRUint32
HashString(const PRUnichar *str)
{
PRUint32 code = 0;

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

@ -106,7 +106,7 @@ public:
const char* GetKeyPointer() const { return mNode->mStr; }
PRBool KeyEquals(const char* aEntity) const { return !strcmp(mNode->mStr, aEntity); }
static const char* KeyToPointer(const char* aEntity) { return aEntity; }
static PLDHashNumber HashKey(const char* aEntity) { return HashCString(aEntity); }
static PLDHashNumber HashKey(const char* aEntity) { return HashString(aEntity); }
enum { ALLOW_MEMMOVE = PR_TRUE };
const EntityNode* mNode;