o rewritten -deleteLastElementIDComponent and

-incrementLastElementIDComponent for speed
and memory usage improvement


git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gsweb/trunk@19953 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
mguesdon 2004-09-01 14:27:26 +00:00
parent fe146dbe3c
commit a8f85a3ec1

View file

@ -386,10 +386,14 @@ static NSCharacterSet* nonNumericSet=nil;
//-------------------------------------------------------------------- //--------------------------------------------------------------------
-(void)deleteLastElementIDComponent -(void)deleteLastElementIDComponent
{ {
NSArray* ids=nil; // NSArray* ids=nil;
int length=0;
LOGObjectFnStart(); LOGObjectFnStart();
if ([self length]>0)
length=[self length];
if (length>0)
{ {
/*
ids=[self componentsSeparatedByString:@"."]; ids=[self componentsSeparatedByString:@"."];
NSAssert([ids count]>0,@"PROBLEM"); NSAssert([ids count]>0,@"PROBLEM");
if ([ids count]==1) if ([ids count]==1)
@ -399,6 +403,16 @@ static NSCharacterSet* nonNumericSet=nil;
[self setString:[[ids subarrayWithRange:NSMakeRange(0,[ids count]-1)] [self setString:[[ids subarrayWithRange:NSMakeRange(0,[ids count]-1)]
componentsJoinedByString:@"."]]; componentsJoinedByString:@"."]];
}; };
*/
NSRange dotRange=[self rangeOfString:@"."
options:NSBackwardsSearch];
if (dotRange.length>0)
{
[self deleteCharactersInRange:
NSMakeRange(dotRange.location,length-dotRange.location)];
}
else
[self setString:@""];
} }
else else
{ {
@ -410,6 +424,7 @@ static NSCharacterSet* nonNumericSet=nil;
//-------------------------------------------------------------------- //--------------------------------------------------------------------
-(void)incrementLastElementIDComponent -(void)incrementLastElementIDComponent
{ {
/*
NSArray* ids=nil; NSArray* ids=nil;
int idsCount=0; int idsCount=0;
LOGObjectFnStart(); LOGObjectFnStart();
@ -447,7 +462,7 @@ static NSCharacterSet* nonNumericSet=nil;
else else
{ {
// it's a number // it's a number
lastPart=[NSString stringWithFormat:@"%d",([lastPart intValue]+1)]; lastPart=GSWIntToNSString([lastPart intValue]+1);
}; };
if (idsCount>1) if (idsCount>1)
[self setString:[[[ids subarrayWithRange:NSMakeRange(0,idsCount-1)] [self setString:[[[ids subarrayWithRange:NSMakeRange(0,idsCount-1)]
@ -458,6 +473,71 @@ static NSCharacterSet* nonNumericSet=nil;
}; };
}; };
LOGObjectFnStop(); LOGObjectFnStop();
*/
int length=0;
LOGObjectFnStart();
length=[self length];
if (length>0)
{
NSString* lastPart=nil;
NSRange dotRange=[self rangeOfString:@"."
options:NSBackwardsSearch];
if (dotRange.length>0)
{
if (dotRange.location+1<length)
lastPart=[self substringFromIndex:dotRange.location+1];
else
lastPart=@"";
}
else
lastPart=self;
if ([lastPart length]==0) // not possible ?
{
// add a '1' at the end
[self appendString:@"1"];
}
else
{
// find last 'number'
// search for last non '0'-'9' char
NSRange range=[lastPart rangeOfCharacterFromSet:nonNumericSet
options:NSBackwardsSearch];
if (range.length>0) // a string and (may be) a number
{
if ((range.location+range.length)==[lastPart length]) // no number
{
lastPart=[lastPart stringByAppendingString:@"1"]; // add '1' at the end
}
else
{
NSString* numberString=[lastPart substringFromIndex:range.location+range.length];
NSString* nonNumberString=[lastPart substringToIndex:range.location+range.length];
lastPart=[NSString stringWithFormat:@"%@%d",
nonNumberString,
[numberString intValue]+1];
};
}
else
{
// it's a number
lastPart=GSWIntToNSString([lastPart intValue]+1);
};
if (dotRange.length>0)
{
//Remove after last dot
[self deleteCharactersInRange:
NSMakeRange(dotRange.location+1,length-(dotRange.location+1))];
//Append lastPart
[self appendString:lastPart];
}
else
{
// Set last Part
[self setString:lastPart];
};
};
};
LOGObjectFnStop();
}; };
//-------------------------------------------------------------------- //--------------------------------------------------------------------
@ -477,7 +557,10 @@ static NSCharacterSet* nonNumericSet=nil;
NSRange range; NSRange range;
LOGObjectFnStart(); LOGObjectFnStart();
if (self && [self length]>0) if (self && [self length]>0)
[self appendFormat:@".%@",element]; {
[self appendString:@"."];
[self appendString:element];
}
else else
[self setString:element]; [self setString:element];
range=[self rangeOfCharacterFromSet:nonNumericSet range=[self rangeOfCharacterFromSet:nonNumericSet
@ -504,10 +587,26 @@ static NSCharacterSet* nonNumericSet=nil;
#ifndef NDEBBUG #ifndef NDEBBUG
-(int)elementsNb -(int)elementsNb
{ {
if ([self length]==0) int length=[self length];
if (length==0)
return 0; return 0;
else else
return [[self componentsSeparatedByString:@"."] count]; {
int count=1;
NSRange dotRange=[self rangeOfString:@"."];
while(dotRange.length>0)
{
count++;
dotRange.location++;
dotRange.length=length-dotRange.location;
if (dotRange.location>=length)
break;
dotRange=[self rangeOfString:@"."
options:0
range:dotRange];
};
return count;
}
}; };
#endif #endif