• July 09, 2020, 11:27:10 PM

Author Topic: How To: Optimized Wildcard Checking Function  (Read 3550 times)

ben_fb

  • Addict
  • *****
  • Posts: 246
How To: Optimized Wildcard Checking Function
« on: September 19, 2010, 08:08:15 AM »
This function will check if a user contains the 'wildcard' text. It does all forms of wildcard checking, this includes:
*Name*, *Name, Name*, Na*me, and even *N*am*e*.

You should avoid the last option at all cost: *N*am*e*
Visual Basic's built in 'Like' routine is VERY slow, and is used only as a last resort. This function does support it but I suggest you discourage against your bot users applying complex tags.

Code: [Select]

Public Function WildCard_Match(ByRef theName As String, ByRef theWildcard As String) As Boolean
   
    'AStar = "*"
    'both theName and theWildcard should be lowercase for these checks, so you
    'can do that here, or before the routine if you know theres going theres going to be wildcards.
    If AscW(theWildcard) = 42 And AscW(Right$(theWildcard, 1)) = 42 Then
    'if *wildcard*
        If InStrB(Mid$(theWildcard, 2, (Len(theWildcard) - 2)), AStar) Then
        'if using complex wildcards use like
            WildCard_Match = theName Like theWildcard
        Else
            'typical wildcards
            WildCard_Match = InStrB(theName, Mid$(theWildcard, 2, (Len(theWildcard) - 2)))
        End If
        '
    ElseIf AscW(theWildcard) = 42 And AscW(Right$(theWildcard, 1)) <> 42 Then
    'if *wildcard
        If InStrB(Mid$(theWildcard, 2), AStar) Then
        'complex wildcards
            WildCard_Match = theName Like theWildcard
        Else
        'typical wild cards
            If Right$(theName, (Len(theWildcard) - 1)) = Mid$(theWildcard, 2) Then WildCard_Match = True
        End If
        '
    ElseIf AscW(theWildcard) <> 42 And AscW(Right$(theWildcard, 1)) = 42 Then
    'if wildcard*
        If InStrB(Left$(theWildcard, Len(theWildcard) - 1), AStar) Then
        'complex wildcards
            WildCard_Match = theName Like theWildcard
        Else
        'typical wildcards
            If Left$(theName, (Len(theWildcard) - 1)) = Left$(theWildcard, (Len(theWildcard) - 1)) Then WildCard_Match = True
        End If
        '
    Else
    'if wild*card
        WildCard_Match = theName Like theWildcard
        '
    End If
       
End Function
'checks if the name contains the wildcard
« Last Edit: September 27, 2010, 01:43:48 PM by ben_fb »

eX.Pro

  • Enthusiast
  • ****
  • Posts: 54
  • I'm cool, I'm hot....I'm everything you're not.
Re: How To: Optimized Wildcard Checking Function
« Reply #1 on: September 26, 2010, 11:05:37 AM »
Good Work, ben.

i never heard of "Like" function.

Good Link:
Like Operator (Visual Basic)
http://msdn.microsoft.com/en-us/library/swf8kaxw.aspx

why not the simply right / left to detect the "*" and use instrB ? since the "like" function is slow..

eX.Pro, shut the fuck up, I'm removing your posts for a reason.
[17:23:19] <ZekcaH:0> im following u around the board removing ur posts eX.Pro
[17:23:22] <ZekcaH:0> its nearly a full time job

ben_fb

  • Addict
  • *****
  • Posts: 246
Re: How To: Optimized Wildcard Checking Function
« Reply #2 on: September 26, 2010, 11:10:17 AM »
Good Work, ben.

i never heard of "Like" function.

Good Link:
Like Operator (Visual Basic)
http://msdn.microsoft.com/en-us/library/swf8kaxw.aspx

why not the simply right / left to detect the "*" and use instrB ? since the "like" function is slow..


The like command is only used if the routine cant parse without it
This post has been thanked 1 time eX.Pro

pikachu

  • Administrator
  • Hero Member
  • *******
  • Posts: 3,344
