1995-04-03 01:38:23 +00:00
|
|
|
|
/* Implementation for GNUStep of NSStrings with C-string backing
|
1998-10-15 05:03:16 +00:00
|
|
|
|
Copyright (C) 1993,1994, 1996, 1997, 1998 Free Software Foundation, Inc.
|
1995-04-03 01:38:23 +00:00
|
|
|
|
|
1996-04-17 20:17:45 +00:00
|
|
|
|
Written by: Andrew Kachites McCallum <mccallum@gnu.ai.mit.edu>
|
1995-04-03 01:38:23 +00:00
|
|
|
|
Date: March 1995
|
1998-10-15 05:03:16 +00:00
|
|
|
|
Optimised by: Richard frith-Macdoanld <richard@brainstorm.co.uk>
|
|
|
|
|
Date: October 1998
|
1995-04-03 01:38:23 +00:00
|
|
|
|
|
1996-05-12 00:56:10 +00:00
|
|
|
|
This file is part of the GNUstep Base Library.
|
1995-04-03 01:38:23 +00:00
|
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
|
License along with this library; if not, write to the Free
|
|
|
|
|
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
*/
|
|
|
|
|
|
1997-11-06 00:51:23 +00:00
|
|
|
|
#include <config.h>
|
1996-04-17 15:34:35 +00:00
|
|
|
|
#include <gnustep/base/preface.h>
|
1995-04-17 21:13:20 +00:00
|
|
|
|
#include <Foundation/NSString.h>
|
1998-09-30 07:42:38 +00:00
|
|
|
|
#include <Foundation/NSData.h>
|
1998-10-15 05:03:16 +00:00
|
|
|
|
#include <Foundation/NSCoder.h>
|
1998-10-03 21:23:04 +00:00
|
|
|
|
#include <gnustep/base/NSGString.h>
|
|
|
|
|
#include <gnustep/base/NSGCString.h>
|
1996-04-17 15:23:00 +00:00
|
|
|
|
#include <gnustep/base/IndexedCollection.h>
|
|
|
|
|
#include <gnustep/base/IndexedCollectionPrivate.h>
|
1996-02-22 16:05:47 +00:00
|
|
|
|
#include <Foundation/NSValue.h>
|
1996-04-17 15:23:00 +00:00
|
|
|
|
#include <gnustep/base/behavior.h>
|
1995-04-03 01:38:23 +00:00
|
|
|
|
|
1998-10-03 21:23:04 +00:00
|
|
|
|
#include <gnustep/base/Unicode.h>
|
1998-10-06 15:11:27 +00:00
|
|
|
|
#include <gnustep/base/fast.x>
|
1997-05-03 18:05:21 +00:00
|
|
|
|
|
1998-10-26 08:15:20 +00:00
|
|
|
|
static SEL csInitSel = @selector(initWithCStringNoCopy:length:fromZone:);
|
|
|
|
|
static SEL msInitSel = @selector(initWithCapacity:);
|
|
|
|
|
static IMP csInitImp; /* designated initialiser for cString */
|
|
|
|
|
static IMP msInitImp; /* designated initialiser for mutable */
|
|
|
|
|
|
1995-08-09 15:45:46 +00:00
|
|
|
|
@implementation NSGCString
|
1995-04-03 01:38:23 +00:00
|
|
|
|
|
1998-10-26 08:15:20 +00:00
|
|
|
|
+ (void) initialize
|
|
|
|
|
{
|
|
|
|
|
static int done = 0;
|
|
|
|
|
if (!done)
|
|
|
|
|
{
|
|
|
|
|
done = 1;
|
|
|
|
|
csInitImp = [NSGCString instanceMethodForSelector: csInitSel];
|
|
|
|
|
msInitImp = [NSGMutableCString instanceMethodForSelector: msInitSel];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
1998-10-25 07:56:22 +00:00
|
|
|
|
+ allocWithZone: (NSZone*)z
|
|
|
|
|
{
|
|
|
|
|
return NSAllocateObject (self, 0, z);
|
|
|
|
|
}
|
|
|
|
|
|
1998-10-25 08:31:37 +00:00
|
|
|
|
+ alloc
|
|
|
|
|
{
|
|
|
|
|
return NSAllocateObject (self, 0, NSDefaultMallocZone());
|
|
|
|
|
}
|
|
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
|
- (void)dealloc
|
|
|
|
|
{
|
1998-10-26 08:15:20 +00:00
|
|
|
|
if (_zone)
|
|
|
|
|
{
|
|
|
|
|
NSZoneFree(_zone, (void*)_contents_chars);
|
|
|
|
|
_zone = 0;
|
1998-10-15 05:03:16 +00:00
|
|
|
|
}
|
1998-10-26 08:15:20 +00:00
|
|
|
|
[super dealloc];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-08-03 15:31:33 +00:00
|
|
|
|
- (unsigned) hash
|
|
|
|
|
{
|
|
|
|
|
if (_hash == 0)
|
1998-10-26 08:15:20 +00:00
|
|
|
|
{
|
|
|
|
|
_hash = _fastImp._NSString_hash(self, @selector(hash));
|
|
|
|
|
}
|
1998-08-03 15:31:33 +00:00
|
|
|
|
return _hash;
|
|
|
|
|
}
|
|
|
|
|
|
1998-10-26 08:15:20 +00:00
|
|
|
|
/*
|
|
|
|
|
* This is the GNUstep designated initializer for this class.
|
|
|
|
|
* NB. this does NOT change the '_hash' instance variable, so the copy
|
|
|
|
|
* methods can safely allocate a new object, copy the _hash into place,
|
|
|
|
|
* and then invoke this method to complete the copy operation.
|
|
|
|
|
*/
|
1995-04-03 01:38:23 +00:00
|
|
|
|
- (id) initWithCStringNoCopy: (char*)byteString
|
1998-10-15 05:03:16 +00:00
|
|
|
|
length: (unsigned int)length
|
|
|
|
|
fromZone: (NSZone*)zone
|
|
|
|
|
{
|
1998-10-26 08:15:20 +00:00
|
|
|
|
_count = length;
|
|
|
|
|
_contents_chars = byteString;
|
|
|
|
|
_zone = byteString ? zone : 0;
|
|
|
|
|
return self;
|
1998-10-15 05:03:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* This is the OpenStep designated initializer for this class. */
|
|
|
|
|
- (id) initWithCStringNoCopy: (char*)byteString
|
|
|
|
|
length: (unsigned int)length
|
|
|
|
|
freeWhenDone: (BOOL)flag
|
|
|
|
|
{
|
1998-10-26 08:15:20 +00:00
|
|
|
|
NSZone *z;
|
|
|
|
|
|
|
|
|
|
if (flag)
|
|
|
|
|
{
|
|
|
|
|
z = NSZoneFromPointer(byteString);
|
1998-10-15 05:03:16 +00:00
|
|
|
|
}
|
1998-10-26 08:15:20 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
z = 0;
|
|
|
|
|
}
|
|
|
|
|
return (*csInitImp)(self, csInitSel, byteString, length, z);
|
1998-10-15 05:03:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithCharactersNoCopy: (unichar*)chars
|
|
|
|
|
length: (unsigned int)length
|
|
|
|
|
fromZone: (NSZone*)zone
|
1995-04-03 01:38:23 +00:00
|
|
|
|
{
|
1998-10-26 08:15:20 +00:00
|
|
|
|
NSZone *z = zone ? zone : fastZone(self);
|
|
|
|
|
id a = [[NSGString allocWithZone: z] initWithCharactersNoCopy: chars
|
|
|
|
|
length: length
|
|
|
|
|
fromZone: z];
|
|
|
|
|
[self release];
|
|
|
|
|
return a;
|
1995-04-03 01:38:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
|
- (id) initWithCharactersNoCopy: (unichar*)chars
|
1998-10-26 08:15:20 +00:00
|
|
|
|
length: (unsigned int)length
|
|
|
|
|
freeWhenDone: (BOOL)flag
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
1998-10-26 08:15:20 +00:00
|
|
|
|
NSZone *z = fastZone(self);
|
|
|
|
|
id a = [[NSGString allocWithZone: z] initWithCharactersNoCopy: chars
|
|
|
|
|
length: length
|
|
|
|
|
freeWhenDone: flag];
|
|
|
|
|
[self release];
|
|
|
|
|
return a;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
1997-12-08 20:04:16 +00:00
|
|
|
|
- (id) init
|
|
|
|
|
{
|
1998-10-15 05:03:16 +00:00
|
|
|
|
return [self initWithCStringNoCopy: 0 length: 0 fromZone: 0];
|
1997-12-08 20:04:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
1996-02-22 16:05:47 +00:00
|
|
|
|
- (void) _collectionReleaseContents
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) _collectionDealloc
|
1995-10-18 16:47:59 +00:00
|
|
|
|
{
|
1998-10-26 08:15:20 +00:00
|
|
|
|
if (_zone)
|
|
|
|
|
{
|
|
|
|
|
NSZoneFree(_zone, (void*)_contents_chars);
|
|
|
|
|
_zone = 0;
|
1998-10-15 05:03:16 +00:00
|
|
|
|
}
|
1995-04-03 01:38:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-10-23 15:47:07 +00:00
|
|
|
|
- (Class) classForArchiver
|
|
|
|
|
{
|
|
|
|
|
return [self class];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (Class) classForCoder
|
|
|
|
|
{
|
|
|
|
|
return [self class];
|
|
|
|
|
}
|
|
|
|
|
|
1997-09-29 14:39:53 +00:00
|
|
|
|
- (Class) classForPortCoder
|
1997-09-12 17:54:10 +00:00
|
|
|
|
{
|
|
|
|
|
return [self class];
|
|
|
|
|
}
|
1998-10-15 05:03:16 +00:00
|
|
|
|
|
1997-09-12 17:54:10 +00:00
|
|
|
|
- replacementObjectForPortCoder:(NSPortCoder*)aCoder
|
|
|
|
|
{
|
1998-10-26 08:15:20 +00:00
|
|
|
|
return self;
|
1997-09-12 17:54:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
1995-04-03 01:38:23 +00:00
|
|
|
|
- (void) encodeWithCoder: aCoder
|
|
|
|
|
{
|
1998-10-21 14:02:33 +00:00
|
|
|
|
[aCoder encodeValueOfObjCType:@encode(unsigned) at:&_count];
|
1998-10-26 18:06:51 +00:00
|
|
|
|
if (_count > 0)
|
|
|
|
|
{
|
|
|
|
|
[aCoder encodeArrayOfObjCType:@encode(unsigned char)
|
|
|
|
|
count:_count
|
|
|
|
|
at:_contents_chars];
|
|
|
|
|
}
|
1995-04-03 01:38:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
1995-04-09 01:53:53 +00:00
|
|
|
|
- initWithCoder: aCoder
|
1995-04-03 01:38:23 +00:00
|
|
|
|
{
|
1998-10-26 08:15:20 +00:00
|
|
|
|
[aCoder decodeValueOfObjCType:@encode(unsigned)
|
|
|
|
|
at:&_count];
|
|
|
|
|
if (_count > 0)
|
|
|
|
|
{
|
|
|
|
|
_zone = fastZone(self);
|
|
|
|
|
_contents_chars = NSZoneMalloc(_zone, _count);
|
|
|
|
|
[aCoder decodeArrayOfObjCType:@encode(unsigned char)
|
|
|
|
|
count:_count
|
|
|
|
|
at:_contents_chars];
|
|
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- copy
|
|
|
|
|
{
|
|
|
|
|
NSZone *z = NSDefaultMallocZone();
|
|
|
|
|
|
|
|
|
|
if (NSShouldRetainWithZone(self, z) == NO)
|
|
|
|
|
{
|
|
|
|
|
NSGCString *obj;
|
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
|
|
obj = (NSGCString*)NSAllocateObject(_fastCls._NSGCString, 0, z);
|
|
|
|
|
tmp = NSZoneMalloc(z, _count);
|
|
|
|
|
memcpy(tmp, _contents_chars, _count);
|
|
|
|
|
obj = (*csInitImp)(obj, csInitSel, tmp, _count, z);
|
|
|
|
|
if (_hash && obj)
|
|
|
|
|
{
|
|
|
|
|
obj->_hash = _hash;
|
|
|
|
|
}
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return [self retain];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- copyWithZone: (NSZone*)z
|
|
|
|
|
{
|
|
|
|
|
if (NSShouldRetainWithZone(self, z) == NO)
|
|
|
|
|
{
|
|
|
|
|
NSGCString *obj;
|
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
|
|
obj = (NSGCString*)NSAllocateObject(_fastCls._NSGCString, 0, z);
|
|
|
|
|
tmp = NSZoneMalloc(z, _count);
|
|
|
|
|
memcpy(tmp, _contents_chars, _count);
|
|
|
|
|
obj = (*csInitImp)(obj, csInitSel, tmp, _count, z);
|
|
|
|
|
if (_hash && obj)
|
|
|
|
|
{
|
|
|
|
|
obj->_hash = _hash;
|
|
|
|
|
}
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return [self retain];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- mutableCopy
|
|
|
|
|
{
|
|
|
|
|
NSGMutableCString *obj;
|
|
|
|
|
|
|
|
|
|
obj = (NSGMutableCString*)NSAllocateObject(_fastCls._NSGMutableCString,
|
|
|
|
|
0, NSDefaultMallocZone());
|
|
|
|
|
if (obj)
|
|
|
|
|
{
|
|
|
|
|
obj = (*msInitImp)(obj, msInitSel, _count);
|
|
|
|
|
if (obj)
|
|
|
|
|
{
|
|
|
|
|
NSGCString *tmp = (NSGCString*)obj; // Same ivar layout!
|
|
|
|
|
|
|
|
|
|
memcpy(tmp->_contents_chars, _contents_chars, _count);
|
|
|
|
|
tmp->_count = _count;
|
|
|
|
|
tmp->_hash = _hash;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- mutableCopyWithZone: (NSZone*)z
|
|
|
|
|
{
|
|
|
|
|
NSGMutableCString *obj;
|
|
|
|
|
|
|
|
|
|
obj = (NSGMutableCString*)NSAllocateObject(_fastCls._NSGMutableCString, 0, z);
|
|
|
|
|
if (obj)
|
|
|
|
|
{
|
|
|
|
|
obj = (*msInitImp)(obj, msInitSel, _count);
|
|
|
|
|
if (obj)
|
|
|
|
|
{
|
|
|
|
|
NSGCString *tmp = (NSGCString*)obj; // Same ivar layout!
|
|
|
|
|
|
|
|
|
|
memcpy(tmp->_contents_chars, _contents_chars, _count);
|
|
|
|
|
tmp->_count = _count;
|
|
|
|
|
tmp->_hash = _hash;
|
|
|
|
|
}
|
1998-10-15 05:03:16 +00:00
|
|
|
|
}
|
1998-10-26 08:15:20 +00:00
|
|
|
|
return obj;
|
1995-04-03 01:38:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (const char *) cString
|
|
|
|
|
{
|
1998-10-26 08:15:20 +00:00
|
|
|
|
char *r = NSZoneMalloc(NSDefaultMallocZone(), _count+1);
|
1995-04-03 01:38:23 +00:00
|
|
|
|
|
|
|
|
|
memcpy(r, _contents_chars, _count);
|
|
|
|
|
r[_count] = '\0';
|
1998-09-30 07:42:38 +00:00
|
|
|
|
[NSData dataWithBytesNoCopy:r length: _count+1];
|
1995-04-03 01:38:23 +00:00
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
1998-08-03 15:31:33 +00:00
|
|
|
|
- (void) getCString: (char*)buffer
|
1995-04-03 01:38:23 +00:00
|
|
|
|
{
|
1998-08-03 15:31:33 +00:00
|
|
|
|
memcpy(buffer, _contents_chars, _count);
|
|
|
|
|
buffer[_count] = '\0';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) getCString: (char*)buffer
|
1998-10-15 05:03:16 +00:00
|
|
|
|
maxLength: (unsigned int)maxLength
|
1998-08-03 15:31:33 +00:00
|
|
|
|
{
|
|
|
|
|
if (maxLength > _count)
|
|
|
|
|
maxLength = _count;
|
|
|
|
|
memcpy(buffer, _contents_chars, maxLength);
|
|
|
|
|
buffer[maxLength] = '\0';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) getCString: (char*)buffer
|
1998-10-26 08:15:20 +00:00
|
|
|
|
maxLength: (unsigned int)maxLength
|
|
|
|
|
range: (NSRange)aRange
|
|
|
|
|
remainingRange: (NSRange*)leftoverRange
|
1998-08-03 15:31:33 +00:00
|
|
|
|
{
|
|
|
|
|
int len;
|
|
|
|
|
|
|
|
|
|
if (aRange.location >= _count)
|
|
|
|
|
[NSException raise: NSRangeException format:@"Invalid location."];
|
|
|
|
|
if (aRange.length > (_count - aRange.location))
|
|
|
|
|
[NSException raise: NSRangeException format:@"Invalid location+length."];
|
|
|
|
|
if (maxLength < aRange.length)
|
|
|
|
|
{
|
|
|
|
|
len = maxLength;
|
|
|
|
|
if (leftoverRange)
|
|
|
|
|
{
|
|
|
|
|
leftoverRange->location = 0;
|
|
|
|
|
leftoverRange->length = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
len = aRange.length;
|
|
|
|
|
if (leftoverRange)
|
|
|
|
|
{
|
|
|
|
|
leftoverRange->location = aRange.location + maxLength;
|
|
|
|
|
leftoverRange->length = aRange.length - maxLength;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memcpy(buffer, &_contents_chars[aRange.location], len);
|
|
|
|
|
buffer[len] = '\0';
|
1995-04-03 01:38:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (unsigned) count
|
|
|
|
|
{
|
|
|
|
|
return _count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (unsigned int) cStringLength
|
|
|
|
|
{
|
|
|
|
|
return _count;
|
|
|
|
|
}
|
|
|
|
|
|
1997-05-03 18:05:21 +00:00
|
|
|
|
- (unsigned int) length
|
|
|
|
|
{
|
|
|
|
|
return _count;
|
|
|
|
|
}
|
|
|
|
|
|
1995-04-03 01:38:23 +00:00
|
|
|
|
- (unichar) characterAtIndex: (unsigned int)index
|
|
|
|
|
{
|
|
|
|
|
CHECK_INDEX_RANGE_ERROR(index, _count);
|
1997-05-03 18:05:21 +00:00
|
|
|
|
return chartouni(_contents_chars[index]);
|
1995-04-03 01:38:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-08-03 15:31:33 +00:00
|
|
|
|
- (void) getCharacters: (unichar*)buffer
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < _count; i++)
|
|
|
|
|
buffer[i] = chartouni(((unsigned char *)_contents_chars)[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) getCharacters: (unichar*)buffer range: (NSRange)aRange
|
|
|
|
|
{
|
|
|
|
|
int e, i;
|
|
|
|
|
|
|
|
|
|
if (aRange.location >= _count)
|
|
|
|
|
[NSException raise: NSRangeException format:@"Invalid location."];
|
|
|
|
|
if (aRange.length > (_count - aRange.location))
|
|
|
|
|
[NSException raise: NSRangeException format:@"Invalid location+length."];
|
|
|
|
|
e = aRange.location + aRange.length;
|
|
|
|
|
for (i = aRange.location; i < e; i++)
|
|
|
|
|
*buffer++ = chartouni(((unsigned char *)_contents_chars)[i]);
|
|
|
|
|
}
|
1997-05-03 18:05:21 +00:00
|
|
|
|
|
1996-09-07 17:47:24 +00:00
|
|
|
|
- (NSString*) substringFromRange: (NSRange)aRange
|
|
|
|
|
{
|
|
|
|
|
if (aRange.location > _count)
|
|
|
|
|
[NSException raise: NSRangeException format:@"Invalid location."];
|
|
|
|
|
if (aRange.length > (_count - aRange.location))
|
|
|
|
|
[NSException raise: NSRangeException format:@"Invalid location+length."];
|
1996-09-07 17:54:08 +00:00
|
|
|
|
return [[self class] stringWithCString: _contents_chars + aRange.location
|
|
|
|
|
length: aRange.length];
|
1996-09-07 17:47:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
|
- (NSStringEncoding) fastestEncoding
|
|
|
|
|
{
|
|
|
|
|
if(([NSString defaultCStringEncoding]==NSASCIIStringEncoding) || ([NSString defaultCStringEncoding]==NSISOLatin1StringEncoding))
|
|
|
|
|
return [NSString defaultCStringEncoding];
|
|
|
|
|
else
|
|
|
|
|
return NSUnicodeStringEncoding;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSStringEncoding) smallestEncoding
|
|
|
|
|
{
|
|
|
|
|
return [NSString defaultCStringEncoding];
|
|
|
|
|
}
|
|
|
|
|
|
1998-10-01 05:22:47 +00:00
|
|
|
|
- (BOOL) isEqual: (id)anObject
|
|
|
|
|
{
|
|
|
|
|
Class c;
|
|
|
|
|
if (anObject == self)
|
|
|
|
|
return YES;
|
1998-10-07 20:17:16 +00:00
|
|
|
|
if (anObject == nil)
|
|
|
|
|
return NO;
|
1998-10-06 15:11:27 +00:00
|
|
|
|
c = fastClassOfInstance(anObject);
|
1998-10-03 21:23:04 +00:00
|
|
|
|
|
1998-10-06 15:11:27 +00:00
|
|
|
|
if (c == _fastCls._NSGCString || c == _fastCls._NSGMutableCString || c == _fastCls._NXConstantString)
|
1998-10-01 05:22:47 +00:00
|
|
|
|
{
|
|
|
|
|
NSGCString *other = (NSGCString*)anObject;
|
|
|
|
|
|
|
|
|
|
if (_count != other->_count)
|
|
|
|
|
return NO;
|
|
|
|
|
if (_hash == 0)
|
1998-10-09 04:24:56 +00:00
|
|
|
|
_hash = _fastImp._NSString_hash(self, @selector(hash));
|
1998-10-06 15:11:27 +00:00
|
|
|
|
if (other->_hash == 0)
|
1998-10-09 04:24:56 +00:00
|
|
|
|
other->_hash = _fastImp._NSString_hash(other, @selector(hash));
|
1998-10-01 05:22:47 +00:00
|
|
|
|
if (_hash != other->_hash)
|
|
|
|
|
return NO;
|
|
|
|
|
if (memcmp(_contents_chars, other->_contents_chars, _count) != 0)
|
|
|
|
|
return NO;
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
1998-10-09 04:24:56 +00:00
|
|
|
|
else if (c == nil)
|
|
|
|
|
return NO;
|
|
|
|
|
else if (fastClassIsKindOfClass(c, _fastCls._NSString))
|
|
|
|
|
return _fastImp._NSString_isEqualToString_(self,
|
|
|
|
|
@selector(isEqualToString:), anObject);
|
1998-10-01 05:22:47 +00:00
|
|
|
|
else
|
1998-10-09 04:24:56 +00:00
|
|
|
|
return NO;
|
1998-10-01 05:22:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
|
- (BOOL) isEqualToString: (NSString*)aString
|
|
|
|
|
{
|
1998-10-01 05:22:47 +00:00
|
|
|
|
Class c;
|
|
|
|
|
|
1998-10-07 20:17:16 +00:00
|
|
|
|
if (aString == self)
|
|
|
|
|
return YES;
|
|
|
|
|
if (aString == nil)
|
|
|
|
|
return NO;
|
1998-10-06 15:11:27 +00:00
|
|
|
|
c = fastClassOfInstance(aString);
|
|
|
|
|
if (c == _fastCls._NSGCString || c == _fastCls._NSGMutableCString || c == _fastCls._NXConstantString)
|
1998-08-03 15:31:33 +00:00
|
|
|
|
{
|
|
|
|
|
NSGCString *other = (NSGCString*)aString;
|
|
|
|
|
|
|
|
|
|
if (_count != other->_count)
|
|
|
|
|
return NO;
|
1998-10-06 15:11:27 +00:00
|
|
|
|
if (_hash == 0)
|
1998-10-09 04:24:56 +00:00
|
|
|
|
_hash = _fastImp._NSString_hash(self, @selector(hash));
|
1998-10-06 15:11:27 +00:00
|
|
|
|
if (other->_hash == 0)
|
1998-10-09 04:24:56 +00:00
|
|
|
|
other->_hash = _fastImp._NSString_hash(other, @selector(hash));
|
1998-10-01 05:22:47 +00:00
|
|
|
|
if (_hash != other->_hash)
|
1998-08-03 15:31:33 +00:00
|
|
|
|
return NO;
|
|
|
|
|
if (memcmp(_contents_chars, other->_contents_chars, _count) != 0)
|
|
|
|
|
return NO;
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
1998-10-09 04:24:56 +00:00
|
|
|
|
else if (c == nil)
|
|
|
|
|
return NO;
|
|
|
|
|
else if (fastClassIsKindOfClass(c, _fastCls._NSString))
|
|
|
|
|
return _fastImp._NSString_isEqualToString_(self,
|
|
|
|
|
@selector(isEqualToString:), aString);
|
1997-09-01 21:59:51 +00:00
|
|
|
|
else
|
1998-10-09 04:24:56 +00:00
|
|
|
|
return NO;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
1997-05-03 18:05:21 +00:00
|
|
|
|
|
1998-08-03 15:31:33 +00:00
|
|
|
|
|
1995-04-03 01:38:23 +00:00
|
|
|
|
// FOR IndexedCollection SUPPORT;
|
|
|
|
|
|
1996-02-22 16:05:47 +00:00
|
|
|
|
- objectAtIndex: (unsigned)index
|
1995-04-03 01:38:23 +00:00
|
|
|
|
{
|
|
|
|
|
CHECK_INDEX_RANGE_ERROR(index, _count);
|
1996-02-22 16:05:47 +00:00
|
|
|
|
return [NSNumber numberWithChar: _contents_chars[index]];
|
1995-04-03 01:38:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
1997-05-03 18:05:21 +00:00
|
|
|
|
- (int) _baseLength
|
|
|
|
|
{
|
|
|
|
|
return _count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithString: (NSString*)string
|
|
|
|
|
{
|
1998-10-15 05:03:16 +00:00
|
|
|
|
NSZone *z = fastZone(self);
|
|
|
|
|
unsigned length = [string cStringLength];
|
|
|
|
|
char *buf = NSZoneMalloc(z, length+1); // getCString appends a nul.
|
|
|
|
|
|
|
|
|
|
[string getCString: buf];
|
|
|
|
|
return [self initWithCStringNoCopy: buf length: length fromZone: z];
|
1997-05-03 18:05:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-10-09 08:36:37 +00:00
|
|
|
|
- (NSString*) descriptionForPropertyList
|
|
|
|
|
{
|
|
|
|
|
if (_count == 0) {
|
|
|
|
|
return @"\"\"";
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
unsigned i;
|
|
|
|
|
unsigned length = _count;
|
|
|
|
|
BOOL needQuote = NO;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < _count; i++) {
|
|
|
|
|
char val = _contents_chars[i];
|
|
|
|
|
|
1998-10-23 18:57:27 +00:00
|
|
|
|
if (isalnum(val)) {
|
1998-10-23 04:34:46 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
1998-10-09 08:36:37 +00:00
|
|
|
|
switch (val) {
|
|
|
|
|
case '\a':
|
|
|
|
|
case '\b':
|
|
|
|
|
case '\t':
|
|
|
|
|
case '\r':
|
|
|
|
|
case '\n':
|
|
|
|
|
case '\v':
|
|
|
|
|
case '\f':
|
|
|
|
|
case '\\':
|
|
|
|
|
case '"' :
|
1998-10-15 05:03:16 +00:00
|
|
|
|
length += 1;
|
1998-10-09 08:36:37 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
1998-10-23 04:34:46 +00:00
|
|
|
|
if (val == ' ' || isprint(val)) {
|
1998-10-09 08:36:37 +00:00
|
|
|
|
needQuote = YES;
|
|
|
|
|
}
|
1998-10-23 04:34:46 +00:00
|
|
|
|
else {
|
1998-10-15 05:03:16 +00:00
|
|
|
|
length += 3;
|
1998-10-09 08:36:37 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (needQuote || length != _count) {
|
1998-10-15 05:03:16 +00:00
|
|
|
|
NSZone *z = fastZone(self);
|
|
|
|
|
char *buf = NSZoneMalloc(z, length+3);
|
1998-10-09 08:36:37 +00:00
|
|
|
|
char *ptr = buf;
|
|
|
|
|
NSString *result;
|
|
|
|
|
|
|
|
|
|
*ptr++ = '"';
|
|
|
|
|
for (i = 0; i < _count; i++) {
|
|
|
|
|
char val = _contents_chars[i];
|
|
|
|
|
|
|
|
|
|
switch (val) {
|
|
|
|
|
case '\a': *ptr++ = '\\'; *ptr++ = 'a'; break;
|
|
|
|
|
case '\b': *ptr++ = '\\'; *ptr++ = 'b'; break;
|
|
|
|
|
case '\t': *ptr++ = '\\'; *ptr++ = 't'; break;
|
|
|
|
|
case '\r': *ptr++ = '\\'; *ptr++ = 'r'; break;
|
|
|
|
|
case '\n': *ptr++ = '\\'; *ptr++ = 'n'; break;
|
|
|
|
|
case '\v': *ptr++ = '\\'; *ptr++ = 'v'; break;
|
|
|
|
|
case '\f': *ptr++ = '\\'; *ptr++ = 'f'; break;
|
|
|
|
|
case '\\': *ptr++ = '\\'; *ptr++ = '\\'; break;
|
|
|
|
|
case '"' : *ptr++ = '\\'; *ptr++ = '"'; break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
if (isprint(val) || val == ' ') {
|
|
|
|
|
*ptr++ = val;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
*ptr++ = '\\';
|
|
|
|
|
*ptr++ = '0';
|
|
|
|
|
*ptr++ = ((val&0700)>>6)+'0';
|
|
|
|
|
*ptr++ = ((val&070)>>3)+'0';
|
|
|
|
|
*ptr++ = (val&07)+'0';
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
*ptr++ = '"';
|
|
|
|
|
*ptr = '\0';
|
|
|
|
|
result = [[[_fastCls._NSGCString alloc] initWithCStringNoCopy: buf
|
1998-10-15 05:03:16 +00:00
|
|
|
|
length: length+2 fromZone: z] autorelease];
|
1998-10-09 08:36:37 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
}
|
1995-04-03 01:38:23 +00:00
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
1995-08-09 15:46:35 +00:00
|
|
|
|
@implementation NSGMutableCString
|
1995-04-03 01:38:23 +00:00
|
|
|
|
|
1998-10-25 07:56:22 +00:00
|
|
|
|
+ allocWithZone: (NSZone*)z
|
|
|
|
|
{
|
|
|
|
|
return NSAllocateObject (self, 0, z);
|
|
|
|
|
}
|
|
|
|
|
|
1998-10-25 08:31:37 +00:00
|
|
|
|
+ alloc
|
|
|
|
|
{
|
|
|
|
|
return NSAllocateObject (self, 0, NSDefaultMallocZone());
|
|
|
|
|
}
|
|
|
|
|
|
1995-04-03 01:38:23 +00:00
|
|
|
|
+ (void) initialize
|
|
|
|
|
{
|
1995-04-05 20:25:08 +00:00
|
|
|
|
static int done = 0;
|
|
|
|
|
if (!done)
|
1995-04-03 01:38:23 +00:00
|
|
|
|
{
|
1995-04-05 20:25:08 +00:00
|
|
|
|
done = 1;
|
1995-08-09 15:45:46 +00:00
|
|
|
|
class_add_behavior(self, [NSGCString class]);
|
1995-04-03 01:38:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
1995-08-09 15:46:35 +00:00
|
|
|
|
@defs(NSGMutableCString)
|
|
|
|
|
} NSGMutableCStringStruct;
|
1995-04-03 01:38:23 +00:00
|
|
|
|
|
|
|
|
|
static inline void
|
1995-08-09 15:46:35 +00:00
|
|
|
|
stringIncrementCountAndMakeHoleAt(NSGMutableCStringStruct *self,
|
1995-04-03 02:04:48 +00:00
|
|
|
|
int index, int size)
|
1995-04-03 01:38:23 +00:00
|
|
|
|
{
|
|
|
|
|
#ifndef STABLE_MEMCPY
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
for (i = self->_count; i >= index; i--)
|
|
|
|
|
self->_contents_chars[i+size] = self->_contents_chars[i];
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
memcpy(self->_contents_chars + index,
|
|
|
|
|
self->_contents_chars + index + size,
|
|
|
|
|
self->_count - index);
|
|
|
|
|
#endif /* STABLE_MEMCPY */
|
|
|
|
|
(self->_count) += size;
|
1998-08-03 15:31:33 +00:00
|
|
|
|
(self->_hash) = 0;
|
1995-04-03 01:38:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void
|
1995-08-09 15:46:35 +00:00
|
|
|
|
stringDecrementCountAndFillHoleAt(NSGMutableCStringStruct *self,
|
1995-04-03 02:04:48 +00:00
|
|
|
|
int index, int size)
|
1995-04-03 01:38:23 +00:00
|
|
|
|
{
|
|
|
|
|
(self->_count) -= size;
|
|
|
|
|
#ifndef STABLE_MEMCPY
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
for (i = index; i <= self->_count; i++)
|
|
|
|
|
self->_contents_chars[i] = self->_contents_chars[i+size];
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
memcpy(self->_contents_chars + index + size,
|
|
|
|
|
self->_contents_chars + index,
|
|
|
|
|
self->_count - index);
|
|
|
|
|
#endif /* STABLE_MEMCPY */
|
1998-08-03 15:31:33 +00:00
|
|
|
|
(self->_hash) = 0;
|
1995-04-03 01:38:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* This is the designated initializer for this class */
|
|
|
|
|
- initWithCapacity: (unsigned)capacity
|
|
|
|
|
{
|
|
|
|
|
_count = 0;
|
1998-10-26 08:15:20 +00:00
|
|
|
|
_capacity = capacity;
|
|
|
|
|
if (capacity)
|
|
|
|
|
{
|
|
|
|
|
_zone = fastZone(self);
|
|
|
|
|
_contents_chars = NSZoneMalloc(_zone, _capacity);
|
|
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithCStringNoCopy: (char*)byteString
|
|
|
|
|
length: (unsigned int)length
|
|
|
|
|
fromZone: (NSZone*)zone
|
|
|
|
|
{
|
|
|
|
|
self = (*msInitImp)(self, msInitSel, 0);
|
|
|
|
|
if (self)
|
|
|
|
|
{
|
|
|
|
|
_count = length;
|
|
|
|
|
_capacity = length;
|
|
|
|
|
_contents_chars = byteString;
|
|
|
|
|
_zone = byteString ? zone : 0;
|
|
|
|
|
}
|
1995-04-03 01:38:23 +00:00
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
1998-10-15 05:03:16 +00:00
|
|
|
|
- (id) initWithCharactersNoCopy: (unichar*)chars
|
|
|
|
|
length: (unsigned int)length
|
|
|
|
|
fromZone: (NSZone*)zone
|
|
|
|
|
{
|
1998-10-26 08:15:20 +00:00
|
|
|
|
NSZone *z = zone ? zone : fastZone(self);
|
|
|
|
|
id a = [[NSGMutableString allocWithZone: z] initWithCharactersNoCopy: chars
|
|
|
|
|
length: length
|
|
|
|
|
fromZone: z];
|
|
|
|
|
[self release];
|
|
|
|
|
return a;
|
1998-10-15 05:03:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithCharactersNoCopy: (unichar*)chars
|
1998-10-26 08:15:20 +00:00
|
|
|
|
length: (unsigned int)length
|
|
|
|
|
freeWhenDone: (BOOL)flag
|
1998-10-15 05:03:16 +00:00
|
|
|
|
{
|
1998-10-26 08:15:20 +00:00
|
|
|
|
NSZone *z = fastZone(self);
|
|
|
|
|
id a = [[NSGMutableString allocWithZone: z] initWithCharactersNoCopy: chars
|
1998-10-15 05:03:16 +00:00
|
|
|
|
length: length
|
|
|
|
|
freeWhenDone: flag];
|
1998-10-26 08:15:20 +00:00
|
|
|
|
[self release];
|
|
|
|
|
return a;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- copy
|
|
|
|
|
{
|
|
|
|
|
char *tmp;
|
|
|
|
|
NSGCString *obj;
|
|
|
|
|
NSZone *z = NSDefaultMallocZone();
|
|
|
|
|
|
|
|
|
|
obj = (NSGCString*)NSAllocateObject(_fastCls._NSGCString, 0, z);
|
|
|
|
|
tmp = NSZoneMalloc(z, _count);
|
|
|
|
|
memcpy(tmp, _contents_chars, _count);
|
|
|
|
|
obj = (*csInitImp)(obj, csInitSel, tmp, _count, z);
|
|
|
|
|
if (_hash && obj)
|
|
|
|
|
{
|
|
|
|
|
NSGMutableCString *tmp = (NSGMutableCString*)obj; // Same ivar layout
|
|
|
|
|
|
|
|
|
|
tmp->_hash = _hash;
|
|
|
|
|
}
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- copyWithZone: (NSZone*)z
|
|
|
|
|
{
|
|
|
|
|
char *tmp;
|
|
|
|
|
NSGCString *obj;
|
|
|
|
|
|
|
|
|
|
obj = (NSGCString*)NSAllocateObject(_fastCls._NSGCString, 0, z);
|
|
|
|
|
tmp = NSZoneMalloc(z, _count);
|
|
|
|
|
memcpy(tmp, _contents_chars, _count);
|
|
|
|
|
obj = (*csInitImp)(obj, csInitSel, tmp, _count, z);
|
|
|
|
|
if (_hash && obj)
|
|
|
|
|
{
|
|
|
|
|
NSGMutableCString *tmp = (NSGMutableCString*)obj; // Same ivar layout
|
|
|
|
|
|
|
|
|
|
tmp->_hash = _hash;
|
|
|
|
|
}
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- mutableCopy
|
|
|
|
|
{
|
|
|
|
|
NSGMutableCString *obj;
|
|
|
|
|
|
|
|
|
|
obj = (NSGMutableCString*)NSAllocateObject(_fastCls._NSGMutableCString,
|
|
|
|
|
0, NSDefaultMallocZone());
|
|
|
|
|
if (obj)
|
|
|
|
|
{
|
|
|
|
|
obj = (*msInitImp)(obj, msInitSel, _count);
|
|
|
|
|
if (obj)
|
|
|
|
|
{
|
|
|
|
|
memcpy(obj->_contents_chars, _contents_chars, _count);
|
|
|
|
|
obj->_count = _count;
|
|
|
|
|
obj->_hash = _hash;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- mutableCopyWithZone: (NSZone*)z
|
|
|
|
|
{
|
|
|
|
|
NSGMutableCString *obj;
|
|
|
|
|
|
|
|
|
|
obj = (NSGMutableCString*)NSAllocateObject(_fastCls._NSGMutableCString, 0, z);
|
|
|
|
|
if (obj)
|
|
|
|
|
{
|
|
|
|
|
obj = (*msInitImp)(obj, msInitSel, _count);
|
|
|
|
|
if (obj)
|
|
|
|
|
{
|
|
|
|
|
memcpy(obj->_contents_chars, _contents_chars, _count);
|
|
|
|
|
obj->_count = _count;
|
|
|
|
|
obj->_hash = _hash;
|
|
|
|
|
}
|
|
|
|
|
}
|
1998-10-15 05:03:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
1995-04-03 01:38:23 +00:00
|
|
|
|
- (void) deleteCharactersInRange: (NSRange)range
|
|
|
|
|
{
|
1995-08-09 15:46:35 +00:00
|
|
|
|
stringDecrementCountAndFillHoleAt((NSGMutableCStringStruct*)self,
|
1995-04-03 01:38:23 +00:00
|
|
|
|
range.location, range.length);
|
|
|
|
|
}
|
|
|
|
|
|
1997-12-08 20:04:16 +00:00
|
|
|
|
// xxx This should be primitive method
|
|
|
|
|
- (void) replaceCharactersInRange: (NSRange)range
|
|
|
|
|
withString: (NSString*)aString
|
|
|
|
|
{
|
|
|
|
|
[self deleteCharactersInRange:range];
|
|
|
|
|
[self insertString:aString atIndex:range.location];
|
|
|
|
|
}
|
|
|
|
|
|
1995-04-03 01:38:23 +00:00
|
|
|
|
- (void) insertString: (NSString*)aString atIndex:(unsigned)index
|
|
|
|
|
{
|
|
|
|
|
unsigned c = [aString cStringLength];
|
1998-10-15 05:03:16 +00:00
|
|
|
|
char save;
|
|
|
|
|
if (_count + c >= _capacity)
|
1995-04-03 01:38:23 +00:00
|
|
|
|
{
|
1998-10-15 05:03:16 +00:00
|
|
|
|
_capacity = MAX(_capacity*2, _count+c+1);
|
1998-10-26 08:15:20 +00:00
|
|
|
|
_contents_chars = NSZoneRealloc(_zone, _contents_chars, _capacity);
|
1995-04-03 01:38:23 +00:00
|
|
|
|
}
|
1995-08-09 15:46:35 +00:00
|
|
|
|
stringIncrementCountAndMakeHoleAt((NSGMutableCStringStruct*)self, index, c);
|
1998-10-15 05:03:16 +00:00
|
|
|
|
save = _contents_chars[index+c]; // getCString will put a nul here.
|
1998-09-30 07:42:38 +00:00
|
|
|
|
[aString getCString: _contents_chars + index];
|
1998-10-15 05:03:16 +00:00
|
|
|
|
_contents_chars[index+c] = save;
|
1995-04-03 01:38:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
1997-05-03 17:05:57 +00:00
|
|
|
|
- (void) appendString: (NSString*)aString
|
|
|
|
|
{
|
1998-10-15 05:03:16 +00:00
|
|
|
|
Class c;
|
|
|
|
|
|
|
|
|
|
if (aString == nil || (c = fastClassOfInstance(aString)) == nil)
|
|
|
|
|
return;
|
|
|
|
|
if (c == _fastCls._NSGCString || c == _fastCls._NSGMutableCString)
|
1997-05-03 17:05:57 +00:00
|
|
|
|
{
|
1998-10-15 05:03:16 +00:00
|
|
|
|
NSGMutableCString *other = (NSGMutableCString*)aString;
|
|
|
|
|
unsigned l = other->_count;
|
|
|
|
|
|
|
|
|
|
if (_count + l > _capacity)
|
|
|
|
|
{
|
|
|
|
|
_capacity = MAX(_capacity*2, _count+l);
|
|
|
|
|
_contents_chars =
|
|
|
|
|
NSZoneRealloc(fastZone(self), _contents_chars, _capacity);
|
|
|
|
|
}
|
|
|
|
|
memcpy(_contents_chars + _count, other->_contents_chars, l);
|
|
|
|
|
_count += l;
|
|
|
|
|
_hash = 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
unsigned l = [aString cStringLength];
|
|
|
|
|
if (_count + l >= _capacity)
|
|
|
|
|
{
|
|
|
|
|
_capacity = MAX(_capacity*2, _count+l+1);
|
|
|
|
|
_contents_chars =
|
|
|
|
|
NSZoneRealloc(fastZone(self), _contents_chars, _capacity);
|
|
|
|
|
}
|
|
|
|
|
[aString getCString: _contents_chars + _count];
|
|
|
|
|
_count += l;
|
|
|
|
|
_hash = 0;
|
1997-05-03 17:05:57 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
|
- (void) setString: (NSString*)aString
|
|
|
|
|
{
|
1998-09-30 07:42:38 +00:00
|
|
|
|
unsigned length = [aString cStringLength];
|
1998-10-15 05:03:16 +00:00
|
|
|
|
if (_capacity <= length)
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
1998-10-15 05:03:16 +00:00
|
|
|
|
_capacity = length+1;
|
|
|
|
|
_contents_chars =
|
|
|
|
|
NSZoneRealloc(fastZone(self), _contents_chars, _capacity);
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
1998-09-30 07:42:38 +00:00
|
|
|
|
[aString getCString: _contents_chars];
|
1995-04-03 01:38:23 +00:00
|
|
|
|
_count = length;
|
1998-08-03 15:31:33 +00:00
|
|
|
|
_hash = 0;
|
1995-04-03 01:38:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
1997-12-08 20:04:16 +00:00
|
|
|
|
- (id) init
|
|
|
|
|
{
|
1998-10-15 05:03:16 +00:00
|
|
|
|
return [self initWithCStringNoCopy: 0 length: 0 fromZone: 0];
|
1998-10-26 08:15:20 +00:00
|
|
|
|
}
|
1995-04-03 01:38:23 +00:00
|
|
|
|
|
|
|
|
|
/* For IndexedCollecting Protocol and other GNU libobjects conformity. */
|
|
|
|
|
|
|
|
|
|
/* xxx This should be made to return void, but we need to change
|
|
|
|
|
IndexedCollecting and its conformers */
|
1996-02-22 16:05:47 +00:00
|
|
|
|
- (void) removeRange: (IndexRange)range
|
1995-04-03 01:38:23 +00:00
|
|
|
|
{
|
1995-08-09 15:46:35 +00:00
|
|
|
|
stringDecrementCountAndFillHoleAt((NSGMutableCStringStruct*)self,
|
1995-04-03 01:38:23 +00:00
|
|
|
|
range.location, range.length);
|
|
|
|
|
}
|
|
|
|
|
|
1995-04-09 01:53:53 +00:00
|
|
|
|
- initWithCoder: aCoder
|
1995-04-03 01:38:23 +00:00
|
|
|
|
{
|
|
|
|
|
unsigned cap;
|
|
|
|
|
|
1998-10-22 10:03:52 +00:00
|
|
|
|
[aCoder decodeValueOfObjCType:@encode(unsigned) at:&cap];
|
1995-04-09 01:53:53 +00:00
|
|
|
|
[self initWithCapacity:cap];
|
1998-10-15 05:03:16 +00:00
|
|
|
|
_count = cap;
|
1998-10-26 18:06:51 +00:00
|
|
|
|
if (_count > 0)
|
|
|
|
|
{
|
|
|
|
|
[aCoder decodeArrayOfObjCType: @encode(unsigned char)
|
|
|
|
|
count: _count
|
|
|
|
|
at: _contents_chars];
|
|
|
|
|
}
|
1995-04-09 01:53:53 +00:00
|
|
|
|
return self;
|
1995-04-03 01:38:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* For IndexedCollecting protocol */
|
|
|
|
|
|
|
|
|
|
- (char) charAtIndex: (unsigned)index
|
|
|
|
|
{
|
|
|
|
|
CHECK_INDEX_RANGE_ERROR(index, _count);
|
|
|
|
|
return _contents_chars[index];
|
|
|
|
|
}
|
|
|
|
|
|
1996-02-22 16:05:47 +00:00
|
|
|
|
|
|
|
|
|
// FOR IndexedCollection and OrderedCollection SUPPORT;
|
|
|
|
|
|
|
|
|
|
- (void) insertObject: newObject atIndex: (unsigned)index
|
1995-04-03 01:38:23 +00:00
|
|
|
|
{
|
|
|
|
|
CHECK_INDEX_RANGE_ERROR(index, _count+1);
|
|
|
|
|
// one for the next char, one for the '\0';
|
1998-02-03 14:20:00 +00:00
|
|
|
|
if (_count >= _capacity)
|
1995-04-03 01:38:23 +00:00
|
|
|
|
{
|
|
|
|
|
_capacity *= 2;
|
1998-10-26 08:15:20 +00:00
|
|
|
|
_contents_chars =
|
|
|
|
|
NSZoneRealloc(fastZone(self), _contents_chars, _capacity);
|
1995-04-03 01:38:23 +00:00
|
|
|
|
}
|
1995-08-09 15:46:35 +00:00
|
|
|
|
stringIncrementCountAndMakeHoleAt((NSGMutableCStringStruct*)self, index, 1);
|
1996-02-22 16:05:47 +00:00
|
|
|
|
_contents_chars[index] = [newObject charValue];
|
1995-04-03 01:38:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
1996-02-22 16:05:47 +00:00
|
|
|
|
- (void) removeObjectAtIndex: (unsigned)index
|
1995-04-03 01:38:23 +00:00
|
|
|
|
{
|
|
|
|
|
CHECK_INDEX_RANGE_ERROR(index, _count);
|
1995-08-09 15:46:35 +00:00
|
|
|
|
stringDecrementCountAndFillHoleAt((NSGMutableCStringStruct*)self, index, 1);
|
1995-04-03 01:38:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
1998-10-03 21:23:04 +00:00
|
|
|
|
|
|
|
|
|
@implementation NXConstantString
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* NXConstantString overrides [-dealloc] so that it is never deallocated.
|
1998-10-20 08:50:52 +00:00
|
|
|
|
* If we pass an NXConstantString to another process or record it in an
|
|
|
|
|
* archive and readi it back, the new copy will never be deallocated -
|
|
|
|
|
* causing a memory leak. So we tell the system to use the super class.
|
1998-10-03 21:23:04 +00:00
|
|
|
|
*/
|
1998-10-23 15:47:07 +00:00
|
|
|
|
- (Class)classForArchiver
|
|
|
|
|
{
|
|
|
|
|
return [self superclass];
|
|
|
|
|
}
|
|
|
|
|
|
1998-10-20 08:50:52 +00:00
|
|
|
|
- (Class)classForCoder
|
1998-10-03 21:23:04 +00:00
|
|
|
|
{
|
|
|
|
|
return [self superclass];
|
|
|
|
|
}
|
|
|
|
|
|
1998-10-23 15:47:07 +00:00
|
|
|
|
- (Class)classForPortCoder
|
|
|
|
|
{
|
|
|
|
|
return [self superclass];
|
|
|
|
|
}
|
|
|
|
|
|
1998-10-03 21:23:04 +00:00
|
|
|
|
- (void)dealloc
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (const char*) cString
|
|
|
|
|
{
|
|
|
|
|
return _contents_chars;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- retain
|
|
|
|
|
{
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (oneway void) release
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- autorelease
|
|
|
|
|
{
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- copyWithZone: (NSZone*)z
|
|
|
|
|
{
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSZone*) zone
|
|
|
|
|
{
|
|
|
|
|
return NSDefaultMallocZone();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSStringEncoding) fastestEncoding
|
|
|
|
|
{
|
|
|
|
|
return NSASCIIStringEncoding;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSStringEncoding) smallestEncoding
|
|
|
|
|
{
|
|
|
|
|
return NSASCIIStringEncoding;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (unichar) characterAtIndex: (unsigned int)index
|
|
|
|
|
{
|
|
|
|
|
CHECK_INDEX_RANGE_ERROR(index, _count);
|
|
|
|
|
return (unichar)_contents_chars[index];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|