fix for predicate format output of binary operators

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@36144 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Richard Frith-MacDonald 2013-02-15 15:14:50 +00:00
parent 1d582cabdb
commit 2a05739607
2 changed files with 82 additions and 29 deletions

View file

@ -1,3 +1,8 @@
2013-02-14 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSPredicate.m: updates to output correct predicate format
for binary operators.
2013-02-10 Marcus Muller <znek@mulle-kybernetik.com>
* Source/GSMDNSNetServices.m: enable whether a delegate is set or not.

View file

@ -60,6 +60,8 @@
*/
static NSExpression *evaluatedObjectExpression = nil;
extern void GSPropertyListMake(id,NSDictionary*,BOOL,BOOL,unsigned,id*);
@interface GSPredicateScanner : NSScanner
{
NSEnumerator *_args; // Not retained.
@ -135,7 +137,8 @@ static NSExpression *evaluatedObjectExpression = nil;
NSString *_function;
NSArray *_args;
unsigned int _argc;
SEL _selector;
SEL _selector;
NSString *_op; // Not retained;
}
@end
@ -521,10 +524,10 @@ static NSExpression *evaluatedObjectExpression = nil;
else
{
if (cnt == 1
&& [[_subs objectAtIndex: 0]
isKindOfClass: [NSCompoundPredicate class]]
&& [(NSCompoundPredicate *)[_subs objectAtIndex: 0]
compoundPredicateType] == NSOrPredicateType)
&& [[_subs objectAtIndex: 0]
isKindOfClass: [NSCompoundPredicate class]]
&& [(NSCompoundPredicate *)[_subs objectAtIndex: 0]
compoundPredicateType] == NSOrPredicateType)
{
// we need () around first OR on left side
fmt = [NSString stringWithFormat: @"(%@)", fmt];
@ -793,23 +796,25 @@ static NSExpression *evaluatedObjectExpression = nil;
- (NSPredicate *) predicateWithSubstitutionVariables: (NSDictionary *)variables
{
NSExpression *left = [_left _expressionWithSubstitutionVariables: variables];
NSExpression *right = [_right _expressionWithSubstitutionVariables: variables];
NSExpression *left;
NSExpression *right;
if (_type == NSCustomSelectorPredicateOperatorType)
{
return [NSComparisonPredicate predicateWithLeftExpression: left
rightExpression: right
customSelector: _selector];
}
else
{
return [NSComparisonPredicate predicateWithLeftExpression: left
rightExpression: right
modifier: _modifier
type: _type
options: _options];
}
left = [_left _expressionWithSubstitutionVariables: variables];
right = [_right _expressionWithSubstitutionVariables: variables];
if (_type == NSCustomSelectorPredicateOperatorType)
{
return [NSComparisonPredicate predicateWithLeftExpression: left
rightExpression: right
customSelector: _selector];
}
else
{
return [NSComparisonPredicate predicateWithLeftExpression: left
rightExpression: right
modifier: _modifier
type: _type
options: _options];
}
}
#if GS_USE_ICU == 1
@ -1095,7 +1100,8 @@ GSICUStringMatchesRegex(NSString *string, NSString *regex, NSStringCompareOption
GSFunctionExpression *e;
NSString *s;
e = [[GSFunctionExpression alloc] initWithExpressionType: NSFunctionExpressionType];
e = [[GSFunctionExpression alloc]
initWithExpressionType: NSFunctionExpressionType];
s = [NSString stringWithFormat: @"_eval_%@:", name];
e->_selector = NSSelectorFromString(s);
if (![e respondsToSelector: e->_selector])
@ -1106,6 +1112,11 @@ GSICUStringMatchesRegex(NSString *string, NSString *regex, NSStringCompareOption
ASSIGN(e->_function, name);
e->_argc = [args count];
ASSIGN(e->_args, args);
if ([name isEqualToString: @"_add"]) e->_op = @"+";
else if ([name isEqualToString: @"_sub"]) e->_op = @"-";
else if ([name isEqualToString: @"_mul"]) e->_op = @"*";
else if ([name isEqualToString: @"_div"]) e->_op = @"/";
else if ([name isEqualToString: @"_pow"]) e->_op = @"**";
return AUTORELEASE(e);
}
@ -1237,6 +1248,15 @@ GSICUStringMatchesRegex(NSString *string, NSString *regex, NSStringCompareOption
- (NSString *) description
{
if ([_obj isKindOfClass: [NSString class]])
{
NSMutableString *result = nil;
/* Quote the result string as necessary.
*/
GSPropertyListMake(_obj, nil, NO, YES, 2, &result);
return result;
}
return [_obj description];
}
@ -1386,11 +1406,39 @@ GSICUStringMatchesRegex(NSString *string, NSString *regex, NSStringCompareOption
- (NSString *) description
{
// FIXME: here we should recognize binary and unary operators
// and convert back to standard format
// and add parentheses if required
return [NSString stringWithFormat: @"%@(%@)",
[self function], _args];
if (nil != _op && 1 == [_args count])
{
GSFunctionExpression *a0 = [_args objectAtIndex: 0];
if (YES == [a0 isKindOfClass: [self class]] && nil != a0->_op)
{
return [NSString stringWithFormat: @"%@(%@)", _op, a0];
}
return [NSString stringWithFormat: @"%@%@", _op, a0];
}
if (nil != _op)
{
GSFunctionExpression *a0 = [_args objectAtIndex: 0];
GSFunctionExpression *a1 = [_args objectAtIndex: 1];
if (YES == [a0 isKindOfClass: [self class]] && nil != a0->_op)
{
if (YES == [a1 isKindOfClass: [self class]] && nil != a1->_op)
{
return [NSString stringWithFormat: @"(%@) %@ (%@)", a0, _op, a1];
}
return [NSString stringWithFormat: @"(%@) %@ %@", a0, _op, a1];
}
if (YES == [a1 isKindOfClass: [self class]] && nil != a1->_op)
{
return [NSString stringWithFormat: @"%@ %@ (%@)", a0, _op, a1];
}
return [NSString stringWithFormat: @"%@ %@ %@", a0, _op, a1];
}
return [NSString stringWithFormat: @"%@(%@)", [self function], _args];
}
- (NSString *) function
@ -2449,13 +2497,13 @@ GSICUStringMatchesRegex(NSString *string, NSString *regex, NSStringCompareOption
if ([self scanString: @"+" intoString: NULL])
{
right = [self parseMultiplicationExpression];
left = [NSExpression expressionForFunction: @"_add"
left = [NSExpression expressionForFunction: @"_add"
arguments: [NSArray arrayWithObjects: left, right, nil]];
}
else if ([self scanString: @"-" intoString: NULL])
{
right = [self parseMultiplicationExpression];
left = [NSExpression expressionForFunction: @"_sub"
left = [NSExpression expressionForFunction: @"_sub"
arguments: [NSArray arrayWithObjects: left, right, nil]];
}
else