Mirror of the Vim source from https://github.com/vim/vim
Revision | 7831da568864beda683a0fdaaec473fdb30a24e6 (tree) |
---|---|
Time | 2022-11-22 05:00:06 |
Author | Bram Moolenaar <Bram@vim....> |
Commiter | Bram Moolenaar |
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
@@ -208,6 +208,7 @@ | ||
208 | 208 | getbufinfo([{buf}]) List information about buffers |
209 | 209 | getbufline({buf}, {lnum} [, {end}]) |
210 | 210 | List lines {lnum} to {end} of buffer {buf} |
211 | +getbufoneline({buf}, {lnum}) String line {lnum} of buffer {buf} | |
211 | 212 | getbufvar({buf}, {varname} [, {def}]) |
212 | 213 | any variable {varname} in buffer {buf} |
213 | 214 | getchangelist([{buf}]) List list of change list items |
@@ -3204,7 +3205,8 @@ | ||
3204 | 3205 | getbufline({buf}, {lnum} [, {end}]) |
3205 | 3206 | Return a |List| with the lines starting from {lnum} to {end} |
3206 | 3207 | (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. | |
3208 | 3210 | |
3209 | 3211 | For the use of {buf}, see |bufname()| above. |
3210 | 3212 |
@@ -3227,6 +3229,11 @@ | ||
3227 | 3229 | |
3228 | 3230 | < Can also be used as a |method|: > |
3229 | 3231 | 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. | |
3230 | 3237 | |
3231 | 3238 | getbufvar({buf}, {varname} [, {def}]) *getbufvar()* |
3232 | 3239 | The result is the value of option or local buffer variable |
@@ -3771,7 +3778,8 @@ | ||
3771 | 3778 | < Can also be used as a |method|: > |
3772 | 3779 | ComputeLnum()->getline() |
3773 | 3780 | |
3774 | -< To get lines from another buffer see |getbufline()| | |
3781 | +< To get lines from another buffer see |getbufline()| and | |
3782 | + |getbufoneline()| | |
3775 | 3783 | |
3776 | 3784 | getloclist({nr} [, {what}]) *getloclist()* |
3777 | 3785 | Returns a |List| with all the entries in the location list for |
@@ -935,6 +935,7 @@ | ||
935 | 935 | |
936 | 936 | Working with text in another buffer: |
937 | 937 | getbufline() get a list of lines from the specified buffer |
938 | + getbufoneline() get a one line from the specified buffer | |
938 | 939 | setbufline() replace a line in the specified buffer |
939 | 940 | appendbufline() append a list of lines in the specified buffer |
940 | 941 | deletebufline() delete lines from a specified buffer |
@@ -814,10 +814,11 @@ | ||
814 | 814 | } |
815 | 815 | |
816 | 816 | /* |
817 | - * "getbufline()" function | |
817 | + * "retlist" TRUE: "getbufline()" function | |
818 | + * "retlist" FALSE: "getbufoneline()" function | |
818 | 819 | */ |
819 | - void | |
820 | -f_getbufline(typval_T *argvars, typval_T *rettv) | |
820 | + static void | |
821 | +getbufline(typval_T *argvars, typval_T *rettv, int retlist) | |
821 | 822 | { |
822 | 823 | linenr_T lnum = 1; |
823 | 824 | linenr_T end = 1; |
@@ -842,7 +843,25 @@ | ||
842 | 843 | end = tv_get_lnum_buf(&argvars[2], buf); |
843 | 844 | } |
844 | 845 | |
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); | |
846 | 865 | } |
847 | 866 | |
848 | 867 | /* |
@@ -1923,6 +1923,8 @@ | ||
1923 | 1923 | ret_list_dict_any, f_getbufinfo}, |
1924 | 1924 | {"getbufline", 2, 3, FEARG_1, arg3_buffer_lnum_lnum, |
1925 | 1925 | ret_list_string, f_getbufline}, |
1926 | + {"getbufoneline", 2, 2, FEARG_1, arg2_buffer_lnum, | |
1927 | + ret_string, f_getbufoneline}, | |
1926 | 1928 | {"getbufvar", 2, 3, FEARG_1, arg3_buffer_string_any, |
1927 | 1929 | ret_any, f_getbufvar}, |
1928 | 1930 | {"getchangelist", 0, 1, FEARG_1, arg1_buffer, |
@@ -16,6 +16,7 @@ | ||
16 | 16 | void f_deletebufline(typval_T *argvars, typval_T *rettv); |
17 | 17 | void f_getbufinfo(typval_T *argvars, typval_T *rettv); |
18 | 18 | void f_getbufline(typval_T *argvars, typval_T *rettv); |
19 | +void f_getbufoneline(typval_T *argvars, typval_T *rettv); | |
19 | 20 | void f_getline(typval_T *argvars, typval_T *rettv); |
20 | 21 | void f_setbufline(typval_T *argvars, typval_T *rettv); |
21 | 22 | void f_setline(typval_T *argvars, typval_T *rettv); |
@@ -11,7 +11,9 @@ | ||
11 | 11 | hide |
12 | 12 | call assert_equal(0, setbufline(b, 1, ['foo', 'bar'])) |
13 | 13 | call assert_equal(['foo'], getbufline(b, 1)) |
14 | + call assert_equal('foo', getbufoneline(b, 1)) | |
14 | 15 | call assert_equal(['bar'], getbufline(b, '$')) |
16 | + call assert_equal('bar', getbufoneline(b, '$')) | |
15 | 17 | call assert_equal(['foo', 'bar'], getbufline(b, 1, 2)) |
16 | 18 | exe "bd!" b |
17 | 19 | call assert_equal([], getbufline(b, 1, 2)) |
@@ -35,8 +37,11 @@ | ||
35 | 37 | |
36 | 38 | call assert_equal(0, setbufline(b, 4, ['d', 'e'])) |
37 | 39 | call assert_equal(['c'], b->getbufline(3)) |
40 | + call assert_equal('c', b->getbufoneline(3)) | |
38 | 41 | call assert_equal(['d'], getbufline(b, 4)) |
42 | + call assert_equal('d', getbufoneline(b, 4)) | |
39 | 43 | call assert_equal(['e'], getbufline(b, 5)) |
44 | + call assert_equal('e', getbufoneline(b, 5)) | |
40 | 45 | call assert_equal([], getbufline(b, 6)) |
41 | 46 | call assert_equal([], getbufline(b, 2, 1)) |
42 | 47 |
@@ -1724,15 +1724,23 @@ | ||
1724 | 1724 | getbufline(-1, '$', '$')->assert_equal([]) |
1725 | 1725 | getbufline(-1, 1, '$')->assert_equal([]) |
1726 | 1726 | |
1727 | + getbufoneline('#', 1)->assert_equal(lines[0]) | |
1728 | + | |
1727 | 1729 | assert_equal([7, 7, 7], getbufline('#', 1, '$')->map((_, _) => 7)) |
1728 | 1730 | |
1729 | 1731 | assert_fails('getbufline("", "$a", "$b")', ['E1030: Using a String as a Number: "$a"', 'E1030: Using a String as a Number: "$a"']) |
1730 | 1732 | assert_fails('getbufline("", "$", "$b")', ['E1030: Using a String as a Number: "$b"', 'E1030: Using a String as a Number: "$b"']) |
1731 | 1733 | bwipe! |
1732 | 1734 | |
1735 | + assert_fails('getbufoneline("", "$a")', ['E1030: Using a String as a Number: "$a"', 'E1030: Using a String as a Number: "$a"']) | |
1736 | + bwipe! | |
1737 | + | |
1733 | 1738 | v9.CheckDefAndScriptFailure(['getbufline([], 2)'], ['E1013: Argument 1: type mismatch, expected string but got list<unknown>', 'E1220: String or Number required for argument 1']) |
1734 | 1739 | v9.CheckDefAndScriptFailure(['getbufline("a", [])'], ['E1013: Argument 2: type mismatch, expected string but got list<unknown>', 'E1220: String or Number required for argument 2']) |
1735 | 1740 | 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']) | |
1736 | 1744 | enddef |
1737 | 1745 | |
1738 | 1746 | def Test_getbufvar() |
@@ -696,6 +696,8 @@ | ||
696 | 696 | static int included_patches[] = |
697 | 697 | { /* Add new patch number below this line */ |
698 | 698 | /**/ |
699 | + 916, | |
700 | +/**/ | |
699 | 701 | 915, |
700 | 702 | /**/ |
701 | 703 | 914, |