diff options
author | ZyX <kp-pav@yandex.ru> | 2016-04-04 04:58:21 +0300 |
---|---|---|
committer | ZyX <kp-pav@yandex.ru> | 2016-04-18 02:48:20 +0300 |
commit | 45304b482cbeae01af3a89358d96b0d1511213c3 (patch) | |
tree | 0228e0ccc011a86c34c0b7b2d8975195e2dc558d /src/nvim/eval/encode.c | |
parent | bda0165514a582978c2da672b528562df78a2d1a (diff) | |
download | rneovim-45304b482cbeae01af3a89358d96b0d1511213c3.tar.gz rneovim-45304b482cbeae01af3a89358d96b0d1511213c3.tar.bz2 rneovim-45304b482cbeae01af3a89358d96b0d1511213c3.zip |
eval/encode: Simplify loop in encode_list_write
Patch made up by oni-link.
Diffstat (limited to 'src/nvim/eval/encode.c')
-rw-r--r-- | src/nvim/eval/encode.c | 48 |
1 files changed, 25 insertions, 23 deletions
diff --git a/src/nvim/eval/encode.c b/src/nvim/eval/encode.c index 88c731e92a..c651a50be9 100644 --- a/src/nvim/eval/encode.c +++ b/src/nvim/eval/encode.c @@ -75,36 +75,38 @@ int encode_list_write(void *data, const char *buf, size_t len) const char *const end = buf + len; const char *line_end = buf; listitem_T *li = list->lv_last; - do { + + // Continue the last list element + if (li != NULL) { + line_end = xmemscan(buf, NL, len); + if (line_end != buf) { + const size_t line_length = (size_t)(line_end - buf); + char *str = (char *)li->li_tv.vval.v_string; + const size_t li_len = (str == NULL ? 0 : strlen(str)); + li->li_tv.vval.v_string = xrealloc(str, li_len + line_length + 1); + str = (char *)li->li_tv.vval.v_string + li_len; + memcpy(str, buf, line_length); + str[line_length] = 0; + memchrsub(str, NUL, NL, line_length); + } + line_end++; + } + + while (line_end < end) { const char *line_start = line_end; line_end = xmemscan(line_start, NL, (size_t) (end - line_start)); char *str = NULL; if (line_end != line_start) { - const size_t line_length = (size_t) (line_end - line_start); - if (li == NULL) { - str = xmemdupz(line_start, line_length); - } else { - const size_t li_len = (li->li_tv.vval.v_string == NULL - ? 0 - : STRLEN(li->li_tv.vval.v_string)); - li->li_tv.vval.v_string = xrealloc(li->li_tv.vval.v_string, - li_len + line_length + 1); - str = (char *) li->li_tv.vval.v_string + li_len; - memcpy(str, line_start, line_length); - str[line_length] = 0; - } + const size_t line_length = (size_t)(line_end - line_start); + str = xmemdupz(line_start, line_length); memchrsub(str, NUL, NL, line_length); } - if (li == NULL) { - list_append_allocated_string(list, str); - } else { - li = NULL; - } - if (line_end == end - 1) { - list_append_allocated_string(list, NULL); - } + list_append_allocated_string(list, str); line_end++; - } while (line_end < end); + } + if (line_end == end) { + list_append_allocated_string(list, NULL); + } return 0; } |