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>
* Source/NSUserDefaults.m: force defaults data to be read/write

View file

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

View file

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

View file

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

View file

@ -3604,72 +3604,81 @@ handle_printf_atsign (FILE *stream,
@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
{
NSMutableString *tmp = [self mutableCopy];
NSString *str;
unsigned length = [self length];
[tmp trimLeadSpaces];
str = AUTORELEASE([tmp copy]);
RELEASE(tmp);
return str;
if (length > 0)
{
unsigned location = 0;
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
{
NSMutableString *tmp = [self mutableCopy];
NSString *str;
unsigned length = [self length];
[tmp trimTailSpaces];
str = AUTORELEASE([tmp copy]);
RELEASE(tmp);
return str;
if (length > 0)
{
unsigned location = length;
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
{
NSMutableString *tmp = [self mutableCopy];
NSString *str;
unsigned length = [self length];
[tmp trimLeadSpaces];
[tmp trimTailSpaces];
str = AUTORELEASE([tmp copy]);
RELEASE(tmp);
return str;
if (length > 0)
{
unsigned start = 0;
unsigned end = length;
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
@ -3678,8 +3687,11 @@ handle_printf_atsign (FILE *stream,
- (void) trimLeadSpaces
{
unsigned location = 0;
unsigned length = [self length];
if (length > 0)
{
unsigned location = 0;
unichar (*caiImp)(NSString*, SEL, unsigned);
caiImp = (unichar (*)())[self methodForSelector: caiSel];
@ -3692,6 +3704,7 @@ handle_printf_atsign (FILE *stream,
[self deleteCharactersInRange: NSMakeRange(0,location)];
}
}
}
- (void) trimTailSpaces
{