Whitespace trimming optimisations.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@11332 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
CaS 2001-11-07 16:24:55 +00:00
parent a77ecf9d81
commit d9f3e799b7
5 changed files with 168 additions and 127 deletions

View file

@ -1,3 +1,11 @@
2001-11-07 Richard Frith-Macdonald <rfm@gnu.org>
* Headers/Foundation/NSString.h: removed stringByTrimming...Whitespace
methods as they just duplicate the stringByTrimming...Spaces
* Source/NSString.m: ditto
Optimised the space trimming methods - avoid using character sets and
creating unnecessary intermediary objects.
2001-11-07 Richard Frith-Macdonald <rfm@gnu.org> 2001-11-07 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSUserDefaults.m: force defaults data to be read/write * Source/NSUserDefaults.m: force defaults data to be read/write

View file

@ -369,10 +369,6 @@ enum {
@end @end
@interface NSString(GSTrimming) @interface NSString(GSTrimming)
- (NSString*) stringByTrimmingLeadWhiteSpaces;
- (NSString*) stringByTrimmingTailWhiteSpaces;
- (NSString*) stringByTrimmingWhiteSpaces;
- (NSString*) stringByTrimmingLeadSpaces; - (NSString*) stringByTrimmingLeadSpaces;
- (NSString*) stringByTrimmingTailSpaces; - (NSString*) stringByTrimmingTailSpaces;
- (NSString*) stringByTrimmingSpaces; - (NSString*) stringByTrimmingSpaces;

View file

@ -78,7 +78,7 @@
@implementation NSMutableBitmapCharSet @implementation NSMutableBitmapCharSet
- init - (id) init
{ {
return [self initWithBitmap: NULL]; return [self initWithBitmap: NULL];
} }
@ -129,8 +129,10 @@
} }
for (i = aRange.location; i < NSMaxRange(aRange); i++) for (i = aRange.location; i < NSMaxRange(aRange); i++)
{
SETBIT(_data[i/8], i % 8); SETBIT(_data[i/8], i % 8);
} }
}
- (void) addCharactersInString: (NSString*)aString - (void) addCharactersInString: (NSString*)aString
{ {
@ -168,8 +170,10 @@
other_bytes = [[otherSet bitmapRepresentation] bytes]; other_bytes = [[otherSet bitmapRepresentation] bytes];
for (i = 0; i < BITMAP_SIZE; i++) for (i = 0; i < BITMAP_SIZE; i++)
{
_data[i] = (_data[i] | other_bytes[i]); _data[i] = (_data[i] | other_bytes[i]);
} }
}
- (void) formIntersectionWithCharacterSet: (NSCharacterSet *)otherSet - (void) formIntersectionWithCharacterSet: (NSCharacterSet *)otherSet
{ {
@ -178,8 +182,10 @@
other_bytes = [[otherSet bitmapRepresentation] bytes]; other_bytes = [[otherSet bitmapRepresentation] bytes];
for (i = 0; i < BITMAP_SIZE; i++) for (i = 0; i < BITMAP_SIZE; i++)
{
_data[i] = (_data[i] & other_bytes[i]); _data[i] = (_data[i] & other_bytes[i]);
} }
}
- (void) removeCharactersInRange: (NSRange)aRange - (void) removeCharactersInRange: (NSRange)aRange
{ {
@ -193,8 +199,10 @@
} }
for (i = aRange.location; i < NSMaxRange(aRange); i++) for (i = aRange.location; i < NSMaxRange(aRange); i++)
{
CLRBIT(_data[i/8], i % 8); CLRBIT(_data[i/8], i % 8);
} }
}
- (void) removeCharactersInString: (NSString*)aString - (void) removeCharactersInString: (NSString*)aString
{ {
@ -231,7 +239,9 @@
unsigned i; unsigned i;
for (i = 0; i < BITMAP_SIZE; i++) for (i = 0; i < BITMAP_SIZE; i++)
{
_data[i] = ~_data[i]; _data[i] = ~_data[i];
} }
}
@end @end

View file

