Bug 254252 nsCRT::BufferHashCode has two variants, and only one user, HashCodeAsUTF8 has no users

patch by mikael@parknert.se r=darin sr=bz


git-svn-id: svn://10.0.0.236/trunk@168085 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
timeless%mozdev.org 2005-01-20 21:39:23 +00:00
parent f8a5ef28a3
commit 07472dabed
3 changed files with 2 additions and 110 deletions

View File

@ -137,8 +137,8 @@ public:
*/
static PRUint32 HashCode(const PRUnichar* aBuf) {
NS_ASSERTION(aBuf, "Cannot work on null buffer!");
return nsCRT::BufferHashCode((char*)StrPtr(aBuf),
Length(aBuf)*sizeof(PRUnichar));
return nsCRT::BufferHashCode(StrPtr(aBuf),
Length(aBuf));
}
};

View File

@ -300,105 +300,6 @@ PRUint32 nsCRT::HashCode(const PRUnichar* str, PRUint32* resultingStrLen)
return h;
}
PRUint32 nsCRT::HashCodeAsUTF8(const PRUnichar* str, PRUint32* resultingStrLen)
{
PRUint32 h = 0;
const PRUnichar* s = str;
{
PRUint16 W1 = 0; // the first UTF-16 word in a two word tuple
PRUint32 U = 0; // the current char as UCS-4
int code_length = 0; // the number of bytes in the UTF-8 sequence for the current char
PRUint16 W;
while ( (W = *s++) )
{
/*
* On the fly, decoding from UTF-16 (and/or UCS-2) into UTF-8 as per
* http://www.ietf.org/rfc/rfc2781.txt
* http://www.ietf.org/rfc/rfc2279.txt
*/
if ( !W1 )
{
if ( W < 0xD800 || 0xDFFF < W )
{
U = W;
if ( W <= 0x007F )
code_length = 1;
else if ( W <= 0x07FF )
code_length = 2;
else
code_length = 3;
}
else if ( /* 0xD800 <= W1 && */ W <= 0xDBFF )
W1 = W;
}
else
{
// as required by the standard, this code is careful to
// throw out illegal sequences
if ( 0xDC00 <= W && W <= 0xDFFF )
{
U = PRUint32( (W1&0x03FF)<<10 | (W&0x3FFF) );
if ( U <= 0x001FFFFF )
code_length = 4;
else if ( U <= 0x3FFFFFF )
code_length = 5;
else
code_length = 6;
}
W1 = 0;
}
if ( code_length > 0 )
{
static const PRUint16 sBytePrefix[7] = { 0x0000, 0x0000, 0x00C0, 0x00E0, 0x00F0, 0x00F8, 0x00FC };
static const PRUint16 sShift[7] = { 0, 0, 6, 12, 18, 24, 30 };
/*
* Unlike the algorithm in http://www.ietf.org/rfc/rfc2279.txt
* we must calculate the bytes in left to right order so that
* our hash result matches what the narrow version would calculate
* on an already UTF-8 string.
*/
// hash the first (and often, only, byte)
h = (h>>28) ^ (h<<4) ^ (sBytePrefix[code_length] | (U>>sShift[code_length]));
// an unrolled loop for hashing any remaining bytes in this sequence
switch ( code_length )
{ // falling through in each case
case 6: h = (h>>28) ^ (h<<4) ^ (0x80 | ((U>>24) & 0x003F));
case 5: h = (h>>28) ^ (h<<4) ^ (0x80 | ((U>>18) & 0x003F));
case 4: h = (h>>28) ^ (h<<4) ^ (0x80 | ((U>>12) & 0x003F));
case 3: h = (h>>28) ^ (h<<4) ^ (0x80 | ((U>>6 ) & 0x003F));
case 2: h = (h>>28) ^ (h<<4) ^ (0x80 | ( U & 0x003F));
default: code_length = 0;
break;
}
}
}
}
if ( resultingStrLen )
*resultingStrLen = (s-str)-1;
return h;
}
PRUint32 nsCRT::BufferHashCode(const char* s, PRUint32 len)
{
PRUint32 h = 0;
const char* done = s + len;
while ( s < done )
h = (h>>28) ^ (h<<4) ^ PRUint8(*s++); // cast to unsigned to prevent possible sign extension
return h;
}
PRUint32 nsCRT::BufferHashCode(const PRUnichar* s, PRUint32 len)
{
PRUint32 h = 0;

View File

@ -233,15 +233,6 @@ public:
static PRUint32 HashCode(const PRUnichar* str,
PRUint32* resultingStrLen = nsnull);
// Computes a hashcode for a ucs2 string that returns the same thing
// as the HashCode method taking a |char*| would if the string were
// converted to UTF8. Returns the string length as an added bonus.
static PRUint32 HashCodeAsUTF8(const PRUnichar* str,
PRUint32* resultingStrLen = nsnull);
// Computes the hashcode for a buffer with a specified length.
static PRUint32 BufferHashCode(const char* str, PRUint32 strLen);
// Computes the hashcode for a buffer with a specified length.
static PRUint32 BufferHashCode(const PRUnichar* str, PRUint32 strLen);