[gnumeric] IF: fix more border cases for empty results.



commit 814ebcd39118044ee7a8fa081d98fa322c644ee4
Author: Morten Welinder <terra gnome org>
Date:   Thu Jun 18 19:04:40 2009 -0400

    IF: fix more border cases for empty results.

 ChangeLog          |   10 +++++++++-
 NEWS               |    2 +-
 src/expr.c         |    9 +++++++++
 src/expr.h         |    1 +
 src/func-builtin.c |   10 ++++------
 src/func.c         |    9 ++++++++-
 6 files changed, 32 insertions(+), 9 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index f0bdb33..17a1db7 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,7 +1,15 @@
 2009-06-18  Morten Welinder  <terra gnome org>
 
+	* src/func.c (function_call_with_exprs): Make sure we can tell
+	skipped arguments from present arguments that return empty values.
+	Fixes #586312.
+
+	* src/expr.c (gnm_expr_is_empty): New function.
+
 	* src/func-builtin.c (gnumeric_if2): Allow empty and array results
-	results.
+	results.  Map NULL into value_new_empty() if the argument that
+	produced it is not empty.  (That prevents them from being treated
+	as zero.)  Fixes #586218.
 
 2009-06-18  Andreas J. Guelzow <aguelzow pyrshep ca>
 
diff --git a/NEWS b/NEWS
index a55dedf..b7b7e32 100644
--- a/NEWS
+++ b/NEWS
@@ -49,7 +49,7 @@ Morten:
 	* Start introducing external references for function docs.
 	* Implement new functions COTH and ACOTH.  [#586170]
 	* Fix tsa crash.  [#586191]
-	* Fix IF problem with empties and arrays.  [#586218]
+	* Fix IF problems with empties and arrays.  [#586218, #586312]
 
 --------------------------------------------------------------------------
 Gnumeric 1.9.8
diff --git a/src/expr.c b/src/expr.c
index e2aafdc..a990f03 100644
--- a/src/expr.c
+++ b/src/expr.c
@@ -2621,6 +2621,15 @@ gnm_expr_is_data_table (GnmExpr const *expr, GnmCellPos *c_in, GnmCellPos *r_in)
 	return FALSE;
 }
 
+gboolean
+gnm_expr_is_empty (GnmExpr const *expr)
+{
+	g_return_val_if_fail (expr != NULL, FALSE);
+
+	return (GNM_EXPR_GET_OPER (expr) == GNM_EXPR_OP_CONSTANT &&
+		VALUE_IS_EMPTY (expr->constant.value));
+}
+
 /*
  * This frees the data pointers and the list.
  */
diff --git a/src/expr.h b/src/expr.h
index 019f48a..5952a30 100644
--- a/src/expr.h
+++ b/src/expr.h
@@ -88,6 +88,7 @@ GnmExpr const *gnm_expr_copy         (GnmExpr const *expr);
 gboolean  gnm_expr_is_rangeref	     (GnmExpr const *expr);
 gboolean  gnm_expr_is_data_table     (GnmExpr const *expr,
 				      GnmCellPos *c_in, GnmCellPos *r_in);
+gboolean  gnm_expr_is_empty          (GnmExpr const *expr);
 
 void	  gnm_expr_as_gstring	     (GnmExpr const *expr,
 				      GnmConventionsOut *out);
diff --git a/src/func-builtin.c b/src/func-builtin.c
index e03caf5..56e34c9 100644
--- a/src/func-builtin.c
+++ b/src/func-builtin.c
@@ -335,14 +335,12 @@ gnumeric_if2 (GnmFuncEvalInfo *ei, int argc, GnmExprConstPtr const *argv)
 			GNM_EXPR_EVAL_PERMIT_NON_SCALAR |
 			GNM_EXPR_EVAL_PERMIT_EMPTY;
 
-		if (i >= argc)
-			args[i] = NULL;
-		else if (branch == i) {
-			args[i] = gnm_expr_eval (argv[branch], ei->pos, flags);
+		args[i] = NULL;
+		if (i < argc && i == branch && !gnm_expr_is_empty (argv[i])) {
+			args[i] = gnm_expr_eval (argv[i], ei->pos, flags);
 			if (!args[i])
 				args[i] = value_new_empty ();
-		} else
-			args[i] = value_new_empty ();
+		}
 	}
 
 	res = gnumeric_if (ei, (GnmValue const * const *)args);
diff --git a/src/func.c b/src/func.c
index fb8353d..1d00315 100644
--- a/src/func.c
+++ b/src/func.c
@@ -1188,8 +1188,15 @@ function_call_with_exprs (GnmFuncEvalInfo *ei, GnmExprEvalFlags flags)
 			continue;
 
 		/* optional arguments can be blank */
-		if (i >= fn_def->fn.args.min_args && VALUE_IS_EMPTY (tmp))
+		if (i >= fn_def->fn.args.min_args && VALUE_IS_EMPTY (tmp)) {
+			if (arg_type == 'E' && !gnm_expr_is_empty (expr)) {
+				/* An actual argument produced empty.  Make
+				   sure function sees that.  */
+				args[i] = value_new_empty ();
+			}
+
 			continue;
+		}
 
 		if (tmp == NULL)
 			tmp = args[i] = value_new_empty ();



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]