aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZyX <kp-pav@yandex.ru>2016-08-21 00:20:01 +0300
committerZyX <kp-pav@yandex.ru>2017-03-29 10:07:43 +0300
commit5cdf7177ec71e4b9b3295ead93bedf7ff226a2b2 (patch)
treec8ad8b61d42f667790cc3edae529e762e0e2ec13
parent2dcfc439b2ec2be4d830826061e89860977516be (diff)
downloadrneovim-5cdf7177ec71e4b9b3295ead93bedf7ff226a2b2.tar.gz
rneovim-5cdf7177ec71e4b9b3295ead93bedf7ff226a2b2.tar.bz2
rneovim-5cdf7177ec71e4b9b3295ead93bedf7ff226a2b2.zip
eval: Move get_float_arg to typval.h
Assuming `inline` is there for a reason, so it is kept and function was moved to typval.h and not to typval.c which does not have problems with #including message.h.
-rw-r--r--src/nvim/eval.c60
-rw-r--r--src/nvim/eval/typval.h27
-rw-r--r--src/nvim/gettext.h21
-rw-r--r--src/nvim/vim.h17
4 files changed, 72 insertions, 53 deletions
diff --git a/src/nvim/eval.c b/src/nvim/eval.c
index a64eb3c959..1d060eff33 100644
--- a/src/nvim/eval.c
+++ b/src/nvim/eval.c
@@ -6473,24 +6473,6 @@ static int non_zero_arg(typval_T *argvars)
*/
-/*
- * Get the float value of "argvars[0]" into "f".
- * Returns FAIL when the argument is not a Number or Float.
- */
-static inline int get_float_arg(typval_T *argvars, float_T *f)
-{
- if (argvars[0].v_type == VAR_FLOAT) {
- *f = argvars[0].vval.v_float;
- return OK;
- }
- if (argvars[0].v_type == VAR_NUMBER) {
- *f = (float_T)argvars[0].vval.v_number;
- return OK;
- }
- EMSG(_("E808: Number or Float required"));
- return FAIL;
-}
-
// Apply a floating point C function on a typval with one float_T.
//
// Some versions of glibc on i386 have an optimization that makes it harder to
@@ -6502,7 +6484,7 @@ static void float_op_wrapper(typval_T *argvars, typval_T *rettv, FunPtr fptr)
float_T (*function)(float_T) = (float_T (*)(float_T))fptr;
rettv->v_type = VAR_FLOAT;
- if (get_float_arg(argvars, &f) == OK) {
+ if (tv_get_float(argvars, &f)) {
rettv->vval.v_float = function(f);
} else {
rettv->vval.v_float = 0.0;
@@ -6957,14 +6939,15 @@ static void f_assert_true(typval_T *argvars, typval_T *rettv, FunPtr fptr)
*/
static void f_atan2(typval_T *argvars, typval_T *rettv, FunPtr fptr)
{
- float_T fx, fy;
+ float_T fx;
+ float_T fy;
rettv->v_type = VAR_FLOAT;
- if (get_float_arg(argvars, &fx) == OK
- && get_float_arg(&argvars[1], &fy) == OK)
+ if (tv_get_float(argvars, &fx) && tv_get_float(&argvars[1], &fy)) {
rettv->vval.v_float = atan2(fx, fy);
- else
+ } else {
rettv->vval.v_float = 0.0;
+ }
}
/*
@@ -8557,13 +8540,14 @@ static void f_float2nr(typval_T *argvars, typval_T *rettv, FunPtr fptr)
{
float_T f;
- if (get_float_arg(argvars, &f) == OK) {
- if (f < -0x7fffffff)
- rettv->vval.v_number = -0x7fffffff;
- else if (f > 0x7fffffff)
- rettv->vval.v_number = 0x7fffffff;
- else
+ if (tv_get_float(argvars, &f)) {
+ if (f < VARNUMBER_MIN) {
+ rettv->vval.v_number = VARNUMBER_MIN;
+ } else if (f > VARNUMBER_MAX) {
+ rettv->vval.v_number = VARNUMBER_MAX;
+ } else {
rettv->vval.v_number = (varnumber_T)f;
+ }
}
}
@@ -8572,14 +8556,15 @@ static void f_float2nr(typval_T *argvars, typval_T *rettv, FunPtr fptr)
*/
static void f_fmod(typval_T *argvars, typval_T *rettv, FunPtr fptr)
{
- float_T fx, fy;
+ float_T fx;
+ float_T fy;
rettv->v_type = VAR_FLOAT;
- if (get_float_arg(argvars, &fx) == OK
- && get_float_arg(&argvars[1], &fy) == OK)
+ if (tv_get_float(argvars, &fx) && tv_get_float(&argvars[1], &fy)) {
rettv->vval.v_float = fmod(fx, fy);
- else
+ } else {
rettv->vval.v_float = 0.0;
+ }
}
/*
@@ -12790,14 +12775,15 @@ static void f_pathshorten(typval_T *argvars, typval_T *rettv, FunPtr fptr)
*/
static void f_pow(typval_T *argvars, typval_T *rettv, FunPtr fptr)
{
- float_T fx, fy;
+ float_T fx;
+ float_T fy;
rettv->v_type = VAR_FLOAT;
- if (get_float_arg(argvars, &fx) == OK
- && get_float_arg(&argvars[1], &fy) == OK)
+ if (tv_get_float(argvars, &fx) && tv_get_float(&argvars[1], &fy)) {
rettv->vval.v_float = pow(fx, fy);
- else
+ } else {
rettv->vval.v_float = 0.0;
+ }
}
/*
diff --git a/src/nvim/eval/typval.h b/src/nvim/eval/typval.h
index 6929e37e43..0eef2ddaac 100644
--- a/src/nvim/eval/typval.h
+++ b/src/nvim/eval/typval.h
@@ -13,6 +13,7 @@
#include "nvim/lib/queue.h"
#include "nvim/profile.h" // for proftime_T
#include "nvim/pos.h" // for linenr_T
+#include "nvim/gettext.h"
/// Type used for VimL VAR_NUMBER values
typedef int varnumber_T;
@@ -370,6 +371,32 @@ extern bool tv_in_free_unref_items;
} \
})
+static inline bool tv_get_float(const typval_T *const tv, float_T *const ret_f)
+ REAL_FATTR_NONNULL_ALL REAL_FATTR_WARN_UNUSED_RESULT;
+
+// FIXME circular dependency, cannot import message.h.
+bool emsgf(const char *const fmt, ...);
+
+/// Get the float value
+///
+/// @param[in] tv VimL object to get value from.
+/// @param[out] ret_f Location where resulting float is stored.
+///
+/// @return true in case of success, false if tv is not a number or float.
+static inline bool tv_get_float(const typval_T *const tv, float_T *const ret_f)
+{
+ if (tv->v_type == VAR_FLOAT) {
+ *ret_f = tv->vval.v_float;
+ return true;
+ }
+ if (tv->v_type == VAR_NUMBER) {
+ *ret_f = (float_T)tv->vval.v_number;
+ return true;
+ }
+ emsgf(_("E808: Number or Float required"));
+ return false;
+}
+
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "eval/typval.h.generated.h"
#endif
diff --git a/src/nvim/gettext.h b/src/nvim/gettext.h
new file mode 100644
index 0000000000..aa0e97233e
--- /dev/null
+++ b/src/nvim/gettext.h
@@ -0,0 +1,21 @@
+#ifndef NVIM_GETTEXT_H
+#define NVIM_GETTEXT_H
+
+#ifdef HAVE_WORKING_LIBINTL
+# include <libintl.h>
+# define _(x) gettext((char *)(x))
+// XXX do we actually need this?
+# ifdef gettext_noop
+# define N_(x) gettext_noop(x)
+# else
+# define N_(x) x
+# endif
+#else
+# define _(x) ((char *)(x))
+# define N_(x) x
+# define bindtextdomain(x, y) // empty
+# define bind_textdomain_codeset(x, y) // empty
+# define textdomain(x) // empty
+#endif
+
+#endif // NVIM_GETTEXT_H
diff --git a/src/nvim/vim.h b/src/nvim/vim.h
index ae654251f9..e16ee00309 100644
--- a/src/nvim/vim.h
+++ b/src/nvim/vim.h
@@ -51,22 +51,7 @@ Error: configure did not run properly.Check auto/config.log.
/* ================ end of the header file puzzle =============== */
-#ifdef HAVE_WORKING_LIBINTL
-# include <libintl.h>
-# define _(x) gettext((char *)(x))
-// XXX do we actually need this?
-# ifdef gettext_noop
-# define N_(x) gettext_noop(x)
-# else
-# define N_(x) x
-# endif
-#else
-# define _(x) ((char *)(x))
-# define N_(x) x
-# define bindtextdomain(x, y) /* empty */
-# define bind_textdomain_codeset(x, y) /* empty */
-# define textdomain(x) /* empty */
-#endif
+#include "nvim/gettext.h"
/* special attribute addition: Put message in history */
#define MSG_HIST 0x1000