Ruby array to C pointer

Discussion in 'Ruby' started by Chananya Freiman, Dec 28, 2010.

  1. I want to use Ruby arrays as C pointers in an extension.
    As far as I know, this isn't possible directly, since Ruby arrays can
    contain any Ruby object (hence why their internal type is VALUE).

    Making a new C array (static for my needs) each call and going over
    every element of the Ruby array and converting it wouldn't be really
    nice performance-wise.

    Is there some way to still cast the array to a C pointer of a specific
    type?
    I only want to get flat arrays of floating point numbers, but I can't
    find a way to get the actual address of the Ruby array.

    Should I instead Array#pack my arrays into binary strings?


    Now, in a completely unrelated subject.

    I keep noticing that regular expressions really don't work in a specific
    setting, where you want to get blocks that reside inside some sort of
    "tags".
    An example for this would be HTML.
    I can't find a way to cleanly find blocks that can contain any character
    sequence between two specific tags.
    Here's a small example (the tags could be anything, just chose familiar
    XML tags):

    <my_tag>
    aaa
    </my_tag>
    <my_tag>
    text bla bla could contain "</my_tag>" in strings
    </my_tag>

    There are two problems with this example: first of all, it would
    obviously parse it wrong because of the "</my_tag>", but even assuming
    that string isn't there, it will still fail if you try to search for
    every letter between the tags (as in .*) because it will only stop at
    the last encounter of </my_tag>.

    Right now I only look for the opening tags, split the data at each
    encounter, and then for each resulting block search for the closing tag.
    It doesn't fix the first problem, but it does fix the second one.

    Is there a clean way to do this?


    Thanks for any help :)

    --
    Posted via http://www.ruby-forum.com/.
    Chananya Freiman, Dec 28, 2010
    #1
    1. Advertising

  2. Chananya Freiman wrote in post #971174:
    > it will still fail if you try to search for
    > every letter between the tags (as in .*) because it will only stop at
    > the last encounter of </my_tag>.

    ...
    > Is there a clean way to do this?


    Yes, use non-greedy matching: .*?

    >> a = <<EOS

    <my_tag>
    aaa
    </my_tag>
    <my_tag>
    text bla bla
    </my_tag>
    EOS
    => " <my_tag>\n aaa\n </my_tag>\n <my_tag>\n text
    bla bla\n </my_tag>\n"
    >> a.scan(%r{<my_tag>(.*?)</my_tag>}m)

    => [["\n aaa\n "], ["\n text bla bla\n "]]

    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, Dec 30, 2010
    #2
    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. sangeetha

    Array of pointer Vs Pointer to Array

    sangeetha, Oct 8, 2004, in forum: C Programming
    Replies:
    9
    Views:
    338
    Tim Rentsch
    Oct 9, 2004
  2. erfan

    Array of pointer and pointer of array

    erfan, Jan 28, 2008, in forum: C Programming
    Replies:
    6
    Views:
    664
    Martin Ambuhl
    Jan 28, 2008
  3. RSL
    Replies:
    14
    Views:
    3,218
  4. Hansen
    Replies:
    3
    Views:
    1,083
    rep_movsd
    Apr 24, 2010
  5. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    441
    James Kuyper
    Sep 23, 2011
Loading...

Share This Page