The MASM Forum

Projects => MASM32 => Topic started by: jj2007 on December 12, 2013, 04:27:32 AM

Title: find$() returns -1 if strings are identical
Post by: jj2007 on December 12, 2013, 04:27:32 AM
Not sure if this is intentional, but hlhelp.chm doesn't document this behaviour:
include \masm32\include\masm32rt.inc

.code
start:
mov esi, chr$("Masm32:")
print "esi=", 9, 9, 9, 9
print esi, 13, 10
print "find$(1, esi, 'Masm32:')=", 9
push find$(1, esi, "Masm32:")
print str$(eax), 13, 10
pop eax
exit eax
end start


Output:
esi=                            Masm32:
find$(1, esi, 'Masm32:')=       -1


MasmBasic Instr_(esi, "Masm32:") (http://www.webalice.it/jj2006/MasmBasicQuickReference.htm#Mb1153) would return 1, and that seems to be a standard BASIC behaviour; but C/C++ might be different, of course.

I also had a weird problem with exit eax, being translated to invoke ExitProcess, 0 (with plain Masm32rt.inc), but I can't reproduce it now that I am on the other puter. Will check tomorrow if it's the same macros.asm on both machines 8)
Title: Re: find$() returns -1 if strings are identical
Post by: MichaelW on December 12, 2013, 05:53:39 AM
The documentation for InString, called by find$, is in masmlib.chm under Zero Terminated String Functions.
Title: Re: find$() returns -1 if strings are identical
Post by: jj2007 on December 12, 2013, 06:47:45 AM
Thanks, Michael.

QuoteError Values

If the function fails, the following error values apply.
-1 = substring same length or longer than main string

There is an overview for Find$ at Wikipedia (http://en.wikipedia.org/wiki/Comparison_of_programming_languages_%28string_functions%29#Find), but no info about the special case of identical strings.

GfaBasic and MasmBasic return 1. There is apparently no Instr() in C, but this workaround returns 0:
  char* s1="abcd";
  char* s2="abcd";
  char* result=strstr(s1, s2);
  int res=result-s1;
  printf("Instr identical=%i\n", res);
Title: Re: find$() returns -1 if strings are identical
Post by: MichaelW on December 12, 2013, 02:52:06 PM
In addition to strstr I also tested FreeBASIC, QuickBASIC, and PB3 and they all returned 1, but given the problem with breaking existing code I doubt that this behavior is going to change.