* Bug #7649 fixed: isempty() on struct(), {} or list(,) returned %F
[scilab.git] / scilab / modules / elementary_functions / macros / isempty.sci
index e9d1bcf..75550a8 100644 (file)
@@ -1,6 +1,7 @@
 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 // Copyright (C) 1998 - INRIA
 // Copyright (C) DIGITEO - 2011-2012 - Allan CORNET
+// Copyright (C) 2016 - Samuel GOUGEON
 //
 // Copyright (C) 2012 - 2016 - Scilab Enterprises
 //
@@ -15,13 +16,15 @@ function answ = isempty(m)
 
     rhs = argn(2);
     if rhs <> 1 then
-        error(msprintf(gettext("%s: Wrong number of input argument(s): %d expected.\n"), "isempty", 1));
+        msg = gettext("%s: Wrong number of input argument(s): %d expected.\n")
+        error(msprintf(msg, "isempty", 1))
     end
 
     m_type = type(m);
 
     if( (m_type >= 11) & (m_type <= 13) | (m_type >= 128) ) then
-        error(msprintf(gettext("%s: Wrong type for input argument #%d.\n"), "isempty", 1));
+        msg = gettext("%s: Wrong type for input argument #%d.\n")
+        error(msprintf(msg, "isempty", 1))
     end
 
     select m_type
@@ -34,15 +37,11 @@ function answ = isempty(m)
     case 15
         // list
         answ = %t;
-        for i=1:size(m),
-            clear __element__;
-            __element__ = m(i);
-            if isdef("__element__") then
-                r = isempty(m(i));
-            else
-                r = %F;
+        for i = 1:size(m),
+            answ = answ & (type(m(i))==0 || isempty(m(i)))
+            if ~answ
+                break
             end
-            answ = answ & r;
         end;
 
     case 16
@@ -51,24 +50,50 @@ function answ = isempty(m)
             answ = size(m, "*") == 0;
         else
             answ = %t;
-            for i=2:size(m),
-                clear __element__;
-                __element__ = m(i);
-                if isdef("__element__") then
-                    r = isempty(m(i));
-                else
-                    r = %F;
+            for i = 2:size(m,"*")
+                answ = answ & (type(m(i))==0 || isempty(m(i)))
+                if ~answ
+                    break
                 end
-                answ = answ & r;
-            end;
+            end
         end
 
     case 17
         // mlist
-        answ = %f;
+        answ = %t
+        if typeof(m)=="ce"      // array of cells
+            if size(m,"*")==1
+                answ = answ & isempty(m{1})
+            else
+                n = size(m,"*")
+                for i = 1:n
+                    answ = answ & isempty(m(i))
+                    if ~answ
+                        break
+                    end
+                end
+            end
+
+        elseif typeof(m)=="st"    // array of structures
+            fn = fieldnames(m)
+            if size(m, "*")>0 && fn~=[]
+                for f = fn(:)'
+                    answ = answ & isempty(m(f))
+                    if ~answ
+                        break
+                    end
+                end
+            end
+        else                    // other types of mlist
+            answ = %f
+        end
 
     else
-        answ = size(m,"*")==0;
+        if type(m)~=0
+            answ = size(m,"*")==0
+        else
+            answ = %t
+        end
     end
 
 endfunction