mirror of
https://github.com/vim/vim.git
synced 2025-09-30 04:44:14 -04:00
patch 9.0.0430: cannot use repeat() with a blob
Problem: Cannot use repeat() with a blob. Solution: Implement blob repeat. (closes #11090)
This commit is contained in:
committed by
Bram Moolenaar
parent
0adae2da17
commit
375141e1f8
@@ -471,7 +471,8 @@ remove({blob}, {idx} [, {end}]) Number/Blob
|
|||||||
remove bytes {idx}-{end} from {blob}
|
remove bytes {idx}-{end} from {blob}
|
||||||
remove({dict}, {key}) any remove entry {key} from {dict}
|
remove({dict}, {key}) any remove entry {key} from {dict}
|
||||||
rename({from}, {to}) Number rename (move) file from {from} to {to}
|
rename({from}, {to}) Number rename (move) file from {from} to {to}
|
||||||
repeat({expr}, {count}) String repeat {expr} {count} times
|
repeat({expr}, {count}) List/Blob/String
|
||||||
|
repeat {expr} {count} times
|
||||||
resolve({filename}) String get filename a shortcut points to
|
resolve({filename}) String get filename a shortcut points to
|
||||||
reverse({list}) List reverse {list} in-place
|
reverse({list}) List reverse {list} in-place
|
||||||
round({expr}) Float round off {expr}
|
round({expr}) Float round off {expr}
|
||||||
@@ -7294,8 +7295,8 @@ repeat({expr}, {count}) *repeat()*
|
|||||||
result. Example: >
|
result. Example: >
|
||||||
:let separator = repeat('-', 80)
|
:let separator = repeat('-', 80)
|
||||||
< When {count} is zero or negative the result is empty.
|
< When {count} is zero or negative the result is empty.
|
||||||
When {expr} is a |List| the result is {expr} concatenated
|
When {expr} is a |List| or a |Blob| the result is {expr}
|
||||||
{count} times. Example: >
|
concatenated {count} times. Example: >
|
||||||
:let longlist = repeat(['a', 'b'], 3)
|
:let longlist = repeat(['a', 'b'], 3)
|
||||||
< Results in ['a', 'b', 'a', 'b', 'a', 'b'].
|
< Results in ['a', 'b', 'a', 'b', 'a', 'b'].
|
||||||
|
|
||||||
|
@@ -3329,4 +3329,6 @@ EXTERN char e_window_unexpectedly_close_while_searching_for_tags[]
|
|||||||
#ifdef FEAT_EVAL
|
#ifdef FEAT_EVAL
|
||||||
EXTERN char e_cannot_use_partial_with_dictionary_for_defer[]
|
EXTERN char e_cannot_use_partial_with_dictionary_for_defer[]
|
||||||
INIT(= N_("E1300: Cannot use a partial with dictionary for :defer"));
|
INIT(= N_("E1300: Cannot use a partial with dictionary for :defer"));
|
||||||
|
EXTERN char e_string_number_list_or_blob_required_for_argument_nr[]
|
||||||
|
INIT(= N_("E1301: String, Number, List or Blob required for argument %d"));
|
||||||
#endif
|
#endif
|
||||||
|
@@ -872,6 +872,7 @@ arg_repeat1(type_T *type, type_T *decl_type UNUSED, argcontext_T *context)
|
|||||||
|| type->tt_type == VAR_UNKNOWN
|
|| type->tt_type == VAR_UNKNOWN
|
||||||
|| type->tt_type == VAR_STRING
|
|| type->tt_type == VAR_STRING
|
||||||
|| type->tt_type == VAR_NUMBER
|
|| type->tt_type == VAR_NUMBER
|
||||||
|
|| type->tt_type == VAR_BLOB
|
||||||
|| type->tt_type == VAR_LIST)
|
|| type->tt_type == VAR_LIST)
|
||||||
return OK;
|
return OK;
|
||||||
|
|
||||||
@@ -4400,6 +4401,10 @@ f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* "function()" function
|
||||||
|
* "funcref()" function
|
||||||
|
*/
|
||||||
static void
|
static void
|
||||||
common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
|
common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
|
||||||
{
|
{
|
||||||
@@ -8399,18 +8404,19 @@ f_rename(typval_T *argvars, typval_T *rettv)
|
|||||||
f_repeat(typval_T *argvars, typval_T *rettv)
|
f_repeat(typval_T *argvars, typval_T *rettv)
|
||||||
{
|
{
|
||||||
char_u *p;
|
char_u *p;
|
||||||
int n;
|
varnumber_T n;
|
||||||
int slen;
|
int slen;
|
||||||
int len;
|
int len;
|
||||||
char_u *r;
|
char_u *r;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
if (in_vim9script()
|
if (in_vim9script()
|
||||||
&& (check_for_string_or_number_or_list_arg(argvars, 0) == FAIL
|
&& (check_for_string_or_number_or_list_or_blob_arg(argvars, 0)
|
||||||
|
== FAIL
|
||||||
|| check_for_number_arg(argvars, 1) == FAIL))
|
|| check_for_number_arg(argvars, 1) == FAIL))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
n = (int)tv_get_number(&argvars[1]);
|
n = tv_get_number(&argvars[1]);
|
||||||
if (argvars[0].v_type == VAR_LIST)
|
if (argvars[0].v_type == VAR_LIST)
|
||||||
{
|
{
|
||||||
if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
|
if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
|
||||||
@@ -8419,6 +8425,35 @@ f_repeat(typval_T *argvars, typval_T *rettv)
|
|||||||
argvars[0].vval.v_list, NULL) == FAIL)
|
argvars[0].vval.v_list, NULL) == FAIL)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
else if (argvars[0].v_type == VAR_BLOB)
|
||||||
|
{
|
||||||
|
if (rettv_blob_alloc(rettv) == FAIL
|
||||||
|
|| argvars[0].vval.v_blob == NULL
|
||||||
|
|| n <= 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
slen = argvars[0].vval.v_blob->bv_ga.ga_len;
|
||||||
|
len = (int)slen * n;
|
||||||
|
if (len <= 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (ga_grow(&rettv->vval.v_blob->bv_ga, len) == FAIL)
|
||||||
|
return;
|
||||||
|
|
||||||
|
rettv->vval.v_blob->bv_ga.ga_len = len;
|
||||||
|
|
||||||
|
for (i = 0; i < slen; ++i)
|
||||||
|
if (blob_get(argvars[0].vval.v_blob, i) != 0)
|
||||||
|
break;
|
||||||
|
|
||||||
|
if (i == slen)
|
||||||
|
// No need to copy since all bytes are already zero
|
||||||
|
return;
|
||||||
|
|
||||||
|
for (i = 0; i < n; ++i)
|
||||||
|
blob_set_range(rettv->vval.v_blob,
|
||||||
|
(long)i * slen, ((long)i + 1) * slen - 1, argvars);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
p = tv_get_string(&argvars[0]);
|
p = tv_get_string(&argvars[0]);
|
||||||
|
@@ -42,6 +42,7 @@ int check_for_opt_string_or_list_arg(typval_T *args, int idx);
|
|||||||
int check_for_string_or_dict_arg(typval_T *args, int idx);
|
int check_for_string_or_dict_arg(typval_T *args, int idx);
|
||||||
int check_for_string_or_number_or_list_arg(typval_T *args, int idx);
|
int check_for_string_or_number_or_list_arg(typval_T *args, int idx);
|
||||||
int check_for_opt_string_or_number_or_list_arg(typval_T *args, int idx);
|
int check_for_opt_string_or_number_or_list_arg(typval_T *args, int idx);
|
||||||
|
int check_for_string_or_number_or_list_or_blob_arg(typval_T *args, int idx);
|
||||||
int check_for_string_or_list_or_dict_arg(typval_T *args, int idx);
|
int check_for_string_or_list_or_dict_arg(typval_T *args, int idx);
|
||||||
int check_for_string_or_func_arg(typval_T *args, int idx);
|
int check_for_string_or_func_arg(typval_T *args, int idx);
|
||||||
int check_for_list_or_blob_arg(typval_T *args, int idx);
|
int check_for_list_or_blob_arg(typval_T *args, int idx);
|
||||||
|
@@ -725,6 +725,18 @@ func Test_blob2string()
|
|||||||
call assert_equal(v, string(b))
|
call assert_equal(v, string(b))
|
||||||
endfunc
|
endfunc
|
||||||
|
|
||||||
|
func Test_blob_repeat()
|
||||||
|
call assert_equal(0z, repeat(0z00, 0))
|
||||||
|
call assert_equal(0z00, repeat(0z00, 1))
|
||||||
|
call assert_equal(0z0000, repeat(0z00, 2))
|
||||||
|
call assert_equal(0z00000000, repeat(0z0000, 2))
|
||||||
|
|
||||||
|
call assert_equal(0z, repeat(0z12, 0))
|
||||||
|
call assert_equal(0z, repeat(0z1234, 0))
|
||||||
|
call assert_equal(0z1234, repeat(0z1234, 1))
|
||||||
|
call assert_equal(0z12341234, repeat(0z1234, 2))
|
||||||
|
endfunc
|
||||||
|
|
||||||
" Test for blob allocation failure
|
" Test for blob allocation failure
|
||||||
func Test_blob_alloc_failure()
|
func Test_blob_alloc_failure()
|
||||||
" blob variable
|
" blob variable
|
||||||
|
@@ -3329,12 +3329,14 @@ def Test_rename()
|
|||||||
enddef
|
enddef
|
||||||
|
|
||||||
def Test_repeat()
|
def Test_repeat()
|
||||||
v9.CheckDefAndScriptFailure(['repeat(1.1, 2)'], ['E1013: Argument 1: type mismatch, expected string but got float', 'E1224: String, Number or List required for argument 1'])
|
v9.CheckDefAndScriptFailure(['repeat(1.1, 2)'], ['E1013: Argument 1: type mismatch, expected string but got float', 'E1301: String, Number, List or Blob required for argument 1'])
|
||||||
v9.CheckDefAndScriptFailure(['repeat({a: 10}, 2)'], ['E1013: Argument 1: type mismatch, expected string but got dict<', 'E1224: String, Number or List required for argument 1'])
|
v9.CheckDefAndScriptFailure(['repeat({a: 10}, 2)'], ['E1013: Argument 1: type mismatch, expected string but got dict<', 'E1301: String, Number, List or Blob required for argument 1'])
|
||||||
var lines =<< trim END
|
var lines =<< trim END
|
||||||
assert_equal('aaa', repeat('a', 3))
|
assert_equal('aaa', repeat('a', 3))
|
||||||
assert_equal('111', repeat(1, 3))
|
assert_equal('111', repeat(1, 3))
|
||||||
assert_equal([1, 1, 1], repeat([1], 3))
|
assert_equal([1, 1, 1], repeat([1], 3))
|
||||||
|
assert_equal(0z000102000102000102, repeat(0z000102, 3))
|
||||||
|
assert_equal(0z000000, repeat(0z00, 3))
|
||||||
var s = '-'
|
var s = '-'
|
||||||
s ..= repeat(5, 3)
|
s ..= repeat(5, 3)
|
||||||
assert_equal('-555', s)
|
assert_equal('-555', s)
|
||||||
|
@@ -2533,6 +2533,12 @@ def Test_repeat_return_type()
|
|||||||
endfor
|
endfor
|
||||||
res->assert_equal(3)
|
res->assert_equal(3)
|
||||||
|
|
||||||
|
res = 0
|
||||||
|
for n in repeat(0z01, 3)->blob2list()
|
||||||
|
res += n
|
||||||
|
endfor
|
||||||
|
res->assert_equal(3)
|
||||||
|
|
||||||
res = 0
|
res = 0
|
||||||
for n in add([1, 2], 3)
|
for n in add([1, 2], 3)
|
||||||
res += n
|
res += n
|
||||||
|
18
src/typval.c
18
src/typval.c
@@ -791,6 +791,24 @@ check_for_opt_string_or_number_or_list_arg(typval_T *args, int idx)
|
|||||||
|| check_for_string_or_number_or_list_arg(args, idx) != FAIL);
|
|| check_for_string_or_number_or_list_arg(args, idx) != FAIL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Give an error and return FAIL unless "args[idx]" is a string or a number
|
||||||
|
* or a list or a blob.
|
||||||
|
*/
|
||||||
|
int
|
||||||
|
check_for_string_or_number_or_list_or_blob_arg(typval_T *args, int idx)
|
||||||
|
{
|
||||||
|
if (args[idx].v_type != VAR_STRING
|
||||||
|
&& args[idx].v_type != VAR_NUMBER
|
||||||
|
&& args[idx].v_type != VAR_LIST
|
||||||
|
&& args[idx].v_type != VAR_BLOB)
|
||||||
|
{
|
||||||
|
semsg(_(e_string_number_list_or_blob_required_for_argument_nr), idx + 1);
|
||||||
|
return FAIL;
|
||||||
|
}
|
||||||
|
return OK;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Give an error and return FAIL unless "args[idx]" is a string or a list
|
* Give an error and return FAIL unless "args[idx]" is a string or a list
|
||||||
* or a dict.
|
* or a dict.
|
||||||
|
@@ -703,6 +703,8 @@ static char *(features[]) =
|
|||||||
|
|
||||||
static int included_patches[] =
|
static int included_patches[] =
|
||||||
{ /* Add new patch number below this line */
|
{ /* Add new patch number below this line */
|
||||||
|
/**/
|
||||||
|
430,
|
||||||
/**/
|
/**/
|
||||||
429,
|
429,
|
||||||
/**/
|
/**/
|
||||||
|
Reference in New Issue
Block a user