@ -46,15 +46,18 @@ static NSLock* cache_lock = nil;
if (one_time == NO) if (one_time == NO)
{ {
int i; unsigned i;
for (i = 0; i < MAX_STANDARD_SETS; i++) for (i = 0; i < MAX_STANDARD_SETS; i++)
{
cache_set[i] = 0; cache_set[i] = 0;
}
one_time = YES; one_time = YES;
} }
} }
/* Provide a default object for allocation */ /* Provide a default object for allocation */
+ allocWithZone: (NSZone *)zone + (id) allocWithZone: (NSZone*)zone
{ {
return NSAllocateObject([NSBitmapCharSet self], 0, zone); return NSAllocateObject([NSBitmapCharSet self], 0, zone);
} }
@ -109,9 +112,11 @@ static NSLock* cache_lock = nil;
/* NOT REACHED */ /* NOT REACHED */
} }
else else
{
/* Else cache the set */ /* Else cache the set */
cache_set[number] = RETAIN(set); cache_set[number] = RETAIN(set);
}
NS_HANDLER NS_HANDLER
[cache_lock unlock]; [cache_lock unlock];
[localException raise]; [localException raise];
@ -200,8 +205,9 @@ static NSLock* cache_lock = nil;
+ (NSCharacterSet*) characterSetWithCharactersInString: (NSString*)aString + (NSCharacterSet*) characterSetWithCharactersInString: (NSString*)aString
{ {
int i, length; unsigned i;
char *bytes; unsigned length;
unsigned char *bytes;
NSMutableData *bitmap = [NSMutableData dataWithLength: BITMAP_SIZE]; NSMutableData *bitmap = [NSMutableData dataWithLength: BITMAP_SIZE];
if (!aString) if (!aString)
@ -216,6 +222,7 @@ static NSLock* cache_lock = nil;
for (i = 0; i < length; i++) for (i = 0; i < length; i++)
{ {
unichar letter = [aString characterAtIndex: i]; unichar letter = [aString characterAtIndex: i];
SETBIT(bytes[letter/8], letter % 8); SETBIT(bytes[letter/8], letter % 8);
} }
@ -224,8 +231,8 @@ static NSLock* cache_lock = nil;
+ (NSCharacterSet*)characterSetWithRange: (NSRange)aRange + (NSCharacterSet*)characterSetWithRange: (NSRange)aRange
{ {
int i; unsigned i;
char *bytes; unsigned char *bytes;
NSMutableData *bitmap = [NSMutableData dataWithLength: BITMAP_SIZE]; NSMutableData *bitmap = [NSMutableData dataWithLength: BITMAP_SIZE];
if (NSMaxRange(aRange) > UNICODE_SIZE) if (NSMaxRange(aRange) > UNICODE_SIZE)
@ -235,10 +242,11 @@ static NSLock* cache_lock = nil;
/* NOT REACHED */ /* NOT REACHED */
} }
bytes = (char *)[bitmap mutableBytes]; bytes = (unsigned char*)[bitmap mutableBytes];
for (i = aRange.location; i < NSMaxRange(aRange); i++) for (i = aRange.location; i < NSMaxRange(aRange); i++)
{
SETBIT(bytes[i/8], i % 8); SETBIT(bytes[i/8], i % 8);
}
return [self characterSetWithBitmapRepresentation: bitmap]; return [self characterSetWithBitmapRepresentation: bitmap];
} }
@ -282,12 +290,16 @@ static NSLock* cache_lock = nil;
return YES; return YES;
if ([anObject isKindOfClass: [NSCharacterSet class]]) if ([anObject isKindOfClass: [NSCharacterSet class]])
{ {
int i; unsigned i;
for (i = 0; i <= 0xffff; i++) for (i = 0; i <= 0xffff; i++)
if ([self characterIsMember: (unichar)i] != {
[anObject characterIsMember: (unichar)i]) if ([self characterIsMember: (unichar)i]
!= [anObject characterIsMember: (unichar)i])
{
return NO; return NO;
}
}
return YES; return YES;
} }
return NO; return NO;
@ -295,16 +307,18 @@ static NSLock* cache_lock = nil;
- (NSCharacterSet*) invertedSet - (NSCharacterSet*) invertedSet
{ {
int i, length; unsigned i;
char *bytes; unsigned length;
unsigned char *bytes;
NSMutableData *bitmap; NSMutableData *bitmap;
bitmap = AUTORELEASE([[self bitmapRepresentation] mutableCopy]); 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++)
{
bytes[i] = ~bytes[i]; bytes[i] = ~bytes[i];
}
return [[self class] characterSetWithBitmapRepresentation: bitmap]; return [[self class] characterSetWithBitmapRepresentation: bitmap];
} }
@ -330,7 +344,7 @@ static NSLock* cache_lock = nil;
@implementation NSMutableCharacterSet @implementation NSMutableCharacterSet
/* Provide a default object for allocation */ /* Provide a default object for allocation */
+ allocWithZone: (NSZone *)zone + (id) allocWithZone: (NSZone*)zone
{ {
return NSAllocateObject([NSMutableBitmapCharSet self], 0, zone); return NSAllocateObject([NSMutableBitmapCharSet self], 0, zone);
} }

