aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/nvim/api/buffer.c20
-rw-r--r--src/nvim/api/private/helpers.c38
-rw-r--r--src/nvim/buffer_updates.c33
3 files changed, 46 insertions, 45 deletions
diff --git a/src/nvim/api/buffer.c b/src/nvim/api/buffer.c
index 215859a499..e1fe7617ff 100644
--- a/src/nvim/api/buffer.c
+++ b/src/nvim/api/buffer.c
@@ -238,23 +238,9 @@ ArrayOf(String) nvim_buf_get_lines(uint64_t channel_id,
rv.size = (size_t)(end - start);
rv.items = xcalloc(sizeof(Object), rv.size);
- for (size_t i = 0; i < rv.size; i++) {
- int64_t lnum = start + (int64_t)i;
-
- if (lnum >= MAXLNUM) {
- api_set_error(err, kErrorTypeValidation, "Line index is too high");
- goto end;
- }
-
- const char *bufstr = (char *)ml_get_buf(buf, (linenr_T)lnum, false);
- Object str = STRING_OBJ(cstr_to_string(bufstr));
-
- // Vim represents NULs as NLs, but this may confuse clients.
- if (channel_id != VIML_INTERNAL_CALL) {
- strchrsub(str.data.string.data, '\n', '\0');
- }
-
- rv.items[i] = str;
+ if (!buf_collect_lines(buf, rv.size, start,
+ (channel_id != VIML_INTERNAL_CALL), &rv, err)) {
+ goto end;
}
end:
diff --git a/src/nvim/api/private/helpers.c b/src/nvim/api/private/helpers.c
index 692a0b51fd..f3e883de02 100644
--- a/src/nvim/api/private/helpers.c
+++ b/src/nvim/api/private/helpers.c
@@ -16,6 +16,7 @@
#include "nvim/vim.h"
#include "nvim/buffer.h"
#include "nvim/window.h"
+#include "nvim/memline.h"
#include "nvim/memory.h"
#include "nvim/eval.h"
#include "nvim/eval/typval.h"
@@ -742,6 +743,43 @@ String cstr_as_string(char *str) FUNC_ATTR_PURE
return (String){ .data = str, .size = strlen(str) };
}
+/// Collects `n` buffer lines into array `l`, optionally replacing newlines
+/// with NUL.
+///
+/// @param buf Buffer to get lines from
+/// @param n Number of lines to collect
+/// @param replace_nl Replace newlines ("\n") with NUL
+/// @param start Line number to start from
+/// @param[out] l Lines are copied here
+/// @param err[out] Error, if any
+/// @return true unless `err` was set
+bool buf_collect_lines(buf_T *buf, size_t n, int64_t start, bool replace_nl,
+ Array *l, Error *err)
+{
+ for (size_t i = 0; i < n; i++) {
+ int64_t lnum = start + (int64_t)i;
+
+ if (lnum >= MAXLNUM) {
+ if (err != NULL) {
+ api_set_error(err, kErrorTypeValidation, "Line index is too high");
+ }
+ return false;
+ }
+
+ const char *bufstr = (char *)ml_get_buf(buf, (linenr_T)lnum, false);
+ Object str = STRING_OBJ(cstr_to_string(bufstr));
+
+ if (replace_nl) {
+ // Vim represents NULs as NLs, but this may confuse clients.
+ strchrsub(str.data.string.data, '\n', '\0');
+ }
+
+ l->items[i] = str;
+ }
+
+ return true;
+}
+
/// Converts from type Object to a VimL value.
///
/// @param obj Object to convert from.
diff --git a/src/nvim/buffer_updates.c b/src/nvim/buffer_updates.c
index 157f80e55a..c1b2828666 100644
--- a/src/nvim/buffer_updates.c
+++ b/src/nvim/buffer_updates.c
@@ -47,25 +47,14 @@ bool buf_updates_register(buf_T *buf, uint64_t channel_id, bool send_buffer)
// collect buffer contents
- // True now, but a compile time reminder for future systems we support
- STATIC_ASSERT(SIZE_MAX >= MAXLNUM, "size_t to small to hold the number of"
- " lines in a buffer");
+ STATIC_ASSERT(SIZE_MAX >= MAXLNUM, "size_t smaller than MAXLNUM");
size_t line_count = (size_t)buf->b_ml.ml_line_count;
if (line_count >= 1) {
linedata.size = line_count;
linedata.items = xcalloc(sizeof(Object), line_count);
- for (size_t i = 0; i < line_count; i++) {
- linenr_T lnum = 1 + (linenr_T)i;
- const char *bufstr = (char *)ml_get_buf(buf, lnum, false);
- 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');
-
- linedata.items[i] = str;
- }
+ buf_collect_lines(buf, line_count, 1, true, &linedata, NULL);
}
args.items[4] = ARRAY_OBJ(linedata);
@@ -170,23 +159,11 @@ void buf_updates_send_changes(buf_T *buf,
// linedata of lines being swapped in
Array linedata = ARRAY_DICT_INIT;
if (num_added > 0) {
- // True now, but a compile time reminder for future systems we support
- // Note that `num_added` is a `int64_t`, but still must be lower than
- // `MAX_LNUM`
- STATIC_ASSERT(SIZE_MAX >= MAXLNUM, "size_t to small to hold the number "
- "of lines in a buffer");
+ STATIC_ASSERT(SIZE_MAX >= MAXLNUM, "size_t smaller than MAXLNUM");
linedata.size = (size_t)num_added;
linedata.items = xcalloc(sizeof(Object), (size_t)num_added);
- for (int64_t i = 0; i < num_added; i++) {
- int64_t lnum = firstline + i;
- const char *bufstr = (char *)ml_get_buf(buf, (linenr_T)lnum, false);
- 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');
-
- linedata.items[i] = str;
- }
+ buf_collect_lines(buf, (size_t)num_added, firstline, true, &linedata,
+ NULL);
}
args.items[4] = ARRAY_OBJ(linedata);
args.items[5] = BOOLEAN_OBJ(false);