robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
2.9 KiB134 linesraw
1-- Basic shim for LuaJIT's stringbuffer.
2-- Note this does not implement the full API.
3-- This is intentionally internal-only. If we want to expose it, we should
4-- reimplement this a userdata and ship it as `string.buffer`
5-- (minus the FFI stuff) for Lua 5.1.
6local M = {}
7
8local has_strbuffer, strbuffer = pcall(require, 'string.buffer')
9
10if has_strbuffer then
11  M.new = strbuffer.new
12
13  -- Lua 5.1 does not have __len metamethod so we need to provide a len()
14  -- function to use instead.
15
16  --- @param buf vim._core.stringbuffer
17  --- @return integer
18  function M.len(buf)
19    return #buf
20  end
21
22  return M
23end
24
25--- @class vim._core.stringbuffer.ptr
26--- @field [integer] integer
27
28--- @class vim._core.stringbuffer
29--- @field private ptr vim._core.stringbuffer.ptr
30--- @field private buf string[]
31--- @field package len integer absolute length of the `buf`
32--- @field package skip_ptr integer
33local StrBuffer = {}
34StrBuffer.__index = StrBuffer
35
36function StrBuffer:_normalize()
37  if #self.buf > 1 then
38    self.buf = { table.concat(self.buf) }
39  end
40
41  if self.skip_ptr > 0 then
42    if self.buf[1] then
43      self.buf[1] = self.buf[1]:sub(self.skip_ptr + 1)
44      self.len = self.len - self.skip_ptr
45    end
46    self.skip_ptr = 0
47  end
48
49  return self
50end
51
52--- @return string
53function StrBuffer:tostring()
54  return self:_normalize().buf[1] or ''
55end
56
57StrBuffer.__tostring = StrBuffer.tostring
58
59--- @private
60--- Efficiently peek at the first `n` characters of the buffer.
61--- @param n integer
62--- @return string
63function StrBuffer:_peek(n)
64  local skip, buf1 = self.skip_ptr, self.buf[1]
65  if buf1 and (n + skip) < #buf1 then
66    return buf1:sub(skip + 1, skip + n)
67  end
68  return self:tostring():sub(1, n)
69end
70
71--- @param chunk string
72function StrBuffer:put(chunk)
73  local s = tostring(chunk)
74  self.buf[#self.buf + 1] = s
75  self.len = self.len + #s
76  return self
77end
78
79--- @param str string
80function StrBuffer:set(str)
81  return self:reset():put(str)
82end
83
84--- @param n? integer
85--- @return string
86function StrBuffer:get(n)
87  n = n or self.len
88  local r = self:_peek(n)
89  self:skip(n)
90  return r
91end
92
93--- @param n integer
94function StrBuffer:skip(n)
95  self.skip_ptr = math.min(self.len, self.skip_ptr + n)
96  return self
97end
98
99function StrBuffer:reset()
100  self.buf = {}
101  self.skip_ptr = 0
102  self.len = 0
103  return self
104end
105
106--- Efficiently read the character at the 0-based index `k` in the buffer.
107--- @param k integer
108--- @return integer
109function StrBuffer:_index(k)
110  return assert(self:_normalize().buf[1]):byte(k + 1)
111end
112
113--- @return vim._core.stringbuffer.ptr, integer
114function StrBuffer:ref()
115  return self.ptr, self.len - self.skip_ptr
116end
117
118function M.new()
119  local self = setmetatable({}, StrBuffer):reset()
120  ---@diagnostic disable-next-line: invisible
121  self.ptr = setmetatable({}, {
122    __index = function(_, k)
123      return StrBuffer._index(self, k)
124    end,
125  })
126  return self
127end
128
129--- @param buf vim._core.stringbuffer
130function M.len(buf)
131  return buf.len - buf.skip_ptr
132end
133
134return M