Attribute VB_Name = "StringSearch" '------------------------------------------------------------------------------- Option Explicit '------------------------------------------------------------------------------- Public Function FindStr( _ ByVal Tx As String, _ ByVal TxSought As String, _ ByVal Start As Byte) As Byte ' Returns the position of the first occurrence of string TxSought within ' string Tx, where the search begins at position "Start". ' ' If the search fails, the function returns 0. Otherwise the function returns ' a nonzero value. ' ' This function is similar to VB's InStr function, except that Start is Byte ' instead of Long, and Start is the last argument instead of the first. Dim LenTx As Byte Dim LenTxSought As Byte Dim N As Byte Dim A As Byte Dim AN As Byte Dim B As Byte Dim BN As Byte LenTx = RAMpeek(MemAddressU(Tx)) LenTxSought = RAMpeek(MemAddressU(TxSought)) '----------------------------- ' Check for pathological cases '----------------------------- ' Search fails if Tx is empty. If (LenTx = 0) Then GoTo NotFound ' If Tx is not empty, but TxSought is empty, always return Start. This ' emulates InStr's rather odd behavior. ElseIf (LenTxSought = 0) Then FindStr = Start Exit Function ' Search fails if starting point is illegal. ElseIf (Start < 1) Or (Start > LenTx) Then GoTo NotFound ' Search fails if there isn't enough room to search. ElseIf ((LenTx - Start + 1) < LenTxSought) Then GoTo NotFound End If '-------------- ' Normal search '-------------- ' Look at each character until either a match is found or we run out ' of characters. For N = Start To LenTx AN = N ' Look for a match For BN = 1 To LenTxSought A = RAMpeek(MemAddressU(Tx) + 1 + CuInt(AN)) B = RAMpeek(MemAddressU(TxSought) + 1 + CuInt(BN)) If (A <> B) Then GoTo ContinueSearch End If AN = AN + 1 Next ' If we get to this point, a match was found. FindStr = N Exit Function ContinueSearch: Next ' If we get to this point, the search failed. NotFound: FindStr = 0 End Function '------------------------------------------------------------------------------- Public Function InStr( _ ByVal Start As Byte, _ ByRef Tx As String, _ ByRef TxSought As String) As Byte ' Returns the position of the first occurrence of string TxSought within ' string Tx, where the search begins at position "Start". ' ' If the search fails, the function returns 0. Otherwise the function returns ' a nonzero value. ' ' This function emulates VB's InStr function. Dim LenTx As Byte Dim LenTxSought As Byte Dim N As Byte Dim A As Byte Dim AN As Byte Dim B As Byte Dim BN As Byte LenTx = RAMpeek(MemAddressU(Tx)) LenTxSought = RAMpeek(MemAddressU(TxSought)) '----------------------------- ' Check for pathological cases '----------------------------- ' Search fails if Tx is empty. If (LenTx = 0) Then GoTo NotFound ' If Tx is not empty, but TxSought is empty, always return Start. ElseIf (LenTxSought = 0) Then InStr = Start Exit Function ' Search fails if starting point is illegal. ElseIf (Start < 1) Or (Start > LenTx) Then GoTo NotFound ' Search fails if there isn't enough room to search. ElseIf ((LenTx - Start + 1) < LenTxSought) Then GoTo NotFound End If '-------------- ' Normal search '-------------- ' Look at each character until either a match is found or we run out ' of characters. For N = Start To LenTx AN = N ' Look for a match For BN = 1 To LenTxSought A = RAMpeek(MemAddressU(Tx) + 1 + CuInt(AN)) B = RAMpeek(MemAddressU(TxSought) + 1 + CuInt(BN)) If (A <> B) Then GoTo ContinueSearch End If AN = AN + 1 Next ' If we get to this point, a match was found. InStr = N Exit Function ContinueSearch: Next ' If we get to this point, the search failed. NotFound: InStr = 0 End Function '------------------------------------------------------------------------------- 'Public Sub Main() ' Uncomment this procedure for a test case. ' ' Dim Tx As String ' Dim TxSought As String ' Dim Index As Byte ' ' Debug.Print ' Debug.Print "FindStr function:" ' Debug.Print ' ' Tx = "Hello, world" ' TxSought = "world" ' ' Index = FindStr(Tx, TxSought, 1) ' ' Debug.Print " String to search: """; Tx; """" ' Debug.Print " String sought: """; TxSought; """" ' Debug.Print " Found at position: "; CStr(Index) ' ' Debug.Print ' Debug.Print "InStr function:" ' Debug.Print ' ' Tx = "Hello, world" ' TxSought = "or" ' ' Index = InStr(1, Tx, TxSought) ' ' Debug.Print " String to search: """; Tx; """" ' Debug.Print " String sought: """; TxSought; """" ' Debug.Print " Found at position: "; CStr(Index) ' 'End Sub '-------------------------------------------------------------------------------