diff options
author | takase1121 <20792268+takase1121@users.noreply.github.com> | 2021-09-02 09:16:31 +0800 |
---|---|---|
committer | takase1121 <20792268+takase1121@users.noreply.github.com> | 2021-09-02 09:16:31 +0800 |
commit | 68461a4c89e81d651c9d9af8ffe649606d9d062e (patch) | |
tree | a583223dc7cc0cabf0c9fdc17fa886750845f62a | |
parent | ebbd506d579a718650789d3c51ebfb6d488c4252 (diff) | |
download | lite-xl-plugins-68461a4c89e81d651c9d9af8ffe649606d9d062e.tar.gz lite-xl-plugins-68461a4c89e81d651c9d9af8ffe649606d9d062e.zip |
fix bug caused by file truncation
-rw-r--r-- | plugins/open_ext.lua | 22 |
1 files 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() |