find$, hlhelp.chm:
mov pSubStr, find$(spos, source, sub_string)
Description
Search for a substring in a source string.
Parameters
1. spos The start position in the source string to search from.
2. source The source string to search.
3. sub_string The sub string to search for.
Return Value
The return value is the 1 based character index for the substring or ZERO if the substring is not found.
Comments
The spos parameter is a 1 based index so the initial search position should be set to 1 or higher.
The earlier macro name istring has been equated to the new name find$.
This macro is __UNICODE__ aware.
InString, masmlib.chm:
Error Values
If the function fails, the following error values apply.
-1 = substring same length or longer than main string
-2 = "StartPos" parameter out of range (less than 1 or greater than main string length)
The point is that the documentation for find$, which uses InString under the hood, does not mention the error codes.
I stumbled over this in a proggie using .if find$(1, haystack, needle)
The correct usage would be .if sdword ptr find$(1, haystack, needle)>0
Quote from: jj2007 on June 26, 2022, 09:53:46 PM
Error Values
If the function fails, the following error values apply.
-1 = substring same length or longer than main string
I'm wondering why the substring being the
same length would be considered an error. (Certainly it being longer should be an error.) Is this just the canonical (defined) behavior of
substr()? Seems perfectly logical to have a substring which is in fact exactly the same as the search string (which would be considered the "degenerate case", I guess).
Return value is indeed -1 :sad:
include \masm32\include\masm32rt.inc
.data
HelloW$ db "Hello World", 0
HelloW2$ db "Hello World", 0
.code
start:
invoke InString, 1, addr HelloW$, addr HelloW2$
MsgBox 0, cat$(str$(eax), " is the value"), offset HelloW$, MB_OK
exit
end start
In contrast, Instr_() (https://www.jj2007.eu/MasmBasicQuickReference.htm#Mb1153) returns +1.