This piece of code works like the M32lib InString, except that it returns the position of the char FOLLOWING the substring. Also, it can find a string even when it is split over two buffers, this can be useful when receiving WinSock data for example.

Parameters are like in InString, except the extra parameter pPos which points to a dword which is used to store the position at which to begin matching the substring (0 if no part of the substring was matched in a previous call, else a kind of StartPos for SubStr).

In short, just set the dword that pPos points to to zero and let it do its magic ;)

I realize the code sucks, optimizations are welcome! (Okay, okay, bug reports too...)



InStringEx proc StartPos:dword,pStr:dword,pSubStr:dword,pPos:dword

mov esi,pStr
add esi,StartPos

mov edi,pSubStr
mov eax,pPos
mov eax,[eax]
add edi,eax
dec edi

or eax,eax
jnz FindNextChars

inc edi
FindFirstChar:
mov al,[esi]
inc esi
mov dl,[edi]
cmp al,0
je NotFound
cmp al,dl
jne FindFirstChar
FindNextChars:
mov al,[esi]
inc esi
inc edi
mov dl,[edi]
cmp dl,0
je Found
cmp al,0
je NotFound
cmp al,dl
je FindNextChars
mov edi,pSubStr
NotFound:
mov ebx,pPos
sub edi,pSubStr
mov [ebx],edi
mov eax,-1
ret
Found:
mov eax,esi
sub eax,pStr
dec eax
ret

InStringEx endp

Posted on 2002-06-20 13:02:32 by Qweerdy
Ok, I should have tested it better :) This version works in all cases.



InStringEx proc uses esi edi StartPos:dword,pStr:dword,pSubStr:dword,pPos:dword

mov esi,pStr
add esi,StartPos

mov edi,pSubStr
mov eax,pPos
mov eax,[eax]
add edi,eax
dec edi

or eax,eax
jnz FindNextChars

inc edi
FindFirstChar:
mov al,[esi]
inc esi
mov dl,[edi]
cmp al,0
je NotFound
cmp al,dl
jne FindFirstChar
FindNextChars:
mov al,[esi]
inc esi
inc edi
mov dl,[edi]
cmp dl,0
je Found
cmp al,0
je NotFound
cmp al,dl
je FindNextChars
mov edi,pSubStr
jmp FindFirstChar
NotFound:
mov edx,pPos
sub edi,pSubStr
mov [edx],edi
mov eax,-1
ret
Found:
mov edx,pPos
mov dword ptr [edx],0
mov eax,esi
sub eax,pStr
dec eax
ret

InStringEx endp
Posted on 2002-06-23 00:44:40 by Qweerdy