Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement INSTR and other string functions. #39

Open
ghost opened this issue Feb 27, 2021 · 3 comments
Open

Implement INSTR and other string functions. #39

ghost opened this issue Feb 27, 2021 · 3 comments

Comments

@ghost
Copy link

ghost commented Feb 27, 2021

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

@dmsc
Copy link
Owner

dmsc commented Feb 28, 2021

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!

@ghost
Copy link
Author

ghost commented Mar 2, 2021

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.

@dmsc dmsc changed the title instr Implement INSTR and other string functions. Mar 5, 2021
@dmsc
Copy link
Owner

dmsc commented Mar 5, 2021

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".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant