News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

find$, InString documentation

Started by jj2007, June 26, 2022, 09:53:46 PM

Previous topic - Next topic

jj2007

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

NoCforMe

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).
Assembly language programming should be fun. That's why I do it.

jj2007

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_() returns +1.

jj2007

Inspired by this recent thread, 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).

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.


HSE

Also there is platform independent functions StrEndsWithA/W in ObjMem libraries.
Equations in Assembly: SmplMath

jj2007

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