-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuffer.lua
More file actions
60 lines (56 loc) · 1.59 KB
/
buffer.lua
File metadata and controls
60 lines (56 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
local makeRaw = require('_buffer');
sliceMeta = {
__eq = function(a,b)
return a[3] == b[3] and a[1]:equals(a[2],a[3],b[1],b[2],b[3])
end,
__tostring = function(this)
return '<slice '..this[1]..' ('..this[2]..','..this[3]')>'
end,
__len = function()
return amount
end
}
function make(init)
if init == nil then init = 0x1000 end
raw = makeRaw(init)
function makeSlice(this,offset,amount)
if offset then
offset = math.min(offset,raw.size-1)
else
offset = 0
end
if amount then
amount = math.min(amount,raw.size-offset)
else
amount = amount or (raw.size - offset)
end
slice = {raw,offset,amount}
-- XXX: we could assert whether offset/amount fit inside the slice, not just the buffer.
slice.slice = makeSlice
slice.actually_slice = function()
dest = make(amount)
raw.actually_slice(dest,0,raw,offset,amount)
return dest
end
slice.zero = function()
raw:zero(offset,amount)
end
slice.clone = function()
dest = make(amount)
dest.actually_slice(dest,0,raw,offset,amount)
return dest
end
slice.tostring = function()
return raw:tostring(offset,amount)
end
setmetatable(slice,sliceMeta)
return slice
end
raw.slice = makeSlice
meta = getmetatable(raw)
meta.__eq = function(a,b)
return a:equals(0,a.size,b,0,b.size)
end
return raw
end
return make