Skip to content

Commit dca1d40

Browse files
LemonBoybrammool
authored andcommitted
patch 8.2.4838: checking for absolute path is not trivial
Problem: Checking for absolute path is not trivial. Solution: Add isabsolutepath(). (closes #10303)
1 parent 68a573c commit dca1d40

File tree

7 files changed

+55
-0
lines changed

7 files changed

+55
-0
lines changed

runtime/doc/builtin.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ inputsecret({prompt} [, {text}]) String like input() but hiding the text
295295
insert({object}, {item} [, {idx}]) List insert {item} in {object} [before {idx}]
296296
interrupt() none interrupt script execution
297297
invert({expr}) Number bitwise invert
298+
isabsolutepath({path}) Number |TRUE| if {path} is an absolute path
298299
isdirectory({directory}) Number |TRUE| if {directory} is a directory
299300
isinf({expr}) Number determine if {expr} is infinity value
300301
(positive or negative)
@@ -4672,6 +4673,24 @@ invert({expr}) *invert()*
46724673
< Can also be used as a |method|: >
46734674
:let bits = bits->invert()
46744675
4676+
isabsolutepath({directory}) *isabsolutepath()*
4677+
The result is a Number, which is |TRUE| when {path} is an
4678+
absolute path.
4679+
< On Unix, a path is considered absolute when it starts with '/'.
4680+
On MS-Windows, it is considered absolute when it starts with an
4681+
optional drive prefix and is followed by a '\' or '/'. UNC paths
4682+
are always absolute.
4683+
Example: >
4684+
echo isabsolutepath('/usr/share/') " 1
4685+
echo isabsolutepath('./foobar') " 0
4686+
echo isabsolutepath('C:\Windows') " 1
4687+
echo isabsolutepath('foobar') " 0
4688+
echo isabsolutepath('\\remote\file') " 1
4689+
4690+
Can also be used as a |method|: >
4691+
GetName()->isabsolutepath()
4692+
4693+
46754694
isdirectory({directory}) *isdirectory()*
46764695
The result is a Number, which is |TRUE| when a directory
46774696
with the name {directory} exists. If {directory} doesn't

runtime/doc/usr_41.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,7 @@ System functions and manipulation of files:
904904
getfperm() get the permissions of a file
905905
setfperm() set the permissions of a file
906906
getftype() get the kind of a file
907+
isabsolutepath() check if a path is absolute
907908
isdirectory() check if a directory exists
908909
getfsize() get the size of a file
909910
getcwd() get the current working directory

src/evalfunc.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1969,6 +1969,8 @@ static funcentry_T global_functions[] =
19691969
ret_void, f_interrupt},
19701970
{"invert", 1, 1, FEARG_1, arg1_number,
19711971
ret_number, f_invert},
1972+
{"isabsolutepath", 1, 1, FEARG_1, arg1_string,
1973+
ret_number_bool, f_isabsolutepath},
19721974
{"isdirectory", 1, 1, FEARG_1, arg1_string,
19731975
ret_number_bool, f_isdirectory},
19741976
{"isinf", 1, 1, FEARG_1, arg1_float_or_nr,

src/filepath.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,6 +1416,18 @@ f_isdirectory(typval_T *argvars, typval_T *rettv)
14161416
rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
14171417
}
14181418

1419+
/*
1420+
* "isabsolutepath()" function
1421+
*/
1422+
void
1423+
f_isabsolutepath(typval_T *argvars, typval_T *rettv)
1424+
{
1425+
if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
1426+
return;
1427+
1428+
rettv->vval.v_number = mch_isFullName(tv_get_string_strict(&argvars[0]));
1429+
}
1430+
14191431
/*
14201432
* Create the directory in which "dir" is located, and higher levels when
14211433
* needed.

src/proto/filepath.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ void f_glob(typval_T *argvars, typval_T *rettv);
2222
void f_glob2regpat(typval_T *argvars, typval_T *rettv);
2323
void f_globpath(typval_T *argvars, typval_T *rettv);
2424
void f_isdirectory(typval_T *argvars, typval_T *rettv);
25+
void f_isabsolutepath(typval_T *argvars, typval_T *rettv);
2526
void f_mkdir(typval_T *argvars, typval_T *rettv);
2627
void f_pathshorten(typval_T *argvars, typval_T *rettv);
2728
void f_readdir(typval_T *argvars, typval_T *rettv);

src/testdir/test_functions.vim

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2887,5 +2887,23 @@ func Test_funcref_to_string()
28872887
call assert_equal("function('g:Test_funcref_to_string')", string(Fn))
28882888
endfunc
28892889

2890+
" Test for isabsolutepath()
2891+
func Test_isabsolutepath()
2892+
call assert_false(isabsolutepath(''))
2893+
call assert_false(isabsolutepath('.'))
2894+
call assert_false(isabsolutepath('../Foo'))
2895+
call assert_false(isabsolutepath('Foo/'))
2896+
if has('win32')
2897+
call assert_true(isabsolutepath('A:\'))
2898+
call assert_true(isabsolutepath('A:\Foo'))
2899+
call assert_true(isabsolutepath('A:/Foo'))
2900+
call assert_false(isabsolutepath('A:Foo'))
2901+
call assert_false(isabsolutepath('\Windows'))
2902+
call assert_true(isabsolutepath('\\Server2\Share\Test\Foo.txt'))
2903+
else
2904+
call assert_true(isabsolutepath('/'))
2905+
call assert_true(isabsolutepath('/usr/share/'))
2906+
endif
2907+
endfunc
28902908

28912909
" vim: shiftwidth=2 sts=2 expandtab

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,8 @@ static char *(features[]) =
746746

747747
static int included_patches[] =
748748
{ /* Add new patch number below this line */
749+
/**/
750+
4838,
749751
/**/
750752
4837,
751753
/**/

0 commit comments

Comments
 (0)