vim-patch:8.2.4838: checking for absolute path is not trivial (#29990)

Problem:    Checking for absolute path is not trivial.
Solution:   Add isabsolutepath(). (closes vim/vim#10303)

dca1d40cd0

vim-patch:8a3b805c6c9c

Co-authored-by: LemonBoy <thatlemon@gmail.com>
This commit is contained in:
zeertzjq 2024-08-06 21:19:12 +08:00 committed by GitHub
parent 28fbba2092
commit 37952bf7b4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 82 additions and 0 deletions

View File

@ -3868,6 +3868,21 @@ invert({expr}) *invert()*
let bits = invert(bits)
<
isabsolutepath({path}) *isabsolutepath()*
The result is a Number, which is |TRUE| when {path} is an
absolute path.
On Unix, a path is considered absolute when it starts with '/'.
On MS-Windows, it is considered absolute when it starts with an
optional drive prefix and is followed by a '\' or '/'. UNC paths
are always absolute.
Example: >vim
echo isabsolutepath('/usr/share/') " 1
echo isabsolutepath('./foobar') " 0
echo isabsolutepath('C:\Windows') " 1
echo isabsolutepath('foobar') " 0
echo isabsolutepath('\\remote\file') " 1
<
isdirectory({directory}) *isdirectory()*
The result is a Number, which is |TRUE| when a directory
with the name {directory} exists. If {directory} doesn't

View File

@ -836,6 +836,7 @@ System functions and manipulation of files:
getfperm() get the permissions of a file
setfperm() set the permissions of a file
getftype() get the kind of a file
isabsolutepath() check if a path is absolute
isdirectory() check if a directory exists
getfsize() get the size of a file
getcwd() get the current working directory

View File

@ -4659,6 +4659,24 @@ function vim.fn.interrupt() end
--- @return any
function vim.fn.invert(expr) end
--- The result is a Number, which is |TRUE| when {path} is an
--- absolute path.
--- On Unix, a path is considered absolute when it starts with '/'.
--- On MS-Windows, it is considered absolute when it starts with an
--- optional drive prefix and is followed by a '\' or '/'. UNC paths
--- are always absolute.
--- Example: >vim
--- echo isabsolutepath('/usr/share/') " 1
--- echo isabsolutepath('./foobar') " 0
--- echo isabsolutepath('C:\Windows') " 1
--- echo isabsolutepath('foobar') " 0
--- echo isabsolutepath('\\remote\file') " 1
--- <
---
--- @param path any
--- @return 0|1
function vim.fn.isabsolutepath(path) end
--- The result is a Number, which is |TRUE| when a directory
--- with the name {directory} exists. If {directory} doesn't
--- exist, or isn't a directory, the result is |FALSE|. {directory}

View File

@ -5686,6 +5686,30 @@ M.funcs = {
params = { { 'expr', 'any' } },
signature = 'invert({expr})',
},
isabsolutepath = {
args = 1,
base = 1,
desc = [=[
The result is a Number, which is |TRUE| when {path} is an
absolute path.
On Unix, a path is considered absolute when it starts with '/'.
On MS-Windows, it is considered absolute when it starts with an
optional drive prefix and is followed by a '\' or '/'. UNC paths
are always absolute.
Example: >vim
echo isabsolutepath('/usr/share/') " 1
echo isabsolutepath('./foobar') " 0
echo isabsolutepath('C:\Windows') " 1
echo isabsolutepath('foobar') " 0
echo isabsolutepath('\\remote\file') " 1
<
]=],
fast = true,
name = 'isabsolutepath',
params = { { 'path', 'any' } },
returns = '0|1',
signature = 'isabsolutepath({path})',
},
isdirectory = {
args = 1,
base = 1,

View File

@ -688,6 +688,12 @@ void f_haslocaldir(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
}
}
/// "isabsolutepath()" function
void f_isabsolutepath(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{
rettv->vval.v_number = path_is_absolute(tv_get_string(&argvars[0]));
}
/// "isdirectory()" function
void f_isdirectory(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{

View File

@ -3559,6 +3559,24 @@ func Test_builtin_check()
unlet bar
endfunc
" Test for isabsolutepath()
func Test_isabsolutepath()
call assert_false(isabsolutepath(''))
call assert_false(isabsolutepath('.'))
call assert_false(isabsolutepath('../Foo'))
call assert_false(isabsolutepath('Foo/'))
if has('win32')
call assert_true(isabsolutepath('A:\'))
call assert_true(isabsolutepath('A:\Foo'))
call assert_true(isabsolutepath('A:/Foo'))
call assert_false(isabsolutepath('A:Foo'))
call assert_false(isabsolutepath('\Windows'))
call assert_true(isabsolutepath('\\Server2\Share\Test\Foo.txt'))
else
call assert_true(isabsolutepath('/'))
call assert_true(isabsolutepath('/usr/share/'))
endif
endfunc
" Test for virtcol()
func Test_virtcol()