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().

ce30ccc06a

Cherry-pick part of usr_41.txt from patch 8.1.1628.

Co-authored-by: Bram Moolenaar <Bram@vim.org>
This commit is contained in:
zeertzjq 2022-11-22 06:52:21 +08:00
parent 5836c89ed0
commit 9b76875235
5 changed files with 42 additions and 9 deletions

View File

@ -170,6 +170,7 @@ get({func}, {what}) any get property of funcref/partial {func}
getbufinfo([{buf}]) List information about buffers getbufinfo([{buf}]) List information about buffers
getbufline({buf}, {lnum} [, {end}]) getbufline({buf}, {lnum} [, {end}])
List lines {lnum} to {end} of buffer {buf} List lines {lnum} to {end} of buffer {buf}
getbufoneline({buf}, {lnum}) String line {lnum} of buffer {buf}
getbufvar({buf}, {varname} [, {def}]) getbufvar({buf}, {varname} [, {def}])
any variable {varname} in buffer {buf} any variable {varname} in buffer {buf}
getchangelist([{buf}]) List list of change list items getchangelist([{buf}]) List list of change list items
@ -2685,11 +2686,13 @@ getbufinfo([{dict}])
Can also be used as a |method|: > Can also be used as a |method|: >
GetBufnr()->getbufinfo() GetBufnr()->getbufinfo()
< <
*getbufline()* *getbufline()*
getbufline({buf}, {lnum} [, {end}]) getbufline({buf}, {lnum} [, {end}])
Return a |List| with the lines starting from {lnum} to {end} Return a |List| with the lines starting from {lnum} to {end}
(inclusive) in the buffer {buf}. If {end} is omitted, a (inclusive) in the buffer {buf}. If {end} is omitted, a
|List| with only the line {lnum} is returned. |List| with only the line {lnum} is returned. See
`getbufoneline()` for only getting the line.
For the use of {buf}, see |bufname()| above. For the use of {buf}, see |bufname()| above.
@ -2712,6 +2715,11 @@ getbufline({buf}, {lnum} [, {end}])
< Can also be used as a |method|: > < Can also be used as a |method|: >
GetBufnr()->getbufline(lnum) GetBufnr()->getbufline(lnum)
<
*getbufoneline()*
getbufoneline({buf}, {lnum})
Just like `getbufline()` but only get one line and return it
as a string.
getbufvar({buf}, {varname} [, {def}]) *getbufvar()* getbufvar({buf}, {varname} [, {def}]) *getbufvar()*
The result is the value of option or local buffer variable The result is the value of option or local buffer variable
@ -3199,7 +3207,8 @@ getline({lnum} [, {end}])
< Can also be used as a |method|: > < Can also be used as a |method|: >
ComputeLnum()->getline() ComputeLnum()->getline()
< To get lines from another buffer see |getbufline()| < To get lines from another buffer see |getbufline()| and
|getbufoneline()|
getloclist({nr} [, {what}]) *getloclist()* getloclist({nr} [, {what}]) *getloclist()*
Returns a |List| with all the entries in the location list for Returns a |List| with all the entries in the location list for

View File

@ -785,6 +785,13 @@ Working with text in the current buffer: *text-functions*
getcharsearch() return character search information getcharsearch() return character search information
setcharsearch() set character search information setcharsearch() set character search information
Working with text in another buffer:
getbufline() get a list of lines from the specified buffer
getbufoneline() get a one line from the specified buffer
setbufline() replace a line in the specified buffer
appendbufline() append a list of lines in the specified buffer
deletebufline() delete lines from a specified buffer
*system-functions* *file-functions* *system-functions* *file-functions*
System functions and manipulation of files: System functions and manipulation of files:
glob() expand wildcards glob() expand wildcards
@ -838,8 +845,10 @@ Buffers, windows and the argument list:
argidx() current position in the argument list argidx() current position in the argument list
arglistid() get id of the argument list arglistid() get id of the argument list
argv() get one entry from the argument list argv() get one entry from the argument list
bufadd() add a file to the list of buffers
bufexists() check if a buffer exists bufexists() check if a buffer exists
buflisted() check if a buffer exists and is listed buflisted() check if a buffer exists and is listed
bufload() ensure a buffer is loaded
bufloaded() check if a buffer exists and is loaded bufloaded() check if a buffer exists and is loaded
bufname() get the name of a specific buffer bufname() get the name of a specific buffer
bufnr() get the buffer number of a specific buffer bufnr() get the buffer number of a specific buffer
@ -850,10 +859,6 @@ Buffers, windows and the argument list:
bufwinid() get the window ID of a specific buffer bufwinid() get the window ID of a specific buffer
bufwinnr() get the window number of a specific buffer bufwinnr() get the window number of a specific buffer
winbufnr() get the buffer number of a specific window winbufnr() get the buffer number of a specific window
getbufline() get a list of lines from the specified buffer
setbufline() replace a line in the specified buffer
appendbufline() append a list of lines in the specified buffer
deletebufline() delete lines from a specified buffer
win_findbuf() find windows containing a buffer win_findbuf() find windows containing a buffer
win_getid() get window ID of a window win_getid() get window ID of a window
win_gettype() get type of window win_gettype() get type of window

View File

@ -146,6 +146,7 @@ return {
get={args={2, 3}, base=1}, get={args={2, 3}, base=1},
getbufinfo={args={0, 1}, base=1}, getbufinfo={args={0, 1}, base=1},
getbufline={args={2, 3}, base=1}, getbufline={args={2, 3}, base=1},
getbufoneline={args=2, base=1},
getbufvar={args={2, 3}, base=1}, getbufvar={args={2, 3}, base=1},
getchangelist={args={0, 1}, base=1}, getchangelist={args={0, 1}, base=1},
getchar={args={0, 1}}, getchar={args={0, 1}},

View File

@ -2671,8 +2671,9 @@ static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retli
} }
} }
/// "getbufline()" function /// @param retlist true: "getbufline()" function
static void f_getbufline(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) /// false: "getbufoneline()" function
static void getbufline(typval_T *argvars, typval_T *rettv, bool retlist)
{ {
const int did_emsg_before = did_emsg; const int did_emsg_before = did_emsg;
buf_T *const buf = tv_get_buf_from_arg(&argvars[0]); buf_T *const buf = tv_get_buf_from_arg(&argvars[0]);
@ -2684,7 +2685,19 @@ static void f_getbufline(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
? lnum ? lnum
: tv_get_lnum_buf(&argvars[2], buf)); : 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
static void f_getbufline(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{
getbufline(argvars, rettv, true);
}
/// "getbufoneline()" function
static void f_getbufoneline(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{
getbufline(argvars, rettv, false);
} }
/// "getchangelist()" function /// "getchangelist()" function

View File

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