diff options
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 --- 写入文件 |