libs-base icon indicating copy to clipboard operation
libs-base copied to clipboard

NSPredicate array reference keywords FIRST, LAST, and SIZE don't work

Open lcampbel opened this issue 2 years ago • 1 comments

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.

lcampbel avatar Feb 10 '22 17:02 lcampbel

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
             {

lcampbel avatar Feb 10 '22 17:02 lcampbel

It would be great if you could write a small unit test and open up a PR with the changes :)

hmelder avatar Sep 09 '23 18:09 hmelder

I added minimal testcases to Tests/base/NSPredicate/basic.m

rfm avatar Nov 14 '23 20:11 rfm