fix use of preprocessor constants.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@31621 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
rfm 2010-11-18 09:46:51 +00:00
parent 4f86b23b2d
commit e594ebafce
2 changed files with 108 additions and 84 deletions

View file

@ -1,3 +1,7 @@
2010-11-18 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSPredicate.m: Fixup use of preprocessor constants.
2010-11-17 Eric Wasylishen 2010-11-17 Eric Wasylishen
* Source/NSPredicate.m: Implement MATCHES and LIKE using ICU * Source/NSPredicate.m: Implement MATCHES and LIKE using ICU
@ -6,7 +10,8 @@
2010-11-13 Riccardo Mottola 2010-11-13 Riccardo Mottola
* Source/NSPathUtilities.m: * Source/NSPathUtilities.m:
Make NSDownloadDirectory and NSDocumentDIrectory relative to the user home and for the user domain only. Behaviour checked on the Mac. Make NSDownloadDirectory and NSDocumentDIrectory relative to the
user home and for the user domain only. Behaviour checked on the Mac.
2010-11-05 Richard Frith-Macdonald <rfm@gnu.org> 2010-11-05 Richard Frith-Macdonald <rfm@gnu.org>

View file

@ -812,8 +812,9 @@ static NSExpression *evaluatedObjectExpression = nil;
} }
} }
#if defined(GS_USE_ICU) #if GS_USE_ICU == 1
static BOOL GSICUStringMatchesRegex(NSString *string, NSString *regex, NSStringCompareOptions opts) static BOOL
GSICUStringMatchesRegex(NSString *string, NSString *regex, NSStringCompareOptions opts)
{ {
BOOL result = NO; BOOL result = NO;
UErrorCode error = 0; UErrorCode error = 0;
@ -880,95 +881,113 @@ static BOOL GSICUStringMatchesRegex(NSString *string, NSString *regex, NSStringC
|| (_type == NSGreaterThanOrEqualToPredicateOperatorType))); || (_type == NSGreaterThanOrEqualToPredicateOperatorType)));
} }
// Change predicate options into string options. // Change predicate options into string options.
if (!(_options & NSDiacriticInsensitivePredicateOption)) if (!(_options & NSDiacriticInsensitivePredicateOption))
{ {
compareOptions |= NSLiteralSearch; compareOptions |= NSLiteralSearch;
} }
if (_options & NSCaseInsensitivePredicateOption) if (_options & NSCaseInsensitivePredicateOption)
{ {
compareOptions |= NSCaseInsensitiveSearch; compareOptions |= NSCaseInsensitiveSearch;
} }
/* This is a very optimistic implementation, /* This is a very optimistic implementation,
* hoping that the values are of the right type. * hoping that the values are of the right type.
*/ */
switch (_type) switch (_type)
{ {
case NSLessThanPredicateOperatorType: case NSLessThanPredicateOperatorType:
return ([leftResult compare: rightResult] == NSOrderedAscending); return ([leftResult compare: rightResult] == NSOrderedAscending);
case NSLessThanOrEqualToPredicateOperatorType: case NSLessThanOrEqualToPredicateOperatorType:
return ([leftResult compare: rightResult] != NSOrderedDescending); return ([leftResult compare: rightResult] != NSOrderedDescending);
case NSGreaterThanPredicateOperatorType: case NSGreaterThanPredicateOperatorType:
return ([leftResult compare: rightResult] == NSOrderedDescending); return ([leftResult compare: rightResult] == NSOrderedDescending);
case NSGreaterThanOrEqualToPredicateOperatorType: case NSGreaterThanOrEqualToPredicateOperatorType:
return ([leftResult compare: rightResult] != NSOrderedAscending); return ([leftResult compare: rightResult] != NSOrderedAscending);
case NSEqualToPredicateOperatorType: case NSEqualToPredicateOperatorType:
return [leftResult isEqual: rightResult]; return [leftResult isEqual: rightResult];
case NSNotEqualToPredicateOperatorType: case NSNotEqualToPredicateOperatorType:
return ![leftResult isEqual: rightResult]; return ![leftResult isEqual: rightResult];
case NSMatchesPredicateOperatorType: case NSMatchesPredicateOperatorType:
#if defined(GS_USE_ICU) #if GS_USE_ICU == 1
return GSICUStringMatchesRegex(leftResult, rightResult, compareOptions); return GSICUStringMatchesRegex(leftResult, rightResult, compareOptions);
#else #else
return [leftResult compare: rightResult options: compareOptions] == NSOrderedSame; return [leftResult compare: rightResult options: compareOptions]
== NSOrderedSame;
#endif #endif
case NSLikePredicateOperatorType: case NSLikePredicateOperatorType:
#if defined(GS_USE_ICU) #if GS_USE_ICU == 1
{ {
// The right hand is a pattern with ? meaning match one character, and NSString *regex;
// * meaning match zero or more characters, so translate that into a regex
NSString *regex = [rightResult stringByReplacingOccurrencesOfString: @"*" /* The right hand is a pattern with '?' meaning match one character,
withString: @".*"]; * and '*' meaning match zero or more characters, so translate that
regex = [regex stringByReplacingOccurrencesOfString: @"?" * into a regex.
withString: @".?"]; */
regex = [NSString stringWithFormat: @"^%@$", regex]; regex = [rightResult stringByReplacingOccurrencesOfString: @"*"
return GSICUStringMatchesRegex(leftResult, regex, compareOptions); withString: @".*"];
} regex = [regex stringByReplacingOccurrencesOfString: @"?"
withString: @".?"];
regex = [NSString stringWithFormat: @"^%@$", regex];
return GSICUStringMatchesRegex(leftResult, regex, compareOptions);
}
#else #else
return [leftResult compare: rightResult options: compareOptions] == NSOrderedSame; return [leftResult compare: rightResult options: compareOptions]
== NSOrderedSame;
#endif #endif
case NSBeginsWithPredicateOperatorType: case NSBeginsWithPredicateOperatorType:
{ {
NSRange range = NSMakeRange(0, [rightResult length]); NSRange range = NSMakeRange(0, [rightResult length]);
return ([leftResult compare: rightResult options: compareOptions range: range] == NSOrderedSame); return ([leftResult compare: rightResult
} options: compareOptions
case NSEndsWithPredicateOperatorType: range: range] == NSOrderedSame);
{ }
NSRange range = NSMakeRange([leftResult length] - [rightResult length], [rightResult length]); case NSEndsWithPredicateOperatorType:
return ([leftResult compare: rightResult options: compareOptions range: range] == NSOrderedSame); {
} NSRange range;
case NSInPredicateOperatorType:
// Handle special case where rightResult is a collection and leftResult an element of it.
if (![rightResult isKindOfClass: [NSString class]])
{
NSEnumerator *e;
id value;
if (![rightResult respondsToSelector: @selector(objectEnumerator)]) range = NSMakeRange([leftResult length] - [rightResult length],
{ [rightResult length]);
[NSException raise: NSInvalidArgumentException return ([leftResult compare: rightResult
format: @"The right hand side for an IN operator must be a collection"]; options: compareOptions
} range: range] == NSOrderedSame);
}
case NSInPredicateOperatorType:
/* Handle special case where rightResult is a collection
* and leftResult an element of it.
*/
if (![rightResult isKindOfClass: [NSString class]])
{
NSEnumerator *e;
id value;
e = [rightResult objectEnumerator]; if (![rightResult respondsToSelector: @selector(objectEnumerator)])
while ((value = [e nextObject])) {
{ [NSException raise: NSInvalidArgumentException
if ([value isEqual: leftResult]) format: @"The right hand side for an IN operator "
return YES; @"must be a collection"];
} }
return NO; e = [rightResult objectEnumerator];
} while ((value = [e nextObject]))
return ([rightResult rangeOfString: leftResult options: compareOptions].location != NSNotFound); {
case NSCustomSelectorPredicateOperatorType: if ([value isEqual: leftResult])
{ return YES;
BOOL (*function)(id,SEL,id) = (BOOL (*)(id,SEL,id))[leftResult methodForSelector: _selector]; }
return function(leftResult, _selector, rightResult);
} return NO;
default: }
return NO; return ([rightResult rangeOfString: leftResult
} options: compareOptions].location
!= NSNotFound);
case NSCustomSelectorPredicateOperatorType:
{
BOOL (*function)(id,SEL,id) = (BOOL (*)(id,SEL,id))[leftResult methodForSelector: _selector];
return function(leftResult, _selector, rightResult);
}
default:
return NO;
}
} }
- (BOOL) evaluateWithObject: (id)object - (BOOL) evaluateWithObject: (id)object