InstrB

Zeul1

Newcomer
Joined
Nov 1, 2006
Messages
5
Location
Hampshire, UK
I am in the process of upgrading my VB6 app to .net I make extensive use of InstrB for byte searching within an array of bytes eg:

Dim test(3) as byte
test(2) = 1
test(3) = &HB3

r = instrB(offset, bArray, test)

Now i see there is no intrinsic equivalent in .net or is there?
Are there any functions that can be used for byte searching?

Many thanks
 
Please could you elaborate. Can IndexOf() be used to search for a byte sequence instead of one element? The test i ran always resulted in -1. For example in the first post i would be searching for a byte sequence of 00 00 01 B3, with variable start offsets.
 
IndexOf will only find a match for a single byte. We aren't really supposed to give code handouts here because it tends to discourage thinking, but I was bored so I whipped this up.
Visual Basic:
'This function searches by using the Array.IndexOf method to find a match for the first
'item in the search array and then looping through the rest of the search array testing for a match.
'This is repeated until either a match is found or the end of the array is reached.
Public Function SearchArray(Of T)(ByVal array As T(), ByVal search As T(), ByVal start As Integer) As Integer
    'In the event of empty or null arrays return no match
    If array Is Nothing Or search Is Nothing Or array.Length = 0 Or search.Length = 0 Then _
        Return -1
 
    'Looping variables
    Dim index As Integer = start - 1 'Where we will begin searching
    Dim match As Boolean = False 'Will be set to true when a match is found
 
    While Not match
        'Find the index where we have a match for the first item in the search array
        'starting from 'start' or after our last match
        index = System.Array.IndexOf(Of T)(array, search(0), index + 1)
        'A value of -1 means we have searched to the end of the array and found nothing
        If index = -1 Then _
            Return -1
 
        match = True 'Assume we have a match until we discover that we don't.
 
        'Loop through the search array from the second item to the last
        For searchIndex As Integer = 1 To search.Length - 1
            'If an item doesn't then abort the for loop and continue searching array for matches
            If Not (array(searchIndex + index).Equals(search(searchIndex))) Then
                match = False
                Exit For
            End If
        Next
    End While
 
    'If this code has been reached we have found a match. Return the index.
    Return index
End Function
 
'This overload provides a default index of 0 (we could also use an optional argument)
Public Function SearchArray(Of T)(ByVal array As T(), ByVal search As T()) As Integer
    Return SearchArray(Of T)(array, search, 0)
End Function
Usage is as follows:
Visual Basic:
'Search char array "MONKEYS" for "KEY"
Dim Data As Char() = {"M"c, "O"c, "N"c, "K"c, "E"c, "Y"c, "S"c}
Dim Search As Char() = {"K"c, "E"c, "Y"c}
MessageBox.Show( _
    SearchArray(Of Char)(Data, Search).ToString() _
)
 
Thanks i'll give it a try out. I wonder what sort of speed will be achieved compared to instrB? My arrays are normally upto 200,000 bytes

What a shame .net doesn't have anything built in.
 
I am the author of DVDPlanner which is a DVD Authoring Solution, so i am dealing with elementry video and audio streams. From the video perspective, as the video file is piped through it is continually scanned for Header details which determines pretty much everything in the mux. Now with native VB InstrB has proved to be very quick, and i was hoping for further speed improvements with .net. Because .net now has intrinsic bit handling ie [<< and >>], these areas should be much faster than using lookup tables as i do now. If i can't match or beat the speed of InstrB, though, then the whole port to .net will not be worth it. I have already converted all of the API to remove the 'As Any' that is no longer supported, so to hit a hurdle now is irritating.
 
I didn't go to great lengths to optimize the code I wrote. If you are looking for raw speed you might want to write your own new function. Just because the function is written in VB doesn't mean that it will be slow. You can use a tool like .Net Reflector to examine the compiled code and do some benchmark tests to tweak the code. You could even write the code in MSIL if you want to get the most out of your CPU cycles.
 
Back
Top