1995-04-03 01:38:23 +00:00
|
|
|
|
/* Implementation for GNUStep of NSStrings with C-string backing
|
1997-01-05 23:17:52 +00:00
|
|
|
|
Copyright (C) 1993,1994, 1996, 1997 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
|
|
|
|
|
|
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-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
|
|
|
|
|
1995-08-09 15:45:46 +00:00
|
|
|
|
@implementation NSGCString
|
1995-04-03 01:38:23 +00:00
|
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
|
- (void)dealloc
|
|
|
|
|
{
|
1997-12-08 20:04:16 +00:00
|
|
|
|
if (_free_contents)
|
|
|
|
|
OBJC_FREE(_contents_chars);
|
1997-09-01 21:59:51 +00:00
|
|
|
|
[super dealloc];
|
|
|
|
|
}
|
|
|
|
|
|
1998-08-03 15:31:33 +00:00
|
|
|
|
- (unsigned) hash
|
|
|
|
|
{
|
|
|
|
|
if (_hash == 0)
|
1998-10-09 04:24:56 +00:00
|
|
|
|
_hash = _fastImp._NSString_hash(self, @selector(hash));
|
1998-08-03 15:31:33 +00:00
|
|
|
|
return _hash;
|
|
|
|
|
}
|
|
|
|
|
|
1995-04-03 01:38:23 +00:00
|
|
|
|
/* This is the designated initializer for this class. */
|
|
|
|
|
- (id) initWithCStringNoCopy: (char*)byteString
|
|
|
|
|
length: (unsigned int)length
|
|
|
|
|
freeWhenDone: (BOOL)flag
|
|
|
|
|
{
|
1998-01-08 15:25:59 +00:00
|
|
|
|
/* assert(!flag); xxx need to make a subclass to handle this. */
|
|
|
|
|
[super init];
|
1995-04-03 01:38:23 +00:00
|
|
|
|
_count = length;
|
|
|
|
|
_contents_chars = byteString;
|
1995-10-18 16:47:59 +00:00
|
|
|
|
_free_contents = flag;
|
1995-04-03 01:38:23 +00:00
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
|
- (id) initWithCharactersNoCopy: (unichar*)chars
|
|
|
|
|
length: (unsigned int)length
|
|
|
|
|
freeWhenDone: (BOOL)flag
|
|
|
|
|
{
|
|
|
|
|
id a = [[NSGString alloc] initWithCharactersNoCopy: chars
|
|
|
|
|
length: length
|
|
|
|
|
freeWhenDone: flag];
|
|
|
|
|
[self release];
|
|
|
|
|
return a;
|
|
|
|
|
}
|
|
|
|
|
|
1997-12-08 20:04:16 +00:00
|
|
|
|
- (id) init
|
|
|
|
|
{
|
|
|
|
|
return [self initWithCString:""];
|
|
|
|
|
}
|
|
|
|
|
|
1996-02-22 16:05:47 +00:00
|
|
|
|
- (void) _collectionReleaseContents
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) _collectionDealloc
|
1995-10-18 16:47:59 +00:00
|
|
|
|
{
|
|
|
|
|
if (_free_contents)
|
|
|
|
|
OBJC_FREE(_contents_chars);
|
|
|
|
|
}
|
|
|
|
|
|
1995-04-03 01:38:23 +00:00
|
|
|
|
- (Class) classForConnectedCoder: aRmc
|
|
|
|
|
{
|
|
|
|
|
/* Make sure that Connection's always send us bycopy,
|
|
|
|
|
i.e. as our own class, not a Proxy class. */
|
|
|
|
|
return [self class];
|
|
|
|
|
}
|
|
|
|
|
|
1997-09-29 14:39:53 +00:00
|
|
|
|
- (Class) classForPortCoder
|
1997-09-12 17:54:10 +00:00
|
|
|
|
{
|
|
|
|
|
return [self class];
|
|
|
|
|
}
|
|
|
|
|
- replacementObjectForPortCoder:(NSPortCoder*)aCoder
|
|
|
|
|
{
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
1995-04-03 01:38:23 +00:00
|
|
|
|
- (void) encodeWithCoder: aCoder
|
|
|
|
|
{
|
1996-01-23 23:57:17 +00:00
|
|
|
|
[aCoder encodeValueOfObjCType:@encode(char*) at:&_contents_chars
|
1996-01-24 03:33:21 +00:00
|
|
|
|
withName:@"Concrete String content_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
|
|
|
|
{
|
1996-01-23 23:57:17 +00:00
|
|
|
|
[aCoder decodeValueOfObjCType:@encode(char*) at:&_contents_chars
|
1995-04-03 01:38:23 +00:00
|
|
|
|
withName:NULL];
|
1995-04-09 01:53:53 +00:00
|
|
|
|
_count = strlen(_contents_chars);
|
1995-10-18 16:47:59 +00:00
|
|
|
|
_free_contents = YES;
|
1995-04-09 01:53:53 +00:00
|
|
|
|
return self;
|
1995-04-03 01:38:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (const char *) cString
|
|
|
|
|
{
|
|
|
|
|
char *r;
|
|
|
|
|
|
1995-07-06 17:57:02 +00:00
|
|
|
|
OBJC_MALLOC(r, char, _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;
|
|
|
|
|
}
|
|
|
|
|
|
1996-01-23 21:15:23 +00:00
|
|
|
|
- (const char *) cStringNoCopy
|
|
|
|
|
{
|
|
|
|
|
return _contents_chars;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
maxLength: (unsigned int)maxLength
|
|
|
|
|
{
|
|
|
|
|
if (maxLength > _count)
|
|
|
|
|
maxLength = _count;
|
|
|
|
|
memcpy(buffer, _contents_chars, maxLength);
|
|
|
|
|
buffer[maxLength] = '\0';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) getCString: (char*)buffer
|
|
|
|
|
maxLength: (unsigned int)maxLength
|
|
|
|
|
range: (NSRange)aRange
|
|
|
|
|
remainingRange: (NSRange*)leftoverRange
|
|
|
|
|
{
|
|
|
|
|
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-09-30 07:42:38 +00:00
|
|
|
|
return [self initWithCString:[string cString]];
|
1997-05-03 18:05:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
+ (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 */
|
1998-02-03 14:20:00 +00:00
|
|
|
|
/* NB. capacity does not include the '\0' terminator */
|
1995-04-03 01:38:23 +00:00
|
|
|
|
- initWithCapacity: (unsigned)capacity
|
|
|
|
|
{
|
1998-01-08 15:25:59 +00:00
|
|
|
|
[super init];
|
1995-04-03 01:38:23 +00:00
|
|
|
|
_count = 0;
|
1998-02-03 14:20:00 +00:00
|
|
|
|
_capacity = MAX(capacity, 3);
|
|
|
|
|
OBJC_MALLOC(_contents_chars, char, _capacity+1);
|
1995-04-03 01:38:23 +00:00
|
|
|
|
_contents_chars[0] = '\0';
|
1995-10-18 16:47:59 +00:00
|
|
|
|
_free_contents = YES;
|
1995-04-03 01:38:23 +00:00
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (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-02-03 14:20:00 +00:00
|
|
|
|
if (_count + c > _capacity)
|
1995-04-03 01:38:23 +00:00
|
|
|
|
{
|
|
|
|
|
_capacity = MAX(_capacity*2, _count+c);
|
1998-02-03 14:20:00 +00:00
|
|
|
|
OBJC_REALLOC(_contents_chars, char, _capacity+1);
|
1995-04-03 01:38:23 +00:00
|
|
|
|
}
|
1995-08-09 15:46:35 +00:00
|
|
|
|
stringIncrementCountAndMakeHoleAt((NSGMutableCStringStruct*)self, index, c);
|
1998-09-30 07:42:38 +00:00
|
|
|
|
[aString getCString: _contents_chars + index];
|
1995-04-03 01:38:23 +00:00
|
|
|
|
_contents_chars[_count] = '\0';
|
|
|
|
|
}
|
|
|
|
|
|
1997-05-03 17:05:57 +00:00
|
|
|
|
- (void) appendString: (NSString*)aString
|
|
|
|
|
{
|
|
|
|
|
unsigned c = [aString cStringLength];
|
1998-02-03 14:20:00 +00:00
|
|
|
|
if (_count + c > _capacity)
|
1997-05-03 17:05:57 +00:00
|
|
|
|
{
|
|
|
|
|
_capacity = MAX(_capacity*2, _count+c);
|
1998-02-03 14:20:00 +00:00
|
|
|
|
OBJC_REALLOC(_contents_chars, char, _capacity+1);
|
1997-05-03 17:05:57 +00:00
|
|
|
|
}
|
1998-09-30 07:42:38 +00:00
|
|
|
|
[aString getCString: _contents_chars + _count];
|
1997-05-03 17:05:57 +00:00
|
|
|
|
_count += c;
|
|
|
|
|
_contents_chars[_count] = '\0';
|
1998-08-03 15:31:33 +00:00
|
|
|
|
_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-02-03 14:20:00 +00:00
|
|
|
|
if (_capacity < length)
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
1998-02-03 14:20:00 +00:00
|
|
|
|
_capacity = length;
|
|
|
|
|
OBJC_REALLOC(_contents_chars, char, _capacity+1);
|
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
|
|
|
|
_contents_chars[length] = '\0';
|
|
|
|
|
_count = length;
|
1998-08-03 15:31:33 +00:00
|
|
|
|
_hash = 0;
|
1995-04-03 01:38:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Override NSString's designated initializer for CStrings. */
|
|
|
|
|
- (id) initWithCStringNoCopy: (char*)byteString
|
|
|
|
|
length: (unsigned int)length
|
|
|
|
|
freeWhenDone: (BOOL)flag
|
|
|
|
|
{
|
1998-01-08 15:25:59 +00:00
|
|
|
|
[super init];
|
1997-12-08 20:04:16 +00:00
|
|
|
|
_count = length;
|
1998-02-03 14:20:00 +00:00
|
|
|
|
_capacity = length;
|
1997-12-08 20:04:16 +00:00
|
|
|
|
_contents_chars = byteString;
|
|
|
|
|
_free_contents = flag;
|
1995-04-03 01:38:23 +00:00
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
1997-12-08 20:04:16 +00:00
|
|
|
|
/* Override NSString's designated initializer for Unicode Strings. */
|
|
|
|
|
- (id) initWithCharactersNoCopy: (unichar*)chars
|
|
|
|
|
length: (unsigned int)length
|
|
|
|
|
freeWhenDone: (BOOL)flag
|
|
|
|
|
{
|
|
|
|
|
id a = [[NSGMutableString alloc] initWithCharactersNoCopy: chars
|
|
|
|
|
length: length
|
|
|
|
|
freeWhenDone: flag];
|
|
|
|
|
[self release];
|
|
|
|
|
return a;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
|
{
|
|
|
|
|
return [self initWithCString:""];
|
|
|
|
|
};
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) encodeWithCoder: aCoder
|
|
|
|
|
{
|
1996-01-23 23:57:17 +00:00
|
|
|
|
[aCoder encodeValueOfObjCType:@encode(unsigned) at:&_capacity
|
1996-01-24 03:33:21 +00:00
|
|
|
|
withName:@"String capacity"];
|
1996-01-23 23:57:17 +00:00
|
|
|
|
[aCoder encodeValueOfObjCType:@encode(char*) at:&_contents_chars
|
1996-01-24 03:33:21 +00:00
|
|
|
|
withName:@"String content_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
|
|
|
|
{
|
|
|
|
|
unsigned cap;
|
|
|
|
|
|
1996-01-23 23:57:17 +00:00
|
|
|
|
[aCoder decodeValueOfObjCType:@encode(unsigned) at:&cap withName:NULL];
|
1995-04-09 01:53:53 +00:00
|
|
|
|
[self initWithCapacity:cap];
|
1996-01-23 23:57:17 +00:00
|
|
|
|
[aCoder decodeValueOfObjCType:@encode(char*) at:&_contents_chars
|
1995-04-03 01:38:23 +00:00
|
|
|
|
withName:NULL];
|
1995-04-09 01:53:53 +00:00
|
|
|
|
_count = strlen(_contents_chars);
|
|
|
|
|
_capacity = cap;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
_free_contents = YES;
|
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-02-03 14:20:00 +00:00
|
|
|
|
OBJC_REALLOC(_contents_chars, char, _capacity+1);
|
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
|
|
|
|
_contents_chars[_count] = '\0';
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
_contents_chars[_count] = '\0';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
1998-10-03 21:23:04 +00:00
|
|
|
|
|
|
|
|
|
@implementation NXConstantString
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* NXConstantString overrides [-dealloc] so that it is never deallocated.
|
|
|
|
|
* If we pass an NXConstantString to another process it will never get
|
|
|
|
|
* deallocated in the other process - causing a memory leak. So we tell
|
|
|
|
|
* the DO system to use the super class instead.
|
|
|
|
|
*/
|
|
|
|
|
- (Class)classForPortCoder
|
|
|
|
|
{
|
|
|
|
|
return [self superclass];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (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
|