forked from CopilotC-Nvim/CopilotChat.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringbuffer.lua
More file actions
46 lines (42 loc) · 1.07 KB
/
stringbuffer.lua
File metadata and controls
46 lines (42 loc) · 1.07 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
local ok, jit_buffer = pcall(require, 'string.buffer')
---@class StringBuffer
---@field put fun(self:StringBuffer, s:string)
---@field set fun(self:StringBuffer, s:string)
---@field tostring fun(self:StringBuffer):string
--- Create a string buffer for efficient string concatenation
---@return StringBuffer
local function stringbuffer()
if ok and jit_buffer then
return {
_buf = jit_buffer.new(),
put = function(self, s)
self._buf:put(s)
end,
set = function(self, s)
self._buf:set(s)
end,
tostring = function(self)
return self._buf:tostring()
end,
}
end
return {
_buf = { '' },
put = function(self, s)
table.insert(self._buf, s)
for i = #self._buf - 1, 1, -1 do
if #self._buf[i] > #self._buf[i + 1] then
break
end
self._buf[i] = self._buf[i] .. table.remove(self._buf)
end
end,
set = function(self, s)
self._buf = { s }
end,
tostring = function(self)
return table.concat(self._buf)
end,
}
end
return stringbuffer