1
0
forked from aniani/vim

patch 9.0.0916: getbufline() is inefficient for getting a single line

Problem:    getbufline() is inefficient for getting a single line.
Solution:   Add getbufoneline().
This commit is contained in:
Bram Moolenaar
2022-11-21 19:57:04 +00:00
parent 2996773276
commit ce30ccc06a
8 changed files with 52 additions and 6 deletions

View File

@@ -814,10 +814,11 @@ get_buffer_lines(
}
/*
* "getbufline()" function
* "retlist" TRUE: "getbufline()" function
* "retlist" FALSE: "getbufoneline()" function
*/
void
f_getbufline(typval_T *argvars, typval_T *rettv)
static void
getbufline(typval_T *argvars, typval_T *rettv, int retlist)
{
linenr_T lnum = 1;
linenr_T end = 1;
@@ -842,7 +843,25 @@ f_getbufline(typval_T *argvars, typval_T *rettv)
end = tv_get_lnum_buf(&argvars[2], buf);
}
get_buffer_lines(buf, lnum, end, TRUE, rettv);
get_buffer_lines(buf, lnum, end, retlist, rettv);
}
/*
* "getbufline()" function
*/
void
f_getbufline(typval_T *argvars, typval_T *rettv)
{
getbufline(argvars, rettv, TRUE);
}
/*
* "getbufoneline()" function
*/
void
f_getbufoneline(typval_T *argvars, typval_T *rettv)
{
getbufline(argvars, rettv, FALSE);
}
/*

View File

@@ -1923,6 +1923,8 @@ static funcentry_T global_functions[] =
ret_list_dict_any, f_getbufinfo},
{"getbufline", 2, 3, FEARG_1, arg3_buffer_lnum_lnum,
ret_list_string, f_getbufline},
{"getbufoneline", 2, 2, FEARG_1, arg2_buffer_lnum,
ret_string, f_getbufoneline},
{"getbufvar", 2, 3, FEARG_1, arg3_buffer_string_any,
ret_any, f_getbufvar},
{"getchangelist", 0, 1, FEARG_1, arg1_buffer,

View File

@@ -16,6 +16,7 @@ void f_bufwinnr(typval_T *argvars, typval_T *rettv);
void f_deletebufline(typval_T *argvars, typval_T *rettv);
void f_getbufinfo(typval_T *argvars, typval_T *rettv);
void f_getbufline(typval_T *argvars, typval_T *rettv);
void f_getbufoneline(typval_T *argvars, typval_T *rettv);
void f_getline(typval_T *argvars, typval_T *rettv);
void f_setbufline(typval_T *argvars, typval_T *rettv);
void f_setline(typval_T *argvars, typval_T *rettv);

View File

@@ -11,7 +11,9 @@ func Test_setbufline_getbufline()
hide
call assert_equal(0, setbufline(b, 1, ['foo', 'bar']))
call assert_equal(['foo'], getbufline(b, 1))
call assert_equal('foo', getbufoneline(b, 1))
call assert_equal(['bar'], getbufline(b, '$'))
call assert_equal('bar', getbufoneline(b, '$'))
call assert_equal(['foo', 'bar'], getbufline(b, 1, 2))
exe "bd!" b
call assert_equal([], getbufline(b, 1, 2))
@@ -35,8 +37,11 @@ func Test_setbufline_getbufline()
call assert_equal(0, setbufline(b, 4, ['d', 'e']))
call assert_equal(['c'], b->getbufline(3))
call assert_equal('c', b->getbufoneline(3))
call assert_equal(['d'], getbufline(b, 4))
call assert_equal('d', getbufoneline(b, 4))
call assert_equal(['e'], getbufline(b, 5))
call assert_equal('e', getbufoneline(b, 5))
call assert_equal([], getbufline(b, 6))
call assert_equal([], getbufline(b, 2, 1))

View File

@@ -1724,15 +1724,23 @@ def Test_getbufline()
getbufline(-1, '$', '$')->assert_equal([])
getbufline(-1, 1, '$')->assert_equal([])
getbufoneline('#', 1)->assert_equal(lines[0])
assert_equal([7, 7, 7], getbufline('#', 1, '$')->map((_, _) => 7))
assert_fails('getbufline("", "$a", "$b")', ['E1030: Using a String as a Number: "$a"', 'E1030: Using a String as a Number: "$a"'])
assert_fails('getbufline("", "$", "$b")', ['E1030: Using a String as a Number: "$b"', 'E1030: Using a String as a Number: "$b"'])
bwipe!
assert_fails('getbufoneline("", "$a")', ['E1030: Using a String as a Number: "$a"', 'E1030: Using a String as a Number: "$a"'])
bwipe!
v9.CheckDefAndScriptFailure(['getbufline([], 2)'], ['E1013: Argument 1: type mismatch, expected string but got list<unknown>', 'E1220: String or Number required for argument 1'])
v9.CheckDefAndScriptFailure(['getbufline("a", [])'], ['E1013: Argument 2: type mismatch, expected string but got list<unknown>', 'E1220: String or Number required for argument 2'])
v9.CheckDefAndScriptFailure(['getbufline("a", 2, 0z10)'], ['E1013: Argument 3: type mismatch, expected string but got blob', 'E1220: String or Number required for argument 3'])
v9.CheckDefAndScriptFailure(['getbufoneline([], 2)'], ['E1013: Argument 1: type mismatch, expected string but got list<unknown>', 'E1220: String or Number required for argument 1'])
v9.CheckDefAndScriptFailure(['getbufoneline("a", [])'], ['E1013: Argument 2: type mismatch, expected string but got list<unknown>', 'E1220: String or Number required for argument 2'])
enddef
def Test_getbufvar()

View File

@@ -695,6 +695,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
916,
/**/
915,
/**/