1- return require (' jls.lang.loader' ).requireOne (' jls.lang.Buffer-buffer' , ' jls.lang.Buffer-' )
1+ --- Represents a byte array to store temporary data.
2+ -- @module jls.lang.Buffer
3+ -- @pragma nostrip
4+
5+ local class = require (' jls.lang.class' )
6+
7+ --- The Buffer class.
8+ -- @type Buffer
9+ return class .create (function (buffer )
10+
11+ --- Returns the size of the buffer.
12+ -- @return The size of the buffer
13+ -- @function buffer:length
14+ buffer .length = class .notImplementedFunction
15+
16+ --- Returns the string at the specified position.
17+ -- @tparam [opt] number from The start position, default to 1
18+ -- @tparam [opt] number to The end position included, default to this buffer size
19+ -- @return The string at the specified position
20+ -- @function buffer:get
21+ buffer .get = class .notImplementedFunction
22+
23+ --- Sets the string at the specified position.
24+ -- @param value The value to set in this buffer, could be a buffer or a string
25+ -- @tparam [opt] number offset The position in this buffer to set, default to 1
26+ -- @tparam [opt] number from The start position in the value, default to 1
27+ -- @tparam [opt] number to The end position in the value included, default to this buffer size
28+ -- @function buffer:set
29+ buffer .set = class .notImplementedFunction
30+
31+ --- Returns the bytes at the specified position.
32+ -- @tparam [opt] number from The start position, default to 1
33+ -- @tparam [opt] number to The end position included, default to from
34+ -- @return The bytes at the specified position
35+ -- @function buffer:getBytes
36+ buffer .getBytes = class .notImplementedFunction
37+
38+ --- Sets the bytes at the specified position.
39+ -- @tparam number at The position in this buffer to set, default to 1
40+ -- @param ... The bytes
41+ -- @function buffer:setBytes
42+ buffer .setBytes = class .notImplementedFunction
43+
44+ --- Returns a reference for this buffer.
45+ -- @treturn string a reference for this buffer
46+ -- @function buffer:toReference
47+ buffer .toReference = class .notImplementedFunction
48+
49+ end , function (Buffer )
50+
51+ local function getClassName (mode )
52+ if mode == ' local' then
53+ return require (' jls.lang.BufferLocal' )
54+ elseif mode == ' global' then
55+ return require (' jls.lang.BufferGlobal' )
56+ elseif mode == ' shared' then
57+ return require (' jls.lang.BufferShared' )
58+ end
59+ error (' invalid buffer mode ' .. tostring (mode ))
60+ end
61+
62+ --- Returns a new buffer for the specified mode.
63+ -- A local buffer resides in the Lua state.
64+ -- A global buffer resides out of the Lua state but in the Lua process.
65+ -- A shared buffer resides out of the Lua process but in the host.
66+ -- @tparam number size the size to allocate, could be a non empty string
67+ -- @tparam [opt] string mode the mode of buffer, defaults to local
68+ -- @return The new allocated buffer
69+ function Buffer .allocate (size , mode )
70+ if type (size ) == ' string' then
71+ local b = Buffer .allocate (# size , mode )
72+ b :set (size )
73+ return b
74+ end
75+ if math.type (size ) ~= ' integer' or size <= 0 then
76+ error (' invalid size ' .. tostring (size ))
77+ end
78+ if mode == nil then
79+ mode = ' local'
80+ end
81+ return getClassName (mode ).allocate (size , mode )
82+ end
83+
84+ --- Returns the buffer represented by the specified reference.
85+ -- @tparam string reference the reference
86+ -- @tparam [opt] string mode the mode of buffer
87+ -- @return The referenced buffer
88+ function Buffer .fromReference (reference , mode )
89+ if mode == nil then
90+ local status , b
91+ for _ , m in ipairs ({' local' , ' global' , ' shared' }) do
92+ status , b = pcall (Buffer .fromReference , reference , m )
93+ if status then
94+ return b
95+ end
96+ end
97+ end
98+ return getClassName (mode ).fromReference (reference , mode )
99+ end
100+
101+ end )
0 commit comments