Skip to content

Commit e0be167

Browse files
committed
patch 8.1.0166: using dict_add_nr_str() is clumsy
Problem: Using dict_add_nr_str() is clumsy. Solution: Split into two functions. (Ozaki Kiichi, closes #3154)
1 parent 4cde86c commit e0be167

13 files changed

Lines changed: 188 additions & 198 deletions

File tree

src/channel.c

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2809,7 +2809,7 @@ channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part)
28092809
status = "buffered";
28102810
else
28112811
status = "closed";
2812-
dict_add_nr_str(dict, namebuf, 0, (char_u *)status);
2812+
dict_add_string(dict, namebuf, (char_u *)status);
28132813

28142814
STRCPY(namebuf + tail, "mode");
28152815
switch (chanpart->ch_mode)
@@ -2819,7 +2819,7 @@ channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part)
28192819
case MODE_JSON: s = "JSON"; break;
28202820
case MODE_JS: s = "JS"; break;
28212821
}
2822-
dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2822+
dict_add_string(dict, namebuf, (char_u *)s);
28232823

28242824
STRCPY(namebuf + tail, "io");
28252825
if (part == PART_SOCK)
@@ -2832,22 +2832,22 @@ channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part)
28322832
case JIO_BUFFER: s = "buffer"; break;
28332833
case JIO_OUT: s = "out"; break;
28342834
}
2835-
dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2835+
dict_add_string(dict, namebuf, (char_u *)s);
28362836

28372837
STRCPY(namebuf + tail, "timeout");
2838-
dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2838+
dict_add_number(dict, namebuf, chanpart->ch_timeout);
28392839
}
28402840

28412841
void
28422842
channel_info(channel_T *channel, dict_T *dict)
28432843
{
2844-
dict_add_nr_str(dict, "id", channel->ch_id, NULL);
2845-
dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel, -1));
2844+
dict_add_number(dict, "id", channel->ch_id);
2845+
dict_add_string(dict, "status", (char_u *)channel_status(channel, -1));
28462846

28472847
if (channel->ch_hostname != NULL)
28482848
{
2849-
dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2850-
dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2849+
dict_add_string(dict, "hostname", (char_u *)channel->ch_hostname);
2850+
dict_add_number(dict, "port", channel->ch_port);
28512851
channel_part_info(channel, dict, "sock", PART_SOCK);
28522852
}
28532853
else
@@ -5737,7 +5737,7 @@ job_info(job_T *job, dict_T *dict)
57375737
list_T *l;
57385738
int i;
57395739

5740-
dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
5740+
dict_add_string(dict, "status", (char_u *)job_status(job));
57415741

57425742
item = dictitem_alloc((char_u *)"channel");
57435743
if (item == NULL)
@@ -5755,15 +5755,13 @@ job_info(job_T *job, dict_T *dict)
57555755
#else
57565756
nr = job->jv_proc_info.dwProcessId;
57575757
#endif
5758-
dict_add_nr_str(dict, "process", nr, NULL);
5759-
dict_add_nr_str(dict, "tty_in", 0L,
5760-
job->jv_tty_in != NULL ? job->jv_tty_in : (char_u *)"");
5761-
dict_add_nr_str(dict, "tty_out", 0L,
5762-
job->jv_tty_out != NULL ? job->jv_tty_out : (char_u *)"");
5763-
5764-
dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
5765-
dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
5766-
dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
5758+
dict_add_number(dict, "process", nr);
5759+
dict_add_string(dict, "tty_in", job->jv_tty_in);
5760+
dict_add_string(dict, "tty_out", job->jv_tty_out);
5761+
5762+
dict_add_number(dict, "exitval", job->jv_exitval);
5763+
dict_add_string(dict, "exit_cb", job->jv_exit_cb);
5764+
dict_add_string(dict, "stoponexit", job->jv_stoponexit);
57675765

57685766
l = list_alloc();
57695767
if (l != NULL)

src/dict.c

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -327,33 +327,43 @@ dict_add(dict_T *d, dictitem_T *item)
327327
}
328328

