mirror of
https://github.com/vim/vim.git
synced 2025-10-04 05:25:06 -04:00
patch 8.2.2343: Vim9: return type of readfile() is any
Problem: Vim9: return type of readfile() is any. Solution: Add readblob() so that readfile() can be expected to always return a list of strings. (closes #7671)
This commit is contained in:
@@ -1344,12 +1344,14 @@ static funcentry_T global_functions[] =
|
||||
ret_number, f_rand},
|
||||
{"range", 1, 3, FEARG_1, NULL,
|
||||
ret_list_number, f_range},
|
||||
{"readblob", 1, 1, FEARG_1, NULL,
|
||||
ret_blob, f_readblob},
|
||||
{"readdir", 1, 3, FEARG_1, NULL,
|
||||
ret_list_string, f_readdir},
|
||||
{"readdirex", 1, 3, FEARG_1, NULL,
|
||||
ret_list_dict_any, f_readdirex},
|
||||
{"readfile", 1, 3, FEARG_1, NULL,
|
||||
ret_any, f_readfile},
|
||||
ret_list_string, f_readfile},
|
||||
{"reduce", 2, 3, FEARG_1, NULL,
|
||||
ret_any, f_reduce},
|
||||
{"reg_executing", 0, 0, 0, NULL,
|
||||
|
@@ -1640,11 +1640,11 @@ f_readdirex(typval_T *argvars, typval_T *rettv)
|
||||
/*
|
||||
* "readfile()" function
|
||||
*/
|
||||
void
|
||||
f_readfile(typval_T *argvars, typval_T *rettv)
|
||||
static void
|
||||
read_file_or_blob(typval_T *argvars, typval_T *rettv, int always_blob)
|
||||
{
|
||||
int binary = FALSE;
|
||||
int blob = FALSE;
|
||||
int blob = always_blob;
|
||||
int failed = FALSE;
|
||||
char_u *fname;
|
||||
FILE *fd;
|
||||
@@ -1796,7 +1796,8 @@ f_readfile(typval_T *argvars, typval_T *rettv)
|
||||
|
||||
if (dest < buf)
|
||||
{
|
||||
adjust_prevlen = (int)(buf - dest); // must be 1 or 2
|
||||
// must be 1 or 2
|
||||
adjust_prevlen = (int)(buf - dest);
|
||||
dest = buf;
|
||||
}
|
||||
if (readlen > p - buf + 1)
|
||||
@@ -1866,6 +1867,24 @@ f_readfile(typval_T *argvars, typval_T *rettv)
|
||||
fclose(fd);
|
||||
}
|
||||
|
||||
/*
|
||||
* "readblob()" function
|
||||
*/
|
||||
void
|
||||
f_readblob(typval_T *argvars, typval_T *rettv)
|
||||
{
|
||||
read_file_or_blob(argvars, rettv, TRUE);
|
||||
}
|
||||
|
||||
/*
|
||||
* "readfile()" function
|
||||
*/
|
||||
void
|
||||
f_readfile(typval_T *argvars, typval_T *rettv)
|
||||
{
|
||||
read_file_or_blob(argvars, rettv, FALSE);
|
||||
}
|
||||
|
||||
/*
|
||||
* "resolve()" function
|
||||
*/
|
||||
|
@@ -1,5 +1,6 @@
|
||||
/* filepath.c */
|
||||
int modify_fname(char_u *src, int tilde_file, int *usedlen, char_u **fnamep, char_u **bufp, int *fnamelen);
|
||||
void shorten_dir(char_u *str);
|
||||
void f_chdir(typval_T *argvars, typval_T *rettv);
|
||||
void f_delete(typval_T *argvars, typval_T *rettv);
|
||||
void f_executable(typval_T *argvars, typval_T *rettv);
|
||||
@@ -21,10 +22,10 @@ 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_mkdir(typval_T *argvars, typval_T *rettv);
|
||||
void shorten_dir(char_u *str);
|
||||
void f_pathshorten(typval_T *argvars, typval_T *rettv);
|
||||
void f_readdir(typval_T *argvars, typval_T *rettv);
|
||||
void f_readdirex(typval_T *argvars, typval_T *rettv);
|
||||
void f_readblob(typval_T *argvars, typval_T *rettv);
|
||||
void f_readfile(typval_T *argvars, typval_T *rettv);
|
||||
void f_resolve(typval_T *argvars, typval_T *rettv);
|
||||
void f_tempname(typval_T *argvars, typval_T *rettv);
|
||||
|
@@ -623,6 +623,32 @@ def Test_readdir()
|
||||
eval expand('sautest')->readdirex((e) => e.name[0] !=# '.')
|
||||
enddef
|
||||
|
||||
def Test_readblob()
|
||||
var blob = 0z12341234
|
||||
writefile(blob, 'Xreadblob')
|
||||
var read: blob = readblob('Xreadblob')
|
||||
assert_equal(blob, read)
|
||||
|
||||
var lines =<< trim END
|
||||
var read: list<string> = readblob('Xreadblob')
|
||||
END
|
||||
CheckDefAndScriptFailure(lines, 'E1012: Type mismatch; expected list<string> but got blob', 1)
|
||||
delete('Xreadblob')
|
||||
enddef
|
||||
|
||||
def Test_readfile()
|
||||
var text = ['aaa', 'bbb', 'ccc']
|
||||
writefile(text, 'Xreadfile')
|
||||
var read: list<string> = readfile('Xreadfile')
|
||||
assert_equal(text, read)
|
||||
|
||||
var lines =<< trim END
|
||||
var read: dict<string> = readfile('Xreadfile')
|
||||
END
|
||||
CheckDefAndScriptFailure(lines, 'E1012: Type mismatch; expected dict<string> but got list<string>', 1)
|
||||
delete('Xreadfile')
|
||||
enddef
|
||||
|
||||
def Test_remove_return_type()
|
||||
var l = remove({one: [1, 2], two: [3, 4]}, 'one')
|
||||
var res = 0
|
||||
|
@@ -750,6 +750,8 @@ static char *(features[]) =
|
||||
|
||||
static int included_patches[] =
|
||||
{ /* Add new patch number below this line */
|
||||
/**/
|
||||
2343,
|
||||
/**/
|
||||
2342,
|
||||
/**/
|
||||
|
Reference in New Issue
Block a user