aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorZyX <kp-pav@yandex.ru>2017-12-31 00:50:31 +0300
committerZyX <kp-pav@yandex.ru>2017-12-31 01:00:13 +0300
commitc55cf5f4c181856d7ebb6697e8558d71279e7adb (patch)
treef1aa827a9fb85a6f4d48beea6f2939f6567b4bba /src
parentb6ee90a2433276175462b81106378009b4893e04 (diff)
downloadrneovim-c55cf5f4c181856d7ebb6697e8558d71279e7adb.tar.gz
rneovim-c55cf5f4c181856d7ebb6697e8558d71279e7adb.tar.bz2
rneovim-c55cf5f4c181856d7ebb6697e8558d71279e7adb.zip
eval,lua/converter: Fix problems spotted in review
Diffstat (limited to 'src')
-rw-r--r--src/nvim/eval.c60
-rw-r--r--src/nvim/lua/converter.c2
2 files changed, 33 insertions, 29 deletions
diff --git a/src/nvim/eval.c b/src/nvim/eval.c
index 370d4f0c0b..d2432e864f 100644
--- a/src/nvim/eval.c
+++ b/src/nvim/eval.c
@@ -1515,7 +1515,6 @@ ex_let_vars (
)
{
char_u *arg = arg_start;
- int i;
typval_T ltv;
if (*arg != '[') {
@@ -1534,17 +1533,17 @@ ex_let_vars (
}
list_T *const l = tv->vval.v_list;
- i = tv_list_len(l);
- if (semicolon == 0 && var_count < i) {
+ const int len = tv_list_len(l);
+ if (semicolon == 0 && var_count < len) {
EMSG(_("E687: Less targets than List items"));
return FAIL;
}
- if (var_count - semicolon > i) {
+ if (var_count - semicolon > len) {
EMSG(_("E688: More targets than List items"));
return FAIL;
}
- // lt may actually be NULL, but it should fail with E688 or even earlier if
- // you try to do ":let [] = v:_null_list".
+ // List l may actually be NULL, but it should fail with E688 or even earlier
+ // if you try to do ":let [] = v:_null_list".
assert(l != NULL);
listitem_T *item = tv_list_first(l);
@@ -12220,10 +12219,11 @@ static void find_some_match(typval_T *const argvars, typval_T *const rettv,
// matchlist(): return empty list when there are no matches.
case kSomeMatchList: {
tv_list_alloc_ret(rettv);
- FALLTHROUGH;
+ break;
}
// matchstrpos(): return ["", -1, -1, -1]
case kSomeMatchStrPos: {
+ tv_list_alloc_ret(rettv);
tv_list_append_string(rettv->vval.v_list, "", 0);
tv_list_append_number(rettv->vval.v_list, -1);
tv_list_append_number(rettv->vval.v_list, -1);
@@ -12385,7 +12385,7 @@ static void find_some_match(typval_T *const argvars, typval_T *const rettv,
if (l != NULL) {
rettv->vval.v_number = idx;
} else {
- if (type == kSomeMatchEnd) {
+ if (type == kSomeMatch) {
rettv->vval.v_number =
(varnumber_T)(regmatch.startp[0] - str);
} else {
@@ -12394,6 +12394,7 @@ static void find_some_match(typval_T *const argvars, typval_T *const rettv,
}
rettv->vval.v_number += (varnumber_T)(str - expr);
}
+ break;
}
}
}
@@ -13020,7 +13021,6 @@ static void f_readfile(typval_T *argvars, typval_T *rettv, FunPtr fptr)
long prevlen = 0; /* length of data in prev */
long prevsize = 0; /* size of prev buffer */
long maxline = MAXLNUM;
- long cnt = 0;
char_u *p; /* position in buf */
char_u *start; /* start of current line */
@@ -13034,6 +13034,7 @@ static void f_readfile(typval_T *argvars, typval_T *rettv, FunPtr fptr)
}
tv_list_alloc_ret(rettv);
+ list_T *const l = rettv->vval.v_list;
// Always open the file in binary mode, library functions have a mind of
// their own about CR-LF conversion.
@@ -13043,7 +13044,7 @@ static void f_readfile(typval_T *argvars, typval_T *rettv, FunPtr fptr)
return;
}
- while (cnt < maxline || maxline < 0) {
+ while (maxline < 0 || tv_list_len(l) < maxline) {
readlen = (int)fread(buf, 1, io_size, fd);
/* This for loop processes what was read, but is also entered at end
@@ -13081,22 +13082,32 @@ static void f_readfile(typval_T *argvars, typval_T *rettv, FunPtr fptr)
prevlen = prevsize = 0;
}
- tv_list_append_owned_tv(rettv->vval.v_list, (typval_T) {
+ tv_list_append_owned_tv(l, (typval_T) {
.v_type = VAR_STRING,
.v_lock = VAR_UNLOCKED,
.vval.v_string = s,
});
- start = p + 1; /* step over newline */
- if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
+ start = p + 1; // Step over newline.
+ if (maxline < 0) {
+ if (tv_list_len(l) > -maxline) {
+ assert(tv_list_len(l) == 1 + (-maxline));
+ tv_list_item_remove(l, tv_list_first(l));
+ }
+ } else if (tv_list_len(l) >= maxline) {
+ assert(tv_list_len(l) == maxline);
break;
- } else if (*p == NUL)
+ }
+ if (readlen <= 0) {
+ break;
+ }
+ } else if (*p == NUL) {
*p = '\n';
- /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
- * when finding the BF and check the previous two bytes. */
- else if (*p == 0xbf && enc_utf8 && !binary) {
- /* Find the two bytes before the 0xbf. If p is at buf, or buf
- * + 1, these may be in the "prev" string. */
+ // Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
+ // when finding the BF and check the previous two bytes.
+ } else if (*p == 0xbf && !binary) {
+ // Find the two bytes before the 0xbf. If p is at buf, or buf + 1,
+ // these may be in the "prev" string.
char_u back1 = p >= buf + 1 ? p[-1]
: prevlen >= 1 ? prev[prevlen - 1] : NUL;
char_u back2 = p >= buf + 2 ? p[-2]
@@ -13130,8 +13141,9 @@ static void f_readfile(typval_T *argvars, typval_T *rettv, FunPtr fptr)
}
} /* for */
- if ((cnt >= maxline && maxline >= 0) || readlen <= 0)
+ if ((maxline >= 0 && tv_list_len(l) >= maxline) || readlen <= 0) {
break;
+ }
if (start < p) {
/* There's part of a line in buf, store it in "prev". */
if (p - start + prevlen >= prevsize) {
@@ -13155,14 +13167,6 @@ static void f_readfile(typval_T *argvars, typval_T *rettv, FunPtr fptr)
}
} /* while */
- // For a negative line count use only the lines at the end of the file,
- // free the rest.
- if (maxline < -tv_list_len(rettv->vval.v_list)) {
- listitem_T *const first_li = tv_list_find(rettv->vval.v_list, maxline);
- listitem_T *const last_li = tv_list_last(rettv->vval.v_list);
- tv_list_remove_items(rettv->vval.v_list, first_li, last_li);
- }
-
xfree(prev);
fclose(fd);
}
diff --git a/src/nvim/lua/converter.c b/src/nvim/lua/converter.c
index 89fedae73a..61cb428923 100644
--- a/src/nvim/lua/converter.c
+++ b/src/nvim/lua/converter.c
@@ -213,7 +213,6 @@ bool nlua_pop_typval(lua_State *lstate, typval_T *ret_tv)
if (cur.special) {
list_T *const kv_pair = tv_list_alloc();
- tv_list_append_list(cur.tv->vval.v_list, kv_pair);
typval_T s_tv = decode_string(s, len, kTrue, false, false);
if (s_tv.v_type == VAR_UNKNOWN) {
ret = false;
@@ -227,6 +226,7 @@ bool nlua_pop_typval(lua_State *lstate, typval_T *ret_tv)
.v_type = VAR_UNKNOWN,
});
kv_push(stack, cur);
+ tv_list_append_list(cur.tv->vval.v_list, kv_pair);
cur = (TVPopStackItem) {
.tv = TV_LIST_ITEM_TV(tv_list_last(kv_pair)),
.container = false,