Shop OBEX P1 Docs P2 Docs Learn Events
Dynamic Strings, Heap Manager, Formatted Output — Parallax Forums

Dynamic Strings, Heap Manager, Formatted Output

Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
edited 2014-07-21 21:02 in Propeller 1
In another thread there was a question about string functions. Awhile back I wrote an object for strings that some may find useful. It's probably not tested sufficiently to include in the object library yet, but perhaps some brave souls may wish to try it out anyway.

Attached is an archive containing a dynamic strings object, a heap manager object, and a C-like formatted output object. The top-level program uses these objects to generate nonsense English sentences derived from a context-free grammar.

For the next week or so, I'm swamped, so won't be able to offer much in the way of support. But leave any questions you may have in this thread, and I'll attempt to answer them when I get the chance.

Cheers!
Phil

Comments

  • Dave ScanlanDave Scanlan Posts: 160
    edited 2006-05-24 15:32
    Thanks, Phil.

    This is really needed.



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··
  • Kaos KiddKaos Kidd Posts: 614
    edited 2006-05-24 15:56
    Sweet...
    Thanks Phil!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Just tossing my two bits worth into the bit bucket


    KK
    ·
  • Ilsie19Ilsie19 Posts: 4
    edited 2014-07-19 23:10
    Hello Phil,
    Sorry for this reply on such old thread, but can i use the strings.spin file to split a string into segments?
    like: string = "Hello World, how are you doing?"

    after a split i need
    Hello
    World,
    how
    ...
    ...
  • ChrisGaddChrisGadd Posts: 310
    edited 2014-07-21 12:49
    It looks like strings.split should do it with, but darned if I can get it to work.

    Here's something I came up with:
    CON
      _clkmode = xtal1 + pll16x
      _xinfreq = 5_000_000
    
    OBJ
      FDS   : "FullDuplexSerial"
    
    VAR
      word  address[20]
      
    PUB Main | i, words
      FDS.start(31,30,0,115200)
      waitcnt(cnt + clkfreq)
      FDS.tx($00)
    
      words := parse(string("Hello World, how are you doing?"))  
                                                             
      waitcnt(cnt + clkfreq)
    
      repeat i from 0 to words - 1
        display(address[i])                                 ' The display method is useful if you're preserving    
        fds.tx($0D)                                         '  the original string with spaces intact
    
      fds.tx($0D)
    
      repeat i from 0 to words - 1
        fds.str(address[i])                                 ' The FullDuplexSerial str method can be used if 
        fds.tx($0D)                                         '  splitting the string with nulls
    
    PUB Display(stringPtr)
    {
     This method displays bytes in a string up to a space or a null
    }
      repeat until byte[stringPtr] == " " or byte[stringPtr] == $00
        FDS.tx(byte[stringPtr++])
    
    PUB parse(stringPtr) | i
    {
      This method stores the addresses of each first byte that follows a space, ignoring leading, trailing, and repeated spaces
      Returns the number of addresses stored 
    }
      i := 0
      if byte[stringPtr] <> " "                                                     ' Ignore leading spaces
        address[i++] := stringPtr                                                   ' Store the first address
      repeat
        if byte[stringPtr] == $00                                                   ' Repeat until the null-terminator
          return i                                                                  ' Return the number of words
        if byte[stringPtr] == " "                                                   ' Look for a space
          if byte[stringPtr + 1] <> " " and byte[stringPtr + 1] <> $00              ' Ignore repeated spaces and nulls
            address[i++] := stringPtr + 1                                           ' Store the address following a space
          byte[stringPtr] := $00                                                    ' Replace spaces with nulls to split the string
                                                                                    '  comment out to keep the string intact
        stringPtr++                                                                 
    
  • Ilsie19Ilsie19 Posts: 4
    edited 2014-07-21 21:02
    Thank you Chris
    Works like i wanted it
Sign In or Register to comment.