Re: How To: Optimized Wildcard Checking Function
« Reply #3 on: September 26, 2010, 11:25:33 AM »
Wouldn't it be easier to just use regex?

http://www.regular-expressions.info/vb.html

eX.Pro

  • Enthusiast
  • ****
  • Posts: 54
  • I'm cool, I'm hot....I'm everything you're not.
Re: How To: Optimized Wildcard Checking Function
« Reply #4 on: September 26, 2010, 12:59:15 PM »
easier.. i doubt. but its a nice way of doing it. so much possibility.

http://www.regular-expressions.info/vbscriptexample.html
^Tester
http://msdn.microsoft.com/en-us/library/1400241x(VS.85).aspx
^Regular Expression Syntax (Scripting)

Code: [Select]
\d*.\d*.\d*.\d*.\d*:\dThis would match a proxy.

Code: [Select]
r+would be like doing a *r*

Code: [Select]
^p*this equal p*

Code: [Select]
o*$this equal *o

Most of the time it return true but its in fact a stupid nullstring.
« Last Edit: September 26, 2010, 01:34:38 PM by eX.Pro »

eX.Pro, shut the fuck up, I'm removing your posts for a reason.
[17:23:19] <ZekcaH:0> im following u around the board removing ur posts eX.Pro
[17:23:22] <ZekcaH:0> its nearly a full time job

pikachu

  • Administrator
  • Hero Member
  • *******
  • Posts: 3,344
Re: How To: Optimized Wildcard Checking Function
« Reply #5 on: September 26, 2010, 01:36:21 PM »
Quote
Code: [Select]
\d*.\d*.\d*.\d*.\d*:\dThis would match a proxy.

No it wouldn't, that would have an extra octet and match things like 999999.9999999999.9999999999999.99999999.999999:99999999 (obviously not a valid IP).

Quote
Code: [Select]
r+would be like doing a *r*

Again, no.  That would be one or more r characters.

Quote
Code: [Select]
^p*this equal p*

Again, no lol.  If you're using the ^, you need to end it with a $ to denote a string regex.  That would also be zero or more p characters.  Go read some more tutorials, there's a ton of documentation out there.

eX.Pro

  • Enthusiast
  • ****
  • Posts: 54
  • I'm cool, I'm hot....I'm everything you're not.
Re: How To: Optimized Wildcard Checking Function
« Reply #6 on: September 26, 2010, 01:40:16 PM »
Wouldn't it be easier to just use regex?

NO ITS NOT.

eX.Pro, shut the fuck up, I'm removing your posts for a reason.
[17:23:19] <ZekcaH:0> im following u around the board removing ur posts eX.Pro
[17:23:22] <ZekcaH:0> its nearly a full time job

pikachu

  • Administrator
  • Hero Member
  • *******
  • Posts: 3,344
Re: How To: Optimized Wildcard Checking Function
« Reply #7 on: September 26, 2010, 01:41:58 PM »
Hahaha, it would be if you knew regular expressions.  They're incredibly, incredibly powerful.

ben_fb

  • Addict
  • *****
  • Posts: 246
Re: How To: Optimized Wildcard Checking Function
« Reply #8 on: September 26, 2010, 11:09:44 PM »
All I read in that was 'more work more work more work' and then the rest became blurry.
Using left, right, and instrb comparisons are EXTREMELY fast any how.

pikachu

  • Administrator
  • Hero Member
  • *******
  • Posts: 3,344
Re: How To: Optimized Wildcard Checking Function
« Reply #9 on: September 27, 2010, 12:30:09 AM »
Do you have any benchmarks?  I'm just curious.

warrior

  • Sr. Member
  • ******
  • Posts: 409
  • Jesus Christ.
Re: How To: Optimized Wildcard Checking Function
« Reply #10 on: September 27, 2010, 05:18:39 AM »
Like comparisons are probably faster than Regex only because of their simplistic use in wildcard banning, but Regex's will always be much more flexible and powerful. So if you're doing pretty much anything more than a wildcard ban, go with a regex. In Superiority I'd use Like comparison for simple wildcards, then a cached Regex object for chat filters.
In capitalist America, bank robs you.

