mirror of
https://github.com/neovim/neovim.git
synced 2025-10-01 23:48:32 +00:00
feat(f_msgpackdump): support dumping to Blob
This commit is contained in:
@@ -250,7 +250,7 @@ return {
|
||||
min={args=1, base=1},
|
||||
mkdir={args={1, 3}},
|
||||
mode={args={0, 1}},
|
||||
msgpackdump={args=1},
|
||||
msgpackdump={args={1, 2}},
|
||||
msgpackparse={args=1},
|
||||
nextnonblank={args=1},
|
||||
nr2char={args={1, 2}},
|
||||
|
@@ -47,6 +47,14 @@ const char *const encode_special_var_names[] = {
|
||||
# include "eval/encode.c.generated.h"
|
||||
#endif
|
||||
|
||||
/// Msgpack callback for writing to a Blob
|
||||
int encode_blob_write(void *const data, const char *const buf, const size_t len)
|
||||
FUNC_ATTR_NONNULL_ARG(1)
|
||||
{
|
||||
ga_concat_len(&((blob_T *)data)->bv_ga, buf, len);
|
||||
return (int)len;
|
||||
}
|
||||
|
||||
/// Msgpack callback for writing to readfile()-style list
|
||||
int encode_list_write(void *const data, const char *const buf, const size_t len)
|
||||
FUNC_ATTR_NONNULL_ARG(1)
|
||||
|
@@ -6506,9 +6506,16 @@ static void f_msgpackdump(typval_T *argvars, typval_T *rettv, FunPtr fptr)
|
||||
EMSG2(_(e_listarg), "msgpackdump()");
|
||||
return;
|
||||
}
|
||||
list_T *const ret_list = tv_list_alloc_ret(rettv, kListLenMayKnow);
|
||||
list_T *const list = argvars[0].vval.v_list;
|
||||
msgpack_packer *lpacker = msgpack_packer_new(ret_list, &encode_list_write);
|
||||
msgpack_packer *packer;
|
||||
if (argvars[1].v_type != VAR_UNKNOWN
|
||||
&& strequal(tv_get_string(&argvars[1]), "B")) {
|
||||
tv_blob_alloc_ret(rettv);
|
||||
packer = msgpack_packer_new(rettv->vval.v_blob, &encode_blob_write);
|
||||
} else {
|
||||
packer = msgpack_packer_new(tv_list_alloc_ret(rettv, kListLenMayKnow),
|
||||
&encode_list_write);
|
||||
}
|
||||
const char *const msg = _("msgpackdump() argument, index %i");
|
||||
// Assume that translation will not take more then 4 times more space
|
||||
char msgbuf[sizeof("msgpackdump() argument, index ") * 4 + NUMBUFLEN];
|
||||
@@ -6516,11 +6523,11 @@ static void f_msgpackdump(typval_T *argvars, typval_T *rettv, FunPtr fptr)
|
||||
TV_LIST_ITER(list, li, {
|
||||
vim_snprintf(msgbuf, sizeof(msgbuf), (char *)msg, idx);
|
||||
idx++;
|
||||
if (encode_vim_to_msgpack(lpacker, TV_LIST_ITEM_TV(li), msgbuf) == FAIL) {
|
||||
if (encode_vim_to_msgpack(packer, TV_LIST_ITEM_TV(li), msgbuf) == FAIL) {
|
||||
break;
|
||||
}
|
||||
});
|
||||
msgpack_packer_free(lpacker);
|
||||
msgpack_packer_free(packer);
|
||||
}
|
||||
|
||||
/// "msgpackparse" function
|
||||
|
Reference in New Issue
Block a user