The MASM Forum

Projects => MASM32 => Topic started by: jj2007 on June 26, 2022, 09:53:46 PM

Title: find$, InString documentation
Post by: jj2007 on June 26, 2022, 09:53:46 PM
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
Title: Re: find$, InString documentation
Post by: NoCforMe on July 23, 2022, 08:26:02 AM
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).
Title: Re: find$, InString documentation
Post by: jj2007 on July 23, 2022, 08:45:07 AM
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.
Title: Re: find$, InString documentation
Post by: jj2007 on January 25, 2024, 12:07:44 AM
Inspired by this recent thread (https://masm32.com/board/index.php?topic=11634.0), I checked the docs for a reverse InString, i.e. one that starts the search from the end.

Apparently, there is no InstrRev in the Masm32 SDK, so I coded one in pure Masm32 code (MasmBasic already has Rinstr (https://www.jj2007.eu/MasmBasicQuickReference.htm#Mb1154)).

Attached also a case-insensitive version called InStringRevCi.

Usage examples:
start:
  invoke InStringRev, chr$("Where is the needle in the haystack?"), chr$("needle")
  print str$(eax), " is the rev position", 13, 10
  invoke InStringRevCi, chr$("Where is the Needle in the haystack?"), chr$("nEEDLE")
  print str$(eax), " is the rCi position", 13, 10
  invoke InString, 1, chr$("Where is the needle in the haystack?"), chr$("needle")
  print str$(eax), " is the fwd position", 13, 10

I put this in an old thread because there is no better place for adding features to the Masm32 SDK, and the find$() thread may already be found by n00bs.
Title: Re: find$, InString documentation
Post by: TimoVJL on January 25, 2024, 12:32:51 AM
StrRStrIA function (shlwapi.h) (https://learn.microsoft.com/en-us/windows/win32/api/shlwapi/nf-shlwapi-strrstria)
Title: Re: find$, InString documentation
Post by: HSE on January 25, 2024, 02:45:42 AM
Also there is platform independent functions StrEndsWithA/W in ObjMem libraries.
Title: Re: find$, InString documentation
Post by: jj2007 on January 25, 2024, 03:23:42 AM
Great :thumbsup:
AMD Athlon Gold 3150U with Radeon Graphics
58 ms for findRevCi$,   pos=14
5772 ms for StrRStrIA,  pos=14
49 ms for findRevCi$,   pos=14
5773 ms for StrRStrIA,  pos=14
36 ms for findRevCi$,   pos=14
5713 ms for StrRStrIA,  pos=14