329329
/*
330-
* Add a number or string entry to dictionary "d".
331-
* When "str" is NULL use number "nr", otherwise use "str".
330+
* Add a number entry to dictionary "d".
332331
* Returns FAIL when out of memory and when key already exists.
333332
*/
334333
int
335-
dict_add_nr_str(
336-
dict_T *d,
337-
char *key,
338-
varnumber_T nr,
339-
char_u *str)
334+
dict_add_number(dict_T *d, char *key, varnumber_T nr)
340335
{
341336
dictitem_T *item;
342337

343338
item = dictitem_alloc((char_u *)key);
344339
if (item == NULL)
345340
return FAIL;
346341
item->di_tv.v_lock = 0;
347-
if (str == NULL)
348-
{
349-
item->di_tv.v_type = VAR_NUMBER;
350-
item->di_tv.vval.v_number = nr;
351-
}
352-
else
342+
item->di_tv.v_type = VAR_NUMBER;
343+
item->di_tv.vval.v_number = nr;
344+
if (dict_add(d, item) == FAIL)
353345
{
354-
item->di_tv.v_type = VAR_STRING;
355-
item->di_tv.vval.v_string = vim_strsave(str);
346+
dictitem_free(item);
347+
return FAIL;
356348
}
349+
return OK;
350+
}
351+
352+
/*
353+
* Add a string entry to dictionary "d".
354+
* Returns FAIL when out of memory and when key already exists.
355+
*/
356+
int
357+
dict_add_string(dict_T *d, char *key, char_u *str)
358+
{
359+
dictitem_T *item;
360+
361+
item = dictitem_alloc((char_u *)key);
362+
if (item == NULL)
363+
return FAIL;
364+
item->di_tv.v_lock = 0;
365+
item->di_tv.v_type = VAR_STRING;
366+
item->di_tv.vval.v_string = str != NULL ? vim_strsave(str) : NULL;
357367
if (dict_add(d, item) == FAIL)
358368
{
359369
dictitem_free(item);

src/edit.c

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4884,18 +4884,13 @@ ins_compl_insert(int in_compl_func)
48844884
dict = dict_alloc_lock(VAR_FIXED);
48854885
if (dict != NULL)
48864886
{
4887-
dict_add_nr_str(dict, "word", 0L,
4888-
EMPTY_IF_NULL(compl_shown_match->cp_str));
4889-
dict_add_nr_str(dict, "abbr", 0L,
4890-
EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_ABBR]));
4891-
dict_add_nr_str(dict, "menu", 0L,
4892-
EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_MENU]));
4893-
dict_add_nr_str(dict, "kind", 0L,
4894-
EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_KIND]));
4895-
dict_add_nr_str(dict, "info", 0L,
4896-
EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_INFO]));
4897-
dict_add_nr_str(dict, "user_data", 0L,
4898-
EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_USER_DATA]));
4887+
dict_add_string(dict, "word", compl_shown_match->cp_str);
4888+
dict_add_string(dict, "abbr", compl_shown_match->cp_text[CPT_ABBR]);
4889+
dict_add_string(dict, "menu", compl_shown_match->cp_text[CPT_MENU]);
4890+
dict_add_string(dict, "kind", compl_shown_match->cp_text[CPT_KIND]);
4891+
dict_add_string(dict, "info", compl_shown_match->cp_text[CPT_INFO]);
4892+
dict_add_string(dict, "user_data",
4893+
compl_shown_match->cp_text[CPT_USER_DATA]);
48994894
}
49004895
set_vim_var_dict(VV_COMPLETED_ITEM, dict);
49014896
if (!in_compl_func)

src/evalfunc.c

Lines changed: 64 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -4338,9 +4338,9 @@ get_buffer_signs(buf_T *buf, list_T *l)
43384338

43394339
if (d != NULL)
43404340
{
4341-
dict_add_nr_str(d, "id", sign->id, NULL);
4342-
dict_add_nr_str(d, "lnum", sign->lnum, NULL);
4343-
dict_add_nr_str(d, "name", 0L, sign_typenr2name(sign->typenr));
4341+
dict_add_number(d, "id", sign->id);
4342+
dict_add_number(d, "lnum", sign->lnum);
4343+
dict_add_string(d, "name", sign_typenr2name(sign->typenr));
43444344

43454345
list_append_dict(l, d);
43464346
}
@@ -4363,18 +4363,16 @@ get_buffer_info(buf_T *buf)
43634363
if (dict == NULL)
43644364
return NULL;
43654365

