Author Topic: find$, InString documentation  (Read 523 times)

jj2007

  • Member
  • *****
  • Posts: 13302
  • Assembly is fun ;-)
    • MasmBasic
find$, InString documentation
« on: June 26, 2022, 09:53:46 PM »
find$, hlhelp.chm:
Code: [Select]
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:
Code: [Select]
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

NoCforMe

  • Member
  • ****
  • Posts: 855
Re: find$, InString documentation
« Reply #1 on: July 23, 2022, 08:26:02 AM »
Code: [Select]
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).

jj2007

  • Member
  • *****
  • Posts: 13302
  • Assembly is fun ;-)
    • MasmBasic
Re: find$, InString documentation
« Reply #2 on: July 23, 2022, 08:45:07 AM »
Return value is indeed -1 :sad:

Code: [Select]
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_() returns +1.