Buffer to string

Discussion in 'Ruby' started by Jari Williamsson, Nov 8, 2007.

  1. I have a buffer (stored in a String class) with a NULL-terminated
    C-style string inside, what's the most efficient approach to get that
    text string up to the NULL?


    Best regards,

    Jari Williamsson
     
    Jari Williamsson, Nov 8, 2007
    #1
    1. Advertising

  2. Jari Williamsson

    Guest

    Hi

    One approach could be to use split and a regexp:

    s = "hi"
    s << 0.chr
    s << "there"
    p s
    p s.split(/\000/).first

    Kristian

    On Thu, 8 Nov 2007 21:24:34 +0900, Jari Williamsson
    <> wrote:
    > I have a buffer (stored in a String class) with a NULL-terminated
    > C-style string inside, what's the most efficient approach to get that
    > text string up to the NULL?
    >
    >
    > Best regards,
    >
    > Jari Williamsson
     
    , Nov 8, 2007
    #2
    1. Advertising

  3. Jari Williamsson

    Paul Irofti Guest

    On 2007-11-08, Jari Williamsson <> wrote:
    > I have a buffer (stored in a String class) with a NULL-terminated
    > C-style string inside, what's the most efficient approach to get that
    > text string up to the NULL?


    $ irb
    irb(main):001:0> s = "hello\0"
    => "hello\000"
    irb(main):002:0> s.chomp("\0")
    => "hello"
    irb(main):003:0>

    See String#chomp(!) for more details.

    --
    everything is simple, we're stupid
    contact at gmail
     
    Paul Irofti, Nov 8, 2007
    #3
  4. Jari Williamsson

    Guest

    If you really only ever care about the first string then

    s.match(/\000/).pre_match

    is an alternative which looks like it could be less work for the machine
    since it can stop after the first \000 rather than have to split the entire
    string.

    Are you looking at strings huge enough that efficiency at this level
    matters?

    Kristian



    On Thu, 8 Nov 2007 22:13:57 +0900, <> wrote:
    >
    > Hi
    >
    > One approach could be to use split and a regexp:
    >
    > s = "hi"
    > s << 0.chr
    > s << "there"
    > p s
    > p s.split(/\000/).first
    >
    > Kristian
    >
    > On Thu, 8 Nov 2007 21:24:34 +0900, Jari Williamsson
    > <> wrote:
    >> I have a buffer (stored in a String class) with a NULL-terminated
    >> C-style string inside, what's the most efficient approach to get that
    >> text string up to the NULL?
    >>
    >>
    >> Best regards,
    >>
    >> Jari Williamsson
     
    , Nov 8, 2007
    #4
  5. Jari Williamsson

    Paul Irofti Guest

    On 2007-11-08, <> wrote:
    >
    > If you really only ever care about the first string then
    >
    > s.match(/\000/).pre_match
    >
    > is an alternative which looks like it could be less work for the machine
    > since it can stop after the first \000 rather than have to split the entire
    > string.
    >
    > Are you looking at strings huge enough that efficiency at this level
    > matters?


    I think the OP made it pretty clear: there is *one* C-Style string
    inside a String Class buffer and wants to retrive it w/o the zero
    termination.
    --
    everything is simple, we're stupid
    contact at gmail
     
    Paul Irofti, Nov 8, 2007
    #5
  6. Jari Williamsson

    Tom M Guest

    On Nov 8, 7:24 am, Jari Williamsson
    <> wrote:
    > I have a buffer (stored in a String class) with a NULL-terminated
    > C-style string inside, what's the most efficient approach to get that
    > text string up to the NULL?
    >
    > Best regards,
    >
    > Jari Williamsson


    Would buf.unpack("a*") work?
     
    Tom M, Nov 8, 2007
    #6
  7. Paul Irofti wrote:

    > $ irb
    > irb(main):001:0> s = "hello\0"
    > => "hello\000"
    > irb(main):002:0> s.chomp("\0")
    > => "hello"


    This particular approach wouldn't work, since it can't be guaranteed
    that every byte remaining in the buffer (after the termination) is NULL:
    irb(main):001:0> s = "Hello\0\1\0"
    => "Hello\000\001\000"
    irb(main):002:0> s.chomp("\0")
    => "Hello\000\001"

    But the other suggestions work. Thanks!


    Best regards,

    Jari Williamsson
     
    Jari Williamsson, Nov 8, 2007
    #7
  8. Jari Williamsson

    Paul Irofti Guest

    On 2007-11-08, Tom M <> wrote:
    > On Nov 8, 7:24 am, Jari Williamsson
    ><> wrote:
    >> I have a buffer (stored in a String class) with a NULL-terminated
    >> C-style string inside, what's the most efficient approach to get that
    >> text string up to the NULL?
    >>
    >> Best regards,
    >>
    >> Jari Williamsson

    >
    > Would buf.unpack("a*") work?


    Depends on the raw data contained within the C zero terminated string.
    If its plain text "a*" wouldn't work, but "Z*" might.

    For unpack I'd suggest seeing String#unpack for other formating and the
    way to combine the different format markers in order to retrieve the
    wanted data.

    $ irb
    irb(main):001:0> s = "Hello\0"
    => "Hello\000"
    irb(main):002:0> s.unpack("a*")
    => ["Hello\000"]
    irb(main):003:0> s.unpack("Z*")
    => ["Hello"]

    --
    everything is simple, we're stupid
    contact at gmail
     
    Paul Irofti, Nov 8, 2007
    #8
  9. Jari Williamsson

    Paul Irofti Guest

    On 2007-11-08, Jari Williamsson <> wrote:
    > Paul Irofti wrote:
    >
    >> $ irb
    >> irb(main):001:0> s = "hello\0"
    >> => "hello\000"
    >> irb(main):002:0> s.chomp("\0")
    >> => "hello"

    >
    > This particular approach wouldn't work, since it can't be guaranteed
    > that every byte remaining in the buffer (after the termination) is NULL:
    > irb(main):001:0> s = "Hello\0\1\0"
    >=> "Hello\000\001\000"
    > irb(main):002:0> s.chomp("\0")
    >=> "Hello\000\001"
    >
    > But the other suggestions work. Thanks!
    >


    I did not consider trailing garbage, sorry about that. I thought, from
    your original question, that you only have a NULL terminated string and
    not anything else.

    --
    everything is simple, we're stupid
    contact at gmail
     
    Paul Irofti, Nov 8, 2007
    #9
  10. On Nov 8, 3:38 pm, Tom M <> wrote:
    > On Nov 8, 7:24 am, Jari Williamsson
    >
    > <> wrote:
    > > I have a buffer (stored in a String class) with a NULL-terminated
    > > C-style string inside, what's the most efficient approach to get that
    > > text string up to the NULL?

    >
    > > Best regards,

    >
    > > Jari Williamsson

    >
    > Would buf.unpack("a*") work?


    No, that's buf.unpack("Z*") that you want

    irb(main):001:0> s = "hi"
    => "hi"
    irb(main):002:0> s << 0.chr
    => "hi\000"
    irb(main):003:0> s << "there"
    => "hi\000there"
    irb(main):007:0> s.unpack("a*")
    => ["hi\000there"]
    irb(main):014:0> s.unpack("Z*")
    => ["hi"]
     
    Paolo Bonzini, Nov 8, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arun Kumar Srinivasan
    Replies:
    2
    Views:
    11,486
    Gregory A. Swarthout
    Jan 29, 2004
  2. Raja
    Replies:
    12
    Views:
    24,707
    John Harrison
    Jun 21, 2004
  3. Replies:
    2
    Views:
    634
    sergejusz
    Mar 26, 2007
  4. Neal Becker

    buffer creates only read-only buffer?

    Neal Becker, Jan 8, 2009, in forum: Python
    Replies:
    0
    Views:
    429
    Neal Becker
    Jan 8, 2009
  5. xingye
    Replies:
    9
    Views:
    299
    Michael Lu
    Apr 19, 2004
Loading...

Share This Page