RE: API to get length of array cdata

  • From: William Adams <william_a_adams@xxxxxxx>
  • To: <luajit@xxxxxxxxxxxxx>
  • Date: Sun, 13 May 2012 14:52:00 +0000

The hacked up way I'm doing it is this: function array_size(buff)
    local typestr = tostring(ffi.typeof(buff))
    local elemtype, nelem = string.match(typestr, "ctype<(%w+)%s+%[(%d+)%]>")   
     return nelem;
end It's fragile because it depends on the string representation returned by 
the "tostring", but it seems to work.I'm sure if the string representation 
changes, this definition could be made a bit more adaptable.Since there's only 
a single numeric value in the string representation, you could just return 
thenumeric part: (%d+) and ignore all the rest.  local nelem = 
string.match(typestr, "(%d+)")
of course, if you've done a double, triple, or more array, then the string will 
look like: local arr2 = ffi.new("int[256][10]")
ctype<int [256][10]> In that case, you'll have to use a more complex pattern, 
and use gmatch to capture them all.   -- William

===============================

- Shaping clay is easier than digging it out of the ground.


http://williamaadams.wordpress.com
http://www.thingiverse.com/WilliamAAdams
https://github.com/Wiladams
 

 > From: cosmin.apreutesei@xxxxxxxxx
> Date: Sat, 12 May 2012 18:24:06 +0300
> Subject: API to get length of array cdata
> To: luajit@xxxxxxxxxxxxx
> 
> Hi,
> 
> Is it possible to have a function to get the length of an array cdata
> (VLA or not)? Right now I have a function that returns sizeof(cdata) /
> sizeof(elem_ctype) but I have to pass it the element's ctype. #cdata
> would be swell.
> 
> Cosmin.
> 
                                          

Other related posts: