Skip to content

Commit

Permalink
fix: better bom handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lewis6991 committed Sep 23, 2023
1 parent ac922e1 commit dfac106
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 33 deletions.
33 changes: 0 additions & 33 deletions lua/gitsigns/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -355,38 +355,6 @@ function Repo:files_changed()
return ret
end

--- @param ... integer
--- @return string
local function make_bom(...)
local r = {}
---@diagnostic disable-next-line:no-unknown
for i, a in ipairs({ ... }) do
---@diagnostic disable-next-line:no-unknown
r[i] = string.char(a)
end
return table.concat(r)
end

local BOM_TABLE = {
['utf-8'] = make_bom(0xef, 0xbb, 0xbf),
['utf-16le'] = make_bom(0xff, 0xfe),
['utf-16'] = make_bom(0xfe, 0xff),
['utf-16be'] = make_bom(0xfe, 0xff),
['utf-32le'] = make_bom(0xff, 0xfe, 0x00, 0x00),
['utf-32'] = make_bom(0xff, 0xfe, 0x00, 0x00),
['utf-32be'] = make_bom(0x00, 0x00, 0xfe, 0xff),
['utf-7'] = make_bom(0x2b, 0x2f, 0x76),
['utf-1'] = make_bom(0xf7, 0x54, 0x4c),
}

local function strip_bom(x, encoding)
local bom = BOM_TABLE[encoding]
if bom and vim.startswith(x, bom) then
return x:sub(bom:len() + 1)
end
return x
end

--- @param encoding string
--- @return boolean
local function iconv_supported(encoding)
Expand All @@ -407,7 +375,6 @@ function Repo:get_show_text(object, encoding)
local stdout, stderr = self:command({ 'show', object }, { raw = true, suppress_stderr = true })

if encoding and encoding ~= 'utf-8' and iconv_supported(encoding) then
stdout[1] = strip_bom(stdout[1], encoding)
for i, l in ipairs(stdout) do
--- @diagnostic disable-next-line:param-type-mismatch
stdout[i] = vim.iconv(l, encoding, 'utf-8')
Expand Down
39 changes: 39 additions & 0 deletions lua/gitsigns/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,41 @@ end

M.path_sep = package.config:sub(1, 1)

--- @param ... integer
--- @return string
local function make_bom(...)
local r = {}
---@diagnostic disable-next-line:no-unknown
for i, a in ipairs({ ... }) do
---@diagnostic disable-next-line:no-unknown
r[i] = string.char(a)
end
return table.concat(r)
end

local BOM_TABLE = {
['utf-8'] = make_bom(0xef, 0xbb, 0xbf),
['utf-16le'] = make_bom(0xff, 0xfe),
['utf-16'] = make_bom(0xfe, 0xff),
['utf-16be'] = make_bom(0xfe, 0xff),
['utf-32le'] = make_bom(0xff, 0xfe, 0x00, 0x00),
['utf-32'] = make_bom(0xff, 0xfe, 0x00, 0x00),
['utf-32be'] = make_bom(0x00, 0x00, 0xfe, 0xff),
['utf-7'] = make_bom(0x2b, 0x2f, 0x76),
['utf-1'] = make_bom(0xf7, 0x54, 0x4c),
}

---@param x string
---@param encoding string
---@return string
local function add_bom(x, encoding)
local bom = BOM_TABLE[encoding]
if bom then
return bom .. x
end
return x
end

--- @param bufnr integer
--- @return string[]
function M.buf_lines(bufnr)
Expand All @@ -65,6 +100,10 @@ function M.buf_lines(bufnr)
buftext[#buftext + 1] = ''
end

if vim.bo[bufnr].bomb then
buftext[1] = add_bom(buftext[1], vim.bo[bufnr].fileencoding)
end

return buftext
end

Expand Down

0 comments on commit dfac106

Please sign in to comment.