spacevim/autoload/SpaceVim/api/iconv/codecs/utf8.vim
JIe 2bb7059579
Some checks failed
Detach Plugins / check (FlyGrep.vim) (push) Has been cancelled
Detach Plugins / check (GitHub.vim) (push) Has been cancelled
Detach Plugins / check (JavaUnit.vim) (push) Has been cancelled
Detach Plugins / check (SourceCounter.vim) (push) Has been cancelled
Detach Plugins / check (cpicker.nvim) (push) Has been cancelled
Detach Plugins / check (dein-ui.vim) (push) Has been cancelled
Detach Plugins / check (git.vim) (push) Has been cancelled
Detach Plugins / check (iedit.vim) (push) Has been cancelled
Detach Plugins / check (scrollbar.vim) (push) Has been cancelled
Detach Plugins / check (vim-chat) (push) Has been cancelled
Detach Plugins / check (vim-cheat) (push) Has been cancelled
Detach Plugins / check (vim-todo) (push) Has been cancelled
Detach Plugins / check (xmake.vim) (push) Has been cancelled
test / Linux (nvim, nightly) (push) Has been cancelled
test / Linux (nvim, v0.3.8) (push) Has been cancelled
test / Linux (nvim, v0.4.0) (push) Has been cancelled
test / Linux (nvim, v0.4.2) (push) Has been cancelled
test / Linux (nvim, v0.4.3) (push) Has been cancelled
test / Linux (nvim, v0.4.4) (push) Has been cancelled
test / Linux (nvim, v0.5.0) (push) Has been cancelled
test / Linux (nvim, v0.5.1) (push) Has been cancelled
test / Linux (nvim, v0.6.0) (push) Has been cancelled
test / Linux (nvim, v0.6.1) (push) Has been cancelled
test / Linux (nvim, v0.7.0) (push) Has been cancelled
test / Linux (nvim, v0.7.2) (push) Has been cancelled
test / Linux (nvim, v0.8.0) (push) Has been cancelled
test / Linux (nvim, v0.8.1) (push) Has been cancelled
test / Linux (nvim, v0.8.2) (push) Has been cancelled
test / Linux (nvim, v0.8.3) (push) Has been cancelled
test / Linux (nvim, v0.9.0) (push) Has been cancelled
test / Linux (nvim, v0.9.1) (push) Has been cancelled
test / Linux (true, vim, v7.4.052) (push) Has been cancelled
test / Linux (true, vim, v7.4.1689) (push) Has been cancelled
test / Linux (true, vim, v7.4.629) (push) Has been cancelled
test / Linux (true, vim, v8.0.0027) (push) Has been cancelled
test / Linux (true, vim, v8.0.0183) (push) Has been cancelled
test / Linux (vim, nightly) (push) Has been cancelled
test / Linux (vim, v8.0.0184) (push) Has been cancelled
test / Linux (vim, v8.0.1453) (push) Has been cancelled
test / Linux (vim, v8.1.2269) (push) Has been cancelled
test / Linux (vim, v8.2.2434) (push) Has been cancelled
test / Linux (vim, v8.2.3995) (push) Has been cancelled
test / Windows (nvim, nightly) (push) Has been cancelled
test / Windows (nvim, v0.3.8) (push) Has been cancelled
test / Windows (nvim, v0.4.2) (push) Has been cancelled
test / Windows (nvim, v0.4.3) (push) Has been cancelled
test / Windows (nvim, v0.4.4) (push) Has been cancelled
test / Windows (nvim, v0.5.0) (push) Has been cancelled
test / Windows (nvim, v0.5.1) (push) Has been cancelled
test / Windows (nvim, v0.6.0) (push) Has been cancelled
test / Windows (nvim, v0.6.1) (push) Has been cancelled
test / Windows (nvim, v0.7.0) (push) Has been cancelled
test / Windows (nvim, v0.7.2) (push) Has been cancelled
test / Windows (nvim, v0.8.0) (push) Has been cancelled
test / Windows (nvim, v0.8.1) (push) Has been cancelled
test / Windows (nvim, v0.8.2) (push) Has been cancelled
test / Windows (nvim, v0.8.3) (push) Has been cancelled
test / Windows (nvim, v0.9.0) (push) Has been cancelled
test / Windows (nvim, v0.9.1) (push) Has been cancelled
test / Windows (vim, nightly) (push) Has been cancelled
test / Windows (vim, v7.4.1185) (push) Has been cancelled
test / Windows (vim, v7.4.1689) (push) Has been cancelled
test / Windows (vim, v8.0.0027) (push) Has been cancelled
test / Windows (vim, v8.0.1453) (push) Has been cancelled
test / Windows (vim, v8.1.2269) (push) Has been cancelled
test / Windows (vim, v8.2.2434) (push) Has been cancelled
test / Windows (vim, v8.2.3995) (push) Has been cancelled
docker / docker (push) Has been cancelled
mirror / check (coding) (push) Has been cancelled
mirror / check (gitee) (push) Has been cancelled
mirror / check (gitlab) (push) Has been cancelled
init
2024-08-21 14:17:26 +08:00

150 lines
4.9 KiB
VimL

