2019-06-03 14:58:18 +00:00
|
|
|
" don't spam the user when Vim is started in Vi compatibility mode
|
|
|
|
let s:cpo_save = &cpo
|
|
|
|
set cpo&vim
|
|
|
|
|
|
|
|
function! go#uri#Encode(value) abort
|
|
|
|
return s:encode(a:value, '[^A-Za-z0-9_.~-]')
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! go#uri#EncodePath(value) abort
|
|
|
|
let l:separator = '/'
|
|
|
|
if go#util#IsWin()
|
|
|
|
let l:separator = '\\'
|
|
|
|
endif
|
|
|
|
return s:encode(a:value, '[^' . l:separator . 'A-Za-z0-9_.~-]')
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:encode(value, unreserved)
|
|
|
|
return substitute(
|
|
|
|
\ a:value,
|
|
|
|
\ a:unreserved,
|
2020-10-28 13:35:24 +00:00
|
|
|
\ '\=s:encodechar(submatch(0))',
|
2019-06-03 14:58:18 +00:00
|
|
|
\ 'g'
|
|
|
|
\)
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! go#uri#Decode(value) abort
|
|
|
|
return substitute(
|
|
|
|
\ a:value,
|
|
|
|
\ '%\(\x\x\)',
|
2020-10-28 13:35:24 +00:00
|
|
|
\ '\=s:decodehex(submatch(1))',
|
2019-06-03 14:58:18 +00:00
|
|
|
\ 'g'
|
|
|
|
\)
|
|
|
|
endfunction
|
2020-10-28 13:35:24 +00:00
|
|
|
|
|
|
|
function! s:encodechar(value)
|
|
|
|
let l:idx = 0
|
|
|
|
let l:encoded = ''
|
|
|
|
while l:idx < strlen(a:value)
|
|
|
|
let l:byte = strpart(a:value, l:idx, 1)
|
|
|
|
let l:encoded = printf('%s%%%02X', l:encoded, char2nr(l:byte))
|
|
|
|
let l:idx += 1
|
|
|
|
endwhile
|
|
|
|
|
|
|
|
return l:encoded
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:decodehex(value)
|
|
|
|
return eval(printf('"\x%s"', a:value))
|
|
|
|
endfunction
|
|
|
|
|
2019-06-03 14:58:18 +00:00
|
|
|
" restore Vi compatibility settings
|
|
|
|
let &cpo = s:cpo_save
|
|
|
|
unlet s:cpo_save
|
|
|
|
|
|
|
|
" vim: sw=2 ts=2 et
|