diff --git a/Headers/Foundation/NSString.h b/Headers/Foundation/NSString.h index 5feec7f91..61eee6451 100644 --- a/Headers/Foundation/NSString.h +++ b/Headers/Foundation/NSString.h @@ -38,7 +38,6 @@ typedef unsigned short unichar; #endif #define NSMaximumStringLength (INT_MAX-1) -#define NSHashStringLength 63 enum { diff --git a/Source/GSString.m b/Source/GSString.m index dcf24b1eb..096bda74a 100644 --- a/Source/GSString.m +++ b/Source/GSString.m @@ -3807,19 +3807,16 @@ agree, create a new GSUnicodeInlineString otherwise. */ - (unsigned) hash { - unsigned ret = 0; + unsigned ret = 0; + unsigned len = _self->_count; - int len = _self->_count; - - if (len > NSHashStringLength) - len = NSHashStringLength; - if (len) + if (len > 0) { const unsigned char *p; unsigned char_count = 0; p = _self->_contents.c; - while (*p != 0 && char_count++ < NSHashStringLength) + while (char_count++ < len) { unichar c = *p++; @@ -3835,9 +3832,13 @@ agree, create a new GSUnicodeInlineString otherwise. * an empty cache value, so we MUST NOT return a hash of zero. */ if (ret == 0) - ret = 0x0fffffff; + { + ret = 0x0fffffff; + } else - ret &= 0x0fffffff; + { + ret &= 0x0fffffff; + } } else { diff --git a/Source/NSString.m b/Source/NSString.m index 79616b2f9..34e2ae80a 100644 --- a/Source/NSString.m +++ b/Source/NSString.m @@ -2161,29 +2161,32 @@ handle_printf_atsign (FILE *stream, */ - (unsigned int) hash { - unsigned ret = 0; + unsigned ret = 0; + unsigned len = [self length]; - int len = [self length]; - - if (len > NSHashStringLength) - { - len = NSHashStringLength; - } if (len > 0) { - unichar buf[len * MAXDEC + 1]; - GSeqStruct s = { buf, len, len * MAXDEC, 0 }; + unichar buf[64]; + unichar *ptr = (len <= 64) ? buf : + NSZoneMalloc(NSDefaultMallocZone(), len * sizeof(unichar)); unichar *p; unsigned char_count = 0; - [self getCharacters: buf range: NSMakeRange(0,len)]; - GSeq_normalize(&s); + [self getCharacters: ptr range: NSMakeRange(0,len)]; - p = buf; + p = ptr; - while (*p && char_count++ < NSHashStringLength) + while (char_count++ < len) { - ret = (ret << 5) + ret + *p++; + unichar c = *p++; + + // FIXME ... should normalize composed character sequences. + ret = (ret << 5) + ret + c; + } + + if (ptr != buf) + { + NSZoneFree(NSDefaultMallocZone(), ptr); } /* @@ -2191,13 +2194,19 @@ handle_printf_atsign (FILE *stream, * an empty cache value, so we MUST NOT return a hash of zero. */ if (ret == 0) - ret = 0x0fffffff; + { + ret = 0x0fffffff; + } else - ret &= 0x0fffffff; + { + ret &= 0x0fffffff; + } return ret; } else - return 0x0ffffffe; /* Hash for an empty string. */ + { + return 0x0ffffffe; /* Hash for an empty string. */ + } } // Getting a Shared Prefix