-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNSPredicate.m
More file actions
2768 lines (2380 loc) · 68.9 KB
/
NSPredicate.m
File metadata and controls
2768 lines (2380 loc) · 68.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Interface for NSPredicate for GNUStep
Copyright (C) 2005 Free Software Foundation, Inc.
Written by: Dr. H. Nikolaus Schaller
Created: 2005
Modifications: Fred Kiefer <FredKiefer@gmx.de>
Date: May 2007
Modifications: Richard Frith-Macdoanld <rfm@gnu.org>
Date: June 2007
This file is part of the GNUstep Base Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110 USA.
*/
#import "common.h"
#define EXPOSE_NSComparisonPredicate_IVARS 1
#define EXPOSE_NSCompoundPredicate_IVARS 1
#define EXPOSE_NSExpression_IVARS 1
#import "Foundation/NSComparisonPredicate.h"
#import "Foundation/NSCompoundPredicate.h"
#import "Foundation/NSExpression.h"
#import "Foundation/NSPredicate.h"
#import "Foundation/NSArray.h"
#import "Foundation/NSDate.h"
#import "Foundation/NSDictionary.h"
#import "Foundation/NSEnumerator.h"
#import "Foundation/NSException.h"
#import "Foundation/NSKeyValueCoding.h"
#import "Foundation/NSNull.h"
#import "Foundation/NSScanner.h"
#import "Foundation/NSValue.h"
#import "GSPrivate.h"
// For pow()
#include <math.h>
#if defined(HAVE_UNICODE_UREGEX_H)
#include <unicode/uregex.h>
#endif
/* Object to represent the expression beign evaluated.
*/
static NSExpression *evaluatedObjectExpression = nil;
extern void GSPropertyListMake(id,NSDictionary*,BOOL,BOOL,unsigned,id*);
@interface GSPredicateScanner : NSScanner
{
NSEnumerator *_args; // Not retained.
unsigned _retrieved;
}
- (id) initWithString: (NSString*)format
args: (NSArray*)args;
- (id) nextArg;
- (BOOL) scanPredicateKeyword: (NSString *) key;
- (NSPredicate *) parse;
- (NSPredicate *) parsePredicate;
- (NSPredicate *) parseAnd;
- (NSPredicate *) parseNot;
- (NSPredicate *) parseOr;
- (NSPredicate *) parseComparison;
- (NSExpression *) parseExpression;
- (NSExpression *) parseFunctionalExpression;
- (NSExpression *) parsePowerExpression;
- (NSExpression *) parseMultiplicationExpression;
- (NSExpression *) parseAdditionExpression;
- (NSExpression *) parseBinaryExpression;
- (NSExpression *) parseSimpleExpression;
@end
@interface GSTruePredicate : NSPredicate
@end
@interface GSFalsePredicate : NSPredicate
@end
@interface GSAndCompoundPredicate : NSCompoundPredicate
@end
@interface GSOrCompoundPredicate : NSCompoundPredicate
@end
@interface GSNotCompoundPredicate : NSCompoundPredicate
@end
@interface NSExpression (Private)
- (id) _expressionWithSubstitutionVariables: (NSDictionary *)variables;
@end
@interface GSConstantValueExpression : NSExpression
{
@public
id _obj;
}
@end
@interface GSEvaluatedObjectExpression : NSExpression
@end
@interface GSVariableExpression : NSExpression
{
@public
NSString *_variable;
}
@end
@interface GSKeyPathExpression : NSExpression
{
@public
NSString *_keyPath;
}
@end
@interface GSFunctionExpression : NSExpression
{
@public
NSString *_function;
NSArray *_args;
unsigned int _argc;
SEL _selector;
NSString *_op; // Not retained;
}
@end
#if OS_API_VERSION(MAC_OS_X_VERSION_10_6, GS_API_LATEST)
@interface GSBlockPredicate : NSPredicate
{
GSBlockPredicateBlock _block;
}
- (instancetype) initWithBlock: (GSBlockPredicateBlock)block;
@end
@interface GSBoundBlockPredicate : GSBlockPredicate
{
GS_GENERIC_CLASS(NSDictionary,NSString*,id)* _bindings;
}
- (instancetype) initWithBlock: (GSBlockPredicateBlock)block
bindings: (GS_GENERIC_CLASS(NSDictionary,NSString*,id)*)bindings;
@end
#endif
@implementation NSPredicate
+ (NSPredicate *) predicateWithFormat: (NSString *) format, ...
{
NSPredicate *p;
va_list va;
va_start(va, format);
p = [self predicateWithFormat: format arguments: va];
va_end(va);
return p;
}
+ (NSPredicate *) predicateWithFormat: (NSString *)format
argumentArray: (NSArray *)args
{
GSPredicateScanner *s;
NSPredicate *p;
s = [[GSPredicateScanner alloc] initWithString: format
args: args];
p = [s parse];
RELEASE(s);
return p;
}
+ (NSPredicate *) predicateWithFormat: (NSString *)format
arguments: (va_list)args
{
GSPredicateScanner *s;
NSPredicate *p;
const char *ptr = [format UTF8String];
NSMutableArray *arr = [NSMutableArray arrayWithCapacity: 10];
while (*ptr != 0)
{
char c = *ptr++;
if (c == '%')
{
c = *ptr;
switch (c)
{
case '%':
ptr++;
break;
case 'K':
case '@':
ptr++;
[arr addObject: va_arg(args, id)];
break;
case 'c':
ptr++;
[arr addObject: [NSNumber numberWithChar:
(char)va_arg(args, NSInteger)]];
break;
case 'C':
ptr++;
[arr addObject: [NSNumber numberWithShort:
(short)va_arg(args, NSInteger)]];
break;
case 'd':
case 'D':
case 'i':
ptr++;
[arr addObject: [NSNumber numberWithInt:
va_arg(args, int)]];
break;
case 'o':
case 'O':
case 'u':
case 'U':
case 'x':
case 'X':
ptr++;
[arr addObject: [NSNumber numberWithUnsignedInt:
va_arg(args, unsigned)]];
break;
case 'e':
case 'E':
case 'f':
case 'g':
case 'G':
ptr++;
[arr addObject: [NSNumber numberWithDouble:
va_arg(args, double)]];
break;
case 'h':
ptr++;
if (*ptr != 0)
{
c = *ptr;
if (c == 'i')
{
[arr addObject: [NSNumber numberWithShort:
(short)va_arg(args, NSInteger)]];
}
if (c == 'u')
{
[arr addObject: [NSNumber numberWithUnsignedShort:
(unsigned short)va_arg(args, NSInteger)]];
}
}
break;
case 'q':
ptr++;
if (*ptr != 0)
{
c = *ptr;
if (c == 'i')
{
[arr addObject: [NSNumber numberWithLongLong:
va_arg(args, long long)]];
}
if (c == 'u' || c == 'x' || c == 'X')
{
[arr addObject: [NSNumber numberWithUnsignedLongLong:
va_arg(args, unsigned long long)]];
}
}
break;
}
}
else if (c == '\'')
{
while (*ptr != 0)
{
if (*ptr++ == '\'')
{
break;
}
}
}
else if (c == '"')
{
while (*ptr != 0)
{
if (*ptr++ == '"')
{
break;
}
}
}
}
s = [[GSPredicateScanner alloc] initWithString: format
args: arr];
p = [s parse];
RELEASE(s);
return p;
}
+ (NSPredicate *) predicateWithValue: (BOOL)value
{
if (value)
{
return AUTORELEASE([GSTruePredicate new]);
}
else
{
return AUTORELEASE([GSFalsePredicate new]);
}
}
// we don't ever instantiate NSPredicate
- (id) copyWithZone: (NSZone *)z
{
return NSCopyObject(self, 0, z);
}
- (BOOL) evaluateWithObject: (id)object
{
[self subclassResponsibility: _cmd];
return NO;
}
- (NSString *) description
{
return [self predicateFormat];
}
- (NSString *) predicateFormat
{
[self subclassResponsibility: _cmd];
return nil;
}
- (NSPredicate *) predicateWithSubstitutionVariables: (NSDictionary *)variables
{
return AUTORELEASE([self copy]);
}
#if OS_API_VERSION(MAC_OS_X_VERSION_10_5, GS_API_LATEST)
- (BOOL) evaluateWithObject: (id)object
substitutionVariables: (GS_GENERIC_CLASS(NSDictionary, NSString*, id)*)variables
{
return [[self predicateWithSubstitutionVariables: variables]
evaluateWithObject: object];
}
#endif
- (Class) classForCoder
{
return [NSPredicate class];
}
- (void) encodeWithCoder: (NSCoder *) coder;
{
// FIXME
[self subclassResponsibility: _cmd];
}
- (id) initWithCoder: (NSCoder *) coder;
{
// FIXME
[self subclassResponsibility: _cmd];
return self;
}
#if OS_API_VERSION(MAC_OS_X_VERSION_10_6, GS_API_LATEST)
+ (NSPredicate*)predicateWithBlock: (GSBlockPredicateBlock)block
{
return [[[GSBlockPredicate alloc] initWithBlock: block] autorelease];
}
#endif
@end
@implementation GSTruePredicate
- (id) copyWithZone: (NSZone *)z
{
return RETAIN(self);
}
- (BOOL) evaluateWithObject: (id)object
{
return YES;
}
- (NSString *) predicateFormat
{
return @"TRUEPREDICATE";
}
@end
@implementation GSFalsePredicate
- (id) copyWithZone: (NSZone *)z
{
return RETAIN(self);
}
- (BOOL) evaluateWithObject: (id)object
{
return NO;
}
- (NSString *) predicateFormat
{
return @"FALSEPREDICATE";
}
@end
@implementation NSCompoundPredicate
+ (NSPredicate *) andPredicateWithSubpredicates: (NSArray *)list
{
return AUTORELEASE([[GSAndCompoundPredicate alloc]
initWithType: NSAndPredicateType subpredicates: list]);
}
+ (NSPredicate *) notPredicateWithSubpredicate: (NSPredicate *)predicate
{
NSArray *list;
list = [NSArray arrayWithObject: predicate];
return AUTORELEASE([[GSNotCompoundPredicate alloc]
initWithType: NSNotPredicateType subpredicates: list]);
}
+ (NSPredicate *) orPredicateWithSubpredicates: (NSArray *)list
{
return AUTORELEASE([[GSOrCompoundPredicate alloc]
initWithType: NSOrPredicateType subpredicates: list]);
}
- (NSCompoundPredicateType) compoundPredicateType
{
return _type;
}
- (id) initWithType: (NSCompoundPredicateType)type
subpredicates: (NSArray *)list
{
if ((self = [super init]) != nil)
{
_type = type;
ASSIGNCOPY(_subs, list);
}
return self;
}
- (void) dealloc
{
RELEASE(_subs);
[super dealloc];
}
- (id) copyWithZone: (NSZone *)z
{
return [[[self class] alloc] initWithType: _type subpredicates: _subs];
}
- (NSArray *) subpredicates
{
return _subs;
}
- (NSPredicate *) predicateWithSubstitutionVariables: (NSDictionary *)variables
{
unsigned int count = [_subs count];
NSMutableArray *esubs = [NSMutableArray arrayWithCapacity: count];
unsigned int i;
NSPredicate *p;
for (i = 0; i < count; i++)
{
[esubs addObject: [[_subs objectAtIndex: i]
predicateWithSubstitutionVariables: variables]];
}
p = [[[self class] alloc] initWithType: _type subpredicates: esubs];
return AUTORELEASE(p);
}
- (Class) classForCoder
{
return [NSCompoundPredicate class];
}
- (void) encodeWithCoder: (NSCoder *)coder
{
// FIXME
[self subclassResponsibility: _cmd];
}
- (id) initWithCoder: (NSCoder *)coder
{
// FIXME
[self subclassResponsibility: _cmd];
return self;
}
@end
@implementation GSAndCompoundPredicate
- (BOOL) evaluateWithObject: (id) object
{
NSEnumerator *e = [_subs objectEnumerator];
NSPredicate *p;
while ((p = [e nextObject]) != nil)
{
if ([p evaluateWithObject: object] == NO)
{
return NO; // any NO returns NO
}
}
return YES; // all are true
}
- (NSString *) predicateFormat
{
NSString *fmt = @"";
NSEnumerator *e = [_subs objectEnumerator];
NSPredicate *sub;
unsigned cnt = 0;
while ((sub = [e nextObject]) != nil)
{
// when to add ()? -> if sub is compound and of type "or"
if (cnt == 0)
{
fmt = [sub predicateFormat]; // first
}
else
{
if (cnt == 1
&& [[_subs objectAtIndex: 0]
isKindOfClass: [NSCompoundPredicate class]]
&& [(NSCompoundPredicate *)[_subs objectAtIndex: 0]
compoundPredicateType] == NSOrPredicateType)
{
// we need () around first OR on left side
fmt = [NSString stringWithFormat: @"(%@)", fmt];
}
if ([sub isKindOfClass: [NSCompoundPredicate class]]
&& [(NSCompoundPredicate *) sub compoundPredicateType]
== NSOrPredicateType)
{
// we need () around right OR
fmt = [NSString stringWithFormat: @"%@ AND (%@)",
fmt, [sub predicateFormat]];
}
else
{
fmt = [NSString stringWithFormat: @"%@ AND %@",
fmt, [sub predicateFormat]];
}
}
cnt++;
}
return fmt;
}
@end
@implementation GSOrCompoundPredicate
- (BOOL) evaluateWithObject: (id)object
{
NSEnumerator *e = [_subs objectEnumerator];
NSPredicate *p;
while ((p = [e nextObject]) != nil)
{
if ([p evaluateWithObject: object] == YES)
{
return YES; // any YES returns YES
}
}
return NO; // none is true
}
- (NSString *) predicateFormat
{
NSString *fmt = @"";
NSEnumerator *e = [_subs objectEnumerator];
NSPredicate *sub;
while ((sub = [e nextObject]) != nil)
{
if ([fmt length] > 0)
{
fmt = [NSString stringWithFormat: @"%@ OR %@",
fmt, [sub predicateFormat]];
}
else
{
fmt = [sub predicateFormat]; // first
}
}
return fmt;
}
@end
@implementation GSNotCompoundPredicate
- (BOOL) evaluateWithObject: (id)object
{
NSPredicate *sub = [_subs objectAtIndex: 0];
return ![sub evaluateWithObject: object];
}
- (NSString *) predicateFormat
{
NSPredicate *sub = [_subs objectAtIndex: 0];
if ([sub isKindOfClass: [NSCompoundPredicate class]]
&& [(NSCompoundPredicate *)sub compoundPredicateType]
!= NSNotPredicateType)
{
return [NSString stringWithFormat: @"NOT(%@)", [sub predicateFormat]];
}
return [NSString stringWithFormat: @"NOT %@", [sub predicateFormat]];
}
@end
@implementation NSComparisonPredicate
+ (NSPredicate *) predicateWithLeftExpression: (NSExpression *)left
rightExpression: (NSExpression *)right
customSelector: (SEL) sel
{
return AUTORELEASE([[self alloc] initWithLeftExpression: left
rightExpression: right
customSelector: sel]);
}
+ (NSPredicate *) predicateWithLeftExpression: (NSExpression *)left
rightExpression: (NSExpression *)right
modifier: (NSComparisonPredicateModifier)modifier
type: (NSPredicateOperatorType)type
options: (NSUInteger)opts
{
return AUTORELEASE([[self alloc] initWithLeftExpression: left
rightExpression: right
modifier: modifier
type: type
options: opts]);
}
- (NSPredicate *) initWithLeftExpression: (NSExpression *)left
rightExpression: (NSExpression *)right
customSelector: (SEL)sel
{
if ((self = [super init]) != nil)
{
ASSIGN(_left, left);
ASSIGN(_right, right);
_selector = sel;
_type = NSCustomSelectorPredicateOperatorType;
}
return self;
}
- (id) initWithLeftExpression: (NSExpression *)left
rightExpression: (NSExpression *)right
modifier: (NSComparisonPredicateModifier)modifier
type: (NSPredicateOperatorType)type
options: (NSUInteger)opts
{
if ((self = [super init]) != nil)
{
ASSIGN(_left, left);
ASSIGN(_right, right);
_modifier = modifier;
_type = type;
_options = opts;
}
return self;
}
- (void) dealloc;
{
RELEASE(_left);
RELEASE(_right);
[super dealloc];
}
- (NSComparisonPredicateModifier) comparisonPredicateModifier
{
return _modifier;
}
- (SEL) customSelector
{
return _selector;
}
- (NSExpression *) leftExpression
{
return _left;
}
- (NSUInteger) options
{
return _options;
}
- (NSPredicateOperatorType) predicateOperatorType
{
return _type;
}
- (NSExpression *) rightExpression
{
return _right;
}
- (NSString *) predicateFormat
{
NSString *modi = @"";
NSString *comp = @"?comparison?";
NSString *opt = @"";
switch (_modifier)
{
case NSDirectPredicateModifier:
break;
case NSAnyPredicateModifier:
modi = @"ANY ";
break;
case NSAllPredicateModifier:
modi = @"ALL";
break;
default:
modi = @"?modifier?";
break;
}
switch (_type)
{
case NSLessThanPredicateOperatorType:
comp = @"<";
break;
case NSLessThanOrEqualToPredicateOperatorType:
comp = @"<=";
break;
case NSGreaterThanPredicateOperatorType:
comp = @">=";
break;
case NSGreaterThanOrEqualToPredicateOperatorType:
comp = @">";
break;
case NSEqualToPredicateOperatorType:
comp = @"=";
break;
case NSNotEqualToPredicateOperatorType:
comp = @"!=";
break;
case NSMatchesPredicateOperatorType:
comp = @"MATCHES";
break;
case NSLikePredicateOperatorType:
comp = @"LIKE";
break;
case NSBeginsWithPredicateOperatorType:
comp = @"BEGINSWITH";
break;
case NSEndsWithPredicateOperatorType:
comp = @"ENDSWITH";
break;
case NSInPredicateOperatorType:
comp = @"IN";
break;
case NSCustomSelectorPredicateOperatorType:
comp = NSStringFromSelector(_selector);
break;
case NSContainsPredicateOperatorType:
comp = @"CONTAINS";
break;
case NSBetweenPredicateOperatorType:
comp = @"BETWEEN";
break;
}
switch (_options)
{
case NSCaseInsensitivePredicateOption:
opt = @"[c]";
break;
case NSDiacriticInsensitivePredicateOption:
opt = @"[d]";
break;
case NSCaseInsensitivePredicateOption
| NSDiacriticInsensitivePredicateOption:
opt = @"[cd]";
break;
default:
//opt = @"[?options?]";
break;
}
return [NSString stringWithFormat: @"%@%@ %@%@ %@",
modi, _left, comp, opt, _right];
}
- (NSPredicate *) predicateWithSubstitutionVariables: (NSDictionary *)variables
{
NSExpression *left;
NSExpression *right;
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
static BOOL
GSICUStringMatchesRegex(NSString *string, NSString *regex, NSStringCompareOptions opts)
{
BOOL result = NO;
UErrorCode error = 0;
uint32_t flags = 0;
NSUInteger stringLength = [string length];
NSUInteger regexLength = [regex length];
unichar *stringBuffer;
unichar *regexBuffer;
URegularExpression *icuregex = NULL;
stringBuffer = malloc(stringLength * sizeof(unichar));
if (NULL == stringBuffer) { return NO; }
regexBuffer = malloc(regexLength * sizeof(unichar));
if (NULL == regexBuffer) { free(stringBuffer); return NO; }
[string getCharacters: stringBuffer range: NSMakeRange(0, stringLength)];
[regex getCharacters: regexBuffer range: NSMakeRange(0, regexLength)];
flags |= UREGEX_DOTALL; // . is supposed to recognize newlines
if ((opts & NSCaseInsensitiveSearch) != 0) { flags |= UREGEX_CASE_INSENSITIVE; }
icuregex = uregex_open(regexBuffer, regexLength, flags, NULL, &error);
if (icuregex != NULL && U_SUCCESS(error))
{
uregex_setText(icuregex, stringBuffer, stringLength, &error);
result = uregex_matches(icuregex, 0, &error);
}
uregex_close(icuregex);
free(stringBuffer);
free(regexBuffer);
return result;
}
#endif
- (double) doubleValueFor: (id)value
{
if ([value isKindOfClass: [NSDate class]])
{
return [(NSDate*)value timeIntervalSinceReferenceDate];
}
else
{
return [value doubleValue];
}
}
- (BOOL) _evaluateLeftValue: (id)leftResult
rightValue: (id)rightResult
object: (id)object
{
unsigned compareOptions = 0;
BOOL leftIsNil;
BOOL rightIsNil;
leftIsNil = (leftResult == nil || [leftResult isEqual: [NSNull null]]);
rightIsNil = (rightResult == nil || [rightResult isEqual: [NSNull null]]);
if (leftIsNil || rightIsNil)
{
if (leftIsNil == rightIsNil)
{
/* Both of the values are nil.
* The result is YES if equality is requested.
*/
if (NSEqualToPredicateOperatorType == _type
|| NSLessThanOrEqualToPredicateOperatorType == _type
|| NSGreaterThanOrEqualToPredicateOperatorType == _type)
{
return YES;
}
}
else if (NSNotEqualToPredicateOperatorType == _type)
{
/* One, but not both of the values are nil.
* The result is YES if inequality is requested.
*/
return YES;
}
return NO;
}
// Change predicate options into string options.
if (!(_options & NSDiacriticInsensitivePredicateOption))
{
compareOptions |= NSLiteralSearch;
}
if (_options & NSCaseInsensitivePredicateOption)
{
compareOptions |= NSCaseInsensitiveSearch;
}
/* This is a very optimistic implementation,
* hoping that the values are of the right type.
*/
switch (_type)
{
case NSLessThanPredicateOperatorType:
{
double ld = [self doubleValueFor: leftResult];
double rd = [self doubleValueFor: rightResult];
return (ld < rd) ? YES : NO;
}
case NSLessThanOrEqualToPredicateOperatorType:
{
double ld = [self doubleValueFor: leftResult];
double rd = [self doubleValueFor: rightResult];
return (ld <= rd) ? YES : NO;
}
case NSGreaterThanPredicateOperatorType:
{
double ld = [self doubleValueFor: leftResult];
double rd = [self doubleValueFor: rightResult];
return (ld > rd) ? YES : NO;
}
case NSGreaterThanOrEqualToPredicateOperatorType:
{
double ld = [self doubleValueFor: leftResult];
double rd = [self doubleValueFor: rightResult];
return (ld >= rd) ? YES : NO;
}
case NSEqualToPredicateOperatorType:
return [leftResult isEqual: rightResult];
case NSNotEqualToPredicateOperatorType:
return ![leftResult isEqual: rightResult];
case NSMatchesPredicateOperatorType:
#if GS_USE_ICU == 1
return GSICUStringMatchesRegex(leftResult, rightResult, compareOptions);
#else
return [leftResult compare: rightResult options: compareOptions]
== NSOrderedSame ? YES : NO;
#endif
case NSLikePredicateOperatorType:
#if GS_USE_ICU == 1
{
NSString *regex;
/* The right hand is a pattern with '?' meaning match one character,
* and '*' meaning match zero or more characters, so translate that
* into a regex.
*/
regex = [rightResult stringByReplacingOccurrencesOfString: @"*"
withString: @".*"];