1. Reading strings
- Posted by Hayden McKay <hmck1 at dodo.com.au> Jan 20, 2007
- 541 views
A function to read 'c' style strings from memory. Does anybody know a faster way?
----------------------------------------------------------------- -- 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 -----------------------------------------------------------------
Hayden.
2. Re: Reading strings
- Posted by Pete Lomax <petelomax at blueyonder.co.uk> Jan 20, 2007
- 511 views
Hayden McKay wrote: > > A function to read 'c' style strings from memory. > Does anybody know a faster way? I've always thought this, from misc_arwen.e, would be as good as it gets:
global function peek_string(atom addr) atom last -- initialize variables last = addr -- find end of string while peek(last) do last += 1 end while -- get the string if addr != last then return peek( {addr, last - addr} ) else return "" end if end function
I have not tested, but theoretically it ought to gain 10%, maybe more. Not that am I sure why you want "" for strings longer than 256 chars. Let me know, Pete
3. Re: Reading strings
- Posted by Al Getz <Xaxo at aol.com> Jan 20, 2007
- 507 views
Hello there, If you are using Windows then the fastest way is to use my contribution to WinLib. I think it's now called "w32peek_string()" and it uses the built in Windows API funciotn "lstrlen()" to first get the string length, then it gets the string. There's no limit on the string length either. Take care, Al E boa sorte com sua programacao Euphoria! My bumper sticker: "I brake for LED's" From "Black Knight": "I can live with losing the good fight, but i can not live without fighting it". "Well on second thought, maybe not."
4. Re: Reading strings
- Posted by Bernie Ryan <xotron at bluefrog.com> Jan 20, 2007
- 501 views
- Last edited Jan 21, 2007
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: Try this I don't know how fast it is.
----------------------------------------------------------------- global function getstring(atom p) sequence st st = {peek(p)} while st[$] do st &= peek(p + length(st)) end while return st end function -----------------------------------------------------------------
Bernie My files in archive: WMOTOR, XMOTOR, W32ENGIN, MIXEDLIB, EU_ENGIN, WIN32ERU, WIN32API Can be downloaded here: http://www.rapideuphoria.com/cgi-bin/asearch.exu?dos=on&win=on&lnx=on&gen=on&keywords=bernie+ryan
5. Re: Reading strings
- Posted by Chris Bensler <bensler at nt.net> Jan 21, 2007
- 494 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