Re: Reading strings
- Posted by Chris Bensler <bensler at nt.net> Jan 21, 2007
- 493 views
Bernie Ryan wrote: > > Hayden McKay wrote: > > > > }}} <eucode> > > ----------------------------------------------------------------- > > -- string(atom p) > > global function -- returns a euphoria represented string. > > > > string(atom p) > > sequence st -- place holder for string > > > > st = "" -- just an empty string :) > > for k = 0 to 256 by 1 do > > if peek(p + k) = 0 then -- scan for '0' terminator > > st = peek({p, k}) > > exit end if > > end for > > > > return st > > > > end function > > > > ----------------------------------------------------------------- > > </eucode> {{{ > > A function to read 'c' style strings from memory. > > Does anybody know a faster way? > > Hayden: Is it necessary to limit the string length? > Try this I don't know how fast it is. > > }}} <eucode> > ----------------------------------------------------------------- > global function getstring(atom p) > > sequence st > > st = {peek(p)} > > while st[$] do > st &= peek(p + length(st)) > end while > > return st > > end function > ----------------------------------------------------------------- > </eucode> {{{ > Bernie I haven't tried Hayden's function, but I have compared a function before that was similar to the one Bernie posted (it concatenates), and the following is faster:
------------------------------------------------ global function peek_string(pointer addr) ------------------------------------------------ -- syntax: s = peek_string( x ) -- description: -- retrieves a null-terminated string from a memory address. atom offset offset = addr while peek(offset) do offset +=1 end while return peek({addr,offset-addr}) end function
Pete: peek() will accept peek({lp,0}) which will result in a null string Chris Bensler ~ The difference between ordinary and extraordinary is that little extra ~ http://empire.iwireweb.com - Empire for Euphoria