Hi I needed a string searching routine which was capapble of doing case sensitive and insentive searches as well as inclusive/exclusive searches
I've modified the masm32 InString function to this end...
Please take it to the garage for a tuneup:)
I've modified the masm32 InString function to this end...
Please take it to the garage for a tuneup:)
Here is one I wrote a while ago:
Return value is the same as C's string.h strstr(): if needle is found in haystack, its position is returned. Otherwise, 0 is the return value.
strstr proc pszHaystack:PTR BYTE, pszNeedle:PTR BYTE, dwCaseSensitive:DWORD
push esi
push edi
xor eax, eax
xor ecx, ecx
xor edx, edx
mov esi, [pszHaystack]
mov edi, [pszNeedle]
@@char: mov al, byte ptr [esi]
mov ah, byte ptr [edi]
cmp [dwCaseSensitive], 0
jne @@cmp1
cmp al, "Z"
ja @@cmp0
cmp al, "A"
jb @@cmp0
add al, 32
@@cmp0: cmp [dwCaseSensitive], 0
jne @@cmp1
cmp ah, "Z"
ja @@cmp1
cmp ah, "A"
jb @@cmp1
add ah, 32
@@cmp1: cmp al, ah
jne @@next
inc ecx
mov eax, esi
inc edi
inc esi
cmp byte ptr [esi], 0
je @@quit
inc eax
cmp byte ptr [edi], 0
je @@quit
jmp @@char
@@next: mov edi, [pszNeedle]
test ecx, ecx
setz dl
add esi, edx
xor ecx, ecx
xor eax, eax
cmp byte ptr [esi], 0
jne @@char
@@quit: sub eax, ecx
pop edi
pop esi
ret
strstr endp
Return value is the same as C's string.h strstr(): if needle is found in haystack, its position is returned. Otherwise, 0 is the return value.
MArtial_Code, I am just wondering what inclusive and exclusive mean in your code?
comrade here's the distinction between inclusive and exclusive search:
Say we are searching for "comrade" ;) in the string
"comrade this is an example in the spirit of comraderie"
An inclusive search will find two instances of comrade but an exclusive search(i.e white space must surround the search word) will only find one.
Say we are searching for "comrade" ;) in the string
"comrade this is an example in the spirit of comraderie"
An inclusive search will find two instances of comrade but an exclusive search(i.e white space must surround the search word) will only find one.
aka "Whole Words Only" search :)
If you can only specify one whitespace character, what's the advantage over just adding spaces to the search string :confused:
MArtial_Code,
Just be careful with a search pattern that relies on a blank either side of it to do whole word search as the word can be at the beginning or end of the line which involves ascii 13 and 10 as well. If you search for the word with a space either side of it as the pattern, you will miss the ones at the beginning or end of the line.
Regards,
hutch@movsd.com
PS : Just thinking about it, just do a scan replace of both ascii 13 and 10, append a space at the end of the text then use the method you have designed. It should work in every case.
Just be careful with a search pattern that relies on a blank either side of it to do whole word search as the word can be at the beginning or end of the line which involves ascii 13 and 10 as well. If you search for the word with a space either side of it as the pattern, you will miss the ones at the beginning or end of the line.
Regards,
hutch@movsd.com
PS : Just thinking about it, just do a scan replace of both ascii 13 and 10, append a space at the end of the text then use the method you have designed. It should work in every case.
The routine counts spaces, tabs, carriage return,linefeed, and another character which you specify as white space, plus it checks for the start and end of main string... I think I've got all bases covered:cool: