• R/O
  • SSH

vim: Commit

Mirror of the Vim source from https://github.com/vim/vim


Commit MetaInfo

Revision7831da568864beda683a0fdaaec473fdb30a24e6 (tree)
Time2022-11-22 05:00:06
AuthorBram Moolenaar <Bram@vim....>
CommiterBram Moolenaar

Log Message

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

Commit: https://github.com/vim/vim/commit/ce30ccc06af7f2c03762e5b18dde37b26ea6ec42
Author: Bram Moolenaar <Bram@vim.org>
Date: Mon Nov 21 19:57:04 2022 +0000

patch 9.0.0916: getbufline() is inefficient for getting a single line
Problem: getbufline() is inefficient for getting a single line.
Solution: Add getbufoneline().

Change Summary

Incremental Difference

diff -r 82787a78ff90 -r 7831da568864 runtime/doc/builtin.txt
--- a/runtime/doc/builtin.txt Sun Nov 20 13:15:05 2022 +0100
+++ b/runtime/doc/builtin.txt Mon Nov 21 21:00:06 2022 +0100
@@ -208,6 +208,7 @@
208208 getbufinfo([{buf}]) List information about buffers
209209 getbufline({buf}, {lnum} [, {end}])
210210 List lines {lnum} to {end} of buffer {buf}
211+getbufoneline({buf}, {lnum}) String line {lnum} of buffer {buf}
211212 getbufvar({buf}, {varname} [, {def}])
212213 any variable {varname} in buffer {buf}
213214 getchangelist([{buf}]) List list of change list items
@@ -3204,7 +3205,8 @@
32043205 getbufline({buf}, {lnum} [, {end}])
32053206 Return a |List| with the lines starting from {lnum} to {end}
32063207 (inclusive) in the buffer {buf}. If {end} is omitted, a
3207- |List| with only the line {lnum} is returned.
3208+ |List| with only the line {lnum} is returned. See
3209+ `getbufoneline()` for only getting the line.
32083210
32093211 For the use of {buf}, see |bufname()| above.
32103212
@@ -3227,6 +3229,11 @@
32273229
32283230 < Can also be used as a |method|: >
32293231 GetBufnr()->getbufline(lnum)
3232+<
3233+ *getbufoneline()*
3234+getbufoneline({buf}, {lnum})
3235+ Just like `getbufline()` but only get one line and return it
3236+ as a string.
32303237
32313238 getbufvar({buf}, {varname} [, {def}]) *getbufvar()*
32323239 The result is the value of option or local buffer variable
@@ -3771,7 +3778,8 @@
37713778 < Can also be used as a |method|: >
37723779 ComputeLnum()->getline()
37733780
3774-< To get lines from another buffer see |getbufline()|
3781+< To get lines from another buffer see |getbufline()| and
3782+ |getbufoneline()|
37753783
37763784 getloclist({nr} [, {what}]) *getloclist()*
37773785 Returns a |List| with all the entries in the location list for
diff -r 82787a78ff90 -r 7831da568864 runtime/doc/usr_41.txt
--- a/runtime/doc/usr_41.txt Sun Nov 20 13:15:05 2022 +0100
+++ b/runtime/doc/usr_41.txt Mon Nov 21 21:00:06 2022 +0100
@@ -935,6 +935,7 @@
935935
936936 Working with text in another buffer:
937937 getbufline() get a list of lines from the specified buffer
938+ getbufoneline() get a one line from the specified buffer
938939 setbufline() replace a line in the specified buffer
939940 appendbufline() append a list of lines in the specified buffer
940941 deletebufline() delete lines from a specified buffer
diff -r 82787a78ff90 -r 7831da568864 src/evalbuffer.c
--- a/src/evalbuffer.c Sun Nov 20 13:15:05 2022 +0100
+++ b/src/evalbuffer.c Mon Nov 21 21:00:06 2022 +0100
@@ -814,10 +814,11 @@
814814 }
815815
816816 /*
817- * "getbufline()" function
817+ * "retlist" TRUE: "getbufline()" function
818+ * "retlist" FALSE: "getbufoneline()" function
818819 */
819- void
820-f_getbufline(typval_T *argvars, typval_T *rettv)
820+ static void
821+getbufline(typval_T *argvars, typval_T *rettv, int retlist)
821822 {
822823 linenr_T lnum = 1;
823824 linenr_T end = 1;
@@ -842,7 +843,25 @@
842843 end = tv_get_lnum_buf(&argvars[2], buf);
843844 }
844845
845- get_buffer_lines(buf, lnum, end, TRUE, rettv);
846+ get_buffer_lines(buf, lnum, end, retlist, rettv);
847+}
848+
849+/*
850+ * "getbufline()" function
851+ */
852+ void
853+f_getbufline(typval_T *argvars, typval_T *rettv)
854+{
855+ getbufline(argvars, rettv, TRUE);
856+}
857+
858+/*
859+ * "getbufoneline()" function
860+ */
861+ void
862+f_getbufoneline(typval_T *argvars, typval_T *rettv)
863+{
864+ getbufline(argvars, rettv, FALSE);
846865 }
847866
848867 /*
diff -r 82787a78ff90 -r 7831da568864 src/evalfunc.c
--- a/src/evalfunc.c Sun Nov 20 13:15:05 2022 +0100
+++ b/src/evalfunc.c Mon Nov 21 21:00:06 2022 +0100
@@ -1923,6 +1923,8 @@
19231923 ret_list_dict_any, f_getbufinfo},
19241924 {"getbufline", 2, 3, FEARG_1, arg3_buffer_lnum_lnum,
19251925 ret_list_string, f_getbufline},
1926+ {"getbufoneline", 2, 2, FEARG_1, arg2_buffer_lnum,
1927+ ret_string, f_getbufoneline},
19261928 {"getbufvar", 2, 3, FEARG_1, arg3_buffer_string_any,
19271929 ret_any, f_getbufvar},
19281930 {"getchangelist", 0, 1, FEARG_1, arg1_buffer,
diff -r 82787a78ff90 -r 7831da568864 src/proto/evalbuffer.pro
--- a/src/proto/evalbuffer.pro Sun Nov 20 13:15:05 2022 +0100
+++ b/src/proto/evalbuffer.pro Mon Nov 21 21:00:06 2022 +0100
@@ -16,6 +16,7 @@
1616 void f_deletebufline(typval_T *argvars, typval_T *rettv);
1717 void f_getbufinfo(typval_T *argvars, typval_T *rettv);
1818 void f_getbufline(typval_T *argvars, typval_T *rettv);
19+void f_getbufoneline(typval_T *argvars, typval_T *rettv);
1920 void f_getline(typval_T *argvars, typval_T *rettv);
2021 void f_setbufline(typval_T *argvars, typval_T *rettv);
2122 void f_setline(typval_T *argvars, typval_T *rettv);
diff -r 82787a78ff90 -r 7831da568864 src/testdir/test_bufline.vim
--- a/src/testdir/test_bufline.vim Sun Nov 20 13:15:05 2022 +0100
+++ b/src/testdir/test_bufline.vim Mon Nov 21 21:00:06 2022 +0100
@@ -11,7 +11,9 @@
1111 hide
1212 call assert_equal(0, setbufline(b, 1, ['foo', 'bar']))
1313 call assert_equal(['foo'], getbufline(b, 1))
14+ call assert_equal('foo', getbufoneline(b, 1))
1415 call assert_equal(['bar'], getbufline(b, '$'))
16+ call assert_equal('bar', getbufoneline(b, '$'))
1517 call assert_equal(['foo', 'bar'], getbufline(b, 1, 2))
1618 exe "bd!" b
1719 call assert_equal([], getbufline(b, 1, 2))
@@ -35,8 +37,11 @@
3537
3638 call assert_equal(0, setbufline(b, 4, ['d', 'e']))
3739 call assert_equal(['c'], b->getbufline(3))
40+ call assert_equal('c', b->getbufoneline(3))
3841 call assert_equal(['d'], getbufline(b, 4))
42+ call assert_equal('d', getbufoneline(b, 4))
3943 call assert_equal(['e'], getbufline(b, 5))
44+ call assert_equal('e', getbufoneline(b, 5))
4045 call assert_equal([], getbufline(b, 6))
4146 call assert_equal([], getbufline(b, 2, 1))
4247
diff -r 82787a78ff90 -r 7831da568864 src/testdir/test_vim9_builtin.vim
--- a/src/testdir/test_vim9_builtin.vim Sun Nov 20 13:15:05 2022 +0100
+++ b/src/testdir/test_vim9_builtin.vim Mon Nov 21 21:00:06 2022 +0100
@@ -1724,15 +1724,23 @@
17241724 getbufline(-1, '$', '$')->assert_equal([])
17251725 getbufline(-1, 1, '$')->assert_equal([])
17261726
1727+ getbufoneline('#', 1)->assert_equal(lines[0])
1728+
17271729 assert_equal([7, 7, 7], getbufline('#', 1, '$')->map((_, _) => 7))
17281730
17291731 assert_fails('getbufline("", "$a", "$b")', ['E1030: Using a String as a Number: "$a"', 'E1030: Using a String as a Number: "$a"'])
17301732 assert_fails('getbufline("", "$", "$b")', ['E1030: Using a String as a Number: "$b"', 'E1030: Using a String as a Number: "$b"'])
17311733 bwipe!
17321734
1735+ assert_fails('getbufoneline("", "$a")', ['E1030: Using a String as a Number: "$a"', 'E1030: Using a String as a Number: "$a"'])
1736+ bwipe!
1737+
17331738 v9.CheckDefAndScriptFailure(['getbufline([], 2)'], ['E1013: Argument 1: type mismatch, expected string but got list<unknown>', 'E1220: String or Number required for argument 1'])
17341739 v9.CheckDefAndScriptFailure(['getbufline("a", [])'], ['E1013: Argument 2: type mismatch, expected string but got list<unknown>', 'E1220: String or Number required for argument 2'])
17351740 v9.CheckDefAndScriptFailure(['getbufline("a", 2, 0z10)'], ['E1013: Argument 3: type mismatch, expected string but got blob', 'E1220: String or Number required for argument 3'])
1741+
1742+ v9.CheckDefAndScriptFailure(['getbufoneline([], 2)'], ['E1013: Argument 1: type mismatch, expected string but got list<unknown>', 'E1220: String or Number required for argument 1'])
1743+ v9.CheckDefAndScriptFailure(['getbufoneline("a", [])'], ['E1013: Argument 2: type mismatch, expected string but got list<unknown>', 'E1220: String or Number required for argument 2'])
17361744 enddef
17371745
17381746 def Test_getbufvar()
diff -r 82787a78ff90 -r 7831da568864 src/version.c
--- a/src/version.c Sun Nov 20 13:15:05 2022 +0100
+++ b/src/version.c Mon Nov 21 21:00:06 2022 +0100
@@ -696,6 +696,8 @@
696696 static int included_patches[] =
697697 { /* Add new patch number below this line */
698698 /**/
699+ 916,
700+/**/
699701 915,
700702 /**/
701703 914,
Show on old repository browser