mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
Merge pull request #1341 from splinterofchaos/api-nul
Api: Improve Nul handling
This commit is contained in:
commit
01fc0efdca
@ -132,7 +132,12 @@ ArrayOf(String) buffer_get_line_slice(Buffer buffer,
|
|||||||
}
|
}
|
||||||
|
|
||||||
const char *bufstr = (char *) ml_get_buf(buf, (linenr_T) lnum, false);
|
const char *bufstr = (char *) ml_get_buf(buf, (linenr_T) lnum, false);
|
||||||
rv.items[i] = STRING_OBJ(cstr_to_string(bufstr));
|
Object str = STRING_OBJ(cstr_to_string(bufstr));
|
||||||
|
|
||||||
|
// Vim represents NULs as NLs, but this may confuse clients.
|
||||||
|
strchrsub(str.data.string.data, '\n', '\0');
|
||||||
|
|
||||||
|
rv.items[i] = str;
|
||||||
}
|
}
|
||||||
|
|
||||||
end:
|
end:
|
||||||
@ -201,7 +206,18 @@ void buffer_set_line_slice(Buffer buffer,
|
|||||||
}
|
}
|
||||||
|
|
||||||
String l = replacement.items[i].data.string;
|
String l = replacement.items[i].data.string;
|
||||||
lines[i] = xmemdupz(l.data, l.size);
|
|
||||||
|
// Fill lines[i] with l's contents. Disallow newlines in the middle of a
|
||||||
|
// line and convert NULs to newlines to avoid truncation.
|
||||||
|
lines[i] = xmallocz(l.size);
|
||||||
|
for (size_t j = 0; j < l.size; j++) {
|
||||||
|
if (l.data[j] == '\n') {
|
||||||
|
api_set_error(err, Exception, _("string cannot contain newlines"));
|
||||||
|
new_len = i + 1;
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
lines[i][j] = (char) (l.data[j] == '\0' ? '\n' : l.data[j]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try_start();
|
try_start();
|
||||||
|
@ -34,6 +34,11 @@ describe('buffer_* functions', function()
|
|||||||
curbuf('del_line', 0)
|
curbuf('del_line', 0)
|
||||||
eq('', curbuf('get_line', 0))
|
eq('', curbuf('get_line', 0))
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it('can handle NULs', function()
|
||||||
|
curbuf('set_line', 0, 'ab\0cd')
|
||||||
|
eq('ab\0cd', curbuf('get_line', 0))
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|
||||||
|
@ -36,6 +36,10 @@ describe('vim_* functions', function()
|
|||||||
-- 19 * 2 (each japanese character occupies two cells)
|
-- 19 * 2 (each japanese character occupies two cells)
|
||||||
eq(44, nvim('strwidth', 'neovimのデザインかなりまともなのになってる。'))
|
eq(44, nvim('strwidth', 'neovimのデザインかなりまともなのになってる。'))
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it('cannot handle NULs', function()
|
||||||
|
eq(0, nvim('strwidth', '\0abc'))
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe('{get,set}_current_line', function()
|
describe('{get,set}_current_line', function()
|
||||||
@ -52,6 +56,11 @@ describe('vim_* functions', function()
|
|||||||
eq({1, 2, {['3'] = 1}}, nvim('get_var', 'lua'))
|
eq({1, 2, {['3'] = 1}}, nvim('get_var', 'lua'))
|
||||||
eq({1, 2, {['3'] = 1}}, nvim('eval', 'g:lua'))
|
eq({1, 2, {['3'] = 1}}, nvim('eval', 'g:lua'))
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it('truncates values with NULs in them', function()
|
||||||
|
nvim('set_var', 'xxx', 'ab\0cd')
|
||||||
|
eq('ab', nvim('get_var', 'xxx'))
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe('{get,set}_option', function()
|
describe('{get,set}_option', function()
|
||||||
|
Loading…
Reference in New Issue
Block a user