eval: Refactor get_tv_lnum_buf

This commit is contained in:
ZyX
2016-08-28 08:18:29 +03:00
parent 7ee5cc7429
commit 1b3e13da5b
2 changed files with 29 additions and 23 deletions

View File

@@ -9190,13 +9190,34 @@ static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retli
} }
} }
/// Get the line number from VimL object
///
/// @note Unlike tv_get_lnum(), this one supports only "$" special string.
///
/// @param[in] tv Object to get value from. Is expected to be a number or
/// a special string "$".
/// @param[in] buf Buffer to take last line number from in case tv is "$". May
/// be NULL, in this case "$" results in zero return.
///
/// @return Line number or 0 in case of error.
static linenr_T tv_get_lnum_buf(const typval_T *const tv,
const buf_T *const buf)
FUNC_ATTR_NONNULL_ARG(1) FUNC_ATTR_WARN_UNUSED_RESULT
{
if (tv->v_type == VAR_STRING
&& tv->vval.v_string != NULL
&& tv->vval.v_string[0] == '$'
&& buf != NULL) {
return buf->b_ml.ml_line_count;
}
return get_tv_number_chk(tv, NULL);
}
/* /*
* "getbufline()" function * "getbufline()" function
*/ */
static void f_getbufline(typval_T *argvars, typval_T *rettv, FunPtr fptr) static void f_getbufline(typval_T *argvars, typval_T *rettv, FunPtr fptr)
{ {
linenr_T lnum;
linenr_T end;
buf_T *buf = NULL; buf_T *buf = NULL;
if (tv_check_str_or_nr(&argvars[0])) { if (tv_check_str_or_nr(&argvars[0])) {
@@ -9205,12 +9226,10 @@ static void f_getbufline(typval_T *argvars, typval_T *rettv, FunPtr fptr)
emsg_off--; emsg_off--;
} }
lnum = get_tv_lnum_buf(&argvars[1], buf); const linenr_T lnum = tv_get_lnum_buf(&argvars[1], buf);
if (argvars[2].v_type == VAR_UNKNOWN) { const linenr_T end = (argvars[2].v_type == VAR_UNKNOWN
end = lnum; ? lnum
} else { : tv_get_lnum_buf(&argvars[2], buf));
end = get_tv_lnum_buf(&argvars[2], buf);
}
get_buffer_lines(buf, lnum, end, true, rettv); get_buffer_lines(buf, lnum, end, true, rettv);
} }
@@ -18464,21 +18483,6 @@ varnumber_T get_tv_number_chk(const typval_T *const varp, bool *const denote)
return n; return n;
} }
/*
* Get the lnum from the first argument.
* Also accepts "$", then "buf" is used.
* Returns 0 on error.
*/
static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
{
if (argvars[0].v_type == VAR_STRING
&& argvars[0].vval.v_string != NULL
&& argvars[0].vval.v_string[0] == '$'
&& buf != NULL)
return buf->b_ml.ml_line_count;
return get_tv_number_chk(&argvars[0], NULL);
}
// TODO(ZyX-I): move to eval/typval // TODO(ZyX-I): move to eval/typval
/// Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE! /// Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!

View File

@@ -2026,6 +2026,7 @@ bool tv_check_str_or_nr(const typval_T *const tv)
/// ///
/// @return Line number or -1 or 0. /// @return Line number or -1 or 0.
linenr_T tv_get_lnum(const typval_T *const tv) linenr_T tv_get_lnum(const typval_T *const tv)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
{ {
linenr_T lnum = get_tv_number_chk(tv, NULL); linenr_T lnum = get_tv_number_chk(tv, NULL);
if (lnum == 0) { // No valid number, try using same function as line() does. if (lnum == 0) { // No valid number, try using same function as line() does.
@@ -2046,6 +2047,7 @@ linenr_T tv_get_lnum(const typval_T *const tv)
/// ///
/// @return Floating-point value of the variable or zero. /// @return Floating-point value of the variable or zero.
float_T tv_get_float(const typval_T *const tv) float_T tv_get_float(const typval_T *const tv)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
{ {
switch (tv->v_type) { switch (tv->v_type) {
case VAR_NUMBER: { case VAR_NUMBER: {