vim-patch:9.0.0430: cannot use repeat() with a blob

Problem:    Cannot use repeat() with a blob.
Solution:   Implement blob repeat. (closes vim/vim#11090)

375141e1f8

Co-authored-by: Bakudankun <bakudankun@gmail.com>
This commit is contained in:
zeertzjq 2023-02-28 21:07:56 +08:00
parent 1e513fd112
commit 278aeee3ae
5 changed files with 52 additions and 6 deletions

View File

@ -380,7 +380,8 @@ remove({blob}, {idx} [, {end}]) Number/Blob
remove bytes {idx}-{end} from {blob}
remove({dict}, {key}) any remove entry {key} from {dict}
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
reverse({list}) List reverse {list} in-place
round({expr}) Float round off {expr}
@ -6420,8 +6421,8 @@ repeat({expr}, {count}) *repeat()*
result. Example: >
:let separator = repeat('-', 80)
< When {count} is zero or negative the result is empty.
When {expr} is a |List| the result is {expr} concatenated
{count} times. Example: >
When {expr} is a |List| or a |Blob| the result is {expr}
concatenated {count} times. Example: >
:let longlist = repeat(['a', 'b'], 3)
< Results in ['a', 'b', 'a', 'b', 'a', 'b'].

View File

@ -1613,7 +1613,7 @@ void set_var_lval(lval_T *lp, char *endp, typval_T *rettv, int copy, const bool
lp->ll_n2 = tv_blob_len(lp->ll_blob) - 1;
}
if (tv_blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2, rettv) == FAIL) {
if (tv_blob_set_range(lp->ll_blob, (int)lp->ll_n1, (int)lp->ll_n2, rettv) == FAIL) {
return;
}
} else {
@ -4938,6 +4938,8 @@ theend:
return retval;
}
/// "function()" function
/// "funcref()" function
void common_function(typval_T *argvars, typval_T *rettv, bool is_funcref)
{
char *s;

View File

@ -5976,6 +5976,37 @@ static void f_repeat(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
while (n-- > 0) {
tv_list_extend(rettv->vval.v_list, argvars[0].vval.v_list, NULL);
}
} else if (argvars[0].v_type == VAR_BLOB) {
tv_blob_alloc_ret(rettv);
if (argvars[0].vval.v_blob == NULL || n <= 0) {
return;
}
const int slen = argvars[0].vval.v_blob->bv_ga.ga_len;
const int len = (int)(slen * n);
if (len <= 0) {
return;
}
ga_grow(&rettv->vval.v_blob->bv_ga, len);
rettv->vval.v_blob->bv_ga.ga_len = len;
int i;
for (i = 0; i < slen; i++) {
if (tv_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++) {
tv_blob_set_range(rettv->vval.v_blob, i * slen, (i + 1) * slen - 1, argvars);
}
} else {
rettv->v_type = VAR_STRING;
rettv->vval.v_string = NULL;

View File

@ -2743,14 +2743,14 @@ int tv_blob_check_range(int bloblen, varnumber_T n1, varnumber_T n2, bool quiet)
/// Set bytes "n1" to "n2" (inclusive) in "dest" to the value of "src".
/// Caller must make sure "src" is a blob.
/// Returns FAIL if the number of bytes does not match.
int tv_blob_set_range(blob_T *dest, long n1, long n2, typval_T *src)
int tv_blob_set_range(blob_T *dest, int n1, int n2, typval_T *src)
{
if (n2 - n1 + 1 != tv_blob_len(src->vval.v_blob)) {
emsg(_("E972: Blob value does not have the right number of bytes"));
return FAIL;
}
for (int il = (int)n1, ir = 0; il <= (int)n2; il++) {
for (int il = n1, ir = 0; il <= n2; il++) {
tv_blob_set(dest, il, tv_blob_get(src->vval.v_blob, ir++));
}
return OK;

View File

@ -715,6 +715,18 @@ func Test_blob2string()
call assert_equal(v, string(b))
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
func Test_blob_alloc_failure()
CheckFunction test_alloc_fail