Prepare for gc and tidy a little

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@4351 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
richard 1999-06-03 10:59:25 +00:00
parent 4941d2932f
commit 4659f829a9
3 changed files with 244 additions and 217 deletions

View file

@ -1,3 +1,8 @@
Thu Jun 4 12:15:00 1999 Richard Frith-Macdonald <richard@brainstorm.co.uk>
* Source/NSCharacterSet.m: prepare for GC
* Source/NSString.m: prepare for GC
Tue Jun 2 10:50:00 1999 Richard Frith-Macdonald <richard@brainstorm.co.uk> Tue Jun 2 10:50:00 1999 Richard Frith-Macdonald <richard@brainstorm.co.uk>
* Source/NSSerializer.m: Added uniquing for deserialized strings. * Source/NSSerializer.m: Added uniquing for deserialized strings.

View file

@ -67,7 +67,7 @@ static NSLock* cache_lock = nil;
NSBundle *user_bundle = nil, *local_bundle = nil, *system_bundle = nil; NSBundle *user_bundle = nil, *local_bundle = nil, *system_bundle = nil;
NSProcessInfo *pInfo; NSProcessInfo *pInfo;
NSDictionary *env; NSDictionary *env;
NSMutableString *user, *local, *system; NSString *user, *local, *system;
/* /*
The path of where to search for the resource files The path of where to search for the resource files
@ -78,15 +78,12 @@ static NSLock* cache_lock = nil;
*/ */
pInfo = [NSProcessInfo processInfo]; pInfo = [NSProcessInfo processInfo];
env = [pInfo environment]; env = [pInfo environment];
user = [[[env objectForKey: @"GNUSTEP_USER_ROOT"] user = [env objectForKey: @"GNUSTEP_USER_ROOT"];
mutableCopy] autorelease]; user = [user stringByAppendingPathComponent: @"Libraries"];
[user appendString: @"/Libraries"]; local = [env objectForKey: @"GNUSTEP_LOCAL_ROOT"];
local = [[[env objectForKey: @"GNUSTEP_LOCAL_ROOT"] local = [local stringByAppendingPathComponent: @"Libraries"];
mutableCopy] autorelease]; system = [env objectForKey: @"GNUSTEP_SYSTEM_ROOT"];
[local appendString: @"/Libraries"]; system = [system stringByAppendingPathComponent: @"Libraries"];
system = [[[env objectForKey: @"GNUSTEP_SYSTEM_ROOT"]
mutableCopy] autorelease];
[system appendString: @"/Libraries"];
if (user) if (user)
user_bundle = [NSBundle bundleWithPath: user]; user_bundle = [NSBundle bundleWithPath: user];
@ -171,7 +168,7 @@ static NSLock* cache_lock = nil;
} }
else else
/* Else cache the set */ /* Else cache the set */
cache_set[number] = [set retain]; cache_set[number] = RETAIN(set);
NS_HANDLER NS_HANDLER
[cache_lock unlock]; [cache_lock unlock];
@ -256,7 +253,7 @@ static NSLock* cache_lock = nil;
+ (NSCharacterSet *)characterSetWithBitmapRepresentation: (NSData *)data + (NSCharacterSet *)characterSetWithBitmapRepresentation: (NSData *)data
{ {
return [[[NSBitmapCharSet alloc] initWithBitmap: data] autorelease]; return AUTORELEASE([[NSBitmapCharSet alloc] initWithBitmap: data]);
} }
+ (NSCharacterSet *)characterSetWithCharactersInString: (NSString *)aString + (NSCharacterSet *)characterSetWithCharactersInString: (NSString *)aString
@ -360,7 +357,7 @@ static NSLock* cache_lock = nil;
char *bytes; char *bytes;
NSMutableData *bitmap; NSMutableData *bitmap;
bitmap = [[[self bitmapRepresentation] mutableCopy] autorelease]; bitmap = AUTORELEASE([[self bitmapRepresentation] mutableCopy]);
length = [bitmap length]; length = [bitmap length];
bytes = [bitmap mutableBytes]; bytes = [bitmap mutableBytes];
for (i=0; i < length; i++) for (i=0; i < length; i++)
@ -371,12 +368,12 @@ static NSLock* cache_lock = nil;
// NSCopying, NSMutableCopying // NSCopying, NSMutableCopying
- (id)copyWithZone: (NSZone *)zone - (id) copyWithZone: (NSZone *)zone
{ {
if (NSShouldRetainWithZone(self, zone)) if (NSShouldRetainWithZone(self, zone))
return [self retain]; return RETAIN(self);
else else
return NSCopyObject (self, 0, zone); return NSCopyObject (self, 0, zone);
} }
- (id)mutableCopyWithZone: (NSZone *)zone - (id)mutableCopyWithZone: (NSZone *)zone
@ -399,7 +396,7 @@ static NSLock* cache_lock = nil;
/* Override this from NSCharacterSet to create the correct class */ /* Override this from NSCharacterSet to create the correct class */
+ (NSCharacterSet *)characterSetWithBitmapRepresentation: (NSData *)data + (NSCharacterSet *)characterSetWithBitmapRepresentation: (NSData *)data
{ {
return [[[NSMutableBitmapCharSet alloc] initWithBitmap: data] autorelease]; return AUTORELEASE([[NSMutableBitmapCharSet alloc] initWithBitmap: data]);
} }
/* Mutable subclasses must implement ALL of these methods. */ /* Mutable subclasses must implement ALL of these methods. */

