Skip to content

Commit 5dfe467

Browse files
yegappanbrammool
authored andcommitted
patch 8.2.3438: cannot manipulate blobs
Problem: Cannot manipulate blobs. Solution: Add blob2list() and list2blob(). (Yegappan Lakshmanan, closes #8868)
1 parent f5785cf commit 5dfe467

File tree

11 files changed

+185
-1
lines changed

11 files changed

+185
-1
lines changed

runtime/doc/eval.txt

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2469,6 +2469,7 @@ atan2({expr1}, {expr2}) Float arc tangent of {expr1} / {expr2}
24692469
balloon_gettext() String current text in the balloon
24702470
balloon_show({expr}) none show {expr} inside the balloon
24712471
balloon_split({msg}) List split {msg} as used for a balloon
2472+
blob2list({blob}) List convert {blob} into a list of numbers
24722473
browse({save}, {title}, {initdir}, {default})
24732474
String put up a file requester
24742475
browsedir({title}, {initdir}) String put up a directory requester
@@ -2721,7 +2722,8 @@ libcallnr({lib}, {func}, {arg}) Number idem, but return a Number
27212722
line({expr} [, {winid}]) Number line nr of cursor, last line or mark
27222723
line2byte({lnum}) Number byte count of line {lnum}
27232724
lispindent({lnum}) Number Lisp indent for line {lnum}
2724-
list2str({list} [, {utf8}]) String turn numbers in {list} into a String
2725+
list2blob({list}) Blob turn {list} of numbers into a Blob
2726+
list2str({list} [, {utf8}]) String turn {list} of numbers into a String
27252727
listener_add({callback} [, {buf}])
27262728
Number add a callback to listen to changes
27272729
listener_flush([{buf}]) none invoke listener callbacks
@@ -3355,6 +3357,17 @@ balloon_split({msg}) *balloon_split()*
33553357
< {only available when compiled with the |+balloon_eval_term|
33563358
feature}
33573359

3360+
blob2list({blob}) *blob2list()*
3361+
Return a List containing the number value of each byte in Blob
3362+
{blob}. Examples: >
3363+
blob2list(0z0102.0304) returns [1, 2, 3, 4]
3364+
blob2list(0z) returns []
3365+
< Returns an empty List on error. |list2blob()| does the
3366+
opposite.
3367+
3368+
Can also be used as a |method|: >
3369+
GetBlob()->blob2list()
3370+
33583371
*browse()*
33593372
browse({save}, {title}, {initdir}, {default})
33603373
Put up a file requester. This only works when "has("browse")"
@@ -7208,6 +7221,19 @@ lispindent({lnum}) *lispindent()*
72087221
Can also be used as a |method|: >
72097222
GetLnum()->lispindent()
72107223

7224+
list2blob({list}) *list2blob()*
7225+
Return a Blob concatenating all the number values in {list}.
7226+
Examples: >
7227+
list2blob([1, 2, 3, 4]) returns 0z01020304
7228+
list2blob([]) returns 0z
7229+
< Returns an empty Blob on error. If one of the numbers is
7230+
negative or more than 255 error *E1239* is given.
7231+
7232+
|blob2list()| does the opposite.
7233+
7234+
Can also be used as a |method|: >
7235+
GetList()->list2blob()
7236+
72117237
list2str({list} [, {utf8}]) *list2str()*
72127238
Convert each number in {list} to a character string can
72137239
concatenate them all. Examples: >

runtime/doc/usr_41.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,10 @@ Floating point computation: *float-functions*
723723
isinf() check for infinity
724724
isnan() check for not a number
725725

726+
Blob manipulation: *blob-functions*
727+
blob2list() get a list of numbers from a blob
728+
list2blob() get a blob from a list of numbers
729+
726730
Other computation: *bitwise-function*
727731
and() bitwise AND
728732
invert() bitwise invert
@@ -1449,6 +1453,8 @@ is a List with arguments.
14491453
Function references are most useful in combination with a Dictionary, as is
14501454
explained in the next section.
14511455

1456+
More information about defining your own functions here: |user-functions|.
1457+
14521458
==============================================================================
14531459
*41.8* Lists and Dictionaries
14541460

src/blob.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,4 +483,65 @@ blob_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg)
483483
}
484484
}
485485

