From c6271f57671e9dae22675fc76e1f2e3e30a2bda0 Mon Sep 17 00:00:00 2001 From: Scott Prager Date: Thu, 4 Sep 2014 12:37:16 -0400 Subject: memory: memcnt and strcnt. --- src/nvim/memory.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/nvim/memory.c b/src/nvim/memory.c index f959ea55e4..c221b13f8b 100644 --- a/src/nvim/memory.c +++ b/src/nvim/memory.c @@ -234,6 +234,43 @@ void memchrsub(void *data, char c, char x, size_t len) } } +/// Counts the number of occurrences of `c` in `str`. +/// +/// @warning Unsafe if `c == NUL`. +/// +/// @param str Pointer to the string to search. +/// @param c The byte to search for. +/// @returns the number of occurrences of `c` in `str`. +size_t strcnt(const char *str, char c, size_t len) + FUNC_ATTR_NONNULL_ALL FUNC_ATTR_PURE +{ + assert(c != 0); + size_t cnt = 0; + while ((str = strchr(str, c))) { + cnt++; + str++; // Skip the instance of c. + } + return cnt; +} + +/// Counts the number of occurrences of byte `c` in `data[len]`. +/// +/// @param data Pointer to the data to search. +/// @param c The byte to search for. +/// @param len The length of `data`. +/// @returns the number of occurrences of `c` in `data[len]`. +size_t memcnt(const void *data, char c, size_t len) + FUNC_ATTR_NONNULL_ALL FUNC_ATTR_PURE +{ + size_t cnt = 0; + const char *ptr = data, *end = ptr + len; + while ((ptr = memchr(ptr, c, (size_t)(end - ptr))) != NULL) { + cnt++; + ptr++; // Skip the instance of c. + } + return cnt; +} + /// The xstpcpy() function shall copy the string pointed to by src (including /// the terminating NUL character) into the array pointed to by dst. /// -- cgit From e18538f3ef44ffeea8e0ce4c0797c72d185e6b5c Mon Sep 17 00:00:00 2001 From: Scott Prager Date: Tue, 2 Sep 2014 22:51:08 -0400 Subject: vim-patch:7.4.242 Problem: getreg() does not distinguish between a NL used for a line break and a NL used for a NUL character. Solution: Add another argument to return a list. (ZyX) https://code.google.com/p/vim/source/detail?r=v7-4-242 --- src/nvim/eval.c | 41 +++++++++++++++++++++----------- src/nvim/ops.c | 68 ++++++++++++++++++++++++++++++++++++------------------ src/nvim/ops.h | 7 ++++++ src/nvim/version.c | 2 +- 4 files changed, 82 insertions(+), 36 deletions(-) diff --git a/src/nvim/eval.c b/src/nvim/eval.c index b4d1677520..3be3dd84d4 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -1835,7 +1835,7 @@ ex_let_one ( p = get_tv_string_chk(tv); if (p != NULL && op != NULL && *op == '.') { - s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE); + s = get_reg_contents(*arg == '@' ? '"' : *arg, kGRegExprSrc); if (s != NULL) { p = ptofree = concat_str(s, p); free(s); @@ -4076,7 +4076,7 @@ eval7 ( case '@': ++*arg; if (evaluate) { rettv->v_type = VAR_STRING; - rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE); + rettv->vval.v_string = get_reg_contents(**arg, kGRegExprSrc); } if (**arg != NUL) ++*arg; @@ -6466,7 +6466,7 @@ static struct fst { {"getpid", 0, 0, f_getpid}, {"getpos", 1, 1, f_getpos}, {"getqflist", 0, 0, f_getqflist}, - {"getreg", 0, 2, f_getreg}, + {"getreg", 0, 3, f_getreg}, {"getregtype", 0, 1, f_getregtype}, {"gettabvar", 2, 3, f_gettabvar}, {"gettabwinvar", 3, 4, f_gettabwinvar}, @@ -9526,30 +9526,45 @@ static void f_getqflist(typval_T *argvars, typval_T *rettv) (void)get_errorlist(wp, rettv->vval.v_list); } -/* - * "getreg()" function - */ +/// "getreg()" function static void f_getreg(typval_T *argvars, typval_T *rettv) { char_u *strregname; int regname; - int arg2 = FALSE; - int error = FALSE; + int arg2 = false; + bool return_list = false; + int error = false; if (argvars[0].v_type != VAR_UNKNOWN) { strregname = get_tv_string_chk(&argvars[0]); error = strregname == NULL; - if (argvars[1].v_type != VAR_UNKNOWN) + if (argvars[1].v_type != VAR_UNKNOWN) { arg2 = get_tv_number_chk(&argvars[1], &error); - } else + if (!error && argvars[2].v_type != VAR_UNKNOWN) { + return_list = get_tv_number_chk(&argvars[2], &error); + } + } + } else { strregname = vimvars[VV_REG].vv_str; + } + + if (error) { + return; + } + regname = (strregname == NULL ? '"' : *strregname); if (regname == 0) regname = '"'; - rettv->v_type = VAR_STRING; - rettv->vval.v_string = error ? NULL : - get_reg_contents(regname, TRUE, arg2); + if (return_list) { + rettv->v_type = VAR_LIST; + rettv->vval.v_list = + (list_T *)get_reg_contents(regname, + (arg2 ? kGRegExprSrc : 0) | kGRegList); + } else { + rettv->v_type = VAR_STRING; + rettv->vval.v_string = get_reg_contents(regname, arg2 ? kGRegExprSrc : 0); + } } /* diff --git a/src/nvim/ops.c b/src/nvim/ops.c index c20a017088..04a8a0017d 100644 --- a/src/nvim/ops.c +++ b/src/nvim/ops.c @@ -4654,30 +4654,42 @@ char_u get_reg_type(int regname, long *reglen) return MAUTO; } -/* - * Return the contents of a register as a single allocated string. - * Used for "@r" in expressions and for getreg(). - * Returns NULL for error. - */ -char_u * -get_reg_contents ( - int regname, - int allowexpr, /* allow "=" register */ - int expr_src /* get expression for "=" register */ -) +/// When `flags` has `kGRegList` return a list with text `s`. +/// Otherwise just return `s`. +static char_u *get_reg_wrap_one_line(char_u *s, int flags) +{ + if (!(flags & kGRegList)) { + return s; + } + list_T *list = list_alloc(); + list_append_string(list, NULL, -1); + list->lv_first->li_tv.vval.v_string = s; + return (char_u *)list; +} + +/// Gets the contents of a register. +/// @remark Used for `@r` in expressions and for `getreg()`. +/// +/// @param regname The register. +/// @param flags see @ref GRegFlags +/// +/// @returns The contents of the register as an allocated string. +/// @returns A linked list cast to a string when given @ref kGRegList. +/// @returns NULL for error. +// TODO(SplinterOfChaos): Maybe this should return a void*. +char_u *get_reg_contents(int regname, int flags) { long i; - char_u *retval; - int allocated; - /* Don't allow using an expression register inside an expression */ + // Don't allow using an expression register inside an expression. if (regname == '=') { - if (allowexpr) { - if (expr_src) - return get_expr_line_src(); - return get_expr_line(); + if (flags & kGRegNoExpr) { + return NULL; } - return NULL; + if (flags & kGRegExprSrc) { + return get_reg_wrap_one_line(get_expr_line_src(), flags); + } + return get_reg_wrap_one_line(get_expr_line(), flags); } if (regname == '@') /* "@@" is used for unnamed register */ @@ -4689,18 +4701,30 @@ get_reg_contents ( get_clipboard(regname); + char_u *retval; + int allocated; if (get_spec_reg(regname, &retval, &allocated, FALSE)) { if (retval == NULL) return NULL; - if (!allocated) - retval = vim_strsave(retval); - return retval; + if (allocated) { + return get_reg_wrap_one_line(retval, flags); + } + return get_reg_wrap_one_line(vim_strsave(retval), flags); } get_yank_register(regname, FALSE); if (y_current->y_array == NULL) return NULL; + if (flags & kGRegList) { + list_T *list = list_alloc(); + for (int i = 0; i < y_current->y_size; ++i) { + list_append_string(list, y_current->y_array[i], -1); + } + + return (char_u *) list; + } + /* * Compute length of resulting string. */ diff --git a/src/nvim/ops.h b/src/nvim/ops.h index ca70684ab8..3251042c8f 100644 --- a/src/nvim/ops.h +++ b/src/nvim/ops.h @@ -47,6 +47,13 @@ typedef int (*Indenter)(void); #define OP_FORMAT2 26 /* "gw" format operator, keeps cursor pos */ #define OP_FUNCTION 27 /* "g@" call 'operatorfunc' */ +/// Flags for get_reg_contents(). +enum GRegFlags { + kGRegNoExpr = 1, ///< Do not allow expression register. + kGRegExprSrc = 2, ///< Return expression itself for "=" register. + kGRegList = 4 ///< Return list. +}; + #ifdef INCLUDE_GENERATED_DECLARATIONS # include "ops.h.generated.h" #endif diff --git a/src/nvim/version.c b/src/nvim/version.c index f73e5c8cae..5a0d8d9d74 100644 --- a/src/nvim/version.c +++ b/src/nvim/version.c @@ -423,7 +423,7 @@ static int included_patches[] = { 245, //244, //243, - //242, + 242, 241, 240, 239, -- cgit From 2f8cc3b9d5f3dd56949d4b9c876fdff1e14e46cf Mon Sep 17 00:00:00 2001 From: Scott Prager Date: Tue, 2 Sep 2014 23:18:15 -0400 Subject: Return void * from get_reg_contents. --- src/nvim/eval.c | 3 +-- src/nvim/ops.c | 13 +++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 3be3dd84d4..815df19476 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -9559,8 +9559,7 @@ static void f_getreg(typval_T *argvars, typval_T *rettv) if (return_list) { rettv->v_type = VAR_LIST; rettv->vval.v_list = - (list_T *)get_reg_contents(regname, - (arg2 ? kGRegExprSrc : 0) | kGRegList); + get_reg_contents(regname, (arg2 ? kGRegExprSrc : 0) | kGRegList); } else { rettv->v_type = VAR_STRING; rettv->vval.v_string = get_reg_contents(regname, arg2 ? kGRegExprSrc : 0); diff --git a/src/nvim/ops.c b/src/nvim/ops.c index 04a8a0017d..82036b1fec 100644 --- a/src/nvim/ops.c +++ b/src/nvim/ops.c @@ -4656,7 +4656,9 @@ char_u get_reg_type(int regname, long *reglen) /// When `flags` has `kGRegList` return a list with text `s`. /// Otherwise just return `s`. -static char_u *get_reg_wrap_one_line(char_u *s, int flags) +/// +/// Returns a void * for use in get_reg_contents(). +static void *get_reg_wrap_one_line(char_u *s, int flags) { if (!(flags & kGRegList)) { return s; @@ -4664,7 +4666,7 @@ static char_u *get_reg_wrap_one_line(char_u *s, int flags) list_T *list = list_alloc(); list_append_string(list, NULL, -1); list->lv_first->li_tv.vval.v_string = s; - return (char_u *)list; + return list; } /// Gets the contents of a register. @@ -4674,10 +4676,9 @@ static char_u *get_reg_wrap_one_line(char_u *s, int flags) /// @param flags see @ref GRegFlags /// /// @returns The contents of the register as an allocated string. -/// @returns A linked list cast to a string when given @ref kGRegList. +/// @returns A linked list when `flags` contains @ref kGRegList. /// @returns NULL for error. -// TODO(SplinterOfChaos): Maybe this should return a void*. -char_u *get_reg_contents(int regname, int flags) +void *get_reg_contents(int regname, int flags) { long i; @@ -4722,7 +4723,7 @@ char_u *get_reg_contents(int regname, int flags) list_append_string(list, y_current->y_array[i], -1); } - return (char_u *) list; + return list; } /* -- cgit From 5fdca47962fac359e616ef8c6dee3a793cb08168 Mon Sep 17 00:00:00 2001 From: Scott Prager Date: Wed, 3 Sep 2014 01:02:47 -0400 Subject: vim-patch:7.4.243 Problem: Cannot use setreg() to add text that includes a NUL. Solution: Make setreg() accept a list. https://code.google.com/p/vim/source/detail?r=v7-4-243 --- src/nvim/eval.c | 33 ++++-- src/nvim/ops.c | 236 ++++++++++++++++++++++++++---------------- src/nvim/testdir/test_eval.in | 145 ++++++++++++++++++++++++++ src/nvim/testdir/test_eval.ok | Bin 432 -> 10307 bytes src/nvim/version.c | 2 +- 5 files changed, 321 insertions(+), 95 deletions(-) diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 815df19476..2e2871abc5 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -13328,7 +13328,6 @@ static void f_setreg(typval_T *argvars, typval_T *rettv) int regname; char_u *strregname; char_u *stropt; - char_u *strval; int append; char_u yank_type; long block_len; @@ -13345,8 +13344,6 @@ static void f_setreg(typval_T *argvars, typval_T *rettv) regname = *strregname; if (regname == 0 || regname == '@') regname = '"'; - else if (regname == '=') - return; if (argvars[2].v_type != VAR_UNKNOWN) { stropt = get_tv_string_chk(&argvars[2]); @@ -13374,10 +13371,32 @@ static void f_setreg(typval_T *argvars, typval_T *rettv) } } - strval = get_tv_string_chk(&argvars[1]); - if (strval != NULL) - write_reg_contents_ex(regname, strval, -1, - append, yank_type, block_len); + if (argvars[1].v_type == VAR_LIST) { + int len = argvars[1].vval.v_list->lv_len; + char_u **lstval = xmalloc(sizeof(char_u *) * (len + 1)); + char_u **curval = lstval; + + for (listitem_T *li = argvars[1].vval.v_list->lv_first; + li != NULL; + li = li->li_next) { + char_u *strval = get_tv_string_chk(&li->li_tv); + if (strval == NULL) { + free(lstval); + return; + } + *curval++ = strval; + } + *curval++ = NULL; + + write_reg_contents_lst(regname, lstval, -1, append, yank_type, block_len); + free(lstval); + } else { + char_u *strval = get_tv_string_chk(&argvars[1]); + if (strval == NULL) { + return; + } + write_reg_contents_ex(regname, strval, -1, append, yank_type, block_len); + } rettv->vval.v_number = 0; } diff --git a/src/nvim/ops.c b/src/nvim/ops.c index 82036b1fec..1fa8d15303 100644 --- a/src/nvim/ops.c +++ b/src/nvim/ops.c @@ -4762,17 +4762,77 @@ void *get_reg_contents(int regname, int flags) return retval; } +static bool init_write_reg(int name, struct yankreg **old_y_previous, + struct yankreg **old_y_current, int must_append) +{ + if (!valid_yank_reg(name, true)) { // check for valid reg name + emsg_invreg(name); + return false; + } + + // Don't want to change the current (unnamed) register. + *old_y_previous = y_previous; + *old_y_current = y_current; + + get_yank_register(name, true); + if (!y_append && !must_append) { + free_yank_all(); + } + return true; +} + +static void finish_write_reg(int name, struct yankreg *old_y_previous, + struct yankreg *old_y_current) +{ + // Send text of clipboard register to the clipboard. + set_clipboard(name); + + // ':let @" = "val"' should change the meaning of the "" register + if (name != '"') { + y_previous = old_y_previous; + } + y_current = old_y_current; +} + /// write_reg_contents - store `str` in register `name` /// /// @see write_reg_contents_ex -void write_reg_contents(int name, - const char_u *str, - ssize_t len, +void write_reg_contents(int name, const char_u *str, ssize_t len, int must_append) { write_reg_contents_ex(name, str, len, must_append, MAUTO, 0L); } +void write_reg_contents_lst(int name, char_u **strings, int maxlen, + int must_append, int yank_type, long block_len) +{ + if (name == '/' || name == '=') { + char_u *s = strings[0]; + if (strings[0] == NULL) { + s = (char_u *)""; + } else if (strings[1] != NULL) { + EMSG(_("E883: search pattern and expression register may not " + "contain two or more lines")); + return; + } + write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len); + return; + } + + // black hole: nothing to do + if (name == '_') { + return; + } + + struct yankreg *old_y_previous, *old_y_current; + if (!init_write_reg(name, &old_y_previous, &old_y_current, must_append)) { + return; + } + + str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, true); + finish_write_reg(name, old_y_previous, old_y_current); +} + /// write_reg_contents_ex - store `str` in register `name` /// /// If `str` ends in '\n' or '\r', use linewise, otherwise use @@ -4799,8 +4859,6 @@ void write_reg_contents_ex(int name, int yank_type, long block_len) { - struct yankreg *old_y_previous, *old_y_current; - if (len < 0) { len = (ssize_t) STRLEN(str); } @@ -4834,29 +4892,16 @@ void write_reg_contents_ex(int name, return; } - if (!valid_yank_reg(name, TRUE)) { /* check for valid reg name */ - emsg_invreg(name); + if (name == '_') { // black hole: nothing to do return; } - if (name == '_') /* black hole: nothing to do */ - return; - - /* Don't want to change the current (unnamed) register */ - old_y_previous = y_previous; - old_y_current = y_current; - - get_yank_register(name, TRUE); - if (!y_append && !must_append) - free_yank_all(); - str_to_reg(y_current, yank_type, str, len, block_len); - set_clipboard(name); - - - /* ':let @" = "val"' should change the meaning of the "" register */ - if (name != '"') - y_previous = old_y_previous; - y_current = old_y_current; + struct yankreg *old_y_previous, *old_y_current; + if (!init_write_reg(name, &old_y_previous, &old_y_current, must_append)) { + return; + } + str_to_reg(y_current, yank_type, str, len, block_len, false); + finish_write_reg(name, old_y_previous, old_y_current); } /// str_to_reg - Put a string into a register. @@ -4868,97 +4913,114 @@ void write_reg_contents_ex(int name, /// @param str string to put in register /// @param len length of the string /// @param blocklen width of visual block -static void str_to_reg(struct yankreg *y_ptr, - int yank_type, - const char_u *str, - long len, - long blocklen) +/// @param str_list True if str is `char_u **`. +static void str_to_reg(struct yankreg *y_ptr, int yank_type, const char_u *str, + long len, long blocklen, bool str_list) { int type; /* MCHAR, MLINE or MBLOCK */ int lnum; long start; long i; int extra; - size_t newlines; /* number of lines added */ int extraline = 0; /* extra line at the end */ int append = FALSE; /* append to last line in register */ - char_u *s; - char_u **pp; long maxlen; if (y_ptr->y_array == NULL) /* NULL means empty register */ y_ptr->y_size = 0; - if (yank_type == MAUTO) - type = ((len > 0 && (str[len - 1] == NL || str[len - 1] == CAR)) + if (yank_type == MAUTO) { + type = ((str_list || + (len > 0 && (str[len - 1] == NL || str[len - 1] == CAR))) ? MLINE : MCHAR); - else + } else { type = yank_type; - - /* - * Count the number of lines within the string - */ - newlines = 0; - for (i = 0; i < len; i++) - if (str[i] == '\n') - ++newlines; - if (type == MCHAR || len == 0 || str[len - 1] != '\n') { - extraline = 1; - ++newlines; /* count extra newline at the end */ } - if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR) { - append = TRUE; - --newlines; /* uncount newline when appending first line */ + + // Count the number of lines within the string + size_t newlines = 0; + if (str_list) { + for (char_u **ss = (char_u **) str; *ss != NULL; ++ss) { + newlines++; + } + } else { + for (i = 0; i < len; i++) { + if (str[i] == '\n') { + ++newlines; + } + } + if (type == MCHAR || len == 0 || str[len - 1] != '\n') { + extraline = 1; + ++newlines; // count extra newline at the end + } + if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR) { + append = true; + --newlines; // uncount newline when appending first line + } } - /* - * Allocate an array to hold the pointers to the new register lines. - * If the register was not empty, move the existing lines to the new array. - */ - pp = xcalloc((y_ptr->y_size + newlines), sizeof(char_u *)); - for (lnum = 0; lnum < y_ptr->y_size; ++lnum) + // Allocate an array to hold the pointers to the new register lines. + // If the register was not empty, move the existing lines to the new array. + char_u **pp = xcalloc(y_ptr->y_size + newlines, sizeof(char_u *)); + for (lnum = 0; lnum < y_ptr->y_size; ++lnum) { pp[lnum] = y_ptr->y_array[lnum]; + } free(y_ptr->y_array); y_ptr->y_array = pp; maxlen = 0; - /* - * Find the end of each line and save it into the array. - */ - for (start = 0; start < len + extraline; start += i + 1) { - // Let i represent the length of one line. - const char_u *p = str + start; - i = (char_u *)xmemscan(p, '\n', len - start) - p; - if (i > maxlen) - maxlen = i; - if (append) { - --lnum; - extra = (int)STRLEN(y_ptr->y_array[lnum]); - } else - extra = 0; - s = xmalloc(i + extra + 1); - if (extra) - memmove(s, y_ptr->y_array[lnum], (size_t)extra); - if (append) - free(y_ptr->y_array[lnum]); - if (i) - memmove(s + extra, str + start, (size_t)i); - extra += i; - s[extra] = NUL; - y_ptr->y_array[lnum++] = s; - while (--extra >= 0) { - if (*s == NUL) - *s = '\n'; /* replace NUL with newline */ - ++s; + // Find the end of each line and save it into the array. + if (str_list) { + for (char_u **ss = (char_u **) str; *ss != NULL; ++ss, ++lnum) { + int i = STRLEN(*ss); + pp[lnum] = vim_strnsave(*ss, i); + if (i > maxlen) { + maxlen = i; + } + } + } else { + for (start = 0; start < len + extraline; start += i + 1) { + // Let i represent the length of one line. + const char_u *p = str + start; + i = (char_u *)xmemscan(p, '\n', len - start) - p; + if (i > maxlen) { + maxlen = i; + } + if (append) { + --lnum; + extra = (int)STRLEN(y_ptr->y_array[lnum]); + } else { + extra = 0; + } + char_u *s = xmalloc(i + extra + 1); + if (extra) { + memmove(s, y_ptr->y_array[lnum], (size_t)extra); + } + if (append) { + free(y_ptr->y_array[lnum]); + } + if (i) { + memmove(s + extra, str + start, (size_t)i); + } + extra += i; + s[extra] = NUL; + y_ptr->y_array[lnum++] = s; + while (--extra >= 0) { + if (*s == NUL) { + *s = '\n'; // replace NUL with newline + } + ++s; + } + append = FALSE; // only first line is appended } - append = FALSE; /* only first line is appended */ } y_ptr->y_type = type; y_ptr->y_size = lnum; - if (type == MBLOCK) + if (type == MBLOCK) { y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen); - else + } else { y_ptr->y_width = 0; + } } void clear_oparg(oparg_T *oap) diff --git a/src/nvim/testdir/test_eval.in b/src/nvim/testdir/test_eval.in index a29fefc3b6..176e62b9cc 100644 --- a/src/nvim/testdir/test_eval.in +++ b/src/nvim/testdir/test_eval.in @@ -1,7 +1,152 @@ +Test for various eval features. vim: set ft=vim : + +Note: system clipboard support is not tested. I do not think anybody will thank +me for messing with clipboard. + STARTTEST :so small.vim :set encoding=latin1 :set noswapfile +:lang C +:fun AppendRegContents(reg) + call append('$', printf('%s: type %s; value: %s (%s), expr: %s (%s)', a:reg, getregtype(a:reg), getreg(a:reg), string(getreg(a:reg, 0, 1)), getreg(a:reg, 1), string(getreg(a:reg, 1, 1)))) +endfun +:command -nargs=? AR :call AppendRegContents() +:fun SetReg(...) + call call('setreg', a:000) + call append('$', printf('{{{2 setreg(%s)', string(a:000)[1:-2])) + call AppendRegContents(a:1) + if a:1 isnot# '=' + execute "silent normal! Go==\n==\e\"".a:1."P" + endif +endfun +:fun ErrExe(str) + call append('$', 'Executing '.a:str) + try + execute a:str + catch + $put =v:exception + endtry +endfun +:fun Test() +$put ='{{{1 let tests' +let @" = 'abc' +AR " +let @" = "abc\n" +AR " +let @" = "abc\" +AR " +let @= = '"abc"' +AR = + +$put ='{{{1 Basic setreg tests' +call SetReg('a', 'abcA', 'c') +call SetReg('b', 'abcB', 'v') +call SetReg('c', 'abcC', 'l') +call SetReg('d', 'abcD', 'V') +call SetReg('e', 'abcE', 'b') +call SetReg('f', 'abcF', "\") +call SetReg('g', 'abcG', 'b10') +call SetReg('h', 'abcH', "\10") +call SetReg('I', 'abcI') + +$put ='{{{1 Appending single lines with setreg()' +call SetReg('A', 'abcAc', 'c') +call SetReg('A', 'abcAl', 'l') +call SetReg('A', 'abcAc2','c') +call SetReg('b', 'abcBc', 'ca') +call SetReg('b', 'abcBb', 'ba') +call SetReg('b', 'abcBc2','ca') +call SetReg('b', 'abcBb2','b50a') + +call SetReg('C', 'abcCl', 'l') +call SetReg('C', 'abcCc', 'c') +call SetReg('D', 'abcDb', 'b') + +call SetReg('E', 'abcEb', 'b') +call SetReg('E', 'abcEl', 'l') +call SetReg('F', 'abcFc', 'c') + +$put ='{{{1 Appending NL with setreg()' +call setreg('a', 'abcA2', 'c') +call setreg('b', 'abcB2', 'v') +call setreg('c', 'abcC2', 'l') +call setreg('d', 'abcD2', 'V') +call setreg('e', 'abcE2', 'b') +call setreg('f', 'abcF2', "\") +call setreg('g', 'abcG2', 'b10') +call setreg('h', 'abcH2', "\10") +call setreg('I', 'abcI2') + +call SetReg('A', "\n") +call SetReg('B', "\n", 'c') +call SetReg('C', "\n") +call SetReg('D', "\n", 'l') +call SetReg('E', "\n") +call SetReg('F', "\n", 'b') + +$put ='{{{1 Setting lists with setreg()' +call SetReg('a', ['abcA3'], 'c') +call SetReg('b', ['abcB3'], 'l') +call SetReg('c', ['abcC3'], 'b') +call SetReg('d', ['abcD3']) + +$put ='{{{1 Appending lists with setreg()' +call SetReg('A', ['abcA3c'], 'c') +call SetReg('b', ['abcB3l'], 'la') +call SetReg('C', ['abcC3b'], 'lb') +call SetReg('D', ['abcD32']) + +call SetReg('A', ['abcA32']) +call SetReg('B', ['abcB3c'], 'c') +call SetReg('C', ['abcC3l'], 'l') +call SetReg('D', ['abcD3b'], 'b') + +$put ='{{{1 Appending lists with NL with setreg()' +call SetReg('A', ["\n", 'abcA3l2'], 'l') +call SetReg('B', ["\n", 'abcB3c2'], 'c') +call SetReg('C', ["\n", 'abcC3b2'], 'b') +call SetReg('D', ["\n", 'abcD3b50'],'b50') + +$put ='{{{1 Setting lists with NLs with setreg()' +call SetReg('a', ['abcA4-0', "\n", "abcA4-2\n", "\nabcA4-3", "abcA4-4\nabcA4-4-2"]) +call SetReg('b', ['abcB4c-0', "\n", "abcB4c-2\n", "\nabcB4c-3", "abcB4c-4\nabcB4c-4-2"], 'c') +call SetReg('c', ['abcC4l-0', "\n", "abcC4l-2\n", "\nabcC4l-3", "abcC4l-4\nabcC4l-4-2"], 'l') +call SetReg('d', ['abcD4b-0', "\n", "abcD4b-2\n", "\nabcD4b-3", "abcD4b-4\nabcD4b-4-2"], 'b') +call SetReg('e', ['abcE4b10-0', "\n", "abcE4b10-2\n", "\nabcE4b10-3", "abcE4b10-4\nabcE4b10-4-2"], 'b10') + +$put ='{{{1 Search and expressions' +call SetReg('/', ['abc/']) +call SetReg('/', ["abc/\n"]) +call SetReg('=', ['"abc/"']) +call SetReg('=', ["\"abc/\n\""]) + +$put ='{{{1 Errors' +call ErrExe('call setreg()') +call ErrExe('call setreg(1)') +call ErrExe('call setreg(1, 2, 3, 4)') +call ErrExe('call setreg([], 2)') +call ErrExe('call setreg(1, {})') +call ErrExe('call setreg(1, 2, [])') +call ErrExe('call setreg("/", [1, 2])') +call ErrExe('call setreg("=", [1, 2])') +call ErrExe('call setreg(1, ["", "", [], ""])') +endfun +:" +:call Test() +:" +:delfunction SetReg +:delfunction AppendRegContents +:delfunction ErrExe +:delfunction Test +:delcommand AR +:call garbagecollect(1) +:" +:/^start:/+1,$wq! test.out +:" vim: et ts=4 isk-=\: fmr=???,??? +:call getchar() +:e test.out +:%d :" function name not starting with a capital :try diff --git a/src/nvim/testdir/test_eval.ok b/src/nvim/testdir/test_eval.ok index 63b9156442..7fe5f1bd1b 100644 Binary files a/src/nvim/testdir/test_eval.ok and b/src/nvim/testdir/test_eval.ok differ diff --git a/src/nvim/version.c b/src/nvim/version.c index 5a0d8d9d74..722a725df2 100644 --- a/src/nvim/version.c +++ b/src/nvim/version.c @@ -422,7 +422,7 @@ static int included_patches[] = { //246, 245, //244, - //243, + 243, 242, 241, 240, -- cgit From 171445ef3441a4dbc9958871760e42460ba7404c Mon Sep 17 00:00:00 2001 From: Scott Prager Date: Wed, 3 Sep 2014 22:22:50 -0400 Subject: Refactor str_to_reg(). - Update the doxygen comments. - Use more descriptive types. - Localize variables. - Find the '\n' with memchr instead of a for loop. - Remove `if (size)` checks before memmove since memmove(dst,src,0) is a noop. - Use memcpy instead since the pointers don't alias. - Use xmemdupz instead of vim_strnsave. - xrealloc instead of xmalloc/memcpy. - Use memcnt/xmemscan/memchrsub. --- src/nvim/ops.c | 113 ++++++++++++++++++++++++--------------------------------- 1 file changed, 48 insertions(+), 65 deletions(-) diff --git a/src/nvim/ops.c b/src/nvim/ops.c index 1fa8d15303..32b37b6458 100644 --- a/src/nvim/ops.c +++ b/src/nvim/ops.c @@ -4910,45 +4910,36 @@ void write_reg_contents_ex(int name, /// /// @param y_ptr pointer to yank register /// @param yank_type MCHAR, MLINE, MBLOCK or MAUTO -/// @param str string to put in register -/// @param len length of the string -/// @param blocklen width of visual block +/// @param str string or list of strings to put in register +/// @param len length of the string (Ignored when str_list=true.) +/// @param blocklen width of visual block, or -1 for "I don't know." /// @param str_list True if str is `char_u **`. static void str_to_reg(struct yankreg *y_ptr, int yank_type, const char_u *str, - long len, long blocklen, bool str_list) + size_t len, colnr_T blocklen, bool str_list) + FUNC_ATTR_NONNULL_ALL { - int type; /* MCHAR, MLINE or MBLOCK */ - int lnum; - long start; - long i; - int extra; - int extraline = 0; /* extra line at the end */ - int append = FALSE; /* append to last line in register */ - long maxlen; - - if (y_ptr->y_array == NULL) /* NULL means empty register */ + if (y_ptr->y_array == NULL) { // NULL means empty register y_ptr->y_size = 0; + } + int type = yank_type; // MCHAR, MLINE or MBLOCK if (yank_type == MAUTO) { type = ((str_list || (len > 0 && (str[len - 1] == NL || str[len - 1] == CAR))) ? MLINE : MCHAR); - } else { - type = yank_type; } - // Count the number of lines within the string size_t newlines = 0; + bool extraline = false; // extra line at the end + bool append = false; // append to last line in register + + // Count the number of lines within the string if (str_list) { for (char_u **ss = (char_u **) str; *ss != NULL; ++ss) { newlines++; } } else { - for (i = 0; i < len; i++) { - if (str[i] == '\n') { - ++newlines; - } - } + newlines = memcnt(str, '\n', len); if (type == MCHAR || len == 0 || str[len - 1] != '\n') { extraline = 1; ++newlines; // count extra newline at the end @@ -4959,65 +4950,57 @@ static void str_to_reg(struct yankreg *y_ptr, int yank_type, const char_u *str, } } - // Allocate an array to hold the pointers to the new register lines. - // If the register was not empty, move the existing lines to the new array. - char_u **pp = xcalloc(y_ptr->y_size + newlines, sizeof(char_u *)); - for (lnum = 0; lnum < y_ptr->y_size; ++lnum) { - pp[lnum] = y_ptr->y_array[lnum]; - } - free(y_ptr->y_array); + + // Grow the register array to hold the pointers to the new lines. + char_u **pp = xrealloc(y_ptr->y_array, + (y_ptr->y_size + newlines) * sizeof(char_u *)); y_ptr->y_array = pp; - maxlen = 0; + + linenr_T lnum = y_ptr->y_size; // The current line number. + + // If called with `blocklen < 0`, we have to update the yank reg's width. + size_t maxlen = 0; // Find the end of each line and save it into the array. if (str_list) { for (char_u **ss = (char_u **) str; *ss != NULL; ++ss, ++lnum) { - int i = STRLEN(*ss); - pp[lnum] = vim_strnsave(*ss, i); - if (i > maxlen) { - maxlen = i; + size_t ss_len = STRLEN(*ss); + pp[lnum] = xmemdupz(*ss, ss_len); + if (ss_len > maxlen) { + maxlen = ss_len; } } } else { - for (start = 0; start < len + extraline; start += i + 1) { - // Let i represent the length of one line. - const char_u *p = str + start; - i = (char_u *)xmemscan(p, '\n', len - start) - p; - if (i > maxlen) { - maxlen = i; - } - if (append) { - --lnum; - extra = (int)STRLEN(y_ptr->y_array[lnum]); - } else { - extra = 0; - } - char_u *s = xmalloc(i + extra + 1); - if (extra) { - memmove(s, y_ptr->y_array[lnum], (size_t)extra); + size_t line_len; + for (const char_u *start = str, *end = str + len; + start < end + extraline; + start += line_len + 1, lnum++) { + line_len = (const char_u *) xmemscan(start, '\n', end - start) - start; + if (line_len > maxlen) { + maxlen = line_len; } + + // When appending, copy the previous line and free it after. + size_t extra = append ? STRLEN(pp[--lnum]) : 0; + char_u *s = xmallocz(line_len + extra); + memcpy(s, pp[lnum], extra); + memcpy(s + extra, start, line_len); + ssize_t s_len = extra + line_len; + if (append) { - free(y_ptr->y_array[lnum]); - } - if (i) { - memmove(s + extra, str + start, (size_t)i); + free(pp[lnum]); + append = false; // only first line is appended } - extra += i; - s[extra] = NUL; - y_ptr->y_array[lnum++] = s; - while (--extra >= 0) { - if (*s == NUL) { - *s = '\n'; // replace NUL with newline - } - ++s; - } - append = FALSE; // only first line is appended + pp[lnum] = s; + + // Convert NULs to '\n' to prevent truncation. + memchrsub(pp[lnum], NUL, '\n', s_len); } } y_ptr->y_type = type; y_ptr->y_size = lnum; if (type == MBLOCK) { - y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen); + y_ptr->y_width = (blocklen == -1 ? (colnr_T) maxlen - 1 : blocklen); } else { y_ptr->y_width = 0; } -- cgit From 3d93e47d9af5722c0e1be1d073090347d6ae28e3 Mon Sep 17 00:00:00 2001 From: Scott Prager Date: Fri, 5 Sep 2014 13:15:48 -0400 Subject: vim-patch:7.4.249 Problem: Using setreg() with a list of numbers does not work. Solution: Use a separate buffer for numbers. (ZyX) https://code.google.com/p/vim/source/detail?r=v7-4-249 --- src/nvim/eval.c | 23 +++++++++++++++++++---- src/nvim/testdir/test_eval.in | 6 ++++-- src/nvim/testdir/test_eval.ok | Bin 10307 -> 10578 bytes src/nvim/version.c | 2 +- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 2e2871abc5..86ac112212 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -13373,22 +13373,36 @@ static void f_setreg(typval_T *argvars, typval_T *rettv) if (argvars[1].v_type == VAR_LIST) { int len = argvars[1].vval.v_list->lv_len; - char_u **lstval = xmalloc(sizeof(char_u *) * (len + 1)); + // First half: use for pointers to result lines; second half: use for + // pointers to allocated copies. + char_u **lstval = xmalloc(sizeof(char_u *) * ((len + 1) * 2)); char_u **curval = lstval; + char_u **allocval = lstval + len + 2; + char_u **curallocval = allocval; + char_u buf[NUMBUFLEN]; for (listitem_T *li = argvars[1].vval.v_list->lv_first; li != NULL; li = li->li_next) { - char_u *strval = get_tv_string_chk(&li->li_tv); + char_u *strval = get_tv_string_buf_chk(&li->li_tv, buf); if (strval == NULL) { - free(lstval); - return; + goto free_lstval; + } + if (strval == buf) { + // Need to make a copy, + // next get_tv_string_buf_chk() will overwrite the string. + strval = vim_strsave(buf); + *curallocval++ = strval; } *curval++ = strval; } *curval++ = NULL; write_reg_contents_lst(regname, lstval, -1, append, yank_type, block_len); + +free_lstval: + while (curallocval > allocval) + free(*--curallocval); free(lstval); } else { char_u *strval = get_tv_string_chk(&argvars[1]); @@ -16334,6 +16348,7 @@ static char_u *get_tv_string_buf(typval_T *varp, char_u *buf) return res != NULL ? res : (char_u *)""; } +/// Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE! char_u *get_tv_string_chk(typval_T *varp) { static char_u mybuf[NUMBUFLEN]; diff --git a/src/nvim/testdir/test_eval.in b/src/nvim/testdir/test_eval.in index 176e62b9cc..95a59ee42a 100644 --- a/src/nvim/testdir/test_eval.in +++ b/src/nvim/testdir/test_eval.in @@ -90,6 +90,8 @@ call SetReg('a', ['abcA3'], 'c') call SetReg('b', ['abcB3'], 'l') call SetReg('c', ['abcC3'], 'b') call SetReg('d', ['abcD3']) +call SetReg('e', [1, 2, 'abc', 3]) +call SetReg('f', [1, 2, 3]) $put ='{{{1 Appending lists with setreg()' call SetReg('A', ['abcA3c'], 'c') @@ -128,8 +130,8 @@ call ErrExe('call setreg(1, 2, 3, 4)') call ErrExe('call setreg([], 2)') call ErrExe('call setreg(1, {})') call ErrExe('call setreg(1, 2, [])') -call ErrExe('call setreg("/", [1, 2])') -call ErrExe('call setreg("=", [1, 2])') +call ErrExe('call setreg("/", ["1", "2"])') +call ErrExe('call setreg("=", ["1", "2"])') call ErrExe('call setreg(1, ["", "", [], ""])') endfun :" diff --git a/src/nvim/testdir/test_eval.ok b/src/nvim/testdir/test_eval.ok index 7fe5f1bd1b..061e0cfd2f 100644 Binary files a/src/nvim/testdir/test_eval.ok and b/src/nvim/testdir/test_eval.ok differ diff --git a/src/nvim/version.c b/src/nvim/version.c index 722a725df2..6f37982f4d 100644 --- a/src/nvim/version.c +++ b/src/nvim/version.c @@ -416,7 +416,7 @@ static int included_patches[] = { //252 NA 251, //250 NA - //249, + 249, 248, 247, //246, -- cgit From a192865f908f4b9b66a73d41053cc3ee2d9ad22f Mon Sep 17 00:00:00 2001 From: Scott Prager Date: Fri, 5 Sep 2014 13:22:35 -0400 Subject: Attribute/constify get_tv_string(_buf(_chk)). --- src/nvim/eval.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 86ac112212..c58e6ee996 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -16334,14 +16334,16 @@ static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf) * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return * NULL on error. */ -static char_u *get_tv_string(typval_T *varp) +static char_u *get_tv_string(const typval_T *varp) + FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_RET { static char_u mybuf[NUMBUFLEN]; return get_tv_string_buf(varp, mybuf); } -static char_u *get_tv_string_buf(typval_T *varp, char_u *buf) +static char_u *get_tv_string_buf(const typval_T *varp, char_u *buf) + FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_RET { char_u *res = get_tv_string_buf_chk(varp, buf); @@ -16349,14 +16351,16 @@ static char_u *get_tv_string_buf(typval_T *varp, char_u *buf) } /// Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE! -char_u *get_tv_string_chk(typval_T *varp) +char_u *get_tv_string_chk(const typval_T *varp) + FUNC_ATTR_NONNULL_ALL { static char_u mybuf[NUMBUFLEN]; return get_tv_string_buf_chk(varp, mybuf); } -static char_u *get_tv_string_buf_chk(typval_T *varp, char_u *buf) +static char_u *get_tv_string_buf_chk(const typval_T *varp, char_u *buf) + FUNC_ATTR_NONNULL_ALL { switch (varp->v_type) { case VAR_NUMBER: -- cgit