-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LanguageNormalizingExpressionVisitor for VB.NET string comparisons
Fixes #19592
- Loading branch information
Showing
2 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
src/EFCore/Query/Internal/LanguageNormalizingExpressionVisitor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Query.Internal | ||
{ | ||
/// <summary> | ||
/// Normalizes certain language-specific aspects of the expression trees produced by languages other | ||
/// than C#, e.g. Visual Basic. | ||
/// </summary> | ||
public class LanguageNormalizingExpressionVisitor : ExpressionVisitor | ||
{ | ||
private static readonly MethodInfo _stringCompareMethod | ||
= typeof(string).GetRuntimeMethod( | ||
nameof(string.Compare), | ||
new[] { typeof(string), typeof(string), typeof(StringComparison) }); | ||
|
||
protected override Expression VisitMethodCall(MethodCallExpression methodCallExpression) | ||
{ | ||
var visited = (MethodCallExpression)base.VisitMethodCall(methodCallExpression); | ||
|
||
// In VB.NET, comparison operators between strings (equality, greater-than, less-than) yield | ||
// calls to a VB-specific CompareString method. Normalize that to string.Compare. | ||
if (visited.Method.Name == "CompareString" | ||
&& visited.Method.DeclaringType?.Name == "Operators" | ||
&& visited.Method.DeclaringType?.Namespace == "Microsoft.VisualBasic.CompilerServices" | ||
&& visited.Object == null | ||
&& visited.Arguments.Count == 3 | ||
&& visited.Arguments[2] is ConstantExpression textCompareConstantExpression) | ||
{ | ||
return Expression.Call( | ||
_stringCompareMethod, | ||
visited.Arguments[0], | ||
visited.Arguments[1], | ||
(bool)textCompareConstantExpression.Value | ||
? Expression.Constant(StringComparison.OrdinalIgnoreCase) | ||
: Expression.Constant(StringComparison.Ordinal)); | ||
} | ||
|
||
return visited; | ||
} | ||
|
||
protected override Expression VisitBinary(BinaryExpression binaryExpression) | ||
{ | ||
var visitedLeft = Visit(binaryExpression.Left); | ||
var visitedRight = Visit(binaryExpression.Right); | ||
|
||
// In VB.NET, str1 = str2 yields CompareString(str1, str2, false) == 0. | ||
// Rewrite this is as a regular equality node. | ||
if (binaryExpression.NodeType == ExpressionType.Equal | ||
|| binaryExpression.NodeType == ExpressionType.NotEqual) | ||
{ | ||
var (compareStringExpression, otherExpression) = | ||
IsOrdinalStringCompare(visitedLeft) | ||
? ((MethodCallExpression)visitedLeft, visitedRight) | ||
: IsOrdinalStringCompare(visitedRight) | ||
? ((MethodCallExpression)visitedRight, visitedLeft) | ||
: (null, null); | ||
|
||
if (compareStringExpression != null | ||
&& otherExpression is ConstantExpression otherConstantExpression | ||
&& (int)otherConstantExpression.Value == 0) | ||
{ | ||
return Expression.MakeBinary( | ||
binaryExpression.NodeType, | ||
compareStringExpression.Arguments[0], | ||
compareStringExpression.Arguments[1]); | ||
} | ||
} | ||
|
||
return binaryExpression.Update(visitedLeft, binaryExpression.Conversion, visitedRight); | ||
|
||
static bool IsOrdinalStringCompare(Expression expression) | ||
=> expression is MethodCallExpression methodCallExpression | ||
&& methodCallExpression.Method == _stringCompareMethod | ||
&& methodCallExpression.Arguments[2] is ConstantExpression stringComparisonConstantExpression | ||
&& (StringComparison)stringComparisonConstantExpression.Value == StringComparison.Ordinal; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters