Possible bug in VB.NET 'Like' operator?

大兔子大兔子 提交于 2020-01-01 08:36:06

问题


Why is it that the following evaluates as True?

Dim result = "b" Like "*a*b"

Thanks.

EDIT:
To generalize this a bit, the following returns True:

"String1" Like "*AnyText1*AnyText2*AnyText???******????*String1"

VBA works correctly, returning False.
PowerShell works correctly, returning False:

PS C:\Users\XXX> "b" -Like "*a*b"
False

EDIT2:
The link to the bug report:
https://connect.microsoft.com/VisualStudio/feedback/details/748415/a-bug-in-net-like-operator


回答1:


I decided, for fun, to open up ilspy to debug this :-)

in this method;

    private static void MatchAsterisk(string Source, int SourceLength, int SourceIndex, LigatureInfo[] SourceLigatureInfo, string Pattern, int PatternLength, int PatternIndex, LigatureInfo[] PattternLigatureInfo, ref bool Mismatch, ref bool PatternError, CompareInfo Comparer, CompareOptions Options)

for this condition

                if (SourceLength <= 0)
                {
                    return;
                }

by changing it to

                if (SourceLength < 0)
                {
                    return;
                }

it seem to be make it work as intended

I only did a few small test, nothing big

the issue is; it is only looking at the LAST asterisk and when it was valid, was stopping right there

my small change make sure to check the previous one, or anything in fact before it

with my fix

 Dim result = "b" Like "*a*b"
 now return false

 "String1" Like "*AnyText1*AnyText2*AnyText???******????*String1"
 now return false

but does return true when it need to return true



来源:https://stackoverflow.com/questions/10971777/possible-bug-in-vb-net-like-operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!