Newbie question - string buffers
ramman345
Posts: 24
Hey guys, I'm making a transition from the Javelin and I'm curious about the best way to do string buffers on the Propeller. The manual says to use the DAT block to define buffers but I'm just not sure on the exact syntax to declare a string buffer, then to append to the buffer. Also, anybody got a good "indexOf" string search?
Josh
Josh
Comments
To append to an existing string in the array "X", you'd first find the current length of the string using STRSIZE(@X), then copy the new string to the end of the existing one like:
L := STRSIZE(@X) ' Get length of existing piece
N := STRSIZE(@Y) ' Get length of new piece
BYTEMOVE(@X+L,@Y,N+1) ' Copy new piece plus its terminator
Note that I've included no checking for too long a result string.
One more question, how do you append a constant string to that? Suppose you want to append "World" to string X.
Josh
BYTEMOVE(@X+STRSIZE(@X),STRING("World"),6)
VAR
byte buffer[noparse][[/noparse]buffer_size]
byte base_buffer[noparse][[/noparse]buffer_size]
byte cmp_buffer[noparse][[/noparse]buffer_size]
CON
buffer_size = 32
PUB clear | index
repeat index from 0 to buffer_size-1
buffer[noparse][[/noparse]index] := 0
PUB append(strptr) : Success
if strsize(strptr) > buffer_size 'Supplied string is too big for buffer
return -1
if strsize(strptr) > (buffer_size - strsize(@buffer)) 'Not enough room in the buffer for string
return -1
bytemove(@buffer+strsize(@buffer), @strptr, strsize(@strptr) + 1)
return 1
PUB toUpper(strptr) | index 'Convert each character to uppercase ASCII
repeat index from 0 to strsize(@strptr) - 1
if strptr[noparse][[/noparse]index] != 0
strptr[noparse][[/noparse]index] -= 32
PUB indexOf(strptr) : strindex | index
bytemove(@base_buffer, @buffer, strsize(@buffer) + 1)
bytemove(@cmp_buffer, @strptr, strsize(strptr) + 1)
toUpper(base_buffer)
toUpper(cmp_buffer)
strindex := -1 'Init strindex to -1 for failed search
repeat index from 0 to strsize(@base_buffer) 'Slide cmp_buffer through base_buffer
if strcomp(@basebuffer + index, @cmp_buffer)
strindex := index 'Store the current index
return strindex
return strindex
Post Edited (ramman345) : 5/29/2008 7:48:27 PM GMT
For the "append" method, you don't need to test separately for the size of the string to be added. Just a test for the total length of the result is enough. Also, it would help to compute the current length of the buffer string only once and save it in a local variable. Similarly, compute the length of the new piece only once and save it in a local variable. It might be useful to return the total length of the result (or -1 if an error occurs). Note that strptr is already a pointer. You'd have:
Post Edited (Mike Green) : 5/29/2008 7:44:15 PM GMT
PUB toUpper(strptr) | index 'Convert each character to uppercase ASCII
repeat index from 0 to strsize(@strptr) - 1
if strptr[noparse][[/noparse]index] != 0
strptr[noparse][[/noparse]index] -= 32
PUB indexOf(strptr) : strindex | index
bytemove(@base_buffer, @buffer, strsize(@buffer) + 1)
bytemove(@cmp_buffer, @strptr, strsize(strptr) + 1)
toUpper(base_buffer)
toUpper(cmp_buffer)
strindex := -1 'Init strindex to -1 for failed search
repeat index from 0 to strsize(@base_buffer) 'Slide cmp_buffer through base_buffer
if strcomp(@basebuffer + index, @cmp_buffer)
strindex := index 'Store the current index
return strindex
return strindex
For the "indexOf" method, you want to check first to see if a match is possible based on length.
PUB toUpper(strptr) | t
repeat while t := byte[noparse][[/noparse]strptr++] <- cast whatever's at strptr++ as a byte and assign to t
if t =< "a" and t => "z" <- make sure t is a character between a-z ** I was missing this condition
byte[noparse][[/noparse]strptr-1] -= 32 <- strptr was already incremented above so we need the previous element
How does it get out of the loop?
Josh
as long as the condition evaluates to not zero it will be true so:
strings end with a zero terminator so when it hits the end it stops
Thanks so much Jimmy!
Josh