diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2021-11-02 11:46:16 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2021-11-15 16:08:29 +0800 |
commit | 47c0a57dc43f698f0152616fccd183516ad769d0 (patch) | |
tree | 12d97075587b71d68bcc21de97dc09ffc9f86204 /script | |
parent | b4db281f699eac34b5e6f6b00bd6dcc6351d5f7e (diff) | |
download | lua-language-server-47c0a57dc43f698f0152616fccd183516ad769d0.zip |
remove utf16 bom
Diffstat (limited to 'script')
-rw-r--r-- | script/fs-utility.lua | 18 | ||||
-rw-r--r-- | script/utility.lua | 18 |
2 files changed, 24 insertions, 12 deletions
diff --git a/script/fs-utility.lua b/script/fs-utility.lua index f4e55716..a0fdc02f 100644 --- a/script/fs-utility.lua +++ b/script/fs-utility.lua @@ -16,7 +16,7 @@ _ENV = nil local m = {} --- 读取文件 ---@param path string -function m.loadFile(path) +function m.loadFile(path, keepBom) if type(path) ~= 'string' then ---@diagnostic disable-next-line: undefined-field path = path:string() @@ -25,12 +25,18 @@ function m.loadFile(path) if not f then return nil, e end - if f:read(3) ~= '\xEF\xBB\xBF' then - f:seek("set") - end - local buf = f:read 'a' + local text = f:read 'a' f:close() - return buf + if not keepBom then + if text:sub(1, 3) == '\xEF\xBB\xBF' then + return text:sub(4) + end + if text:sub(1, 2) == '\xFF\xFE' + or text:sub(1, 2) == '\xFE\xFF' then + return text:sub(3) + end + end + return text end --- 写入文件 diff --git a/script/utility.lua b/script/utility.lua index 8d377708..b5eb4095 100644 --- a/script/utility.lua +++ b/script/utility.lua @@ -275,17 +275,23 @@ end --- 读取文件 ---@param path string -function m.loadFile(path) +function m.loadFile(path, keepBom) local f, e = ioOpen(path, 'rb') if not f then return nil, e end - if f:read(3) ~= '\xEF\xBB\xBF' then - f:seek("set") - end - local buf = f:read 'a' + local text = f:read 'a' f:close() - return buf + if not keepBom then + if text:sub(1, 3) == '\xEF\xBB\xBF' then + return text:sub(4) + end + if text:sub(1, 2) == '\xFF\xFE' + or text:sub(1, 2) == '\xFE\xFF' then + return text:sub(3) + end + end + return text end --- 写入文件 |