Minor string fixes - range checking etc.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@7450 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Richard Frith-MacDonald 2000-09-08 17:06:18 +00:00
parent 6fdd577b58
commit 0f11471fdf
4 changed files with 65 additions and 64 deletions

View file

@ -1,3 +1,8 @@
2000-09-08 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSGString.m: Removed obsolete methods and added range checks.
* Source/NSGCString.m: ditto.
2000-09-08 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSRunLoop.m: ([-_checkPerformers:]) When a loop executes the

View file

@ -1115,23 +1115,71 @@ stringDecrementCountAndFillHoleAt(NSGMutableCStringStruct *self,
- (void) deleteCharactersInRange: (NSRange)range
{
GS_RANGE_CHECK(range, _count);
stringDecrementCountAndFillHoleAt((NSGMutableCStringStruct*)self,
range.location, range.length);
}
// xxx This should be primitive method
- (void) replaceCharactersInRange: (NSRange)range
- (void) replaceCharactersInRange: (NSRange)aRange
withString: (NSString*)aString
{
[self deleteCharactersInRange: range];
[self insertString: aString atIndex: range.location];
int offset;
unsigned stringLength = (aString == nil) ? 0 : [aString length];
int tmp;
GS_RANGE_CHECK(aRange, _count);
offset = stringLength - aRange.length;
/*
* Make sure we have enough space for the string plus a terminating nul
* because we may need to use the getCString method, and that will write
* a nul into our buffer.
*/
if (_count + stringLength >= _capacity + aRange.length)
{
_capacity += stringLength - aRange.length;
if (_capacity < 2)
_capacity = 2;
_contents_chars =
NSZoneRealloc(_zone, _contents_chars, sizeof(char)*(_capacity+1));
}
#ifdef HAVE_MEMMOVE
if (offset != 0)
{
char *src = _contents_chars + aRange.location + aRange.length;
memmove(src + offset, src, (_count - aRange.location - aRange.length));
}
#else
if (offset > 0)
{
int first = aRange.location + aRange.length;
int i;
for (i = _count - 1; i >= first; i--)
_contents_chars[i+offset] = _contents_chars[i];
}
else if (offset < 0)
{
int i;
for (i = aRange.location + aRange.length; i < _count; i++)
_contents_chars[i+offset] = _contents_chars[i];
}
#endif
tmp = _contents_chars[aRange.location + stringLength];
[aString getCString: &_contents_chars[aRange.location]];
_contents_chars[aRange.location + stringLength] = tmp;
_count += offset;
_hash = 0;
}
- (void) insertString: (NSString*)aString atIndex: (unsigned)index
{
unsigned c = [aString cStringLength];
unsigned c;
unsigned char save;
CHECK_INDEX_RANGE_ERROR(index, _count);
c = [aString cStringLength];
if (_count + c >= _capacity)
stringGrowBy((NSGMutableCStringStruct *)self, c);
stringIncrementCountAndMakeHoleAt((NSGMutableCStringStruct*)self, index, c);
@ -1187,12 +1235,6 @@ stringDecrementCountAndFillHoleAt(NSGMutableCStringStruct *self,
return [self initWithCStringNoCopy: 0 length: 0 fromZone: 0];
}
- (void) removeRange: (NSRange)range
{
stringDecrementCountAndFillHoleAt((NSGMutableCStringStruct*)self,
range.location, range.length);
}
- (id) initWithCoder: (NSCoder*)aCoder
{
unsigned cap;
@ -1216,25 +1258,6 @@ stringDecrementCountAndFillHoleAt(NSGMutableCStringStruct *self,
}
- (void) insertObject: newObject atIndex: (unsigned)index
{
CHECK_INDEX_RANGE_ERROR(index, _count+1);
// one for the next char, one for the '\0';
if (_count >= _capacity)
{
_capacity *= 2;
_contents_chars =
NSZoneRealloc(_zone, _contents_chars, _capacity);
}
stringIncrementCountAndMakeHoleAt((NSGMutableCStringStruct*)self, index, 1);
_contents_chars[index] = [newObject charValue];
}
- (void) removeObjectAtIndex: (unsigned)index
{
CHECK_INDEX_RANGE_ERROR(index, _count);
stringDecrementCountAndFillHoleAt((NSGMutableCStringStruct*)self, index, 1);
}
- (unsigned char*) _extendBy: (unsigned)len
{

View file

@ -762,6 +762,7 @@ stringDecrementCountAndFillHoleAt(NSGMutableStringStruct *self,
- (void) deleteCharactersInRange: (NSRange)range
{
GS_RANGE_CHECK(range, _count);
stringDecrementCountAndFillHoleAt((NSGMutableStringStruct*)self,
range.location, range.length);
}
@ -828,12 +829,6 @@ stringDecrementCountAndFillHoleAt(NSGMutableStringStruct *self,
_hash = 0;
}
- (void) removeRange: (NSRange)range
{
stringDecrementCountAndFillHoleAt((NSGMutableStringStruct*)self,
range.location, range.length);
}
- (id) initWithCoder: (NSCoder*)aCoder
{
unsigned cap;
@ -856,31 +851,4 @@ stringDecrementCountAndFillHoleAt(NSGMutableStringStruct *self,
return unitochar(_contents_chars[index]);
}
// FOR IndexedCollection and OrderedCollection SUPPORT;
- (void) insertObject: newObject atIndex: (unsigned)index
{
CHECK_INDEX_RANGE_ERROR(index, _count+1);
// one for the next char, one for the '\0';
if (_count >= _capacity)
{
_capacity = _count;
if (_capacity < 2)
_capacity = 2;
_contents_chars =
NSZoneRealloc(_zone, _contents_chars, sizeof(unichar)*_capacity);
}
stringIncrementCountAndMakeHoleAt((NSGMutableStringStruct*)self, index, 1);
_contents_chars[index] = [newObject charValue];
}
- (void) removeObjectAtIndex: (unsigned)index
{
CHECK_INDEX_RANGE_ERROR(index, _count);
stringDecrementCountAndFillHoleAt((NSGMutableStringStruct*)self, index, 1);
}
@end

View file

@ -93,10 +93,15 @@ void testAttributedString(void)
attributes:colorAttributes];
[muAttrString autorelease];
printAttrString(muAttrString);
[muAttrString addAttribute:NSFontAttributeName value: @"Helvetica 12-point"
range:NSMakeRange(2,4)];
printAttrString(muAttrString);
/*
[muAttrString setAttributes:attributes
range:NSMakeRange(2,4)];
printAttrString(muAttrString);
*/
[muAttrString setAttributes:attributes
range:NSMakeRange(8,16)];