Merge pull request #6225 from jamessan/vim-7.4.2051

vim-patch:7.4.2051,7.4.2068,7.4.2097
This commit is contained in:
James McCoy 2017-03-07 09:03:52 -05:00 committed by GitHub
commit f613dd016a
4 changed files with 86 additions and 23 deletions

View File

@ -237,17 +237,19 @@ msg_strtrunc (
* Truncate a string "s" to "buf" with cell width "room".
* "s" and "buf" may be equal.
*/
void trunc_string(char_u *s, char_u *buf, int room, int buflen)
void trunc_string(char_u *s, char_u *buf, int room_in, int buflen)
{
int half;
int len;
size_t room = room_in - 3; // "..." takes 3 chars
size_t half;
size_t len = 0;
int e;
int i;
int n;
room -= 3;
if (room_in < 3) {
room = 0;
}
half = room / 2;
len = 0;
/* First part: Start of the string. */
for (e = 0; len < half && e < buflen; ++e) {
@ -257,8 +259,9 @@ void trunc_string(char_u *s, char_u *buf, int room, int buflen)
return;
}
n = ptr2cells(s + e);
if (len + n >= half)
if (len + n > half) {
break;
}
len += n;
buf[e] = s[e];
if (has_mbyte)
@ -274,9 +277,9 @@ void trunc_string(char_u *s, char_u *buf, int room, int buflen)
for (;;) {
do {
half = half - (*mb_head_off)(s, s + half - 1) - 1;
} while (utf_iscomposing(utf_ptr2char(s + half)) && half > 0);
} while (half > 0 && utf_iscomposing(utf_ptr2char(s + half)));
n = ptr2cells(s + half);
if (len + n > room) {
if (len + n > room || half == 0) {
break;
}
len += n;
@ -287,7 +290,7 @@ void trunc_string(char_u *s, char_u *buf, int room, int buflen)
// text fits without truncating
if (s != buf) {
len = STRLEN(s);
if (len >= buflen) {
if (len >= (size_t)buflen) {
len = buflen - 1;
}
len = len - e + 1;
@ -300,9 +303,10 @@ void trunc_string(char_u *s, char_u *buf, int room, int buflen)
} else if (e + 3 < buflen) {
// set the middle and copy the last part
memmove(buf + e, "...", (size_t)3);
len = (int)STRLEN(s + i) + 1;
if (len >= buflen - e - 3)
len = STRLEN(s + i) + 1;
if (len >= (size_t)buflen - e - 3) {
len = buflen - e - 3 - 1;
}
memmove(buf + e + 3, s + i, len);
buf[e + 3 + len - 1] = NUL;
} else {

View File

@ -343,7 +343,7 @@ static int included_patches[] = {
2100,
2099,
2098,
// 2097,
2097,
2096,
2095,
// 2094 NA
@ -372,7 +372,7 @@ static int included_patches[] = {
2071,
// 2070 NA
// 2069,
// 2068,
2068,
2067,
2066,
2065,
@ -389,7 +389,7 @@ static int included_patches[] = {
// 2054 NA
// 2053 NA
// 2052 NA
// 2051,
2051,
2050,
2049,
// 2048 NA

View File

@ -482,7 +482,7 @@ describe(":substitute, 'inccommand' preserves undo", function()
{15:~ }|
{15:~ }|
{15:~ }|
Already...st change |
Already ...t change |
]])
else
screen:expect([[
@ -495,7 +495,7 @@ describe(":substitute, 'inccommand' preserves undo", function()
{15:~ }|
{15:~ }|
{15:~ }|
Already...st change |
Already ...t change |
]])
end
end
@ -534,7 +534,7 @@ describe(":substitute, 'inccommand' preserves undo", function()
{15:~ }|
{15:~ }|
{15:~ }|
Already...st change |
Already ...t change |
]])
else
screen:expect([[
@ -547,7 +547,7 @@ describe(":substitute, 'inccommand' preserves undo", function()
{15:~ }|
{15:~ }|
{15:~ }|
Already...st change |
Already ...t change |
]])
end
@ -574,7 +574,7 @@ describe(":substitute, 'inccommand' preserves undo", function()
{15:~ }|
{15:~ }|
{15:~ }|
Already...st change |
Already ...t change |
]])
else
screen:expect([[
@ -587,7 +587,7 @@ describe(":substitute, 'inccommand' preserves undo", function()
{15:~ }|
{15:~ }|
{15:~ }|
Already...st change |
Already ...t change |
]])
end
screen:detach()
@ -616,7 +616,7 @@ describe(":substitute, 'inccommand' preserves undo", function()
{15:~ }|
{15:~ }|
{15:~ }|
Already...st change |
Already ...t change |
]])
else
screen:expect([[
@ -629,7 +629,7 @@ describe(":substitute, 'inccommand' preserves undo", function()
{15:~ }|
{15:~ }|
{15:~ }|
Already...st change |
Already ...t change |
]])
end
@ -653,7 +653,7 @@ describe(":substitute, 'inccommand' preserves undo", function()
{15:~ }|
{15:~ }|
{15:~ }|
Already...st change |
Already ...t change |
]])
end
screen:detach()

View File

@ -0,0 +1,59 @@
local helpers = require("test.unit.helpers")
local ffi = helpers.ffi
local eq = helpers.eq
local to_cstr = helpers.to_cstr
local cimp = helpers.cimport('./src/nvim/message.h', './src/nvim/memory.h',
'./src/nvim/strings.h')
describe('trunc_string', function()
local buflen = 40
local function test_inplace(s, expected, room)
room = room and room or 20
local buf = cimp.xmalloc(ffi.sizeof('char_u') * buflen)
ffi.C.strcpy(buf, s)
cimp.trunc_string(buf, buf, room, buflen)
eq(expected, ffi.string(buf))
cimp.xfree(buf)
end
local function test_copy(s, expected, room)
room = room and room or 20
local buf = cimp.xmalloc(ffi.sizeof('char_u') * buflen)
local str = cimp.vim_strsave(to_cstr(s))
cimp.trunc_string(str, buf, room, buflen)
eq(expected, ffi.string(buf))
cimp.xfree(buf)
cimp.xfree(str)
end
local permutations = {
{ ['desc'] = 'in-place', ['func'] = test_inplace },
{ ['desc'] = 'by copy', ['func'] = test_copy },
}
for _,t in ipairs(permutations) do
describe('populates buf '..t.desc, function()
it('with a small string', function()
t.func('text', 'text')
end)
it('with a medium string', function()
t.func('a short text', 'a short text')
end)
it('with a string of length == 1/2 room', function()
t.func('a text that fits', 'a text that fits', 34)
end)
it('with a string exactly the truncate size', function()
t.func('a text tha just fits', 'a text tha just fits')
end)
it('with a string that must be truncated', function()
t.func('a text that nott fits', 'a text t...nott fits')
end)
end)
end
end)