4366-
dict_add_nr_str(dict, "bufnr", buf->b_fnum, NULL);
4367-
dict_add_nr_str(dict, "name", 0L,
4368-
buf->b_ffname != NULL ? buf->b_ffname : (char_u *)"");
4369-
dict_add_nr_str(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4370-
: buflist_findlnum(buf), NULL);
4371-
dict_add_nr_str(dict, "loaded", buf->b_ml.ml_mfp != NULL, NULL);
4372-
dict_add_nr_str(dict, "listed", buf->b_p_bl, NULL);
4373-
dict_add_nr_str(dict, "changed", bufIsChanged(buf), NULL);
4374-
dict_add_nr_str(dict, "changedtick", CHANGEDTICK(buf), NULL);
4375-
dict_add_nr_str(dict, "hidden",
4376-
buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0,
4377-
NULL);
4366+
dict_add_number(dict, "bufnr", buf->b_fnum);
4367+
dict_add_string(dict, "name", buf->b_ffname);
4368+
dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4369+
: buflist_findlnum(buf));
4370+
dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4371+
dict_add_number(dict, "listed", buf->b_p_bl);
4372+
dict_add_number(dict, "changed", bufIsChanged(buf));
4373+
dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4374+
dict_add_number(dict, "hidden",
4375+
buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
43784376

43794377
/* Get a reference to buffer variables */
43804378
dict_add_dict(dict, "variables", buf->b_vars);
@@ -4663,10 +4661,10 @@ f_getchangelist(typval_T *argvars, typval_T *rettv)
46634661
return;
46644662
if (list_append_dict(l, d) == FAIL)
46654663
return;
4666-
dict_add_nr_str(d, "lnum", (long)buf->b_changelist[i].lnum, NULL);
4667-
dict_add_nr_str(d, "col", (long)buf->b_changelist[i].col, NULL);
4664+
dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4665+
dict_add_number(d, "col", (long)buf->b_changelist[i].col);
46684666
# ifdef FEAT_VIRTUALEDIT
4669-
dict_add_nr_str(d, "coladd", (long)buf->b_changelist[i].coladd, NULL);
4667+
dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
46704668
# endif
46714669
}
46724670
#endif
@@ -4790,9 +4788,9 @@ f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
47904788
{
47914789
dict_T *dict = rettv->vval.v_dict;
47924790

4793-
dict_add_nr_str(dict, "char", 0L, last_csearch());
4794-
dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
4795-
dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
4791+
dict_add_string(dict, "char", last_csearch());
4792+
dict_add_number(dict, "forward", last_csearch_forward());
4793+
dict_add_number(dict, "until", last_csearch_until());
47964794
}
47974795
}
47984796

@@ -5193,17 +5191,14 @@ f_getjumplist(typval_T *argvars, typval_T *rettv)
51935191
return;
51945192
if (list_append_dict(l, d) == FAIL)
51955193
return;
5196-
dict_add_nr_str(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum,
5197-
NULL);
5198-
dict_add_nr_str(d, "col", (long)wp->w_jumplist[i].fmark.mark.col,
5199-
NULL);
5194+
dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5195+
dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
52005196
# ifdef FEAT_VIRTUALEDIT
5201-
dict_add_nr_str(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd,
5202-
NULL);
5197+
dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
52035198
# endif
5204-
dict_add_nr_str(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum, NULL);
5199+
dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
52055200
if (wp->w_jumplist[i].fname != NULL)
5206-
dict_add_nr_str(d, "filename", 0L, wp->w_jumplist[i].fname);
5201+
dict_add_string(d, "filename", wp->w_jumplist[i].fname);
52075202
}
52085203
#endif
52095204
}
@@ -5321,18 +5316,18 @@ f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
53215316
}
53225317
else
53235318
{
5324-
dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
5319+
dict_add_string(dict, "pattern", cur->pattern);
53255320
}
5326-
dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
5327-
dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
5328-
dict_add_nr_str(dict, "id", (long)cur->id, NULL);
5321+
dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5322+
dict_add_number(dict, "priority", (long)cur->priority);
5323+
dict_add_number(dict, "id", (long)cur->id);
53295324
# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
53305325
if (cur->conceal_char)
53315326
{
53325327
char_u buf[MB_MAXBYTES + 1];
53335328

53345329
buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5335-
dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
5330+
dict_add_string(dict, "conceal", (char_u *)&buf);
53365331
}
53375332
# endif
53385333
list_append_dict(rettv->vval.v_list, dict);
@@ -5533,7 +5528,7 @@ get_tabpage_info(tabpage_T *tp, int tp_idx)
55335528
if (dict == NULL)
55345529
return NULL;
55355530

5536-
dict_add_nr_str(dict, "tabnr", tp_idx, NULL);
5531+
dict_add_number(dict, "tabnr", tp_idx);
55375532

