1. Reading strings

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.

new topic     » topic index » view message » categorize

2. Re: Reading strings

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

new topic     » goto parent     » topic index » view message » categorize

3. Re: Reading strings

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."

new topic     » goto parent     » topic index » view message » categorize

4. Re: Reading strings

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

new topic     » goto parent     » topic index » view message » categorize

5. Re: Reading strings

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

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu