aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames McCoy <jamessan@jamessan.com>2017-03-07 09:03:52 -0500
committerGitHub <noreply@github.com>2017-03-07 09:03:52 -0500
commitf613dd016a75073d2fb251dc54315967b9bd6b93 (patch)
treeeca09d83d72adb3bdfdc8409f5c8d1973189e667
parent70bbd5a7ef617d5048406cd737882c82253e5cfa (diff)
parent532197b4f95596e202fb230523ceb0c9e6233dae (diff)
downloadrneovim-f613dd016a75073d2fb251dc54315967b9bd6b93.tar.gz
rneovim-f613dd016a75073d2fb251dc54315967b9bd6b93.tar.bz2
rneovim-f613dd016a75073d2fb251dc54315967b9bd6b93.zip
Merge pull request #6225 from jamessan/vim-7.4.2051
vim-patch:7.4.2051,7.4.2068,7.4.2097
-rw-r--r--src/nvim/message.c26
-rw-r--r--src/nvim/version.c6
-rw-r--r--test/functional/ui/inccommand_spec.lua18
-rw-r--r--test/unit/message_spec.lua59
4 files changed, 86 insertions, 23 deletions
diff --git a/src/nvim/message.c b/src/nvim/message.c
index 699f4b87b9..4cd0db21e8 100644
--- a/src/nvim/message.c
+++ b/src/nvim/message.c
@@ -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 {
diff --git a/src/nvim/version.c b/src/nvim/version.c
index 9c816457c1..eae0da98f4 100644
--- a/src/nvim/version.c
+++ b/src/nvim/version.c
@@ -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
diff --git a/test/functional/ui/inccommand_spec.lua b/test/functional/ui/inccommand_spec.lua
index 6da22b6a3a..3b31da0397 100644
--- a/test/functional/ui/inccommand_spec.lua
+++ b/test/functional/ui/inccommand_spec.lua
@@ -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()
diff --git a/test/unit/message_spec.lua b/test/unit/message_spec.lua
new file mode 100644
index 0000000000..afb572347f
--- /dev/null
+++ b/test/unit/message_spec.lua
@@ -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)