2002-08-04 18:00:11 +00:00
|
|
|
/** NSString+Trimming.m - <title>GSWeb: Class NSString with Trimming </title>
|
|
|
|
|
|
|
|
Copyright (C) 1999-2002 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
Written by: Manuel Guesdon <mguesdon@orange-concept.com>
|
2000-01-22 12:49:49 +00:00
|
|
|
Date: Jan 1999
|
|
|
|
|
2002-08-04 18:00:11 +00:00
|
|
|
$Revision$
|
|
|
|
$Date$
|
|
|
|
|
2000-01-22 12:49:49 +00:00
|
|
|
This file is part of the GNUstep Web Library.
|
|
|
|
|
2002-08-04 18:00:11 +00:00
|
|
|
<license>
|
2000-01-22 12:49:49 +00:00
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
License along with this library; if not, write to the Free
|
|
|
|
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
2002-08-04 18:00:11 +00:00
|
|
|
</license>
|
|
|
|
**/
|
2000-01-22 12:49:49 +00:00
|
|
|
|
|
|
|
static char rcsId[] = "$Id$";
|
|
|
|
|
2000-10-30 15:36:50 +00:00
|
|
|
#include <GSWeb/GSWeb.h>
|
2000-01-22 12:49:49 +00:00
|
|
|
|
|
|
|
//====================================================================
|
|
|
|
@implementation NSString (SBString)
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
-(BOOL)isANumber
|
|
|
|
{
|
|
|
|
return [self isAFloatNumber];
|
|
|
|
};
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
-(BOOL)isAFloatNumber
|
|
|
|
{
|
|
|
|
//TODOV
|
|
|
|
NSRange nonNumberRange;
|
|
|
|
NSMutableCharacterSet* nonNumberCS=[[NSCharacterSet decimalDigitCharacterSet]
|
2002-08-04 18:00:11 +00:00
|
|
|
mutableCopy];
|
2000-01-22 12:49:49 +00:00
|
|
|
[nonNumberCS addCharactersInString:@".Ee-+"];
|
|
|
|
[nonNumberCS invert];
|
|
|
|
nonNumberRange = [self rangeOfCharacterFromSet:nonNumberCS];
|
|
|
|
return (nonNumberRange.length<=0);
|
|
|
|
};
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
-(BOOL)isAnIntegerNumber
|
|
|
|
{
|
|
|
|
//TODOV
|
|
|
|
NSRange nonNumberRange;
|
|
|
|
NSMutableCharacterSet* nonNumberCS=[[NSCharacterSet decimalDigitCharacterSet]
|
2002-08-04 18:00:11 +00:00
|
|
|
mutableCopy];
|
2000-01-22 12:49:49 +00:00
|
|
|
[nonNumberCS addCharactersInString:@".-+"];
|
|
|
|
[nonNumberCS invert];
|
|
|
|
nonNumberRange = [self rangeOfCharacterFromSet:nonNumberCS];
|
|
|
|
return (nonNumberRange.length<=0);
|
|
|
|
};
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
#ifdef LONG_LONG_MAX
|
2002-08-04 18:00:11 +00:00
|
|
|
-(BOOL)isAnIntegerNumberWithMin:(long long)min
|
|
|
|
max:(long long)max
|
2000-01-22 12:49:49 +00:00
|
|
|
#else
|
2002-08-04 18:00:11 +00:00
|
|
|
-(BOOL)isAnIntegerNumberWithMin:(long)min
|
|
|
|
max:(long)max
|
2000-01-22 12:49:49 +00:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
if ([self isAnIntegerNumber])
|
2002-08-04 18:00:11 +00:00
|
|
|
{
|
|
|
|
//TODO
|
|
|
|
long v=[self longValue];
|
|
|
|
if (v>=min && v<=max)
|
|
|
|
return YES;
|
|
|
|
else
|
|
|
|
return NO;
|
|
|
|
}
|
2000-01-22 12:49:49 +00:00
|
|
|
else
|
2002-08-04 18:00:11 +00:00
|
|
|
return NO;
|
2000-01-22 12:49:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
-(BOOL)isAnUnsignedIntegerNumber
|
|
|
|
{
|
|
|
|
//TODOV
|
|
|
|
NSRange nonNumberRange;
|
|
|
|
NSMutableCharacterSet* nonNumberCS=[[NSCharacterSet decimalDigitCharacterSet]
|
2002-08-04 18:00:11 +00:00
|
|
|
mutableCopy];
|
2000-01-22 12:49:49 +00:00
|
|
|
[nonNumberCS addCharactersInString:@".+"];
|
|
|
|
[nonNumberCS invert];
|
|
|
|
nonNumberRange = [self rangeOfCharacterFromSet:nonNumberCS];
|
|
|
|
return (nonNumberRange.length<=0);
|
|
|
|
};
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
#ifdef LONG_LONG_MAX
|
2002-08-04 18:00:11 +00:00
|
|
|
-(BOOL)isAnUnsignedIntegerNumberWithMax:(unsigned long long)max
|
2000-01-22 12:49:49 +00:00
|
|
|
#else
|
2002-08-04 18:00:11 +00:00
|
|
|
-(BOOL)isAnUnsignedIntegerNumberWithMax:(unsigned long)max
|
2000-01-22 12:49:49 +00:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
if ([self isAnUnsignedIntegerNumber])
|
2002-08-04 18:00:11 +00:00
|
|
|
{
|
|
|
|
//TODO
|
|
|
|
unsigned long v=[self ulongValue];
|
|
|
|
if (v<=max)
|
|
|
|
return YES;
|
|
|
|
else
|
|
|
|
return NO;
|
|
|
|
}
|
2000-01-22 12:49:49 +00:00
|
|
|
else
|
2002-08-04 18:00:11 +00:00
|
|
|
return NO;
|
2000-01-22 12:49:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
-(BOOL)isStartingWithNumber
|
|
|
|
{
|
|
|
|
//TODOV
|
|
|
|
NSRange numberRange;
|
|
|
|
NSMutableCharacterSet* numberCS=[[NSCharacterSet decimalDigitCharacterSet]
|
2002-08-04 18:00:11 +00:00
|
|
|
mutableCopy];
|
2000-01-22 12:49:49 +00:00
|
|
|
[numberCS addCharactersInString:@".-+"];
|
|
|
|
numberRange = [self rangeOfCharacterFromSet:numberCS];
|
|
|
|
return (numberRange.location==0 && numberRange.length>0);
|
|
|
|
};
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
-(long)longValue
|
|
|
|
{
|
|
|
|
return atol([self cString]);
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
-(unsigned long)ulongValue
|
|
|
|
{
|
|
|
|
return strtoul([self cString],NULL,10);
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
-(long long)longLongValue
|
|
|
|
{
|
2002-08-04 18:00:11 +00:00
|
|
|
long long v=0;
|
|
|
|
NSScanner* scanner = [NSScanner scannerWithString:self];
|
|
|
|
[scanner scanLongLong:&v];
|
|
|
|
return v;
|
2000-01-22 12:49:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
//====================================================================
|
|
|
|
@implementation NSString (UniqueIdString)
|
|
|
|
//--------------------------------------------------------------------
|
2002-08-04 18:00:11 +00:00
|
|
|
+(NSString*)stringUniqueIdWithLength:(int)length
|
2000-01-22 12:49:49 +00:00
|
|
|
{
|
|
|
|
int i=0;
|
|
|
|
NSTimeInterval ti=[[NSDate date]timeIntervalSinceReferenceDate];
|
|
|
|
int size=0;
|
|
|
|
NSMutableData* data=nil;
|
|
|
|
void* pData=NULL;
|
|
|
|
NSString* dataHex=nil;
|
2002-08-04 18:00:11 +00:00
|
|
|
int intLength=(length/sizeof(int))-sizeof(ti);
|
2000-01-22 12:49:49 +00:00
|
|
|
if (intLength<0)
|
2002-08-04 18:00:11 +00:00
|
|
|
intLength=0;
|
2000-01-22 12:49:49 +00:00
|
|
|
size=sizeof(ti)+intLength*sizeof(int);
|
|
|
|
data=[NSMutableData dataWithLength:size];
|
|
|
|
pData=[data mutableBytes];
|
|
|
|
dataHex=nil;
|
|
|
|
*((NSTimeInterval*)pData)=ti;//TODO: NSSwapHostLongToBig(ti);
|
|
|
|
pData+=sizeof(ti);
|
|
|
|
for(i=0;i<intLength;i++)
|
2002-08-04 18:00:11 +00:00
|
|
|
{
|
|
|
|
*((int*)pData)=rand(); //TODO: NSSwapHostIntToBig(rand());
|
|
|
|
pData+=sizeof(int);
|
|
|
|
};
|
2000-01-22 12:49:49 +00:00
|
|
|
dataHex=DataToHexString(data);
|
|
|
|
return dataHex;
|
|
|
|
};
|
|
|
|
|
|
|
|
//====================================================================
|
|
|
|
@implementation NSString (stringWithObject)
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
2002-08-04 18:00:11 +00:00
|
|
|
+(NSString*)stringWithObject:(id)object
|
2000-01-22 12:49:49 +00:00
|
|
|
{
|
2002-08-04 18:00:11 +00:00
|
|
|
NSString* string=nil;
|
|
|
|
if (object)
|
|
|
|
{
|
|
|
|
if ([object isKindOfClass:[NSString class]])
|
|
|
|
string=[[object copy] autorelease];
|
2001-10-27 10:27:33 +00:00
|
|
|
#ifdef GDL2
|
2002-08-04 18:00:11 +00:00
|
|
|
else if ([object isKindOfClass:[EONull class]])
|
|
|
|
string=@"";
|
2001-10-27 10:27:33 +00:00
|
|
|
#else
|
2002-08-04 18:00:11 +00:00
|
|
|
else if ([object isKindOfClass:[NSNull class]])
|
|
|
|
string=@"";
|
2001-10-27 10:27:33 +00:00
|
|
|
#endif
|
2002-08-04 18:00:11 +00:00
|
|
|
else if ([object respondsToSelector:@selector(stringValue)])
|
|
|
|
string=[object stringValue];
|
|
|
|
else if ([object respondsToSelector:@selector(description)])
|
|
|
|
string=[object description];
|
|
|
|
else
|
|
|
|
string=object;
|
|
|
|
};
|
|
|
|
return string;
|
2000-01-22 12:49:49 +00:00
|
|
|
};
|
|
|
|
@end
|
|
|
|
|
|
|
|
//====================================================================
|
|
|
|
@implementation NSString (uniqueFileName)
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
2002-08-04 18:00:11 +00:00
|
|
|
+(NSString*)stringForUniqueFilenameInDirectory:(NSString*)directory
|
|
|
|
withPrefix:(NSString*)prefix
|
|
|
|
withSuffix:(NSString*)suffix
|
2000-01-22 12:49:49 +00:00
|
|
|
{
|
2002-08-04 18:00:11 +00:00
|
|
|
NSString* filename=nil;
|
|
|
|
NSFileManager* fileManager=nil;
|
|
|
|
NSArray* directoryContents=nil;
|
2000-01-22 12:49:49 +00:00
|
|
|
LOGObjectFnStart();
|
2002-08-04 18:00:11 +00:00
|
|
|
fileManager=[NSFileManager defaultManager];
|
|
|
|
directoryContents=[fileManager directoryContentsAtPath:directory];
|
|
|
|
if (!directoryContents)
|
|
|
|
{
|
|
|
|
//ERROR
|
|
|
|
}
|
2000-01-22 12:49:49 +00:00
|
|
|
else
|
2002-08-04 18:00:11 +00:00
|
|
|
{
|
|
|
|
int attempts=16;
|
|
|
|
while(attempts-->0 && !filename)
|
|
|
|
{
|
|
|
|
NSString* unique=[NSString stringUniqueIdWithLength:16];
|
|
|
|
filename=[NSString stringWithFormat:@"%@_%@_%@",prefix,unique,suffix];
|
|
|
|
if ([directoryContents containsObject:filename])
|
|
|
|
filename=nil;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
if (filename)
|
|
|
|
filename=[directory stringByAppendingPathComponent:filename];
|
2000-01-22 12:49:49 +00:00
|
|
|
LOGObjectFnStop();
|
2002-08-04 18:00:11 +00:00
|
|
|
return filename;
|
2000-01-22 12:49:49 +00:00
|
|
|
};
|
|
|
|
@end
|
|
|
|
|
|
|
|
//====================================================================
|
|
|
|
@implementation NSString (Qutotes)
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
2002-08-04 18:00:11 +00:00
|
|
|
-(BOOL)hasPrefix:(NSString*)prefix
|
|
|
|
andSuffix:(NSString*)suffix
|
2000-01-22 12:49:49 +00:00
|
|
|
{
|
2002-08-04 18:00:11 +00:00
|
|
|
return [self hasPrefix:prefix] && [self hasSuffix:suffix];
|
2000-01-22 12:49:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
2002-08-04 18:00:11 +00:00
|
|
|
-(NSString*)stringWithoutPrefix:(NSString*)prefix
|
|
|
|
andSuffix:(NSString*)suffix
|
2000-01-22 12:49:49 +00:00
|
|
|
{
|
2002-08-04 18:00:11 +00:00
|
|
|
return [[self stringWithoutPrefix:prefix]stringWithoutSuffix:suffix];
|
2000-01-22 12:49:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
2002-08-04 18:00:11 +00:00
|
|
|
-(BOOL)isQuotedWith:(NSString*)quote
|
2000-01-22 12:49:49 +00:00
|
|
|
{
|
2002-08-04 18:00:11 +00:00
|
|
|
return [self hasPrefix:quote
|
|
|
|
andSuffix:quote];
|
2000-01-22 12:49:49 +00:00
|
|
|
};
|
|
|
|
//--------------------------------------------------------------------
|
2002-08-04 18:00:11 +00:00
|
|
|
-(NSString*)stringWithoutQuote:(NSString*)quote
|
2000-01-22 12:49:49 +00:00
|
|
|
{
|
2002-08-04 18:00:11 +00:00
|
|
|
return [self stringWithoutPrefix:quote
|
|
|
|
andSuffix:quote];
|
2000-01-22 12:49:49 +00:00
|
|
|
};
|
|
|
|
@end
|
|
|
|
|
|
|
|
//====================================================================
|
|
|
|
@implementation NSMutableString (Qutotes)
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
2002-08-04 18:00:11 +00:00
|
|
|
-(void)removePrefix:(NSString*)prefix
|
|
|
|
andSuffix:(NSString*)suffix
|
2000-01-22 12:49:49 +00:00
|
|
|
{
|
2002-08-04 18:00:11 +00:00
|
|
|
[self removePrefix:prefix];
|
|
|
|
[self removeSuffix:suffix];
|
2000-01-22 12:49:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
2002-08-04 18:00:11 +00:00
|
|
|
-(void)removeQuote:(NSString*)quote
|
2000-01-22 12:49:49 +00:00
|
|
|
{
|
2002-08-04 18:00:11 +00:00
|
|
|
[self removePrefix:quote
|
|
|
|
andSuffix:quote];
|
2000-01-22 12:49:49 +00:00
|
|
|
};
|
|
|
|
@end
|
|
|
|
|