PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] Writing your own InStr function-VBForums
Results 1 to 31 of 31

Thread: [RESOLVED] Writing your own InStr function

  1. #1

    Thread Starter
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Resolved [RESOLVED] Writing your own InStr function

    Howdy all,

    I'm wanting to write my own InStr function, from scratch. I know, you all think it's pointless, but I'd really like to see if it's possible to write some of the in built functions, in VB itself. I'm totally stumped on InStr. Anyone have a way of writing an InStr function?

    TIA

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  2. #2
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Writing your own InStr function

    Well, it seems like you are doing it to challange yourself, so I would keep trying if I were you. I like to look around in the Object Browser when I am stumped. But if you want some help try applything this:

    Use a for loop to search the string and use the Mid() function to compare things. Use the object browser to get the syntax for the Mid() function.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  3. #3
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Writing your own InStr function

    And if you want to go to an even more basic level, use left and right to analyze mid


    Has someone helped you? Then you can Rate their helpful post.

  4. #4
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Writing your own InStr function

    chemicalNova,

    You can but it is a waste. I have done it and it is remarkably slow. You would do better to look at the bytes yourself HAHA.

    Not worth the trouble...

  5. #5
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Writing your own InStr function

    I can't see it taking that long? Well, I guess that totally depends on how long the string is. What kind of length are you talking about randem?
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  6. #6
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Writing your own InStr function

    eyeRmonkey,

    Well, If you are writing a function you have to be open minded on the length or what would be the point? The whole idea of re-writing a VB function would be basically for speed. Of course short strings would be relatively ok, but large string would burden down the computer.

  7. #7

    Thread Starter
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Writing your own InStr function

    I honestly can't get it. Would one of you give me a quick example just to chuck into my knowledge base?

    Ta in advance

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  8. #8
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Writing your own InStr function

    chemicalNova,

    When creating an Instr function you need to take into account UniCode strings also. But aside from that let start with a basic string. What you need to do is to copy the bytes into a byte array then, search looping through the byte array for what you are looking for.

    If you have any assembler background this will be a simple task to search for the sequence of bytes in the byte array that you want to find. But basically what you have to do is to search for the first byte of the string you want to find then check each nyte after that until you get a complete match. If you do not you must start with the first byte of your search string again and resume searching after the last found match on the first byte.

    Have I confused you yet...

  9. #9
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,425

    Re: Writing your own InStr function

    For example:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     MsgBox My_InStr("This is a test", "S", True)
    5. End Sub
    6.  
    7. Private Function My_InStr(ByVal strInput As String, ByVal strFind As String, Optional blnCaseSensitive As Boolean) As Long
    8. Dim lngIdx As Long
    9. Dim lngCount As Long
    10.  
    11.     'Capture the number of characters
    12.     lngCount = Len(strInput)
    13.  
    14.     If blnCaseSensitive = True Then
    15.         'A Case Sensitve search is required
    16.         For lngIdx = 1 To lngCount
    17.             If Asc(Mid$(strInput, lngIdx, 1)) = Asc(strFind) Then
    18.                 My_InStr = lngIdx
    19.                 Exit For
    20.             End If
    21.         Next
    22.     Else
    23.         'Case Sensitivity is irrelevant
    24.         For lngIdx = 1 To lngCount
    25.             If LCase(Mid$(strInput, lngIdx, 1)) = LCase(strFind) Then
    26.                 My_InStr = lngIdx
    27.                 Exit For
    28.             End If
    29.         Next
    30.     End If
    31.  
    32. End Function




    Bruce.
    Last edited by Bruce Fox; Jul 6th, 2005 at 05:57 PM.
    Handy References:
    MSDN Library
    ADO Tutorial Excel Tutorial MZTools (VB6)

    • Please pull down the Thread Tools menu and click the 'Mark Thread Resolved' button, or alternately edit your original post and add "Resolved" or place a in the subject when your question(s) have been answered.
    • Please use code tags [highlight=vb] your code goes here between the tags [/highlight] when posting code.

  10. #10
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Writing your own InStr function

    VB Code:
    1. Function InStr2(Start As Integer, Str1 As String, Str2 As String) As Integer
    2.     Dim i As Integer
    3.    
    4.     For i = Start To Len(Str1)
    5.         If Mid$(Str1, i, Len(Str2)) = Str2 Then
    6.             InStr2 = i
    7.             Exit For
    8.         End If
    9.     Next
    10. End Function


    Has someone helped you? Then you can Rate their helpful post.

  11. #11
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Writing your own InStr function

    Bruce, that only compares the first character... If you search for "st" in your example, it returns 4 again (just like "s")...


    Has someone helped you? Then you can Rate their helpful post.

  12. #12

    Thread Starter
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Writing your own InStr function

    Thanks for the reply Bruce.

    I had something similar to that, but I was hoping it could accept any length of find string. IE:
    VB Code:
    1. My_InStr("This is a test", "test", True)
    Anyone? My head is exploding..

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  13. #13

    Thread Starter
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Writing your own InStr function

    Wow. I had no idea it would be so simple. Meh, its 9.02am, and I've been up all night. Thanks a heap manavo

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  14. #14
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,425

    Re: Writing your own InStr function

    Yep, my bad
    Handy References:
    MSDN Library
    ADO Tutorial Excel Tutorial MZTools (VB6)

    • Please pull down the Thread Tools menu and click the 'Mark Thread Resolved' button, or alternately edit your original post and add "Resolved" or place a in the subject when your question(s) have been answered.
    • Please use code tags [highlight=vb] your code goes here between the tags [/highlight] when posting code.

  15. #15
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Using an array like this?

    VB Code:
    1. Function InStr3(Start As Integer, Str1 As String, Str2 As String) As Integer
    2.     Dim i As Integer
    3.     Dim Ar() As String
    4.     Dim iPos As Integer
    5.    
    6.     Ar = Split(Str1, Str2)
    7.    
    8.     i = 0
    9.    
    10.     Do
    11.         iPos = iPos + Len(Ar(i)) + 1
    12.         i = i + 1
    13.     Loop While iPos <= Len(Str1) And iPos <= Start
    14.    
    15.     InStr3 = iPos
    16. End Function

    Edit: Bruce, where's you post?


    Has someone helped you? Then you can Rate their helpful post.

  16. #16
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,425

    Re: [RESOLVED] Writing your own InStr function

    Ok, I had to redeem myself.

    Case Sensitive and Start position (thanks Manavo11) options:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     MsgBox My_InStr("This iS a test", "iS", , True)
    5. End Sub
    6.  
    7. Private Function My_InStr(ByVal strInput As String, ByVal strFind As String, Optional intStartPos As Integer, Optional blnCaseSensitive As Boolean) As Integer
    8. Dim intIdx As Integer
    9. Dim intCount As Integer, intStart As Integer, intLength As Integer
    10.  
    11.     If intStartPos > 0 Then
    12.         intStart = intStartPos
    13.     Else
    14.         intStart = 1
    15.     End If
    16.  
    17.     'Capture the number of characters of the input text string
    18.     intCount = Len(strInput)
    19.  
    20.     'Capture the number of characters of the find string
    21.     intLength = Len(strFind)
    22.  
    23.     If blnCaseSensitive = True Then
    24.         'A Case Sensitve search is required
    25.         For intIdx = intStart To intCount
    26.             If Mid$(strInput, intIdx, intLength) = strFind Then
    27.                 My_InStr = intIdx
    28.                 Exit For
    29.             End If
    30.         Next
    31.     Else
    32.         'Case Sensitivity is irrelevant
    33.         For intIdx = intStart To intCount
    34.             If LCase(Mid$(strInput, intIdx, intLength)) = LCase(strFind) Then
    35.                 My_InStr = intIdx
    36.                 Exit For
    37.             End If
    38.         Next
    39.     End If
    40.  
    41. End Function




    Bruce.
    Last edited by Bruce Fox; Jul 6th, 2005 at 06:30 PM.
    Handy References:
    MSDN Library
    ADO Tutorial Excel Tutorial MZTools (VB6)

    • Please pull down the Thread Tools menu and click the 'Mark Thread Resolved' button, or alternately edit your original post and add "Resolved" or place a in the subject when your question(s) have been answered.
    • Please use code tags [highlight=vb] your code goes here between the tags [/highlight] when posting code.

  17. #17
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,425

    Re: [RESOLVED] Writing your own InStr function

    Nice examples Manavo




    Bruce.
    Handy References:
    MSDN Library
    ADO Tutorial Excel Tutorial MZTools (VB6)

    • Please pull down the Thread Tools menu and click the 'Mark Thread Resolved' button, or alternately edit your original post and add "Resolved" or place a in the subject when your question(s) have been answered.
    • Please use code tags [highlight=vb] your code goes here between the tags [/highlight] when posting code.

  18. #18
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: [RESOLVED] Writing your own InStr function

    When I have holidays, nothing to do and it's 2:30 AM I'm the most productive


    Has someone helped you? Then you can Rate their helpful post.

  19. #19
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: [RESOLVED] Writing your own InStr function

    I got bored and made my own. I hadn't look at the thread until just now and the examples already posted blows mine out of the water, but here it is anyway:

    VB Code:
    1. ''''''''''''''''''''''''''''''''''''
    2. ' Date:           7/6/2005         '
    3. ' Date Modified:  7/6/2006         '
    4. ' Author :        Jeremy Blanchard '
    5. ''''''''''''''''''''''''''''''''''''
    6.  
    7.  
    8. Option Explicit
    9.  
    10. Public Function MyInStr(ByVal StringCheck As String, _
    11.                         ByVal StringMatch As String, _
    12.                Optional ByVal Start As Long = 1) As Long
    13.   Dim i As Long
    14.   Dim lngStrCheckLen As Long
    15.   Dim lngStrMatchLen As Long
    16.   Dim strCurrent As String
    17.  
    18.   lngStrCheckLen = Len(StringCheck)
    19.   lngStrMatchLen = Len(StringMatch)
    20.  
    21.   MsgBox "Check Length: " & lngStrCheckLen
    22.   MsgBox "Match Length: " & lngStrMatchLen
    23.  
    24.   If lngStrCheckLen <= 0 Then GoTo endIt
    25.   If lngStrMatchLen <= 0 Then GoTo endIt
    26.  
    27.   For i = Start To lngStrCheckLen - 1
    28.     strCurrent = Mid(StringCheck, i, lngStrMatchLen)
    29.     If strCurrent = StringMatch Then
    30.       MyInStr = i
    31.       Exit Function
    32.     End If
    33.   Next i
    34.  
    35. endIt:
    36.   MyInStr = -1
    37. End Function

    I'll have to add something for case sensitivity.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  20. #20
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,651

    Re: [RESOLVED] Writing your own InStr function

    Hmm, I couldn't resist trying my own API version
    VB Code:
    1. Private Declare Function lstrlenW Lib "kernel32" _
    2.     (ByRef lpszString As Long) _
    3.     As Long
    4.  
    5. Private Declare Sub RtlMoveMemory Lib "ntdll.dll" _
    6.     (ByRef lpvDest As Any, _
    7.      ByRef lpvSrc As Any, _
    8.      ByVal cbLen As Long)
    9.  
    10. Private Declare Function RtlCompareMemory Lib "ntdll.dll" _
    11.     (ByRef lpvSource1 As Any, _
    12.      ByRef lpvSource2 As Any, _
    13.      ByVal cbLen As Long) _
    14.     As Long
    15. '
    16.  
    17. Public Function InTheString(ByVal lpszStringToSearch As Long, _
    18.                             ByRef pstrSearchFor As String, _
    19.                             Optional ByVal plngStartPos As Long = 1) _
    20.                            As Long
    21.  
    22. Dim charBuffer()    As Byte
    23. Dim lngStringLen    As Long
    24. Dim lngString2Len   As Long
    25. Dim i               As Long
    26.  
    27.     lngStringLen = lstrlenW(lpszStringToSearch) * 2
    28.     lngString2Len = Len(pstrSearchFor) * 2
    29.  
    30.     ReDim charBuffer(lngStringLen)
    31.    
    32.     RtlMoveMemory charBuffer(0), _
    33.                   ByVal lpszStringToSearch, _
    34.                   lngStringLen
    35.  
    36.     If (lngString2Len < lngStringLen) Then
    37.         For i = (plngStartPos - 1) To (lngStringLen - lngString2Len)
    38.             If (RtlCompareMemory(charBuffer(i), _
    39.                                  ByVal StrPtr(pstrSearchFor), _
    40.                                  lngString2Len _
    41.                                 ) = lngString2Len) Then
    42.                 InTheString = (i \ 2) + plngStartPos
    43.                 Exit Function
    44.             End If
    45.         Next i
    46.     End If
    47.  
    48.     InTheString = -1
    49. End Function
    Last edited by penagate; Jul 7th, 2005 at 10:11 AM.

  21. #21
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: [RESOLVED] Writing your own InStr function

    Quote Originally Posted by manavo11
    Using an array like this?

    VB Code:
    1. Function InStr3(Start As Integer, Str1 As String, Str2 As String) As Integer
    2.     Dim i As Integer
    3.     Dim Ar() As String
    4.     Dim iPos As Integer
    5.    
    6.     Ar = Split(Str1, Str2)
    7.    
    8.     i = 0
    9.    
    10.     Do
    11.         iPos = iPos + Len(Ar(i)) + 1
    12.         i = i + 1
    13.     Loop While iPos <= Len(Str1) And iPos <= Start
    14.    
    15.     InStr3 = iPos
    16. End Function

    Edit: Bruce, where's you post?
    This actually has a mistake. In the loop. But this seems to work correct :

    VB Code:
    1. Function InStr3(Start As Integer, Str1 As String, Str2 As String) As Integer
    2.     Dim i As Integer
    3.     Dim Ar() As String
    4.     Dim iPos As Integer
    5.         Ar = Split(Str1, Str2)
    6.    
    7.     i = 0
    8.    
    9.     Do
    10.         iPos = iPos + Len(Ar(i)) + Len(Str2)
    11.         i = i + 1
    12.     Loop While iPos <= Len(Str1) And iPos <= Start
    13.    
    14.     iPos = iPos - 1
    15.    
    16.     InStr3 = iPos
    17. End Function


    Has someone helped you? Then you can Rate their helpful post.

  22. #22
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: [RESOLVED] Writing your own InStr function

    The best way is a byte array and the API function as Penagate states. It is the fastest. I have written one with the API and it is great!

  23. #23
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: [RESOLVED] Writing your own InStr function

    For some reason, the function Penagate posted doesn't work. It is always giving me -1 whatever I try to give as input value. It seems to calculate the length of the string all wrong.


    Edit Got it working after some changes. It is kind of slow, I've made a faster function. Though it is still in need of some fixes and it is meant for byte arrays, not strings.
    Last edited by Merri; Jul 7th, 2005 at 06:32 AM.

  24. #24
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,651

    Re: [RESOLVED] Writing your own InStr function

    Sorry, I thought it worked. I declared lstrlenW wrong. Here is the correct version.

    VB Code:
    1. Private Declare Function lstrlenW Lib "kernel32" _
    2.     (ByVal lpszString As Long) _
    3.     As Long


  25. #25
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,651

    Re: [RESOLVED] Writing your own InStr function

    Also, there is a mistake in this line which you see if you use the Starting possition parameter.
    It should be:
    VB Code:
    1. InTheString = (i \ 2) + plngStartPos

    Not my day is it

  26. #26
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: [RESOLVED] Writing your own InStr function

    Still giving me -1 when I do the changes. When I changed it to use strings, it worked but it isn't all that fast.

  27. #27
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,651

    Re: [RESOLVED] Writing your own InStr function

    No, it wasn't meant to pass strings around. The point was to do a direct memory read on the exisiting string, instead of wasting time copying it back and forward.

    It works for me. How did you call it?

    VB Code:
    1. ?InTheString(StrPtr("What a very long but not long at all string!"), "very")

  28. #28
    Fanatic Member wildcat_2000's Avatar
    Join Date
    Nov 2000
    Location
    Italy
    Posts
    727

    Re: [RESOLVED] Writing your own InStr function

    well,

    i believe this can be worthwhile only if you use bytearrays to try and improve speed of such a function.

    i have some code about that if you want.

    cheers,

    wc.
    When your car breaks down,
    close all windows and retry

    => please rate all users posts! <=

  29. #29
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,651

    Re: [RESOLVED] Writing your own InStr function

    Mine uses a byte array

    Anyway, if you use API memory functions, it doesn't matter how you store the data. It's all the same.
    Last edited by penagate; Jul 7th, 2005 at 10:24 AM.

  30. #30
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: [RESOLVED] Writing your own InStr function

    VB Code:
    1. Result = InTheString(StrPtr(Buffer), KeyWord, 1)

    This is the way I call it. Buffer is defined as a string, I've loaded a file into it. KeyWord is a string as well. The code is exactly the same used with InStr to test it's speed. The problem lies in the lstrlenW line, because it always returns 5 for some reason. And no, there is no null characters in the string.

    Btw, shouldn't your code have Step 2 in the For Loop?


    Fixed function that works for me:
    VB Code:
    1. Public Function InTheString(ByRef lpszStringToSearch As Long, _
    2.                             ByRef lngStringLen As Long, _
    3.                             ByRef pstrSearchFor As String, _
    4.                             Optional ByVal plngStartPos As Long = 1) _
    5.                            As Long
    6.  
    7. Dim charBuffer()    As Byte
    8. Dim lngString2Len   As Long
    9. Dim i               As Long
    10.  
    11.     lngString2Len = LenB(pstrSearchFor)
    12.  
    13.     ReDim charBuffer(lngStringLen - 1)
    14.    
    15.     RtlMoveMemory charBuffer(0), _
    16.                   ByVal lpszStringToSearch, _
    17.                   lngStringLen
    18.  
    19.     If (lngString2Len < lngStringLen) Then
    20.         For i = (plngStartPos + plngStartPos - 2) To (lngStringLen - lngString2Len) Step 2
    21.             If (RtlCompareMemory(charBuffer(i), _
    22.                                  ByVal StrPtr(pstrSearchFor), _
    23.                                  lngString2Len _
    24.                                 ) = lngString2Len) Then
    25.                 InTheString = (i \ 2) + 1
    26.                 Exit Function
    27.             End If
    28.         Next i
    29.     End If
    30.  
    31.     InTheString = -1
    32. End Function

    The way called:
    Result = InTheString(StrPtr(TextToSearch), LenB(TextToSearch), SearchKeyword, StartPosition)
    Last edited by Merri; Jul 8th, 2005 at 02:40 AM.

  31. #31
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: [RESOLVED] Writing your own InStr function

    Here we have a function to work with byte arrays:
    - supports both ANSI and Unicode byte arrays (easy and fast to work with both text files and converted strings)
    - two times faster in ANSI mode (not so suprising)
    - beats InStr in TextCompare
    - about four times slower than InStr in BinaryCompare
    - benchmarking results vary depending on the keyword length: InBArr gets slower with longer keywords when compared to native InStr
    - about 11 times faster than InTheString

    Some test results (compiled, all advanced optimizations turned on):
    InStr, vbBinaryCompare: 2 ms
    InBArr, vbBinaryCompare: 8 ms
    InStr, vbTextCompare: 30 ms
    InBArr, vbTextCompare: 9 ms
    InTheString: 90 ms

    Testfile: c:\hotfix.txt
    Keyword: "PARTICULAR"

    I'll also fix my InBArrRev function before I release the benchmarking program, at the moment it supports ANSI strings only.

    Edit Removed the code as there were bugs; you can find the code at Planet Source Code (link in my signature).
    Last edited by Merri; Jul 8th, 2005 at 08:53 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width