486+
/*
487+
* blob2list() function
488+
*/
489+
void
490+
f_blob2list(typval_T *argvars, typval_T *rettv)
491+
{
492+
blob_T *blob;
493+
list_T *l;
494+
int i;
495+
496+
if (rettv_list_alloc(rettv) == FAIL)
497+
return;
498+
499+
if (check_for_blob_arg(argvars, 0) == FAIL)
500+
return;
501+
502+
blob = argvars->vval.v_blob;
503+
l = rettv->vval.v_list;
504+
for (i = 0; i < blob_len(blob); i++)
505+
list_append_number(l, blob_get(blob, i));
506+
}
507+
508+
/*
509+
* list2blob() function
510+
*/
511+
void
512+
f_list2blob(typval_T *argvars, typval_T *rettv)
513+
{
514+
list_T *l;
515+
listitem_T *li;
516+
blob_T *blob;
517+
518+
if (rettv_blob_alloc(rettv) == FAIL)
519+
return;
520+
blob = rettv->vval.v_blob;
521+
522+
if (check_for_list_arg(argvars, 0) == FAIL)
523+
return;
524+
525+
l = argvars->vval.v_list;
526+
if (l == NULL)
527+
return;
528+
529+
FOR_ALL_LIST_ITEMS(l, li)
530+
{
531+
int error;
532+
varnumber_T n;
533+
534+
error = FALSE;
535+
n = tv_get_number_chk(&li->li_tv, &error);
536+
if (error == TRUE || n < 0 || n > 255)
537+
{
538+
if (!error)
539+
semsg(_(e_invalid_value_for_blob_nr), n);
540+
ga_clear(&blob->bv_ga);
541+
return;
542+
}
543+
ga_append(&blob->bv_ga, n);
544+
}
545+
}
546+
486547
#endif // defined(FEAT_EVAL)

src/errors.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,3 +660,7 @@ EXTERN char e_cannot_use_str_itself_it_is_imported_with_star[]
660660
INIT(= N_("E1236: Cannot use %s itself, it is imported with '*'"));
661661
EXTERN char e_no_such_user_defined_command_in_current_buffer_str[]
662662
INIT(= N_("E1237: No such user-defined command in current buffer: %s"));
663+
EXTERN char e_blob_required_for_argument_nr[]
664+
INIT(= N_("E1238: Blob required for argument %d"));
665+
EXTERN char e_invalid_value_for_blob_nr[]
666+
INIT(= N_("E1239: Invalid value for blob: %d"));

src/evalfunc.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,15 @@ arg_string(type_T *type, argcontext_T *context)
288288
return check_arg_type(&t_string, type, context);
289289
}
290290

291+
/*
292+
* Check "type" is a blob
293+
*/
294+
static int
295+
arg_blob(type_T *type, argcontext_T *context)
296+
{
297+
return check_arg_type(&t_blob, type, context);
298+
}
299+
291300
/*
292301
* Check "type" is a bool or number 0 or 1.
293302
*/
@@ -680,6 +689,7 @@ arg_cursor1(type_T *type, argcontext_T *context)
680689
/*
681690
* Lists of functions that check the argument types of a builtin function.
682691
*/
692+
static argcheck_T arg1_blob[] = {arg_blob};
683693
static argcheck_T arg1_bool[] = {arg_bool};
684694
static argcheck_T arg1_buffer[] = {arg_buffer};
685695
static argcheck_T arg1_buffer_or_dict_any[] = {arg_buffer_or_dict_any};
@@ -1169,6 +1179,8 @@ static funcentry_T global_functions[] =
11691179
NULL
11701180
#endif
11711181
},
1182+
{"blob2list", 1, 1, FEARG_1, arg1_blob,
1183+
ret_list_number, f_blob2list},
11721184
{"browse", 4, 4, 0, arg4_browse,
11731185
ret_string, f_browse},
11741186
{"browsedir", 2, 2, 0, arg2_string,
@@ -1589,6 +1601,8 @@ static funcentry_T global_functions[] =
15891601
ret_number, f_line2byte},
15901602
{"lispindent", 1, 1, FEARG_1, arg1_lnum,
15911603
ret_number, f_lispindent},
1604+
{"list2blob", 1, 1, FEARG_1, arg1_list_number,
1605+
ret_blob, f_list2blob},
15921606
{"list2str", 1, 2, FEARG_1, arg2_list_number_bool,
15931607
ret_string, f_list2str},
15941608
{"listener_add", 1, 2, FEARG_2, arg2_any_buffer,

src/proto/blob.pro

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ int check_blob_index(long bloblen, varnumber_T n1, int quiet);
1919
int check_blob_range(long bloblen, varnumber_T n1, varnumber_T n2, int quiet);
2020
int blob_set_range(blob_T *dest, long n1, long n2, typval_T *src);
2121
void blob_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg);
22+
void f_blob2list(typval_T *argvars, typval_T *rettv);
23+
void f_list2blob(typval_T *argvars, typval_T *rettv);
2224
/* vim: set ft=c : */

src/proto/typval.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ int check_for_opt_number_arg(typval_T *args, int idx);
1717
int check_for_float_or_nr_arg(typval_T *args, int idx);
1818
int check_for_bool_arg(typval_T *args, int idx);
1919
int check_for_opt_bool_arg(typval_T *args, int idx);
20+
int check_for_blob_arg(typval_T *args, int idx);
2021
int check_for_list_arg(typval_T *args, int idx);
2122
int check_for_opt_list_arg(typval_T *args, int idx);
2223
int check_for_dict_arg(typval_T *args, int idx);

src/testdir/test_blob.vim

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,4 +638,43 @@ func Test_blob_sort()
638638
call CheckLegacyAndVim9Failure(['call sort([11, 0z11], "N")'], 'E974:')
639639
endfunc
640640