55385533
l = list_alloc();
55395534
if (l != NULL)
@@ -5649,23 +5644,23 @@ get_win_info(win_T *wp, short tpnr, short winnr)
56495644
if (dict == NULL)
56505645
return NULL;
56515646

5652-
dict_add_nr_str(dict, "tabnr", tpnr, NULL);
5653-
dict_add_nr_str(dict, "winnr", winnr, NULL);
5654-
dict_add_nr_str(dict, "winid", wp->w_id, NULL);
5655-
dict_add_nr_str(dict, "height", wp->w_height, NULL);
5647+
dict_add_number(dict, "tabnr", tpnr);
5648+
dict_add_number(dict, "winnr", winnr);
5649+
dict_add_number(dict, "winid", wp->w_id);
5650+
dict_add_number(dict, "height", wp->w_height);
56565651
#ifdef FEAT_MENU
5657-
dict_add_nr_str(dict, "winbar", wp->w_winbar_height, NULL);
5652+
dict_add_number(dict, "winbar", wp->w_winbar_height);
56585653
#endif
5659-
dict_add_nr_str(dict, "width", wp->w_width, NULL);
5660-
dict_add_nr_str(dict, "bufnr", wp->w_buffer->b_fnum, NULL);
5654+
dict_add_number(dict, "width", wp->w_width);
5655+
dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
56615656

56625657
#ifdef FEAT_TERMINAL
5663-
dict_add_nr_str(dict, "terminal", bt_terminal(wp->w_buffer), NULL);
5658+
dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
56645659
#endif
56655660
#ifdef FEAT_QUICKFIX
5666-
dict_add_nr_str(dict, "quickfix", bt_quickfix(wp->w_buffer), NULL);
5667-
dict_add_nr_str(dict, "loclist",
5668-
(bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL), NULL);
5661+
dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5662+
dict_add_number(dict, "loclist",
5663+
(bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
56695664
#endif
56705665

56715666
/* Add a reference to window variables */
@@ -7652,15 +7647,15 @@ get_maparg(typval_T *argvars, typval_T *rettv, int exact)
76527647
char_u *mapmode = map_mode_to_chars(mp->m_mode);
76537648
dict_T *dict = rettv->vval.v_dict;
76547649

7655-
dict_add_nr_str(dict, "lhs", 0L, lhs);
7656-
dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
7657-
dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
7658-
dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
7659-
dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
7660-
dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
7661-
dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
7662-
dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
7663-
dict_add_nr_str(dict, "mode", 0L, mapmode);
7650+
dict_add_string(dict, "lhs", lhs);
7651+
dict_add_string(dict, "rhs", mp->m_orig_str);
7652+
dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7653+
dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7654+
dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
7655+
dict_add_number(dict, "sid", (long)mp->m_script_ID);
7656+
dict_add_number(dict, "buffer", (long)buffer_local);
7657+
dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7658+
dict_add_string(dict, "mode", mapmode);
76647659

76657660
vim_free(lhs);
76667661
vim_free(mapmode);
@@ -13652,13 +13647,12 @@ f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
1365213647
dict_T *dict = rettv->vval.v_dict;
1365313648
list_T *list;
1365413649

13655-
dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
13656-
dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
13657-
dict_add_nr_str(dict, "save_last",
13658-
(long)curbuf->b_u_save_nr_last, NULL);
13659-
dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
13660-
dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
13661-
dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
13650+
dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
13651+
dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
13652+
dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
13653+
dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
13654+
dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
13655+
dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
1366213656

1366313657
list = list_alloc();
1366413658
if (list != NULL)
@@ -13882,20 +13876,20 @@ f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
1388213876
return;
1388313877
dict = rettv->vval.v_dict;
1388413878

13885-
dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
13886-
dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
13879+
dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
13880+
dict_add_number(dict, "col", (long)curwin->w_cursor.col);
1388713881
#ifdef FEAT_VIRTUALEDIT
13888-
dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
13882+
dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
1388913883
#endif
1389013884
update_curswant();
13891-
dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
13885+
dict_add_number(dict, "curswant", (long)curwin->w_curswant);
1389213886

13893-
dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
13887+
dict_add_number(dict, "topline", (long)curwin->w_topline);
1389413888
#ifdef FEAT_DIFF
13895-
dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
13889+
dict_add_number(dict, "topfill", (long)curwin->w_topfill);
1389613890
#endif
13897-
dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
13898-
dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
13891+
dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
13892+
dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
1389913893
}
1390013894

1390113895
/*

0 commit comments

Comments
 (0)