View file

@ -116,8 +116,15 @@ static Class NSString_class; /* For speed */
#define GSPLUNI 1 #define GSPLUNI 1
#include "propList.h" #include "propList.h"
#if defined(__WIN32__)
static unichar pathSepChar = (unichar)'\\';
static NSString *pathSepString = @"\\";
static NSString *rootPath = @"C:\\";
#else
static unichar pathSepChar = (unichar)'/'; static unichar pathSepChar = (unichar)'/';
static NSString *pathSepString = @"/"; static NSString *pathSepString = @"/";
static NSString *rootPath = @"/";
#endif
/* /*
* We can't have a 'pathSeps' variable initialized in the +initialize * We can't have a 'pathSeps' variable initialized in the +initialize
@ -135,7 +142,7 @@ pathSeps()
#else #else
pathSeps = [NSCharacterSet characterSetWithCharactersInString: @"/"]; pathSeps = [NSCharacterSet characterSetWithCharactersInString: @"/"];
#endif #endif
[pathSeps retain]; RETAIN(pathSeps);
} }
return pathSeps; return pathSeps;
} }
@ -289,40 +296,39 @@ handle_printf_atsign (FILE *stream,
+ (NSString*) string + (NSString*) string
{ {
return [[[self allocWithZone: NSDefaultMallocZone()] init] autorelease]; return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()] init]);
} }
+ (NSString*) stringWithString: (NSString*)aString + (NSString*) stringWithString: (NSString*)aString
{ {
return [[[self allocWithZone: NSDefaultMallocZone()] return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()]
initWithString: aString] autorelease]; initWithString: aString]);
} }
+ (NSString*) stringWithCharacters: (const unichar*)chars + (NSString*) stringWithCharacters: (const unichar*)chars
length: (unsigned int)length length: (unsigned int)length
{ {
return [[[self allocWithZone: NSDefaultMallocZone()] return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()]
initWithCharacters: chars length: length] initWithCharacters: chars length: length]);
autorelease];
} }
+ (NSString*) stringWithCString: (const char*) byteString + (NSString*) stringWithCString: (const char*) byteString
{ {
return [[[NSString_c_concrete_class allocWithZone: NSDefaultMallocZone()] return AUTORELEASE([[NSString_c_concrete_class allocWithZone:
initWithCString: byteString] autorelease]; NSDefaultMallocZone()] initWithCString: byteString]);
} }
+ (NSString*) stringWithCString: (const char*)byteString + (NSString*) stringWithCString: (const char*)byteString
length: (unsigned int)length length: (unsigned int)length
{ {
return [[[NSString_c_concrete_class allocWithZone: NSDefaultMallocZone()] return AUTORELEASE([[NSString_c_concrete_class allocWithZone:
initWithCString: byteString length: length] autorelease]; NSDefaultMallocZone()] initWithCString: byteString length: length]);
} }
+ (NSString*) stringWithContentsOfFile: (NSString *)path + (NSString*) stringWithContentsOfFile: (NSString *)path
{ {
return [[[self allocWithZone: NSDefaultMallocZone()] return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()]
initWithContentsOfFile: path] autorelease]; initWithContentsOfFile: path]);
} }
+ (NSString*) stringWithFormat: (NSString*)format,... + (NSString*) stringWithFormat: (NSString*)format,...
@ -334,8 +340,8 @@ handle_printf_atsign (FILE *stream,
if (format == nil) if (format == nil)
ret = nil; ret = nil;
else else
ret = [[[self allocWithZone: NSDefaultMallocZone()] ret = AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()]
initWithFormat: format arguments: ap] autorelease]; initWithFormat: format arguments: ap]);
va_end(ap); va_end(ap);
return ret; return ret;
} }
@ -343,8 +349,8 @@ handle_printf_atsign (FILE *stream,
+ (NSString*) stringWithFormat: (NSString*)format + (NSString*) stringWithFormat: (NSString*)format
arguments: (va_list)argList arguments: (va_list)argList
{ {
return [[[self allocWithZone: NSDefaultMallocZone()] return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()]
initWithFormat: format arguments: argList] autorelease]; initWithFormat: format arguments: argList]);
} }
@ -688,7 +694,7 @@ handle_printf_atsign (FILE *stream,
[aString getCharacters: s+len]; [aString getCharacters: s+len];
tmp = [[[self class] allocWithZone: z] initWithCharactersNoCopy: s tmp = [[[self class] allocWithZone: z] initWithCharactersNoCopy: s
length: len+otherLength fromZone: z]; length: len+otherLength fromZone: z];
return [tmp autorelease]; return AUTORELEASE(tmp);
} }
// Dividing Strings into Substrings // Dividing Strings into Substrings
@ -745,7 +751,7 @@ handle_printf_atsign (FILE *stream,
[self getCharacters: buf range: aRange]; [self getCharacters: buf range: aRange];
ret = [[[self class] allocWithZone: NSDefaultMallocZone()] ret = [[[self class] allocWithZone: NSDefaultMallocZone()]
initWithCharactersNoCopy: buf length: aRange.length fromZone: z]; initWithCharactersNoCopy: buf length: aRange.length fromZone: z];
return [ret autorelease]; return AUTORELEASE(ret);
} }
- (NSString*) substringWithRange: (NSRange)aRange - (NSString*) substringWithRange: (NSRange)aRange
@ -1269,36 +1275,36 @@ handle_printf_atsign (FILE *stream,
}; };
found=NO; found=NO;
}; };
return [[[NSString allocWithZone: NSDefaultMallocZone()] return AUTORELEASE([[NSString allocWithZone: NSDefaultMallocZone()]
initWithCharactersNoCopy: s length: len fromZone: z] autorelease]; initWithCharactersNoCopy: s length: len fromZone: z]);
} }
- (NSString*) lowercaseString - (NSString*) lowercaseString
{ {
NSZone *z = fastZone(self); NSZone *z = fastZone(self);
unichar *s; unichar *s;
int count; unsigned count;
int len=[self length]; unsigned len = [self length];
s = NSZoneMalloc(z, sizeof(unichar)*(len+1)); s = NSZoneMalloc(z, sizeof(unichar)*(len+1));
for(count=0;count<len;count++) for (count = 0; count < len; count++)
s[count]=uni_tolower([self characterAtIndex: count]); s[count] = uni_tolower([self characterAtIndex: count]);
return [[[[self class] allocWithZone: NSDefaultMallocZone()] return AUTORELEASE([[[self class] allocWithZone: NSDefaultMallocZone()]
initWithCharactersNoCopy: s initWithCharactersNoCopy: s length: len fromZone: z]);
length: len
fromZone: z] autorelease];
} }
- (NSString*) uppercaseString; - (NSString*) uppercaseString;
{ {
NSZone *z = fastZone(self); NSZone *z = fastZone(self);
unichar *s; unichar *s;
int count; unsigned count;
int len=[self length]; unsigned len = [self length];
s = NSZoneMalloc(z, sizeof(unichar)*(len+1)); s = NSZoneMalloc(z, sizeof(unichar)*(len+1));
for(count=0;count<len;count++) for (count = 0; count < len; count++)
s[count]=uni_toupper([self characterAtIndex: count]); s[count] = uni_toupper([self characterAtIndex: count]);
return [[[[self class] allocWithZone: NSDefaultMallocZone()] return AUTORELEASE([[[self class] allocWithZone: NSDefaultMallocZone()]
initWithCharactersNoCopy: s length: len fromZone: z] autorelease]; initWithCharactersNoCopy: s length: len fromZone: z]);
} }
// Storing the String // Storing the String
@ -1428,7 +1434,7 @@ handle_printf_atsign (FILE *stream,
Until we have it, just make shure that bundle Until we have it, just make shure that bundle
is initialized. is initialized.
*/ */
ourbundle = [NSBundle bundleWithPath: @"/"]; ourbundle = [NSBundle bundleWithPath: rootPath];
ourname = GetEncodingName(encoding); ourname = GetEncodingName(encoding);
return [ourbundle return [ourbundle
@ -1585,7 +1591,7 @@ handle_printf_atsign (FILE *stream,
*outputName = tmp_path; *outputName = tmp_path;
} }
if (outputArray != NULL) if (outputArray != NULL)
*outputArray = [[op copy] autorelease]; *outputArray = AUTORELEASE([op copy]);
return match_count; return match_count;
} }
@ -1617,11 +1623,11 @@ handle_printf_atsign (FILE *stream,
range = [self rangeOfCharacterFromSet: pathSeps() options: NSBackwardsSearch]; range = [self rangeOfCharacterFromSet: pathSeps() options: NSBackwardsSearch];
if (range.length == 0) if (range.length == 0)
substring = [[self copy] autorelease]; substring = AUTORELEASE([self copy]);
else if (range.location == ([self length] - 1)) else if (range.location == ([self length] - 1))
{ {
if (range.location == 0) if (range.location == 0)
substring = [[NSString new] autorelease]; substring = @"";
else else
substring = [[self substringToIndex: range.location] substring = [[self substringToIndex: range.location]
lastPathComponent]; lastPathComponent];
@ -1655,7 +1661,7 @@ handle_printf_atsign (FILE *stream,
} }
if (!substring) if (!substring)
substring = [[NSString new] autorelease]; substring = @"";
return substring; return substring;
} }
@ -1669,7 +1675,7 @@ handle_printf_atsign (FILE *stream,
NSString *newstring; NSString *newstring;
if ([aString length] == 0) if ([aString length] == 0)
return [[self copy] autorelease]; return AUTORELEASE([self copy]);
range = [aString rangeOfCharacterFromSet: pathSeps()]; range = [aString rangeOfCharacterFromSet: pathSeps()];
if (range.length != 0 && range.location == 0) if (range.length != 0 && range.location == 0)
@ -1677,11 +1683,11 @@ handle_printf_atsign (FILE *stream,
format: @"attempt to append illegal path component"]; format: @"attempt to append illegal path component"];
range = [self rangeOfCharacterFromSet: pathSeps() options: NSBackwardsSearch]; range = [self rangeOfCharacterFromSet: pathSeps() options: NSBackwardsSearch];
if ((range.length == 0 || range.location != [self length] - 1) && [self length] > 0) if ((range.length == 0 || range.location != [self length] - 1)
&& [self length] > 0)
newstring = [self stringByAppendingString: pathSepString]; newstring = [self stringByAppendingString: pathSepString];
else else
newstring = self; newstring = self;
return [newstring stringByAppendingString: aString]; return [newstring stringByAppendingString: aString];
} }
@ -1696,7 +1702,7 @@ handle_printf_atsign (FILE *stream,
NSString *newstring; NSString *newstring;
if ([aString length] == 0) if ([aString length] == 0)
return [[self copy] autorelease]; return AUTORELEASE([self copy]);
range = [aString rangeOfString: @"."]; range = [aString rangeOfString: @"."];
if (range.length != 0 && range.location == 0) if (range.length != 0 && range.location == 0)
@ -1723,9 +1729,9 @@ handle_printf_atsign (FILE *stream,
options: NSBackwardsSearch]; options: NSBackwardsSearch];
if (range.length == 0) if (range.length == 0)
substring = [[self copy] autorelease]; substring = AUTORELEASE([self copy]);
else if (range.location == 0) else if (range.location == 0)
substring = [[NSString new] autorelease]; substring = @"";
else if (range.location > 1) else if (range.location > 1)
substring = [self substringToIndex: range.location-1]; substring = [self substringToIndex: range.location-1];
else else
@ -1744,7 +1750,7 @@ handle_printf_atsign (FILE *stream,
if (range.length != 0) if (range.length != 0)
substring = [self substringToIndex: range.location-1]; substring = [self substringToIndex: range.location-1];
else else
substring = [[self copy] autorelease]; substring = AUTORELEASE([self copy]);
return substring; return substring;
} }
@ -1755,9 +1761,9 @@ handle_printf_atsign (FILE *stream,
NSRange first_slash_range; NSRange first_slash_range;
if ([self length] == 0) if ([self length] == 0)
return [[self copy] autorelease]; return AUTORELEASE([self copy]);
if ([self characterAtIndex: 0] != 0x007E) if ([self characterAtIndex: 0] != 0x007E)
return [[self copy] autorelease]; return AUTORELEASE([self copy]);
first_slash_range = [self rangeOfString: pathSepString]; first_slash_range = [self rangeOfString: pathSepString];
@ -1791,7 +1797,7 @@ handle_printf_atsign (FILE *stream,
NSString *homedir = NSHomeDirectory (); NSString *homedir = NSHomeDirectory ();
if (![self hasPrefix: homedir]) if (![self hasPrefix: homedir])
return [[self copy] autorelease]; return AUTORELEASE([self copy]);
return [NSString stringWithFormat: @"~%c%@", (char)pathSepChar, return [NSString stringWithFormat: @"~%c%@", (char)pathSepChar,
[self substringFromIndex: [homedir length] + 1]]; [self substringFromIndex: [homedir length] + 1]];
@ -1834,7 +1840,7 @@ handle_printf_atsign (FILE *stream,
/* /*
* second_half is an absolute path * second_half is an absolute path
*/ */
if ([second_half hasPrefix: @"/"]) if ([second_half hasPrefix: pathSepString])
return [second_half stringByResolvingSymlinksInPath]; return [second_half stringByResolvingSymlinksInPath];
/* /*
@ -1929,13 +1935,21 @@ handle_printf_atsign (FILE *stream,
+ (NSString*) pathWithComponents: (NSArray*)components + (NSString*) pathWithComponents: (NSArray*)components
{ {
NSString *s = [components objectAtIndex: 0]; NSString *s;
int i; unsigned c;
unsigned i;
for (i = 1; i < [components count]; i++) { c = [components count];
s = [s stringByAppendingPathComponent: [components objectAtIndex: i]]; if (c == 0)
return @"";
s = [components objectAtIndex: 0];
if ([s length] == 0 || [s isEqualToString: pathSepString] == YES)
s = rootPath;
for (i = 1; i < c; i++)
{
s = [s stringByAppendingPathComponent: [components objectAtIndex: i]];
} }
return s; return s;
} }
- (BOOL) isAbsolutePath - (BOOL) isAbsolutePath
@ -1944,13 +1958,15 @@ handle_printf_atsign (FILE *stream,
return NO; return NO;
#if defined(__WIN32__) #if defined(__WIN32__)
if ([self indexOfString: @": "] != NSNotFound) if ([self indexOfString: @":"] != NSNotFound)
return YES; return YES;
#else #else
if ([self characterAtIndex: 0] == (unichar)'/') {
return YES; unichar c = [self characterAtIndex: 0];
if ([self characterAtIndex: 0] == (unichar)'~')
return YES; if (c == (unichar)'/' || c == (unichar)'~')
return YES;
}
#endif #endif
return NO; return NO;
} }
@ -1960,7 +1976,7 @@ handle_printf_atsign (FILE *stream,
NSMutableArray *a; NSMutableArray *a;
NSArray *r; NSArray *r;
a = [[self componentsSeparatedByString: @"/"] mutableCopy]; a = [[self componentsSeparatedByString: pathSepString] mutableCopy];
if ([a count] > 0) { if ([a count] > 0) {
int i; int i;
@ -1969,7 +1985,7 @@ handle_printf_atsign (FILE *stream,
* fed into [+pathWithComponents: ] * fed into [+pathWithComponents: ]
*/ */
if ([[a objectAtIndex: 0] length] == 0) { if ([[a objectAtIndex: 0] length] == 0) {
[a replaceObjectAtIndex: 0 withObject: @"/"]; [a replaceObjectAtIndex: 0 withObject: pathSepString];
} }
/* Any empty path components (except a trailing one) must be removed. */ /* Any empty path components (except a trailing one) must be removed. */
for (i = [a count] - 2; i > 0; i--) { for (i = [a count] - 2; i > 0; i--) {
@ -1979,8 +1995,8 @@ handle_printf_atsign (FILE *stream,
} }
} }
r = [a copy]; r = [a copy];
[a release]; RELEASE(a);
return [r autorelease]; return AUTORELEASE(r);
} }
- (NSArray*) stringsByAppendingPaths: (NSArray*)paths - (NSArray*) stringsByAppendingPaths: (NSArray*)paths
@ -2003,8 +2019,8 @@ handle_printf_atsign (FILE *stream,
[a addObject: s]; [a addObject: s];
} }
r = [a copy]; r = [a copy];
[a release]; RELEASE(a);
return [r autorelease]; return AUTORELEASE(r);
} }
+ (NSString*) localizedStringWithFormat: (NSString*) format, ... + (NSString*) localizedStringWithFormat: (NSString*) format, ...
@ -2129,7 +2145,7 @@ handle_printf_atsign (FILE *stream,
return [[[[self class] _concreteClass] allocWithZone: zone] return [[[[self class] _concreteClass] allocWithZone: zone]
initWithString: self]; initWithString: self];
else else
return [self retain]; return RETAIN(self);
} }
- mutableCopyWithZone: (NSZone*)zone - mutableCopyWithZone: (NSZone*)zone
@ -2242,8 +2258,8 @@ handle_printf_atsign (FILE *stream,
+ (NSMutableString*) stringWithCapacity: (unsigned)capacity + (NSMutableString*) stringWithCapacity: (unsigned)capacity
{ {
return [[[self allocWithZone: NSDefaultMallocZone()] return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()]
initWithCapacity: capacity] autorelease]; initWithCapacity: capacity]);
} }
/* Inefficient. */ /* Inefficient. */
@ -2253,21 +2269,20 @@ handle_printf_atsign (FILE *stream,
id n; id n;
n = [[self allocWithZone: NSDefaultMallocZone()] n = [[self allocWithZone: NSDefaultMallocZone()]
initWithCharacters: characters length: length]; initWithCharacters: characters length: length];
return [n autorelease]; return AUTORELEASE(n);
} }
+ (NSString*) stringWithCString: (const char*)byteString + (NSString*) stringWithCString: (const char*)byteString
{ {
return [[[NSMutableString_c_concrete_class allocWithZone: return AUTORELEASE([[NSMutableString_c_concrete_class allocWithZone:
NSDefaultMallocZone()] initWithCString: byteString] autorelease]; NSDefaultMallocZone()] initWithCString: byteString]);
} }
+ (NSString*) stringWithCString: (const char*)byteString + (NSString*) stringWithCString: (const char*)byteString
length: (unsigned int)length length: (unsigned int)length
{ {
return [[[NSMutableString_c_concrete_class allocWithZone: return AUTORELEASE([[NSMutableString_c_concrete_class allocWithZone:
NSDefaultMallocZone()] initWithCString: byteString length: length] NSDefaultMallocZone()] initWithCString: byteString length: length]);
autorelease];
} }
/* xxx Change this when we have non-CString classes */ /* xxx Change this when we have non-CString classes */
@ -2309,7 +2324,7 @@ handle_printf_atsign (FILE *stream,
initWithFormat: format arguments: ap]; initWithFormat: format arguments: ap];
va_end(ap); va_end(ap);
[self appendString: tmp]; [self appendString: tmp];
[tmp release]; RELEASE(tmp);
} }
- (void) deleteCharactersInRange: (NSRange)range - (void) deleteCharactersInRange: (NSRange)range
@ -2338,182 +2353,192 @@ handle_printf_atsign (FILE *stream,
@end @end
#ifndef NO_GNUSTEP #ifndef NO_GNUSTEP
//==============================================================================
@implementation NSString (GSTrimming) @implementation NSString (GSTrimming)
//------------------------------------------------------------------------------ - (NSString*) stringByTrimmingLeadWhiteSpaces
-(NSString*)stringByTrimmingLeadWhiteSpaces
{ {
NSCharacterSet *nonSPSet= [[NSCharacterSet whitespaceAndNewlineCharacterSet] invertedSet]; NSCharacterSet *nonSPSet;
NSRange nonSPCharRange = [self rangeOfCharacterFromSet: nonSPSet]; NSRange nonSPCharRange;
nonSPSet = [[NSCharacterSet whitespaceAndNewlineCharacterSet] invertedSet];
nonSPCharRange = [self rangeOfCharacterFromSet: nonSPSet];
if (nonSPCharRange.length>0) if (nonSPCharRange.length > 0)
return [self substringFromIndex: nonSPCharRange.location]; return [self substringFromIndex: nonSPCharRange.location];
else else
return [NSString string]; return @"";
}; }
//------------------------------------------------------------------------------ - (NSString*) stringByTrimmingTailWhiteSpaces
-(NSString*)stringByTrimmingTailWhiteSpaces
{ {
NSCharacterSet *nonSPSet= [[NSCharacterSet whitespaceAndNewlineCharacterSet] invertedSet]; NSCharacterSet *nonSPSet;
NSRange nonSPCharRange = [self rangeOfCharacterFromSet: nonSPSet NSRange nonSPCharRange;
options: NSBackwardsSearch];
if (nonSPCharRange.length>0)
return [self substringToIndex: nonSPCharRange.location+1];
else
return [NSString string];
};
//------------------------------------------------------------------------------ nonSPSet= [[NSCharacterSet whitespaceAndNewlineCharacterSet] invertedSet];
-(NSString*)stringByTrimmingWhiteSpaces nonSPCharRange = [self rangeOfCharacterFromSet: nonSPSet
options: NSBackwardsSearch];
if (nonSPCharRange.length > 0)
return [self substringToIndex: nonSPCharRange.location+1];
else
return @"";
}
- (NSString*) stringByTrimmingWhiteSpaces
{ {
return [[self stringByTrimmingLeadWhiteSpaces] return [[self stringByTrimmingLeadWhiteSpaces]
stringByTrimmingTailWhiteSpaces]; stringByTrimmingTailWhiteSpaces];
};; }
//------------------------------------------------------------------------------ - (NSString*) stringByTrimmingLeadSpaces
-(NSString*)stringByTrimmingLeadSpaces
{ {
NSMutableString* tmp = [[self mutableCopy] autorelease]; NSMutableString *tmp = [self mutableCopy];
NSString *str;
[tmp trimLeadSpaces]; [tmp trimLeadSpaces];
// Convert to immuable str = AUTORELEASE([tmp copy]);
return [[tmp copy] autorelease]; RELEASE(tmp);
}; return str;
}
//------------------------------------------------------------------------------ - (NSString*) stringByTrimmingTailSpaces
-(NSString*)stringByTrimmingTailSpaces
{ {
NSMutableString* tmp= [[self mutableCopy] autorelease]; NSMutableString *tmp = [self mutableCopy];
NSString *str;
[tmp trimTailSpaces]; [tmp trimTailSpaces];
// Convert to immuable str = AUTORELEASE([tmp copy]);
return [[tmp copy] autorelease]; RELEASE(tmp);
}; return str;
}
//------------------------------------------------------------------------------ - (NSString*) stringByTrimmingSpaces
-(NSString*) stringByTrimmingSpaces
{ {
NSMutableString* tmp=[[self mutableCopy] autorelease]; NSMutableString *tmp = [self mutableCopy];
NSString *str;
[tmp trimLeadSpaces]; [tmp trimLeadSpaces];
[tmp trimTailSpaces]; [tmp trimTailSpaces];
// Convert to immuable str = AUTORELEASE([tmp copy]);
return [[tmp copy] autorelease]; RELEASE(tmp);
}; return str;
}
@end @end
//==============================================================================
@implementation NSMutableString (GSTrimming) @implementation NSMutableString (GSTrimming)
//------------------------------------------------------------------------------ - (void) trimLeadSpaces
-(void)trimLeadSpaces
{ {
int location = 0; unsigned location = 0;
int length = [self length]; unsigned length = [self length];
while (location<length && isspace([self characterAtIndex: location]))
location++;
if (location>0)
[self deleteCharactersInRange: NSMakeRange(0,location)];
};
//------------------------------------------------------------------------------ while (location < length && isspace([self characterAtIndex: location]))
-(void)trimTailSpaces location++;
{
int length = [self length];
int location = length-1;
while (location>=0 && isspace([self characterAtIndex: location])) if (location > 0)
location--; [self deleteCharactersInRange: NSMakeRange(0,location)];
if (location < length-1)
[self deleteCharactersInRange: NSMakeRange((location == 0) ? 0 : location + 1,
length - ((location == 0) ? 0 : location + 1))];
} }
//------------------------------------------------------------------------------ - (void) trimTailSpaces
-(void)trimSpaces {
unsigned length = [self length];
if (length)
{
unsigned location = length;
while (location > 0)
if (!isspace([self characterAtIndex: --location]))
break;
if (location < length-1)
[self deleteCharactersInRange: NSMakeRange((location == 0) ? 0
: location + 1, length - ((location == 0) ? 0 : location + 1))];
}
}
- (void) trimSpaces
{ {
[self trimLeadSpaces]; [self trimLeadSpaces];
[self trimTailSpaces]; [self trimTailSpaces];
}; }
@end @end
//==============================================================================
@implementation NSString (GSString) @implementation NSString (GSString)
//------------------------------------------------------------------------------ - (NSString*) stringWithoutSuffix: (NSString*)_suffix
-(NSString*)stringWithoutSuffix: (NSString*)_suffix
{ {
NSCAssert2([self hasSuffix: _suffix],@"'%@' has not the suffix '%@'",self,_suffix); NSCAssert2([self hasSuffix: _suffix],
@"'%@' has not the suffix '%@'",self,_suffix);
return [self substringToIndex: ([self length]-[_suffix length])]; return [self substringToIndex: ([self length]-[_suffix length])];
}; }
//------------------------------------------------------------------------------ - (NSString*) stringWithoutPrefix: (NSString*)_prefix
-(NSString*)stringWithoutPrefix: (NSString*)_prefix
{ {
NSCAssert2([self hasPrefix: _prefix],@"'%@' has not the prefix '%@'",self,_prefix); NSCAssert2([self hasPrefix: _prefix],
@"'%@' has not the prefix '%@'",self,_prefix);
return [self substringFromIndex: [_prefix length]]; return [self substringFromIndex: [_prefix length]];
}; }
//------------------------------------------------------------------------------ - (NSString*) stringByReplacingString: (NSString*)_replace
-(NSString*)stringByReplacingString: (NSString*)_replace withString: (NSString*)_by
withString: (NSString*)_by
{ {
NSRange range=[self rangeOfString: _replace]; NSRange range = [self rangeOfString: _replace];
if (range.length>0)
{ if (range.length > 0)
NSMutableString* tmp= [[self mutableCopy] autorelease]; {
[tmp replaceString: _replace NSMutableString *tmp = [self mutableCopy];
withString: _by]; NSString *str;
// Convert to immuable
return [[tmp copy] autorelease]; [tmp replaceString: _replace withString: _by];
} str = AUTORELEASE([tmp copy]);
RELEASE(tmp);
return str;
}
else else
return self; return self;
}; }
@end @end
//==============================================================================
@implementation NSMutableString (GSString) @implementation NSMutableString (GSString)
//------------------------------------------------------------------------------ - (void) removeSuffix: (NSString*)_suffix
-(void)removeSuffix: (NSString*)_suffix
{ {
NSCAssert2([self hasSuffix: _suffix],@"'%@' has not the suffix '%@'",self,_suffix); NSCAssert2([self hasSuffix: _suffix],
[self deleteCharactersInRange: NSMakeRange([self length]-[_suffix length],[_suffix length])]; @"'%@' has not the suffix '%@'",self,_suffix);
}; [self deleteCharactersInRange:
NSMakeRange([self length]-[_suffix length],[_suffix length])];
}
//------------------------------------------------------------------------------ - (void) removePrefix: (NSString*)_prefix;
-(void)removePrefix: (NSString*)_prefix;
{ {
NSCAssert2([self hasPrefix: _prefix],@"'%@' has not the prefix '%@'",self,_prefix); NSCAssert2([self hasPrefix: _prefix],
@"'%@' has not the prefix '%@'",self,_prefix);
[self deleteCharactersInRange: NSMakeRange(0,[_prefix length])]; [self deleteCharactersInRange: NSMakeRange(0,[_prefix length])];
}; }
//------------------------------------------------------------------------------ - (void) replaceString: (NSString*)_replace
-(void)replaceString: (NSString*)_replace withString: (NSString*)_by
withString: (NSString*)_by
{ {
NSRange range=[self rangeOfString: _replace]; NSRange range=[self rangeOfString: _replace];
if (range.length>0) if (range.length>0)
{
unsigned byLen=[_by length];
do
{ {
int byLen=[_by length]; [self replaceCharactersInRange: range
do withString: _by];
{ range.location+=byLen;
[self replaceCharactersInRange: range range.length=[self length]-range.location;
withString: _by]; range=[self rangeOfString: _replace
range.location+=byLen; options: 0
range.length=[self length]-range.location; range: range];
range=[self rangeOfString: _replace }
options: 0 while (range.length>0);
range: range]; }
} }
while (range.length>0);
};
};
@end @end
#endif /* NO_GNUSTEP */ #endif /* NO_GNUSTEP */