diff options
author | Justin M. Keyes <justinkz@gmail.com> | 2018-06-09 09:32:49 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-06-09 09:32:49 +0200 |
commit | b94b59e4e88411ba7c7802827c872c1ffb896169 (patch) | |
tree | 533d9cc9530fd786faff6769c7225d89ad618bca /src/nvim/api/private/helpers.c | |
parent | aaece7849259185b116081641890764b6d459376 (diff) | |
download | rneovim-b94b59e4e88411ba7c7802827c872c1ffb896169.tar.gz rneovim-b94b59e4e88411ba7c7802827c872c1ffb896169.tar.bz2 rneovim-b94b59e4e88411ba7c7802827c872c1ffb896169.zip |
refactor: buf_collect_lines (#8509)
Move redundant common logic into a function.
Diffstat (limited to 'src/nvim/api/private/helpers.c')
-rw-r--r-- | src/nvim/api/private/helpers.c | 38 |
1 files changed, 38 insertions, 0 deletions
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. |