diff options
-rw-r--r-- | script/encoder/ansi.lua | 24 | ||||
-rw-r--r-- | script/encoder/init.lua | 29 | ||||
-rw-r--r-- | script/encoder/utf16be.lua | 46 | ||||
-rw-r--r-- | script/encoder/utf16le.lua | 46 | ||||
-rw-r--r-- | script/files.lua | 2 | ||||
-rw-r--r-- | script/library.lua | 2 |
6 files changed, 123 insertions, 26 deletions
diff --git a/script/encoder/ansi.lua b/script/encoder/ansi.lua new file mode 100644 index 00000000..1016e668 --- /dev/null +++ b/script/encoder/ansi.lua @@ -0,0 +1,24 @@ +local platform = require 'bee.platform' +local unicode + +if platform.OS == 'Windows' then + unicode = require 'bee.unicode' +end + +local m = {} + +function m.decode(text) + if not unicode then + return text + end + return unicode.a2u(text) +end + +function m.encode(text) + if not unicode then + return text + end + return unicode.u2a(text) +end + +return m diff --git a/script/encoder/init.lua b/script/encoder/init.lua index a8f9a8ac..152193e7 100644 --- a/script/encoder/init.lua +++ b/script/encoder/init.lua @@ -1,24 +1,5 @@ -local platform = require 'bee.platform' -local unicode - -if platform.OS == 'Windows' then - unicode = require 'bee.unicode' -end - -local m = {} - -function m.ansi2utf8(text) - if not unicode then - return text - end - return unicode.a2u(text) -end - -function m.utf82ansi(text) - if not unicode then - return text - end - return unicode.u2a(text) -end - -return m +return { + ansi = require 'encoder.ansi', + utf16le = require 'encoder.utf16le', + utf16be = require 'encoder.utf16be' +} diff --git a/script/encoder/utf16be.lua b/script/encoder/utf16be.lua new file mode 100644 index 00000000..179a676d --- /dev/null +++ b/script/encoder/utf16be.lua @@ -0,0 +1,46 @@ +local function tochar(code) + return string.char((code >> 8) & 0xFF, code & 0xFF) +end + +local function tobyte(s, i) + local h, l = string.byte(s, i, i+1) + return (h << 8) | l +end + +local function char(code) + if code <= 0xffff then + return tochar(code) + end + code = code - 0x10000 + return tochar(0xD800 + (code >> 10))..tochar(0xDC00 + (code & 0x3FF)) +end + +local m = {} + +function m.encode(s) + local r = {} + for _, c in utf8.codes(s) do + r[#r+1] = char(c) + end + return table.concat(r) +end + +function m.decode(s) + local r = {} + local i = 1 + while i < #s do + local code1 = tobyte(s, i) + if code1 < 0xD800 then + r[#r+1] = utf8.char(code1) + else + i = i + 2 + local code2 = tobyte(s, i) + local code = 0x10000 + ((code1 - 0xD800) << 10) + ((code2 - 0xDC00) & 0x3FF) + r[#r+1] = utf8.char(code) + end + i = i + 2 + end + return table.concat(r) +end + +return m diff --git a/script/encoder/utf16le.lua b/script/encoder/utf16le.lua new file mode 100644 index 00000000..cfbc33c2 --- /dev/null +++ b/script/encoder/utf16le.lua @@ -0,0 +1,46 @@ +local function tochar(code) + return string.char(code & 0xFF, (code >> 8) & 0xFF) +end + +local function tobyte(s, i) + local l, h = string.byte(s, i, i+1) + return (h << 8) | l +end + +local function char(code) + if code <= 0xffff then + return tochar(code) + end + code = code - 0x10000 + return tochar(0xD800 + (code >> 10))..tochar(0xDC00 + (code & 0x3FF)) +end + +local m = {} + +function m.encode(s) + local r = {} + for _, c in utf8.codes(s) do + r[#r+1] = char(c) + end + return table.concat(r) +end + +function m.decode(s) + local r = {} + local i = 1 + while i < #s do + local code1 = tobyte(s, i) + if code1 < 0xD800 then + r[#r+1] = utf8.char(code1) + else + i = i + 2 + local code2 = tobyte(s, i) + local code = 0x10000 + ((code1 - 0xD800) << 10) + ((code2 - 0xDC00) & 0x3FF) + r[#r+1] = utf8.char(code) + end + i = i + 2 + end + return table.concat(r) +end + +return m diff --git a/script/files.lua b/script/files.lua index dac23b86..566b012c 100644 --- a/script/files.lua +++ b/script/files.lua @@ -141,7 +141,7 @@ function m.setText(uri, text, isTrust, instance) end if not isTrust then if config.get 'Lua.runtime.fileEncoding' == 'ansi' then - text = encoder.ansi2utf8(text) + text = encoder.ansi.decode(text) end end if file.originText == text then diff --git a/script/library.lua b/script/library.lua index ba7dc657..66b26b0d 100644 --- a/script/library.lua +++ b/script/library.lua @@ -237,7 +237,7 @@ local function initBuiltIn() if metaDoc then local outPath = metaPath / libName if encoding == 'ansi' then - metaDoc = encoder.utf82ansi(metaDoc) + metaDoc = encoder.ansi.encode(metaDoc) end out:saveFile(libName, metaDoc) m.metaPaths[#m.metaPaths+1] = outPath:string() |