improve string comparison.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@29104 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
rfm 2009-12-07 05:02:55 +00:00
parent 7abb743f91
commit f8c0ecf186
2 changed files with 75 additions and 25 deletions

View file

@ -1,3 +1,9 @@
2009-12-07 Richard Frith-Macdonald <rfm@gnu.org>
* Source/GSString.m: Use -hash from GSString rather than from NSString
to optimise hashing. Fix string comparison error as pointed out by
Doug Simons.
2009-12-02 Richard Frith-Macdonald <rfm@gnu.org> 2009-12-02 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSMessagePortNameServer.m: * Source/NSMessagePortNameServer.m:

View file

@ -302,7 +302,7 @@ setup(void)
[NSStringClass instanceMethodForSelector: equalSel]; [NSStringClass instanceMethodForSelector: equalSel];
hashSel = @selector(hash); hashSel = @selector(hash);
hashImp = (unsigned (*)(id, SEL)) hashImp = (unsigned (*)(id, SEL))
[NSStringClass instanceMethodForSelector: hashSel]; [GSStringClass instanceMethodForSelector: hashSel];
caiSel = @selector(characterAtIndex:); caiSel = @selector(characterAtIndex:);
gcrSel = @selector(getCharacters:range:); gcrSel = @selector(getCharacters:range:);
@ -2113,7 +2113,7 @@ isEqual_c(GSStr self, id anObject)
return YES; return YES;
return NO; return NO;
} }
else if (c == GSMutableStringClass) else if (c == GSMutableStringClass || GSObjCIsKindOf(c, GSStringClass) == YES)
{ {
GSStr other = (GSStr)anObject; GSStr other = (GSStr)anObject;
NSRange r = {0, self->_count}; NSRange r = {0, self->_count};
@ -2144,26 +2144,6 @@ isEqual_c(GSStr self, id anObject)
} }
return NO; return NO;
} }
else if (GSObjCIsKindOf(c, GSStringClass) == YES)
{
GSStr other = (GSStr)anObject;
/*
* First see if the hash is the same - if not, we can't be equal.
*/
if (self->_flags.hash == 0)
self->_flags.hash = (*hashImp)((id)self, hashSel);
if (other->_flags.hash == 0)
other->_flags.hash = (*hashImp)((id)other, hashSel);
if (self->_flags.hash != other->_flags.hash)
return NO;
if (other->_count == self->_count
&& memcmp(other->_contents.c, self->_contents.c, self->_count) == 0)
return YES;
return NO;
}
else if (GSObjCIsKindOf(c, NSStringClass)) else if (GSObjCIsKindOf(c, NSStringClass))
{ {
return (*equalImp)((id)self, equalSel, anObject); return (*equalImp)((id)self, equalSel, anObject);
@ -2201,7 +2181,7 @@ isEqual_u(GSStr self, id anObject)
return YES; return YES;
return NO; return NO;
} }
else if (GSObjCIsKindOf(c, GSStringClass) == YES || c == GSMutableStringClass) else if (c == GSMutableStringClass || GSObjCIsKindOf(c, GSStringClass) == YES)
{ {
GSStr other = (GSStr)anObject; GSStr other = (GSStr)anObject;
NSRange r = {0, self->_count}; NSRange r = {0, self->_count};
@ -2811,12 +2791,77 @@ transmute(GSStr self, NSString *aString)
setup(); setup();
} }
/*
* Return a 28-bit hash value for the string contents - this
* MUST match the algorithm used by the NSString base class.
*/
- (NSUInteger) hash - (NSUInteger) hash
{ {
if (self->_flags.hash == 0) if (self->_flags.hash == 0)
{ {
self->_flags.hash = (*hashImp)((id)self, hashSel); unsigned ret = 0;
unsigned len = self->_count;
if (len > 0)
{
register unsigned index = 0;
if (self->_flags.wide)
{
register const char *p = self->_contents.u;
while (index < len)
{
ret = (ret << 5) + ret + p[index++];
} }
}
else
{
register const unsigned char *p = self->_contents.c;
if (internalEncoding == NSISOLatin1StringEncoding)
{
while (index < len)
{
ret = (ret << 5) + ret + p[index++];
}
}
else
{
while (index < len)
{
unichar u = p[index++];
if (u > 127)
{
unsigned char c = (unsigned char)u;
unsigned int s = 1;
unichar *d = &u;
GSToUnicode(&d, &s, &c, 1, internalEncoding, 0, 0);
}
ret = (ret << 5) + ret + u;
}
}
}
/*
* The hash caching in our concrete string classes uses zero to denote
* an empty cache value, so we MUST NOT return a hash of zero.
*/
ret &= 0x0fffffff;
if (ret == 0)
{
ret = 0x0fffffff;
}
}
else
{
ret = 0x0ffffffe; /* Hash for an empty string. */
}
self->_flags.hash = ret;
}
return self->_flags.hash; return self->_flags.hash;
} }
@ -4624,7 +4669,6 @@ NSAssert(_flags.owned == 1 && _zone != 0, NSInternalInconsistencyException);
return NSASCIIStringEncoding; return NSASCIIStringEncoding;
} }
/* /*
* Return a 28-bit hash value for the string contents - this * Return a 28-bit hash value for the string contents - this
* MUST match the algorithm used by the NSString base class. * MUST match the algorithm used by the NSString base class.