function! SpaceVim#api#iconv#codecs#utf8#import() abort
return s:utf8
endfunction
let s:base = SpaceVim#api#iconv#codecs#base#import()
let s:utf8 = {}
let s:utf8.Codec = {}
call extend(s:utf8.Codec, s:base.Codec)
let s:utf8.Codec.encoding = "UTF-8"
function s:utf8.Codec.CheckByte(c)
return (a:c >= 0x80) && ((a:c - 0x80) < 0x40)
endfunction
function! s:utf8.Codec.mbtowc(input, start)
let c = a:input[a:start]
if c < 0x80
return [[c], a:start + 1]
elseif c < 0xc2
return self.error('UnicodeDecodeError', 'invalid', a:input, a:start, a:start)
elseif c < 0xe0
if a:start + 1 >= len(a:input)
return self.error('UnicodeDecodeError', 'incomplete', a:input, a:start, len(a:input) - 1)
endif
if !(self.CheckByte(a:input[a:start + 1]))
return self.error('UnicodeDecodeError', 'invalid', a:input, a:start, a:start + 1)
endif
let wc = ((c % 0x20) * 0x40) + (a:input[a:start + 1] - 0x80)
return [[wc], a:start + 2]
elseif c < 0xf0
if a:start + 2 >= len(a:input)
return self.error('UnicodeDecodeError', 'incomplete', a:input, a:start, len(a:input) - 1)
endif
if !(self.CheckByte(a:input[a:start + 1])
\ && self.CheckByte(a:input[a:start + 2])
\ && (c >= 0xe1 || a:input[a:start + 1] >= 0xa0))
return self.error('UnicodeDecodeError', 'invalid', a:input, a:start, a:start + 2)
endif
let wc = ((c % 0x10) * 0x1000)
\ + ((a:input[a:start + 1] - 0x80) * 0x40)
\ + (a:input[a:start + 2] - 0x80)
return [[wc], a:start + 3]
elseif c < 0xf8
if a:start + 3 >= len(a:input)
return self.error('UnicodeDecodeError', 'incomplete', a:input, a:start, len(a:input) - 1)
endif
if !(self.CheckByte(a:input[a:start + 1])
\ && self.CheckByte(a:input[a:start + 2])
\ && self.CheckByte(a:input[a:start + 3])
\ && (c >= 0xf1 || a:input[a:start + 1] >= 0x90))
return self.error('UnicodeDecodeError', 'invalid', a:input, a:start, a:start + 3)
endif
let wc = ((c % 0x08) * 0x40000)
\ + ((a:input[a:start + 1] - 0x80) * 0x1000)
\ + ((a:input[a:start + 2] - 0x80) * 0x40)
\ + (a:input[a:start + 3] - 0x80)
return [[wc], a:start + 4]
elseif c < 0xfc
if a:start + 4 >= len(a:input)
return self.error('UnicodeDecodeError', 'incomplete', a:input, a:start, len(a:input) - 1)
endif
if !(self.CheckByte(a:input[a:start + 1])
\ && self.CheckByte(a:input[a:start + 2])
\ && self.CheckByte(a:input[a:start + 3])
\ && self.CheckByte(a:input[a:start + 4])
\ && (c >= 0xf9 || a:input[a:start + 1] >= 0x88))
return self.error('UnicodeDecodeError', 'invalid', a:input, a:start, a:start + 4)
endif
let wc = ((c % 0x04) * 0x1000000)
\ + ((a:input[a:start + 1] - 0x80) * 0x40000)
\ + ((a:input[a:start + 2] - 0x80) * 0x1000)
\ + ((a:input[a:start + 3] - 0x80) * 0x40)
\ + (a:input[a:start + 4] - 0x80)
return [[wc], a:start + 5]
elseif c < 0xfe
if a:start + 5 >= len(a:input)
return self.error('UnicodeDecodeError', 'incomplete', a:input, a:start, len(a:input) - 1)
endif
if !(self.CheckByte(a:input[a:start + 1])
\ && self.CheckByte(a:input[a:start + 2])
\ && self.CheckByte(a:input[a:start + 3])
\ && self.CheckByte(a:input[a:start + 4])
\ && self.CheckByte(a:input[a:start + 5])
\ && (c >= 0xfd || a:input[a:start + 1] >= 0x84))
return self.error('UnicodeDecodeError', 'invalid', a:input, a:start, a:start + 5)
endif
let wc = ((c % 0x02) * 0x40000000)
\ + ((a:input[a:start + 1] - 0x80) * 0x1000000)
\ + ((a:input[a:start + 2] - 0x80) * 0x40000)
\ + ((a:input[a:start + 3] - 0x80) * 0x1000)
\ + ((a:input[a:start + 4] - 0x80) * 0x40)
\ + (a:input[a:start + 5] - 0x80)
return [[wc], a:start + 6]
else
return self.error('UnicodeDecodeError', 'invalid', a:input, a:start, a:start)
endif
endfunction
function! s:utf8.Codec.wctomb(input, start)
let wc = a:input[a:start]
let cnt = 0
if wc < 0x80
let cnt = 1
elseif wc < 0x800
let cnt = 2
elseif wc < 0x10000
let cnt = 3
elseif wc < 0x200000
let cnt = 4
elseif wc < 0x4000000
let cnt = 5
elseif wc <= 0x7fffffff
let cnt = 6
else
return self.error('UnicodeEncodeError', 'invalid', a:input, a:start, a:start)
endif
let r = []
if cnt >= 6
call insert(r, 0x80 + (wc % 0x40))
let wc = (wc / 0x40) + 0x4000000
endif
if cnt >= 5
call insert(r, 0x80 + (wc % 0x40))
let wc = (wc / 0x40) + 0x200000
endif
if cnt >= 4
call insert(r, 0x80 + (wc % 0x40))
let wc = (wc / 0x40) + 0x10000
endif
if cnt >= 3
call insert(r, 0x80 + (wc % 0x40))
let wc = (wc / 0x40) + 0x800
endif
if cnt >= 2
call insert(r, 0x80 + (wc % 0x40))
let wc = (wc / 0x40) + 0xc0
endif
if cnt >= 1
call insert(r, wc)
endif
return [r, a:start + 1]
endfunction