Substring creation fixes

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@18040 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
CaS 2003-11-03 08:35:44 +00:00
parent 2d71ebd08f
commit ae99be2995
2 changed files with 44 additions and 107 deletions

View file

@ -1,3 +1,10 @@
Mon Nov 03 08:35:00 2003 Richard Frith-Macdonald <rfm@gnu.org>
* Source/GSString.m: Remove unnecessary -copy implementations for
simplicity, and add code to avoid creating substrings of strings
which do not own their buffers ... stopgap until Alexander
reorganises class hierarchy.
2003-11-02 01:53 Alexander Malmberg <alexander@malmberg.org>
* Source/GSString.m ([GSCSubString -copy]): Add as a stopgap

View file

@ -1687,36 +1687,55 @@ rangeOfString_u(ivars self, NSString *aString, unsigned mask, NSRange aRange)
static inline NSString*
substring_c(ivars self, NSRange aRange)
{
GSCSubString *sub;
id sub;
sub = (GSCSubString*)NSAllocateObject(GSCSubStringClass, 0,
NSDefaultMallocZone());
if (self->_flags.free == 1)
{
sub = NSAllocateObject(GSCSubStringClass, 0, NSDefaultMallocZone());
sub = [sub initWithCStringNoCopy: self->_contents.c + aRange.location
length: aRange.length
freeWhenDone: NO];
if (sub != nil)
{
sub->_parent = RETAIN((id)self);
AUTORELEASE(sub);
((GSCSubString*)sub)->_parent = RETAIN((id)self);
}
}
else
{
sub = NSAllocateObject(GSCInlineStringClass,
aRange.length, NSDefaultMallocZone());
sub = [sub initWithCString: self->_contents.c + aRange.location
length: aRange.length];
}
AUTORELEASE(sub);
return sub;
}
static inline NSString*
substring_u(ivars self, NSRange aRange)
{
GSUnicodeSubString *sub;
id sub;
sub = (GSUnicodeSubString*)NSAllocateObject(GSUnicodeSubStringClass, 0,
NSDefaultMallocZone());
if (self->_flags.free == 1)
{
sub = NSAllocateObject(GSUnicodeSubStringClass, 0, NSDefaultMallocZone());
sub = [sub initWithCharactersNoCopy: self->_contents.u + aRange.location
length: aRange.length
freeWhenDone: NO];
if (sub != nil)
{
sub->_parent = RETAIN((id)self);
AUTORELEASE(sub);
((GSUnicodeSubString*)sub)->_parent = RETAIN((id)self);
}
}
else
{
sub = NSAllocateObject(GSUnicodeInlineStringClass,
aRange.length*sizeof(unichar),
NSDefaultMallocZone());
sub = [sub initWithCharacters: self->_contents.u + aRange.location
length: aRange.length];
}
AUTORELEASE(sub);
return sub;
}
@ -1953,30 +1972,6 @@ transmute(ivars self, NSString *aString)
return compare_c((ivars)self, aString, mask, aRange);
}
- (id) copy
{
if (_flags.free == NO
|| NSShouldRetainWithZone(self, NSDefaultMallocZone()) == NO)
{
GSCString *obj;
obj = (GSCString*)NSCopyObject(self, 0, NSDefaultMallocZone());
if (_contents.c != 0)
{
unsigned char *tmp;
tmp = NSZoneMalloc(NSDefaultMallocZone(), _count);
memcpy(tmp, _contents.c, _count);
obj->_contents.c = tmp;
}
return obj;
}
else
{
return RETAIN(self);
}
}
- (id) copyWithZone: (NSZone*)z
{
if (_flags.free == NO
@ -2197,10 +2192,6 @@ transmute(ivars self, NSString *aString)
_flags.wide = 0;
return self;
}
- (id) copy
{
return RETAIN(self);
}
- (id) copyWithZone: (NSZone*)z
{
if (NSShouldRetainWithZone(self, z) == NO)
@ -2232,10 +2223,6 @@ transmute(ivars self, NSString *aString)
/*
* Assume that a copy should be a new string, never just a retained substring.
*/
- (id) copy
{
return [self copyWithZone: NSDefaultMallocZone()];
}
- (id) copyWithZone: (NSZone*)z
{
NSString *obj;
@ -2281,30 +2268,6 @@ transmute(ivars self, NSString *aString)
return compare_u((ivars)self, aString, mask, aRange);
}
- (id) copy
{
if (_flags.free == NO
|| NSShouldRetainWithZone(self, NSDefaultMallocZone()) == NO)
{
GSUnicodeString *obj;
obj = (GSUnicodeString*)NSCopyObject(self, 0, NSDefaultMallocZone());
if (_contents.u != 0)
{
unichar *tmp;
tmp = NSZoneMalloc(NSDefaultMallocZone(), _count*sizeof(unichar));
memcpy(tmp, _contents.u, _count*sizeof(unichar));
obj->_contents.u = tmp;
}
return obj;
}
else
{
return RETAIN(self);
}
}
- (id) copyWithZone: (NSZone*)z
{
if (_flags.free == NO
@ -2531,10 +2494,6 @@ transmute(ivars self, NSString *aString)
_flags.wide = 1;
return self;
}
- (id) copy
{
return RETAIN(self);
}
- (id) copyWithZone: (NSZone*)z
{
if (NSShouldRetainWithZone(self, z) == NO)
@ -2710,25 +2669,6 @@ transmute(ivars self, NSString *aString)
return compare_c((ivars)self, aString, mask, aRange);
}
- (id) copy
{
id copy;
if (_flags.wide == 1)
{
copy = NSAllocateObject(GSUnicodeInlineStringClass,
_count*sizeof(unichar), NSDefaultMallocZone());
copy = [copy initWithCharacters: _contents.u length: _count];
}
else
{
copy = NSAllocateObject(GSCInlineStringClass,
_count, NSDefaultMallocZone());
copy = [copy initWithCString: _contents.c length: _count];
}
return copy;
}
- (id) copyWithZone: (NSZone*)z
{
id copy;
@ -3413,11 +3353,6 @@ transmute(ivars self, NSString *aString)
[super dealloc];
}
- (id) copy
{
return [_parent copy];
}
- (id) copyWithZone: (NSZone*)z
{
return [_parent copyWithZone: z];
@ -3779,11 +3714,6 @@ transmute(ivars self, NSString *aString)
return self;
}
- (id) copy
{
return self;
}
- (id) copyWithZone: (NSZone*)z
{
return self;