Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Ruby array to C pointer

Reply
Thread Tools

Ruby array to C pointer

 
 
Chananya Freiman
Guest
Posts: n/a
 
      12-28-2010
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/.

 
Reply With Quote
 
 
 
 
Brian Candler
Guest
Posts: n/a
 
      12-30-2010
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/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
Cast a pointer to array to base class pointer to array Hansen C++ 3 04-24-2010 03:30 PM
Pointer to array of array of const pointer RSL C++ 14 02-19-2010 02:06 PM
Array of pointer and pointer of array erfan C Programming 6 01-28-2008 08:55 PM
Array of pointer Vs Pointer to Array sangeetha C Programming 9 10-09-2004 07:01 PM



Advertisments