1995-04-03 01:38:23 +00:00
|
|
|
/* Implementation for GNUStep of NSStrings with C-string backing
|
1996-01-08 16:25:10 +00:00
|
|
|
Copyright (C) 1993,1994, 1996 Free Software Foundation, Inc.
|
1995-04-03 01:38:23 +00:00
|
|
|
|
|
|
|
Written by: R. Andrew McCallum <mccallum@gnu.ai.mit.edu>
|
|
|
|
Date: March 1995
|
|
|
|
|
|
|
|
This file is part of the GNU Objective C Class Library.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <objects/stdobjects.h>
|
1995-04-17 21:13:20 +00:00
|
|
|
#include <Foundation/NSString.h>
|
1995-04-03 01:38:23 +00:00
|
|
|
#include <objects/NSString.h>
|
|
|
|
#include <objects/IndexedCollection.h>
|
|
|
|
#include <objects/IndexedCollectionPrivate.h>
|
|
|
|
#include <objects/MallocAddress.h>
|
|
|
|
/* memcpy(), strlen(), strcmp() are gcc builtin's */
|
|
|
|
|
1995-08-09 15:45:46 +00:00
|
|
|
@implementation NSGCString
|
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
|
|
|
|
{
|
1995-10-18 16:47:59 +00:00
|
|
|
/* assert(!flag); /* xxx need to make a subclass to handle this. */
|
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;
|
|
|
|
}
|
|
|
|
|
1995-10-18 16:47:59 +00:00
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
if (_free_contents)
|
|
|
|
OBJC_FREE(_contents_chars);
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (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
|
|
|
{
|
1995-04-09 01:53:53 +00:00
|
|
|
[super initWithCoder:aCoder];
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/* Empty copy must empty an allocCopy'ed version of self */
|
|
|
|
- emptyCopy
|
|
|
|
{
|
1996-01-08 16:25:10 +00:00
|
|
|
NSGCString *copy = [super emptyCopy];
|
1995-04-03 01:38:23 +00:00
|
|
|
OBJC_MALLOC(copy->_contents_chars, char, _count+1);
|
|
|
|
copy->_count = 0;
|
|
|
|
copy->_contents_chars[0] = '\0';
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (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';
|
|
|
|
[[[MallocAddress alloc] initWithAddress:r] autorelease];
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
1996-01-23 21:15:23 +00:00
|
|
|
- (const char *) cStringNoCopy
|
|
|
|
{
|
|
|
|
return _contents_chars;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* xxx Remove this method, now that we have cStringNoCopy */
|
1995-04-03 01:38:23 +00:00
|
|
|
- (const char *) _cStringContents
|
|
|
|
{
|
|
|
|
return _contents_chars;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (unsigned) count
|
|
|
|
{
|
|
|
|
return _count;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (unsigned int) cStringLength
|
|
|
|
{
|
|
|
|
return _count;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (unichar) characterAtIndex: (unsigned int)index
|
|
|
|
{
|
|
|
|
/* xxx This should raise an NSException. */
|
|
|
|
CHECK_INDEX_RANGE_ERROR(index, _count);
|
|
|
|
return (unichar) _contents_chars[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
// FOR IndexedCollection SUPPORT;
|
|
|
|
|
|
|
|
- (elt) elementAtIndex: (unsigned)index
|
|
|
|
{
|
|
|
|
elt ret_elt;
|
|
|
|
CHECK_INDEX_RANGE_ERROR(index, _count);
|
|
|
|
ret_elt.char_u = _contents_chars[index];
|
|
|
|
return ret_elt;
|
|
|
|
}
|
|
|
|
|
|
|
|
@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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is the designated initializer for this class */
|
1995-04-07 20:56:20 +00:00
|
|
|
/* xxx Should capacity include the '\0' terminator? */
|
1995-04-03 01:38:23 +00:00
|
|
|
- initWithCapacity: (unsigned)capacity
|
|
|
|
{
|
|
|
|
_count = 0;
|
1995-04-07 20:56:20 +00:00
|
|
|
_capacity = MAX(capacity, 2);
|
|
|
|
OBJC_MALLOC(_contents_chars, char, _capacity);
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) insertString: (NSString*)aString atIndex:(unsigned)index
|
|
|
|
{
|
|
|
|
unsigned c = [aString cStringLength];
|
|
|
|
if (_count + c >= _capacity)
|
|
|
|
{
|
|
|
|
_capacity = MAX(_capacity*2, _count+c);
|
|
|
|
OBJC_REALLOC(_contents_chars, char, _capacity);
|
|
|
|
}
|
1995-08-09 15:46:35 +00:00
|
|
|
stringIncrementCountAndMakeHoleAt((NSGMutableCStringStruct*)self, index, c);
|
1995-04-03 01:38:23 +00:00
|
|
|
memcpy(_contents_chars + index, [aString _cStringContents], c);
|
|
|
|
_contents_chars[_count] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* xxx This method may be removed in future. */
|
|
|
|
- (void) setCString: (const char *)byteString length: (unsigned)length
|
|
|
|
{
|
|
|
|
if (_capacity < length+1)
|
|
|
|
{
|
|
|
|
_capacity = length+1;
|
|
|
|
OBJC_REALLOC(_contents_chars, char, _capacity);
|
|
|
|
}
|
|
|
|
memcpy(_contents_chars, byteString, length);
|
|
|
|
_contents_chars[length] = '\0';
|
|
|
|
_count = length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Override NSString's designated initializer for CStrings. */
|
|
|
|
- (id) initWithCStringNoCopy: (char*)byteString
|
|
|
|
length: (unsigned int)length
|
|
|
|
freeWhenDone: (BOOL)flag
|
|
|
|
{
|
|
|
|
[self initWithCapacity:length];
|
|
|
|
[self setCString:byteString length:length];
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
- removeRange: (IndexRange)range
|
|
|
|
{
|
1995-08-09 15:46:35 +00:00
|
|
|
stringDecrementCountAndFillHoleAt((NSGMutableCStringStruct*)self,
|
1995-04-03 01:38:23 +00:00
|
|
|
range.location, range.length);
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (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;
|
|
|
|
return self;
|
1995-04-03 01:38:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* For IndexedCollecting protocol */
|
|
|
|
|
|
|
|
/* Empty copy must empty an allocCopy'ed version of self */
|
|
|
|
- emptyCopy
|
|
|
|
{
|
1995-08-09 15:46:35 +00:00
|
|
|
NSGMutableCString *copy = [super emptyCopy];
|
1995-04-03 01:38:23 +00:00
|
|
|
OBJC_MALLOC(copy->_contents_chars, char, _count+1);
|
|
|
|
copy->_count = 0;
|
|
|
|
copy->_contents_chars[0] = '\0';
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (char) charAtIndex: (unsigned)index
|
|
|
|
{
|
|
|
|
CHECK_INDEX_RANGE_ERROR(index, _count);
|
|
|
|
return _contents_chars[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
- insertElement: (elt)newElement atIndex: (unsigned)index
|
|
|
|
{
|
|
|
|
CHECK_INDEX_RANGE_ERROR(index, _count+1);
|
|
|
|
// one for the next char, one for the '\0';
|
|
|
|
if (_count+1 >= _capacity)
|
|
|
|
{
|
|
|
|
_capacity *= 2;
|
|
|
|
OBJC_REALLOC(_contents_chars, char, _capacity);
|
|
|
|
}
|
1995-08-09 15:46:35 +00:00
|
|
|
stringIncrementCountAndMakeHoleAt((NSGMutableCStringStruct*)self, index, 1);
|
1995-04-03 01:38:23 +00:00
|
|
|
_contents_chars[index] = newElement.char_u;
|
|
|
|
_contents_chars[_count] = '\0';
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (elt) removeElementAtIndex: (unsigned)index
|
|
|
|
{
|
|
|
|
elt ret;
|
|
|
|
|
|
|
|
CHECK_INDEX_RANGE_ERROR(index, _count);
|
|
|
|
ret = _contents_chars[index];
|
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';
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (elt) elementAtIndex: (unsigned)index
|
|
|
|
{
|
|
|
|
elt ret_elt;
|
|
|
|
CHECK_INDEX_RANGE_ERROR(index, _count);
|
|
|
|
ret_elt.char_u = _contents_chars[index];
|
|
|
|
return ret_elt;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|