]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 8.2.4838: checking for absolute path is not trivial v8.2.4838
authorLemonBoy <thatlemon@gmail.com>
Thu, 28 Apr 2022 14:26:33 +0000 (15:26 +0100)
committerBram Moolenaar <Bram@vim.org>
Thu, 28 Apr 2022 14:26:33 +0000 (15:26 +0100)
Problem:    Checking for absolute path is not trivial.
Solution:   Add isabsolutepath(). (closes #10303)

runtime/doc/builtin.txt
runtime/doc/usr_41.txt
src/evalfunc.c
src/filepath.c
src/proto/filepath.pro
src/testdir/test_functions.vim
src/version.c

index bfb18eda661f8ae79580541533d8fb059835d27a..1b091b16f8c3e805c3a38b507a2b7c055a4486a6 100644 (file)
@@ -295,6 +295,7 @@ inputsecret({prompt} [, {text}]) String     like input() but hiding the text
 insert({object}, {item} [, {idx}]) List        insert {item} in {object} [before {idx}]
 interrupt()                    none    interrupt script execution
 invert({expr})                 Number  bitwise invert
+isabsolutepath({path})         Number  |TRUE| if {path} is an absolute path
 isdirectory({directory})       Number  |TRUE| if {directory} is a directory
 isinf({expr})                  Number  determine if {expr} is infinity value
                                        (positive or negative)
@@ -4672,6 +4673,24 @@ invert({expr})                                           *invert()*
 <              Can also be used as a |method|: >
                        :let bits = bits->invert()
 
+isabsolutepath({directory})                            *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: >
+                       echo isabsolutepath('/usr/share/')      " 1
+                       echo isabsolutepath('./foobar')         " 0
+                       echo isabsolutepath('C:\Windows')       " 1
+                       echo isabsolutepath('foobar')           " 0
+                       echo isabsolutepath('\\remote\file')    " 1
+
+               Can also be used as a |method|: >
+                       GetName()->isabsolutepath()
+
+
 isdirectory({directory})                               *isdirectory()*
                The result is a Number, which is |TRUE| when a directory
                with the name {directory} exists.  If {directory} doesn't
index e9e16fe5464bc8a5a0cccecf696c9183f549fd8a..a9a1fcd88318b6d6fd77885c724e8104b2e2c0f5 100644 (file)
@@ -904,6 +904,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
index 86a7c0d138f8d836b7bf29581711757f1bc01b07..c4abfddc2cb0906ae4c36154c177bd74e0802560 100644 (file)
@@ -1969,6 +1969,8 @@ static funcentry_T global_functions[] =
                        ret_void,           f_interrupt},
     {"invert",         1, 1, FEARG_1,      arg1_number,
                        ret_number,         f_invert},
+    {"isabsolutepath", 1, 1, FEARG_1,      arg1_string,
+                       ret_number_bool,    f_isabsolutepath},
     {"isdirectory",    1, 1, FEARG_1,      arg1_string,
                        ret_number_bool,    f_isdirectory},
     {"isinf",          1, 1, FEARG_1,      arg1_float_or_nr,
index 1bde4200d3cbeabb9d346a6632548dadeee44199..6ee5fad9603b24a56a0bfa87b536e8f742694bf2 100644 (file)
@@ -1416,6 +1416,18 @@ f_isdirectory(typval_T *argvars, typval_T *rettv)
     rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
 }
 
+/*
+ * "isabsolutepath()" function
+ */
+    void
+f_isabsolutepath(typval_T *argvars, typval_T *rettv)
+{
+    if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
+       return;
+
+    rettv->vval.v_number = mch_isFullName(tv_get_string_strict(&argvars[0]));
+}
+
 /*
  * Create the directory in which "dir" is located, and higher levels when
  * needed.
index bf3d5163dad45b32038dc0ccdcba35f30949e7a1..2970e503e5121b11d18f090958eda39f159bc68d 100644 (file)
@@ -22,6 +22,7 @@ void f_glob(typval_T *argvars, typval_T *rettv);
 void f_glob2regpat(typval_T *argvars, typval_T *rettv);
 void f_globpath(typval_T *argvars, typval_T *rettv);
 void f_isdirectory(typval_T *argvars, typval_T *rettv);
+void f_isabsolutepath(typval_T *argvars, typval_T *rettv);
 void f_mkdir(typval_T *argvars, typval_T *rettv);
 void f_pathshorten(typval_T *argvars, typval_T *rettv);
 void f_readdir(typval_T *argvars, typval_T *rettv);
index a87ca3b585abb243814499bcbfbd731ee5c4e38e..dbed757ff3a968c9f0f493b5c2a1934974939c7d 100644 (file)
@@ -2887,5 +2887,23 @@ func Test_funcref_to_string()
   call assert_equal("function('g:Test_funcref_to_string')", string(Fn))
 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
 
 " vim: shiftwidth=2 sts=2 expandtab
index 2fa8992d775812f7ae97ca47925faae32b9a9366..1b87213bc2ab936dea98a7afab4f8fc197a0005d 100644 (file)
@@ -746,6 +746,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    4838,
 /**/
     4837,
 /**/