@@ -230,33 +230,52 @@ public static bool CanTranslate(Expression expression)
230
230
return false ;
231
231
}
232
232
233
+ public static bool CanTranslateComparisonExpression ( Expression leftExpression , AstComparisonFilterOperator comparisonOperator , Expression rightExpression )
234
+ {
235
+ return
236
+ IsGetCharsComparison ( leftExpression ) ||
237
+ IsStringEqualityComparison ( leftExpression , comparisonOperator ) ||
238
+ IsStringIndexOfComparison ( leftExpression ) ||
239
+ IsStringLengthComparison ( leftExpression ) || IsStringCountComparison ( leftExpression ) ;
240
+ }
241
+
233
242
public static bool TryTranslate ( TranslationContext context , Expression expression , out AstFilter filter )
234
243
{
235
- try
236
- {
237
- filter = Translate ( context , expression ) ;
238
- return true ;
239
- }
240
- catch ( ExpressionNotSupportedException )
244
+ if ( CanTranslate ( expression ) )
241
245
{
242
- filter = null ;
243
- return false ;
246
+ try
247
+ {
248
+ filter = Translate ( context , expression ) ;
249
+ return true ;
250
+ }
251
+ catch ( ExpressionNotSupportedException )
252
+ {
253
+ // ignore exception and return false
254
+ }
244
255
}
256
+
257
+ filter = null ;
258
+ return false ;
245
259
}
246
260
247
261
// caller is responsible for ensuring constant is on the right
248
262
public static bool TryTranslateComparisonExpression ( TranslationContext context , Expression expression , Expression leftExpression , AstComparisonFilterOperator comparisonOperator , Expression rightExpression , out AstFilter filter )
249
263
{
250
- try
251
- {
252
- filter = TranslateComparisonExpression ( context , expression , leftExpression , comparisonOperator , rightExpression ) ;
253
- return true ;
254
- }
255
- catch ( ExpressionNotSupportedException )
264
+ if ( CanTranslateComparisonExpression ( leftExpression , comparisonOperator , rightExpression ) )
256
265
{
257
- filter = null ;
258
- return false ;
266
+ try
267
+ {
268
+ filter = TranslateComparisonExpression ( context , expression , leftExpression , comparisonOperator , rightExpression ) ;
269
+ return true ;
270
+ }
271
+ catch ( ExpressionNotSupportedException )
272
+ {
273
+ // ignore exception and return false
274
+ }
259
275
}
276
+
277
+ filter = null ;
278
+ return false ;
260
279
}
261
280
262
281
public static AstFilter Translate ( TranslationContext context , Expression expression )
@@ -287,9 +306,9 @@ public static AstFilter TranslateComparisonExpression(TranslationContext context
287
306
return TranslateGetCharsComparison ( context , expression , leftExpression , comparisonOperator , rightExpression ) ;
288
307
}
289
308
290
- if ( IsStringComparison ( leftExpression ) )
309
+ if ( IsStringEqualityComparison ( leftExpression , comparisonOperator ) )
291
310
{
292
- return TranslateStringComparison ( context , expression , leftExpression , comparisonOperator , rightExpression ) ;
311
+ return TranslateStringEqualityComparison ( context , expression , leftExpression , comparisonOperator , rightExpression ) ;
293
312
}
294
313
295
314
if ( IsStringIndexOfComparison ( leftExpression ) )
@@ -370,24 +389,19 @@ private static string GetEscapedTrimChars(MethodCallExpression trimExpression)
370
389
371
390
private static bool IsGetCharsComparison ( Expression leftExpression )
372
391
{
373
- if ( leftExpression is UnaryExpression leftUnaryExpression &&
374
- leftUnaryExpression . NodeType == ExpressionType . Convert &&
375
- leftUnaryExpression . Type == typeof ( int ) &&
376
- leftUnaryExpression . Operand is MethodCallExpression leftMethodCallExpression )
377
- {
378
- var method = leftMethodCallExpression . Method ;
379
- if ( method . Is ( StringMethod . GetChars ) )
380
- {
381
- return true ;
382
- }
383
- }
384
-
385
- return false ;
392
+ return
393
+ leftExpression is UnaryExpression leftConvertExpression &&
394
+ leftConvertExpression . NodeType == ExpressionType . Convert &&
395
+ leftConvertExpression . Type == typeof ( int ) &&
396
+ leftConvertExpression . Operand is MethodCallExpression leftGetCharsExpression &&
397
+ leftGetCharsExpression . Method . Is ( StringMethod . GetChars ) ;
386
398
}
387
399
388
- private static bool IsStringComparison ( Expression leftExpression )
400
+ private static bool IsStringEqualityComparison ( Expression leftExpression , AstComparisonFilterOperator comparisonOperator )
389
401
{
390
- return leftExpression . Type == typeof ( string ) ;
402
+ return
403
+ leftExpression . Type == typeof ( string ) &&
404
+ ( comparisonOperator == AstComparisonFilterOperator . Eq || comparisonOperator == AstComparisonFilterOperator . Ne ) ;
391
405
}
392
406
393
407
private static bool IsStringCountComparison ( Expression leftExpression )
@@ -608,7 +622,7 @@ bool IsImpossibleMatch(Modifiers modifiers, string value)
608
622
}
609
623
}
610
624
611
- private static AstFilter TranslateStringComparison ( TranslationContext context , Expression expression , Expression leftExpression , AstComparisonFilterOperator comparisonOperator , Expression rightExpression )
625
+ private static AstFilter TranslateStringEqualityComparison ( TranslationContext context , Expression expression , Expression leftExpression , AstComparisonFilterOperator comparisonOperator , Expression rightExpression )
612
626
{
613
627
var ( field , modifiers ) = TranslateField ( context , expression , leftExpression ) ;
614
628
var comparand = rightExpression . GetConstantValue < string > ( containingExpression : expression ) ;
0 commit comments