Choosing to code in an unmanaged language/platform is like choosing a hotel where you have to clean your own room.

When C++ is your hammer, everything starts to look like your thumb

pikachu

  • Administrator
  • Hero Member
  • *******
  • Posts: 3,344
Re: How To: Optimized Wildcard Checking Function
« Reply #11 on: September 27, 2010, 10:05:18 AM »
I'd still like to see some benchmarks of his function vs. VB6's Like operator.  I mean, have you actually proven your function is faster or did you just kind of assume it was?
This post has been thanked 1 time eX.Pro

ben_fb

  • Addict
  • *****
  • Posts: 246
Re: How To: Optimized Wildcard Checking Function
« Reply #12 on: September 27, 2010, 01:19:53 PM »
I looked into this and I was surprised with the results!

All test used the same data to compare, and were run sequenced 5,000,000 times. Each test was run three times and averaged. I tested with matches, and without -- both seemed to have same difference in speed.
(Option Compare Binary, Compiled)

Measured in seconds.

Comparing - Name*
Like: 6.15
Left$(): 5.5

Comparing - *Name*
Like: 6.1
InstrB(): 5.3

Comparing - *Name
Like: 6.1
Right$(): 5.6

Comparing - Na*me
Like: 5.6
Left$() = and Right$() =: 6.5

Seems Like is faster only when using Na*me, so I was incorrect on the last check, which is now fixed :D
Also checking AscW() seems to be faster even when combined with Right$().
« Last Edit: September 27, 2010, 01:45:00 PM by ben_fb »

warrior

  • Sr. Member
  • ******
  • Posts: 409
  • Jesus Christ.
Re: How To: Optimized Wildcard Checking Function
« Reply #13 on: September 27, 2010, 07:15:20 PM »
I advise against using microbenchmarks like these to guide you in programming. If I went by every microbenchmark, most of my projects would be overengineered gobs of language hacks. If the differences are that imperceptible, then it's probably best to use what gives the best cohesion.

Seems like a far cry from your "VERY slow" claim.
In capitalist America, bank robs you.

Choosing to code in an unmanaged language/platform is like choosing a hotel where you have to clean your own room.

When C++ is your hammer, everything starts to look like your thumb

pikachu

  • Administrator
  • Hero Member
  • *******
  • Posts: 3,344
Re: How To: Optimized Wildcard Checking Function
« Reply #14 on: September 27, 2010, 11:19:26 PM »
Yeah, it really only seems like you'd save about a microsecond if you're lucky.

ben_fb

  • Addict
  • *****
  • Posts: 246
Re: How To: Optimized Wildcard Checking Function
« Reply #15 on: September 28, 2010, 09:25:29 AM »
Those are also just simple test, more complex wildcards would result in much longer times. During a typical join using like wouldn't be a problem, but if a bot is getting flooded it could matter. Just like changing read location of a buffer rather than reshaping it only cuts off a fraction of time...yet people still do it. Fractions of time add up when it's important.

Edit below:
You have to also consider that I have a fairly fast machine, those with lesser specs will suffer more from poorly constructed code.
This post has been thanked 1 time eX.Pro
« Last Edit: September 28, 2010, 10:03:08 AM by ben_fb »

pikachu

  • Administrator
  • Hero Member
  • *******
  • Posts: 3,344
Re: How To: Optimized Wildcard Checking Function
« Reply #16 on: September 28, 2010, 09:46:33 AM »
That's a valid point.

warrior

  • Sr. Member
  • ******
  • Posts: 409
  • Jesus Christ.
Re: How To: Optimized Wildcard Checking Function
« Reply #17 on: September 28, 2010, 03:28:11 PM »
We should talk about the dangers of preoptimization, or optimizing before you know there's a problem. There's a fine line between thinking ahead, and insanity. I doubt even benchmarking it during a flood would bog down the cpu long enough to cause genuine unresponsiveness (an artifact of primitive languages with no background processing).
In capitalist America, bank robs you.

Choosing to code in an unmanaged language/platform is like choosing a hotel where you have to clean your own room.

When C++ is your hammer, everything starts to look like your thumb

 

newBalance by DzinerStudio