Skip to content

Implement INSTR and other string functions. #39

Open

Description

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

Activity

  1. dmsc commented on Feb 28, 2021

    @dmsc
    Owner

    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!

  2. changed the title [-]instr[/-] [+]Implement INSTR and other string functions.[/+] on Mar 5, 2021
  3. dmsc commented on Mar 5, 2021

    @dmsc
    Owner

    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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions