Ruby array to C pointer

C

Chananya Freiman

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 :)
 
B

Brian Candler

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: .*?
<my_tag>
aaa
</my_tag>
<my_tag>
text bla bla
</my_tag>
EOS
=> [["\n aaa\n "], ["\n text bla bla\n "]]
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top