diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2021-11-02 12:04:04 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2021-11-02 12:04:04 +0800 |
commit | 310b96f3e4b285a3a8422745ab01a32ca144e91a (patch) | |
tree | 7859fd38c7719bb8b781e56eff97f56986b991d2 /script/encoder | |
parent | 0e47d4c03d30efd7fe508e3bb78f91156ed1f8e7 (diff) | |
download | lua-language-server-310b96f3e4b285a3a8422745ab01a32ca144e91a.zip |
resolve utf16 bom
Diffstat (limited to 'script/encoder')
-rw-r--r-- | script/encoder/init.lua | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/script/encoder/init.lua b/script/encoder/init.lua index f33f5a1d..d7753c1f 100644 --- a/script/encoder/init.lua +++ b/script/encoder/init.lua @@ -4,6 +4,8 @@ local utf16be = require 'encoder.utf16be' ---@alias encoder.encoding '"utf8"'|'"utf16"'|'"utf16le"'|'"utf16be"' +---@alias encoder.bom '"no"'|'"yes"'|'"auto"' + local m = {} ---@param encoding encoder.encoding @@ -63,9 +65,13 @@ end ---@param encoding encoder.encoding ---@param text string +---@param bom encoder.bom ---@return string -function m.encode(encoding, text) +function m.encode(encoding, text, bom) if encoding == 'utf8' then + if bom == 'yes' then + text = '\xEF\xBB\xBF' .. text + end return text end if encoding == 'ansi' then @@ -73,10 +79,20 @@ function m.encode(encoding, text) end if encoding == 'utf16' or encoding == 'utf16le' then - return utf16le.encode(text) + text = utf16le.encode(text) + if bom == 'yes' + or bom == 'auto' then + text = '\xFF\xFE' .. text + end + return text end if encoding == 'utf16be' then - return utf16be.encode(text) + text = utf16be.encode(text) + if bom == 'yes' + or bom == 'auto' then + text = '\xFE\xFF' .. text + end + return text end log.error('Unsupport encode encoding:', encoding) return text |