From 68461a4c89e81d651c9d9af8ffe649606d9d062e Mon Sep 17 00:00:00 2001 From: takase1121 <20792268+takase1121@users.noreply.github.com> Date: Thu, 2 Sep 2021 09:16:31 +0800 Subject: fix bug caused by file truncation --- plugins/open_ext.lua | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/plugins/open_ext.lua b/plugins/open_ext.lua index 21bf80e..f01ab85 100644 --- a/plugins/open_ext.lua +++ b/plugins/open_ext.lua @@ -11,7 +11,7 @@ local RootView = require "core.rootview" local View = require "core.view" -local function validate_utf8(s) +local function validate_utf8(s, limit) --[[ MIT LICENSE Copyright (c) 2013 Enrique GarcĂ­a Cota @@ -32,7 +32,8 @@ local function validate_utf8(s) TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ]] - local p, len = 1, #s + limit = limit or math.huge + local i, p, len = 1, 1, #s while p <= len do if p == s:find("[%z\1-\127]", p) then p = p + 1 elseif p == s:find("[\194-\223][\128-\191]", p) then p = p + 2 @@ -46,6 +47,8 @@ local function validate_utf8(s) else return false end + i = i + 1 + if i > limit then break end end return true @@ -158,10 +161,21 @@ function OpenExtView:draw() end +local function read_from_doc(doc, limit) + local l, result = 0, {} + for _, line in ipairs(doc.lines) do + result[#result+1] = line + l = l + #line + if l >= limit then break end + end + return table.concat(result, "") +end + + local rootview_open_doc = RootView.open_doc function RootView:open_doc(doc) - local line = string.sub(doc.lines[1] or "", 1, 128) - if line == "\n" or line == "" and validate_utf8(line) then + local str = read_from_doc(doc, 4 * 128) -- max size for 128 codepoints + if str == "\n" or str == "" and validate_utf8(str, 128) then return rootview_open_doc(self, doc) else local node = self:get_active_node_default() -- cgit v1.2.3