Simplify the code for escaping strings for Windows

This commit is contained in:
w0rp 2017-06-14 11:05:49 +01:00
parent f472e04b09
commit 3442e58c8b
1 changed files with 10 additions and 15 deletions

View File

@ -155,25 +155,20 @@ function! ale#Set(variable_name, default) abort
return l:value
endfunction
function! s:EscapePercents(str) abort
return substitute(a:str, '%', '%%', 'g')
endfunction
" Escape a string suitably for each platform.
" shellescape does not work on Windows.
function! ale#Escape(str) abort
if fnamemodify(&shell, ':t') ==? 'cmd.exe'
if a:str =~# '\v^[a-zA-Z0-9-_\\/:%]+$'
return s:EscapePercents(a:str)
endif
if a:str =~# ' '
return '"'
\ . substitute(s:EscapePercents(a:str), '"', '""', 'g')
\ . '"'
endif
return s:EscapePercents(substitute(a:str, '\v([&|<>^])', '^\1', 'g'))
" If the string contains spaces, it will be surrounded by quotes.
" Otherwise, special characters will be escaped with carets (^).
return substitute(
\ a:str =~# ' '
\ ? '"' . substitute(a:str, '"', '""', 'g') . '"'
\ : substitute(a:str, '\v([&|<>^])', '^\1', 'g'),
\ '%',
\ '%%',
\ 'g',
\)
endif
return shellescape (a:str)