[PATCH] Do count line breaks before end of file (bin & noeol)

57 views
Skip to first unread message

Rob Wu

unread,
Jul 12, 2015, 8:58:07 AM7/12/15
to vim...@vim.org, Rob Wu
The counted number of bytes is decreased if 'bin' and 'noeol' are set.
This is incorrect, and the logic should only apply when the requested
line is the last line in the buffer.

Test cases:
vim -b <(printf '123\n567') +':echo line2byte(1)'
Expected: 1
Actual : -1

vim -b <(printf '123\n567') +':echo line2byte(2)'
Expected: 5
Actual : 4

vim -b <(printf '123\r\n678') +':echo line2byte(2)'
Expected: 6
Actual : 5

vim -b <(printf '123\n567') +':echo line2byte(3)'
Expected: 9
Actual : 8

vim -b <(printf '123\r\n678') +':echo line2byte(3)'
Expected: 10 (not 11, because in binary mode fileformat is ignored,
so pressing enter adds a LF and not a CR LF).
Actual : 9
---
src/memline.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/memline.c b/src/memline.c
index e809360..91ef270 100644
--- a/src/memline.c
+++ b/src/memline.c
@@ -5362,7 +5362,7 @@ ml_find_line_or_offset(buf, lnum, offp)
size += lnum - 1;

/* Don't count the last line break if 'bin' and 'noeol'. */
- if (buf->b_p_bin && !buf->b_p_eol)
+ if (buf->b_p_bin && !buf->b_p_eol && buf->b_ml.ml_line_count == lnum)
size -= ffdos + 1;
}

--
2.4.5

Bram Moolenaar

unread,
Jul 12, 2015, 11:53:33 AM7/12/15
to Rob Wu, vim...@vim.org

Rob Wu wrote:

> The counted number of bytes is decreased if 'bin' and 'noeol' are set.
> This is incorrect, and the logic should only apply when the requested
> line is the last line in the buffer.
>
> Test cases:
> vim -b <(printf '123\n567') +':echo line2byte(1)'
> Expected: 1
> Actual : -1
>
> vim -b <(printf '123\n567') +':echo line2byte(2)'
> Expected: 5
> Actual : 4
>
> vim -b <(printf '123\r\n678') +':echo line2byte(2)'
> Expected: 6
> Actual : 5
>
> vim -b <(printf '123\n567') +':echo line2byte(3)'
> Expected: 9
> Actual : 8
>
> vim -b <(printf '123\r\n678') +':echo line2byte(3)'
> Expected: 10 (not 11, because in binary mode fileformat is ignored,
> so pressing enter adds a LF and not a CR LF).
> Actual : 9

Thanks for the patch. I notice there aren't any tests for line2byte(),
perhaps someone can create them?


--
hundred-and-one symptoms of being an internet addict:
212. Your Internet group window has more icons than your Accessories window.

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

Rob Wu

unread,
Jul 12, 2015, 5:33:43 PM7/12/15
to Bram Moolenaar, Rob Wu, vim...@vim.org
2015-07-12 17:53 GMT+02:00 Bram Moolenaar <Br...@moolenaar.net>:
>
> Rob Wu wrote:
>
>> The counted number of bytes is decreased if 'bin' and 'noeol' are set.
>> This is incorrect, and the logic should only apply when the requested
>> line is the last line in the buffer.
>>
>> Test cases:
>> vim -b <(printf '123\n567') +':echo line2byte(1)'
>> Expected: 1
>> Actual : -1
>>
>> vim -b <(printf '123\n567') +':echo line2byte(2)'
>> Expected: 5
>> Actual : 4
>>
>> vim -b <(printf '123\r\n678') +':echo line2byte(2)'
>> Expected: 6
>> Actual : 5
>>
>> vim -b <(printf '123\n567') +':echo line2byte(3)'
>> Expected: 9
>> Actual : 8
>>
>> vim -b <(printf '123\r\n678') +':echo line2byte(3)'
>> Expected: 10 (not 11, because in binary mode fileformat is ignored,
>> so pressing enter adds a LF and not a CR LF).
>> Actual : 9
>
> Thanks for the patch. I notice there aren't any tests for line2byte(),
> perhaps someone can create them?

I've added some manual tests to my commit description. You could copy
them and add more tests to avoid regressions. I just started with Vim
scripts last week, so I'm probably not in the best position to write
these tests. Thanks for your fast response!

Kind regards,
Rob
https://robwu.nl
Reply all
Reply to author
Forward
0 new messages