- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuffer.lua
More file actions
Latest commit
60 lines (56 loc) · 1.59 KB
/
Copy pathbuffer.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
localmakeRaw=require('_buffer');
sliceMeta= {
__eq=function(a,b)
returna[3] ==b[3] anda[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()
returnamount
end
}
functionmake(init)
ifinit==niltheninit=0x1000end
raw=makeRaw(init)
functionmakeSlice(this,offset,amount)
ifoffsetthen
offset=math.min(offset,raw.size-1)
else
offset=0
end
ifamountthen
amount=math.min(amount,raw.size-offset)
else
amount=amountor (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)
returndest
end
slice.zero=function()
raw:zero(offset,amount)
end
slice.clone=function()
dest=make(amount)
dest.actually_slice(dest,0,raw,offset,amount)
returndest
end
slice.tostring=function()
returnraw:tostring(offset,amount)
end
setmetatable(slice,sliceMeta)
returnslice
end
raw.slice=makeSlice
meta=getmetatable(raw)
meta.__eq=function(a,b)
returna:equals(0,a.size,b,0,b.size)
end
returnraw
end
returnmake