Skip to content

Commit

Permalink
add stride
Browse files Browse the repository at this point in the history
  • Loading branch information
justincormack committed Oct 1, 2012
1 parent 7e5c7d0 commit 3811bcd
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
4 changes: 2 additions & 2 deletions slice.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ local function make(ct, len, cap)
local atype = ffi.typeof("$ [?]", ct)
local array = atype(cap)

return setmetatable({len = len, cap = cap, s = array, array = array, type = ct}, mt)
return setmetatable({len = len, cap = cap, s = array, array = array, type = ct, stride = ffi.sizeof(ct)}, mt)
end

local function sametype(a, b) -- not sure how to test if same - should we try casting src to dest?
Expand All @@ -38,7 +38,7 @@ mt = {
if not i then i = 0 end
if not j then j = s.len end
local len = j - i
return setmetatable({len = len, cap = s.cap, s = s.s + i, array = s.array, type = s.type}, mt)
return setmetatable({len = len, cap = s.cap, s = s.s + i, array = s.array, type = s.type, stride = ffi.sizeof(s.type)}, mt)
end
end
if i == 'table' then return totable(s) end
Expand Down
3 changes: 3 additions & 0 deletions test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local ffi = require "ffi"
local slice = require "slice"

local int_t = ffi.typeof("int")
local byte_t = ffi.typeof("uint8_t")

local a, b

Expand Down Expand Up @@ -45,5 +46,7 @@ a = slice.make(int_t, {1, 2, 3})
b = slice.make(int_t, {4, 5, 6})
assert(tostring(a..b) == "[1,2,3,4,5,6]")

a = slice.make(byte_t, {1, 2, 3})
assert(a.stride == 1)


0 comments on commit 3811bcd

Please sign in to comment.