[futurebasic] Re: [FB] String <=> Handle

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : September 1999 : Group Archive : Group : All Groups

From: Jay Reeve <jktr@...>
Date: Sun, 12 Sep 99 02:29:06 -0500
>I modified your code a little to do more specifically what I wanted --
>which was to create two functions that: 1) If given a string, would return
>a handle; 2) if given a handle, would return a string.
>
>You might want to review the following code to see if I blundered.

Looks good to me. There are a couple of insignificant changes I would 
probably make, just because I'm passionate about tight code. (If you 
don't make the changes, I guarantee you'll never know the difference.)

1. If FN NEWHANDLE(0) fails (hard to imagine), theHndl& will already be 
_nil, so you don't really need
  XELSE
    theHndl& = _nil

2. As a matter of course I prefer to use
       POKE @theStr$,0
instead of 
      theStr$ = ""
because it is faster, even though that often doesn't matter.

3. I would avoid a couple of lines and duplicate variable names by 
eliminating
    theStr& = @theStr$
Just use @theStr$ where you have theStr&.

Here are the FNs with my edits. I've not tested them.
'--------------------------

LOCAL FN str2Hndl(@theStr&)
  DIM size
  DIM err
  DIM theHndl&

  theHndl& = FN NEWHANDLE(0)
  LONG IF theHndl&
    size = PEEK(theStr&)
    err = FN SETHANDLESIZE(theHndl&,size)
    BLOCKMOVE theStr&+1,[theHndl&],size
  END IF

END FN = theHndl&

'--------------------------

LOCAL FN hndl2Str$(theHndl&)
  DIM size
  DIM theStr$

  LONG IF theHndl&
    size = FN GETHANDLESIZE(theHndl&)
    IF size > 255 THEN size = 255
    POKE @theStr$, size
    BLOCKMOVE [theHndl&],@theStr$+1,size
  XELSE
    POKE @theStr$, 0
  END IF

END FN = theStr$

'--------------------------