View file

@ -3604,72 +3604,81 @@ handle_printf_atsign (FILE *stream,
@implementation NSString (GSTrimming) @implementation NSString (GSTrimming)
- (NSString*) stringByTrimmingLeadWhiteSpaces
{
NSCharacterSet *nonSPSet;
NSRange nonSPCharRange;
nonSPSet = [[NSCharacterSet whitespaceAndNewlineCharacterSet] invertedSet];
nonSPCharRange = [self rangeOfCharacterFromSet: nonSPSet];
if (nonSPCharRange.length > 0)
return [self substringFromIndex: nonSPCharRange.location];
else
return @"";
}
- (NSString*) stringByTrimmingTailWhiteSpaces
{
NSCharacterSet *nonSPSet;
NSRange nonSPCharRange;
nonSPSet= [[NSCharacterSet whitespaceAndNewlineCharacterSet] invertedSet];
nonSPCharRange = [self rangeOfCharacterFromSet: nonSPSet
options: NSBackwardsSearch];
if (nonSPCharRange.length > 0)
return [self substringToIndex: nonSPCharRange.location+1];
else
return @"";
}
- (NSString*) stringByTrimmingWhiteSpaces
{
return [[self stringByTrimmingLeadWhiteSpaces]
stringByTrimmingTailWhiteSpaces];
}
- (NSString*) stringByTrimmingLeadSpaces - (NSString*) stringByTrimmingLeadSpaces
{ {
NSMutableString *tmp = [self mutableCopy]; unsigned length = [self length];
NSString *str;
[tmp trimLeadSpaces]; if (length > 0)
str = AUTORELEASE([tmp copy]); {
RELEASE(tmp); unsigned location = 0;
return str; unichar (*caiImp)(NSString*, SEL, unsigned);
caiImp = (unichar (*)())[self methodForSelector: caiSel];
while (location < length && isspace((*caiImp)(self, caiSel, location)))
{
location++;
}
if (location > 0)
{
return [self substringFromIndex: location];
}
}
return self;
} }
- (NSString*) stringByTrimmingTailSpaces - (NSString*) stringByTrimmingTailSpaces
{ {
NSMutableString *tmp = [self mutableCopy]; unsigned length = [self length];
NSString *str;
[tmp trimTailSpaces]; if (length > 0)
str = AUTORELEASE([tmp copy]); {
RELEASE(tmp); unsigned location = length;
return str; unichar (*caiImp)(NSString*, SEL, unsigned);
caiImp = (unichar (*)())[self methodForSelector: caiSel];
while (location > 0)
{
if (!isspace((*caiImp)(self, caiSel, --location)))
{
break;
}
}
if (location < length-1)
{
return [self substringToIndex: location+1];
}
}
return self;
} }
- (NSString*) stringByTrimmingSpaces - (NSString*) stringByTrimmingSpaces
{ {
NSMutableString *tmp = [self mutableCopy]; unsigned length = [self length];
NSString *str;
[tmp trimLeadSpaces]; if (length > 0)
[tmp trimTailSpaces]; {
str = AUTORELEASE([tmp copy]); unsigned start = 0;
RELEASE(tmp); unsigned end = length;
return str; unichar (*caiImp)(NSString*, SEL, unsigned);
caiImp = (unichar (*)())[self methodForSelector: caiSel];
while (start < length && isspace((*caiImp)(self, caiSel, start)))
{
start++;
}
while (end > start)
{
if (!isspace((*caiImp)(self, caiSel, --end)))
{
break;
}
}
if (start > 0 || end < length-1)
{
return [self substringFromRange: NSMakeRange(start, end + 1 - start)];
}
}
return self;
} }
@end @end
@ -3678,8 +3687,11 @@ handle_printf_atsign (FILE *stream,
- (void) trimLeadSpaces - (void) trimLeadSpaces
{ {
unsigned location = 0;
unsigned length = [self length]; unsigned length = [self length];
if (length > 0)
{
unsigned location = 0;
unichar (*caiImp)(NSString*, SEL, unsigned); unichar (*caiImp)(NSString*, SEL, unsigned);
caiImp = (unichar (*)())[self methodForSelector: caiSel]; caiImp = (unichar (*)())[self methodForSelector: caiSel];
@ -3692,6 +3704,7 @@ handle_printf_atsign (FILE *stream,
[self deleteCharactersInRange: NSMakeRange(0,location)]; [self deleteCharactersInRange: NSMakeRange(0,location)];
} }
} }
}
- (void) trimTailSpaces - (void) trimTailSpaces
{ {