libs-base
libs-base copied to clipboard
NSPredicate array reference keywords FIRST, LAST, and SIZE don't work
The following code, which works on Mac OS, fails on gnustep:
void testFirst(void)
{
NSArray *object = @[ @1, @2, @3 ];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF[FIRST] = 1"];
BOOL match = [predicate evaluateWithObject:object];
NSLog(@"match = %d", match); // should match
}
with this error:
2022-02-10 15:56:30.924 ptest[178754:178754] Parsing failed for SELF[FIRST] = '1 with <NSException: 0x12c6cb8> NAME:NSInvalidArgumentException REASON:Missing identifier: ] = '1 INFO:(null)
The LAST and SIZE keywords fail similarly.
This patch fixes these bugs:
--- NSPredicate.m.orig 2022-02-09 20:44:25.987056355 +0000
+++ NSPredicate.m 2022-02-10 16:54:05.270872389 +0000
@@ -2535,17 +2535,17 @@
if ([self scanPredicateKeyword: @"FIRST"])
{
left = [NSExpression expressionForFunction: @"_first"
- arguments: [NSArray arrayWithObject: [self parseExpression]]];
+ arguments: [NSArray arrayWithObject: left]];
}
else if ([self scanPredicateKeyword: @"LAST"])
{
left = [NSExpression expressionForFunction: @"_last"
- arguments: [NSArray arrayWithObject: [self parseExpression]]];
+ arguments: [NSArray arrayWithObject: left]];
}
else if ([self scanPredicateKeyword: @"SIZE"])
{
left = [NSExpression expressionForFunction: @"count"
- arguments: [NSArray arrayWithObject: [self parseExpression]]];
+ arguments: [NSArray arrayWithObject: left]];
}
else
{
It would be great if you could write a small unit test and open up a PR with the changes :)
I added minimal testcases to Tests/base/NSPredicate/basic.m