641+
" Tests for the blob2list() function
642+
func Test_blob2list()
643+
call assert_fails('let v = blob2list(10)', 'E1238: Blob required for argument 1')
644+
eval 0zFFFF->blob2list()->assert_equal([255, 255])
645+
let tests = [[0z0102, [1, 2]],
646+
\ [0z00, [0]],
647+
\ [0z, []],
648+
\ [0z00000000, [0, 0, 0, 0]],
649+
\ [0zAABB.CCDD, [170, 187, 204, 221]]]
650+
for t in tests
651+
call assert_equal(t[0]->blob2list(), t[1])
652+
endfor
653+
exe 'let v = 0z' .. repeat('000102030405060708090A0B0C0D0E0F', 64)
654+
call assert_equal(1024, blob2list(v)->len())
655+
call assert_equal([4, 8, 15], [v[100], v[1000], v[1023]])
656+
call assert_equal([], blob2list(test_null_blob()))
657+
endfunc
658+
659+
" Tests for the list2blob() function
660+
func Test_list2blob()
661+
call assert_fails('let b = list2blob(0z10)', 'E1211: List required for argument 1')
662+
let tests = [[[1, 2], 0z0102],
663+
\ [[0], 0z00],
664+
\ [[], 0z],
665+
\ [[0, 0, 0, 0], 0z00000000],
666+
\ [[170, 187, 204, 221], 0zAABB.CCDD],
667+
\ ]
668+
for t in tests
669+
call assert_equal(t[0]->list2blob(), t[1])
670+
endfor
671+
call assert_fails('let b = list2blob([1, []])', 'E745:')
672+
call assert_fails('let b = list2blob([-1])', 'E1239:')
673+
call assert_fails('let b = list2blob([256])', 'E1239:')
674+
let b = range(16)->repeat(64)->list2blob()
675+
call assert_equal(1024, b->len())
676+
call assert_equal([4, 8, 15], [b[100], b[1000], b[1023]])
677+
call assert_equal(0z, list2blob(test_null_list()))
678+
endfunc
679+
641680
" vim: shiftwidth=2 sts=2 expandtab

src/testdir/test_vim9_builtin.vim

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,10 @@ def Test_balloon_split()
287287
assert_fails('balloon_split(true)', 'E1174:')
288288
enddef
289289

290+
def Test_blob2list()
291+
CheckDefAndScriptFailure2(['blob2list(10)'], 'E1013: Argument 1: type mismatch, expected blob but got number', 'E1238: Blob required for argument 1')
292+
enddef
293+
290294
def Test_browse()
291295
CheckFeature browse
292296

@@ -572,6 +576,7 @@ def Test_char2nr()
572576
assert_equal(97, char2nr('a', 0))
573577
assert_equal(97, char2nr('a', true))
574578
assert_equal(97, char2nr('a', false))
579+
char2nr('')->assert_equal(0)
575580
enddef
576581

577582
def Test_charclass()
@@ -786,6 +791,8 @@ def Test_escape()
786791
CheckDefAndScriptFailure2(['escape(true, false)'], 'E1013: Argument 1: type mismatch, expected string but got bool', 'E1174: String required for argument 1')
787792
CheckDefAndScriptFailure2(['escape("a", 10)'], 'E1013: Argument 2: type mismatch, expected string but got number', 'E1174: String required for argument 2')
788793
assert_equal('a\:b', escape("a:b", ":"))
794+
escape('abc', '')->assert_equal('abc')
795+
escape('', ':')->assert_equal('')
789796
enddef
790797

791798
def Test_eval()
@@ -1921,6 +1928,11 @@ def Test_lispindent()
19211928
assert_equal(0, lispindent(1))
19221929
enddef
19231930

1931+
def Test_list2blob()
1932+
CheckDefAndScriptFailure2(['list2blob(10)'], 'E1013: Argument 1: type mismatch, expected list<number> but got number', 'E1211: List required for argument 1')
1933+
CheckDefFailure(['list2blob([0z10, 0z02])'], 'E1013: Argument 1: type mismatch, expected list<number> but got list<blob>')
1934+
enddef
1935+
19241936
def Test_list2str_str2list_utf8()
19251937
var s = "\u3042\u3044"
19261938
var l = [0x3042, 0x3044]

src/typval.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,23 @@ check_for_opt_bool_arg(typval_T *args, int idx)
470470
return check_for_bool_arg(args, idx);
471471
}
472472

473+
/*
474+
* Give an error and return FAIL unless "args[idx]" is a blob.
475+
*/
476+
int
477+
check_for_blob_arg(typval_T *args, int idx)
478+
{
479+
if (args[idx].v_type != VAR_BLOB)
480+
{
481+
if (idx >= 0)
482+
semsg(_(e_blob_required_for_argument_nr), idx + 1);
483+
else
484+
emsg(_(e_blob_required));
485+
return FAIL;
486+
}
487+
return OK;
488+
}
489+
473490
/*
474491
* Give an error and return FAIL unless "args[idx]" is a list.
475492
*/

src/version.c

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

756756
static int included_patches[] =
757757
{ /* Add new patch number below this line */
758+
/**/
759+
3438,
758760
/**/
759761
3437,
760762
/**/

0 commit comments

Comments
 (0)