robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
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 11M . 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 18function M . len ( buf ) 19return # buf 20end 21 22return 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 () 37if # self . buf > 1 then 38self . buf ={ table . concat ( self . buf ) } 39end 40 41if self . skip_ptr > 0 then 42if self . buf [ 1 ] then 43self . buf [ 1 ] =self . buf [ 1 ]: sub ( self . skip_ptr + 1 ) 44self . len =self . len - self . skip_ptr 45end 46self . skip_ptr =0 47end 48 49return self 50end 51 52--- @return string 53function StrBuffer : tostring () 54return 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 ) 64local skip , buf1 =self . skip_ptr , self . buf [ 1 ] 65if buf1 and ( n + skip ) < # buf1 then 66return buf1 : sub ( skip + 1 , skip + n ) 67end 68return self : tostring (): sub ( 1 , n ) 69end 70 71--- @param chunk string 72function StrBuffer : put ( chunk ) 73local s =tostring ( chunk ) 74self . buf [ # self . buf + 1 ] =s 75self . len =self . len + # s 76return self 77end 78 79--- @param str string 80function StrBuffer : set ( str ) 81return self : reset (): put ( str ) 82end 83 84--- @param n? integer 85--- @return string 86function StrBuffer : get ( n ) 87n =n or self . len 88local r =self : _peek ( n ) 89self : skip ( n ) 90return r 91end 92 93--- @param n integer 94function StrBuffer : skip ( n ) 95self . skip_ptr =math . min ( self . len , self . skip_ptr + n ) 96return self 97end 98 99function StrBuffer : reset () 100self . buf ={} 101self . skip_ptr =0 102self . len =0 103return 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 ) 110return assert ( self : _normalize (). buf [ 1 ]): byte ( k + 1 ) 111end 112 113--- @return vim._core.stringbuffer.ptr, integer 114function StrBuffer : ref () 115return self . ptr , self . len - self . skip_ptr 116end 117 118function M . new () 119local self =setmetatable ( {} , StrBuffer ): reset () 120---@diagnostic disable-next-line: invisible 121self . ptr =setmetatable ( {} , { 122__index =function ( _ , k ) 123return StrBuffer . _index ( self , k ) 124end , 125} ) 126return self 127end 128 129--- @param buf vim._core.stringbuffer 130function M . len ( buf ) 131return buf . len - buf . skip_ptr 132end 133 134return M