28 @
typedef CPComparisonPredicateModifier
29 @
typedef CPPredicateOperatorType
42 CPComparisonPredicateModifier _modifier;
43 CPPredicateOperatorType _type;
44 unsigned int _options;
56 + (CPPredicate)predicateWithLeftExpression:(CPExpression)left rightExpression:(CPExpression)right customSelector:(
SEL)selector
58 return [[
self alloc] initWithLeftExpression:left rightExpression:right customSelector:selector];
70 + (CPPredicate)predicateWithLeftExpression:(CPExpression)left rightExpression:(CPExpression)right modifier:(CPComparisonPredicateModifier)modifier type:(
int)type options:(
unsigned)options
72 return [[
self alloc] initWithLeftExpression:left rightExpression:right modifier:modifier type:type options:options];
82 - (id)initWithLeftExpression:(CPExpression)left rightExpression:(CPExpression)right customSelector:(
SEL)selector
93 _customSelector = selector;
108 - (id)initWithLeftExpression:(CPExpression)left rightExpression:(CPExpression)right modifier:(CPComparisonPredicateModifier)modifier type:(CPPredicateOperatorType)type options:(
unsigned)options
116 _modifier = modifier;
125 _customSelector = NULL;
136 - (CPComparisonPredicateModifier)comparisonPredicateModifier
145 - (SEL)customSelector
147 return _customSelector;
154 - (CPExpression)leftExpression
172 - (CPPredicateOperatorType)predicateOperatorType
181 - (CPExpression)rightExpression
199 default: modifier =
"";
215 default: options =
"";
251 return [
CPString stringWithFormat:@"%s%s %s%s %s",modifier,[_left description],operator,options,[_right description]];
254 - (CPPredicate)predicateWithSubstitutionVariables:(
CPDictionary)variables
256 var left = [_left _expressionWithSubstitutionVariables:variables],
257 right = [_right _expressionWithSubstitutionVariables:variables];
267 if (
self === anObject)
270 if (anObject === nil || anObject.isa !==
self.isa || _modifier !== [anObject comparisonPredicateModifier] || _type !== [anObject predicateOperatorType] || _options !== [anObject options] || _customSelector !== [anObject customSelector] || ![_left
isEqual:[anObject leftExpression]] || ![_right
isEqual:[anObject rightExpression]])
276 - (BOOL)_evaluateValue:lhs rightValue:rhs
278 var leftIsNil = (lhs == nil || [lhs isEqual:[
CPNull null]]),
282 return (leftIsNil == rightIsNil &&
287 var string_compare_options = 0;
302 lhs = lhs.stripDiacritics();
303 rhs = rhs.stripDiacritics();
305 return (
new RegExp(rhs,commut)).test(lhs);
309 lhs = lhs.stripDiacritics();
310 rhs = rhs.stripDiacritics();
313 reg =
new RegExp(rhs.escapeForRegExp(),commut);
314 return reg.test(lhs);
321 return ([lhs compare:rhs options:string_compare_options range:range] ==
CPOrderedSame);
328 return ([lhs compare:rhs options:string_compare_options range:range] ==
CPOrderedSame);
337 if (![lhs respondsToSelector:
@selector(objectEnumerator)])
338 [
CPException raise:CPInvalidArgumentException
reason:@"The left/right hand side for a CONTAINS/IN operator must be a collection or a string"];
340 return [lhs containsObject:rhs];
348 return ([lhs rangeOfString:rhs options:string_compare_options].location !=
CPNotFound);
351 [
CPException raise:CPInvalidArgumentException
reason:@"The right hand side for a BETWEEN operator must contain 2 objects"];
359 - (BOOL)evaluateWithObject:(
id)object
364 - (BOOL)evaluateWithObject:(
id)object substitutionVariables:(
CPDictionary)variables
366 var leftValue = [_left expressionValueWithObject:object context:variables],
367 rightValue = [_right expressionValueWithObject:object context:variables];
369 leftValue = (typeof leftValue ==
"boolean") ? [
CPNumber numberWithBool:leftValue] : leftValue;
370 rightValue = (typeof rightValue ==
"boolean") ? [
CPNumber numberWithBool:rightValue] : rightValue;
373 return [
self _evaluateValue:leftValue rightValue:rightValue];
376 if (![leftValue respondsToSelector:
@selector(objectEnumerator)])
379 var e = [leftValue objectEnumerator],
383 while ((value = [e nextObject]) !== nil)
385 var eval = [
self _evaluateValue:value rightValue:rightValue];
404 _left = [coder decodeObjectForKey:@"CPComparisonPredicateLeftExpression"];
405 _right = [coder decodeObjectForKey:@"CPComparisonPredicateRightExpression"];
406 _modifier = [coder decodeIntForKey:@"CPComparisonPredicateModifier"];
407 _type = [coder decodeIntForKey:@"CPComparisonPredicateType"];
408 _options = [coder decodeIntForKey:@"CPComparisonPredicateOptions"];
409 _customSelector = [coder decodeObjectForKey:@"CPComparisonPredicateCustomSelector"];
417 [coder encodeObject:_left forKey:@"CPComparisonPredicateLeftExpression"];
418 [coder encodeObject:_right forKey:@"CPComparisonPredicateRightExpression"];
419 [coder encodeInt:_modifier forKey:@"CPComparisonPredicateModifier"];
420 [coder encodeInt:_type forKey:@"CPComparisonPredicateType"];
421 [coder encodeInt:_options forKey:@"CPComparisonPredicateOptions"];
422 [coder encodeObject:_customSelector forKey:@"CPComparisonPredicateCustomSelector"];
427 var
source = ['*','?','(',')','{','}','.','+','|','/',','^'],
428 dest = ['.*','.?','\\(','\\)','\\{','\\}','\\.','\\+','\\|','\\/','\\$','\\^'];
430 String.prototype.escapeForRegExp =
function()
432 var foundChar =
false,
435 for (; i <
source.length; ++i)
437 if (this.indexOf(
source[i]) !== -1)
449 for (i = 0; i < this.length; ++i)
451 var sourceIndex =
source.indexOf(this.charAt(i));
452 if (sourceIndex !== -1)
453 result +=
dest[sourceIndex];
455 result += this.charAt(i);
Used to implement exception handling (creating & raising).
CPLikePredicateOperatorType
CPGreaterThanPredicateOperatorType
CPCaseInsensitivePredicateOption
An object representation of nil.
CPEqualToPredicateOperatorType
CPInPredicateOperatorType
void raise:reason:(CPString aName, [reason] CPString aReason)
BOOL evaluateWithObject:substitutionVariables:(id object, [substitutionVariables] CPDictionary variables)
CPInvalidArgumentException
A mutable key-value pair collection.
CPNotEqualToPredicateOperatorType
An immutable string (collection of characters).
CPCustomSelectorPredicateOperatorType
CPLessThanPredicateOperatorType
CPPredicate predicateWithLeftExpression:rightExpression:customSelector:(CPExpression left, [rightExpression] CPExpression right, [customSelector] SEL selector)
CPDiacriticInsensitivePredicateOption
Defines methods for use when archiving & restoring (enc/decoding).
CPComparisonPredicate is a subclass of CPPredicate used to compare expressions.
CPBeginsWithPredicateOperatorType
CPContainsPredicateOperatorType
CPDiacriticInsensitiveSearch
global CPInvalidArgumentException global CPRangeException CPCaseInsensitiveSearch
CPPredicate predicateWithLeftExpression:rightExpression:modifier:type:options:(CPExpression left, [rightExpression] CPExpression right, [modifier] CPComparisonPredicateModifier modifier, [type] int type, [options] unsigned options)
CPLessThanOrEqualToPredicateOperatorType
CPEndsWithPredicateOperatorType
function CPStringFromSelector(aSelector)
CPBetweenPredicateOperatorType
CPGreaterThanOrEqualToPredicateOperatorType
CPMatchesPredicateOperatorType
A bridged object to native Javascript numbers.
CPRange function CPMakeRange(location, length)
CPDirectPredicateModifier
id stringWithFormat:(CPString format, [,] ...)