fastbasic icon indicating copy to clipboard operation
fastbasic copied to clipboard

Implement INSTR and other string functions.

Open ghost opened this issue 4 years ago • 3 comments

Does fast basic have anything similar to instr, instr$? could not figure out if it had this or now.

ghost avatar Feb 27 '21 00:02 ghost

Hi!

Does fast basic have anything similar to instr, instr$? could not figure out if it had this or now.

Not currently. Currently, FastBasic is more geared to game programming, where IMHO string searching is not used much, so I have not tough on adding it.

Do you have a specific use case? perhaps a simple loop could be all it is needed, it is not that slow:

A$ = "a big text with many words to search into"
B$ = "any"

for i=LEN(A$)-LEN(B$) to 1 step -1
  if B$ = A$[i,LEN(B$)] then exit
next

if i
  ? "Found at "; i
else
  ? "Not found"
endif

Have Fun!

dmsc avatar Feb 28 '21 00:02 dmsc

I am porting a text adventure game, and uses Instr, Mid$, Left$, Right$, and few other string related functions. I plan to simulate the function with either a USR call or subroutine like you have.

ghost avatar Mar 02 '21 00:03 ghost

Hi!

I am porting a text adventure game, and uses Instr, Mid$, Left$, Right$, and few other string related functions. I plan to simulate the function with either a USR call or subroutine like you have.

With current FastBasic (git version), you could write something like this (this is like C inside Basic):

A$ = "a big text with many words to search into"
B$ = "any"
i=0

EXEC Instr &A$, &B$, &i

if i
  ? "Found at "; i
  ? "->"; A$[i]
else
  ? "Not found"
endif

' Parameters:
'  StrBase: address of string to search into
'  StrSearch: address of string to find
'  Return: address of integer variable with the result
PROC Instr StrBase StrSearch StrReturn
  for StrPos=LEN($(StrBase))-LEN($(StrSearch)) to 1 step -1
    if $(StrSearch) = $(StrBase)[StrPos,LEN($(StrSearch))] then Exit
  next
  dpoke StrReturn, StrPos
ENDPROC

Note that "&VAR" is the same as "ADR(VAR)", and $(address) is the string at address. This means that " $(&(A$))" is the same as "A$", and "& $(X)" is the same as "X".

dmsc avatar Mar 05 '21 13:03 dmsc