1. reading strings
- Posted by Hayden McKay <hmck1 at dodo.com.au> Sep 05, 2006
- 579 views
just wanted to share a small routine for reading strings. I use it to load large strings (no longer than 256 bytes) into euphoria. nb. probably not very fast for smaller type strings. you could change the 'TERMINMATOR' and 'MAXLENGTH' constants to suit your application
--==============================================-- constant -- TERMINATOR = 0, -- the string terminator MAXLENGTH = 256 -- maximum string length -------------------------------------------------- -- returns sequence as a string from memory/device global function string(atom lp) sequence chunk integer loc chunk = peek({lp, MAXLENGTH}) loc = find(TERMINATOR, chunk) if loc then chunk = chunk[1..(loc-1)] end if return chunk end function
2. Re: reading strings
- Posted by Bernie Ryan <xotron at bluefrog.com> Sep 05, 2006
- 566 views
Hayden McKay wrote: > > > just wanted to share a small routine for reading strings. I use it to load > large > strings (no longer than 256 bytes) into euphoria. > > nb. probably not very fast for smaller type strings. > > you could change the 'TERMINMATOR' and 'MAXLENGTH' constants to suit your > application > > }}} <eucode> > --==============================================-- > constant > -- > TERMINATOR = 0, -- the string terminator > MAXLENGTH = 256 -- maximum string length > > -------------------------------------------------- > -- returns sequence as a string from memory/device > > global function string(atom lp) > > sequence chunk > integer loc > > chunk = peek({lp, MAXLENGTH}) > > loc = find(TERMINATOR, chunk) > if loc then > chunk = chunk[1..(loc-1)] > end if > > return chunk > > end function > </eucode> {{{ Hayden: Couldn't you do it this way ?
constant -- TERMINATOR = 0, -- the string terminator MAXLENGTH = 256 -- maximum string length -- global function string(atom lp) return find(TERMINATOR, peek({lp, MAXLENGTH})) 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
3. Re: reading strings
- Posted by Hayden McKay <hmck1 at dodo.com.au> Sep 06, 2006
- 569 views
Bernie Ryan wrote: > > Hayden McKay wrote: > > > > > > just wanted to share a small routine for reading strings. I use it to load > > large > > strings (no longer than 256 bytes) into euphoria. > > > > nb. probably not very fast for smaller type strings. > > > > you could change the 'TERMINMATOR' and 'MAXLENGTH' constants to suit your > > application > > > > }}} <eucode> > > --==============================================-- > > constant > > -- > > TERMINATOR = 0, -- the string terminator > > MAXLENGTH = 256 -- maximum string length > > > > -------------------------------------------------- > > -- returns sequence as a string from memory/device > > > > global function string(atom lp) > > > > sequence chunk > > integer loc > > > > chunk = peek({lp, MAXLENGTH}) > > > > loc = find(TERMINATOR, chunk) > > if loc then > > chunk = chunk[1..(loc-1)] > > end if > > > > return chunk > > > > end function > > </eucode> {{{ > > Hayden: > Couldn't you do it this way ? > > }}} <eucode> > constant > -- > TERMINATOR = 0, -- the string terminator > MAXLENGTH = 256 -- maximum string length > -- > global function string(atom lp) > return find(TERMINATOR, peek({lp, MAXLENGTH})) > end function > </eucode> {{{ > > Bernie > > My files in archive: > WMOTOR, XMOTOR, W32ENGIN, MIXEDLIB, EU_ENGIN, WIN32ERU, WIN32API > > Can be downloaded here: > <a > href="http://www.rapideuphoria.com/cgi-bin/asearch.exu?dos=on&win=on&lnx=on&gen=on&keywords=bernie+ryan">http://www.rapideuphoria.com/cgi-bin/asearch.exu?dos=on&win=on&lnx=on&gen=on&keywords=bernie+ryan</a> The result returned by your function is an 'integer' (the position of the zero terminator) either 1..256 or 0 if none was found. where my function returns an actual sequence. nb. After reading your post and doing some testing I found that my routine will crash if 'atom lp' points to a 0. I have'nt had to read empty strings before so an application crash was never a problem for me, but I should change routine to accomodate as follows.
--==============================================-- constant -- TERMINATOR = 0, -- the string terminator MAXLENGTH = 256 -- maximum string length -------------------------------------------------- -- changes... -- function now returns "{}" if the string -- is empty or the terminator is not found global function string(atom lp) sequence chunk integer loc chunk = peek({lp, MAXLENGTH}) loc = find(TERMINATOR, chunk) -- if loc then if loc >= 1 then chunk = chunk[1..(loc-1)] else chunk = {} -- return empty string. end if return chunk end function
4. Re: reading strings
- Posted by Pete Lomax <petelomax at blueyonder.co.uk> Sep 06, 2006
- 554 views
On Tue, 05 Sep 2006 19:45:02 -0700, Hayden McKay <guest at RapidEuphoria.com> wrote: >nb. After reading your post and doing some testing I found that my routine will >crash if 'atom lp' points to a 0. <snip> >but I should change routine to accomodate as follows. >}}} <eucode> >-- if loc then > if loc >= 1 then > chunk = chunk[1..(loc-1)] > else > chunk = {} -- return empty string. > end if ></eucode> {{{ This rang a few alarm bells. First question is what error message did you get, exactly, that this is supposed to fix? I expect it was an error in the peek, if lp was zero or pointed to a zero in memory which was not followed by 255 bytes of readable memory. The change might solve an error "slice end is xxx ", but I doubt you got that. What you might have done is by adding those 4 lines of code made sure that a zero was now followed by at least 255 bytes of readable memory (code). Note that chunk = chunk[1..0] is a valid Eu statement. If anything, I'd say that as it stands if you peek a 256-character string, with no terminator in it anywhere, it will return {}, or would have crashed before. Regards, Pete
5. Re: reading strings
- Posted by Bernie Ryan <xotron at bluefrog.com> Sep 06, 2006
- 552 views
Hayden McKay wrote: > }}} <eucode> > --==============================================-- > constant > -- > TERMINATOR = 0, -- the string terminator > MAXLENGTH = 256 -- maximum string length > > -------------------------------------------------- > -- changes... > -- function now returns "{}" if the string > -- is empty or the terminator is not found > > global function string(atom lp) > > sequence chunk > integer loc > > chunk = peek({lp, MAXLENGTH}) > > loc = find(TERMINATOR, chunk) > > -- if loc then > if loc >= 1 then > chunk = chunk[1..(loc-1)] > else > chunk = {} -- return empty string. > end if > > return chunk > > end function > </eucode> {{{ Hayden: Try this one.
constant TERMINATOR = 0, -- the string terminator MAXLENGTH = 256 -- maximum string length -- global function string(object lp) if atom(lp) then return string(peek({lp, MAXLENGTH})) else return lp[1..find(TERMINATOR,lp)-1] end if 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