changeset 20922:49081851fddc

Eliminate duplicate input arg checking in which.m and __which__. * which.m: Put input validation and print_usage call at top of fcn. * help.cc (F__which__): Don't check number of inputs or call print_usage in internal function. Call make_argv() with no argument to avoid a lot of +1/-1 arithmetic.
author Rik <rik@octave.org>
date Wed, 16 Dec 2015 16:47:18 -0800
parents 4d3daf7e43f3
children 58263bea2fdf
files libinterp/corefcn/help.cc scripts/help/which.m
diffstat 2 files changed, 42 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/help.cc	Wed Dec 16 18:21:46 2015 -0500
+++ b/libinterp/corefcn/help.cc	Wed Dec 16 16:47:18 2015 -0800
@@ -1282,20 +1282,17 @@
 Undocumented internal function.\n\
 @end deftypefn")
 {
-  string_vector argv = args.make_argv ("which");
+  string_vector argv = args.make_argv ("");
 
-  int argc = argv.numel ();
+  int nargin = argv.numel ();
 
-  if (argc < 2)
-    print_usage ();
+  octave_map m (dim_vector (1, nargin));
 
-  octave_map m (dim_vector (1, argc-1));
+  Cell names (1, nargin);
+  Cell files (1, nargin);
+  Cell types (1, nargin);
 
-  Cell names (1, argc-1);
-  Cell files (1, argc-1);
-  Cell types (1, argc-1);
-
-  for (int i = 1; i < argc; i++)
+  for (int i = 0; i < nargin; i++)
     {
       std::string name = argv[i];
 
@@ -1303,16 +1300,16 @@
 
       std::string file = do_which (name, type);
 
-      names(i-1) = name;
-      files(i-1) = file;
-      types(i-1) = type;
+      names(i) = name;
+      files(i) = file;
+      types(i) = type;
     }
 
   m.assign ("name", names);
   m.assign ("file", files);
   m.assign ("type", types);
 
-  return octave_value (m);
+  return ovl (m);
 }
 
 // FIXME: Are we sure this function always does the right thing?
--- a/scripts/help/which.m	Wed Dec 16 18:21:46 2015 -0500
+++ b/scripts/help/which.m	Wed Dec 16 16:47:18 2015 -0800
@@ -27,43 +27,43 @@
 
 function varargout = which (varargin)
 
-  if (nargin > 0 && iscellstr (varargin))
-    m = __which__ (varargin{:});
+  if (nargin == 0 || ! iscellstr (varargin))
+    print_usage ();
+  endif
+
+  m = __which__ (varargin{:});
+
+  ## Check whether each name is a variable, variables take precedence over
+  ## functions in name resolution.
+  for i = 1:nargin
+    m(i).is_variable = evalin ("caller",
+                               ["exist (\"" m(i).name "\", \"var\")"], false);
+    if (m(i).is_variable)
+      m(i).file = "variable";
+    endif
+  endfor
 
-    ## Check whether each name is a variable, variables take precedence over
-    ## functions in name resolution.
+  if (nargout == 0)
     for i = 1:nargin
-      m(i).is_variable = evalin ("caller",
-                                 ["exist (\"" m(i).name "\", \"var\")"], false);
       if (m(i).is_variable)
-        m(i).file = "variable";
+        printf ("'%s' is a variable\n", m(i).name);
+      elseif (isempty (m(i).file))
+        if (! isempty (m(i).type))
+          printf ("'%s' is a %s\n",
+                  m(i).name, m(i).type);
+        endif
+      else
+        if (isempty (m(i).type))
+          printf ("'%s' is the file %s\n",
+                  m(i).name, m(i).file);
+        else
+          printf ("'%s' is a %s from the file %s\n",
+                  m(i).name, m(i).type, m(i).file);
+        endif
       endif
     endfor
-
-    if (nargout == 0)
-      for i = 1:nargin
-        if (m(i).is_variable)
-          printf ("'%s' is a variable\n", m(i).name);
-        elseif (isempty (m(i).file))
-          if (! isempty (m(i).type))
-            printf ("'%s' is a %s\n",
-                    m(i).name, m(i).type);
-          endif
-        else
-          if (isempty (m(i).type))
-            printf ("'%s' is the file %s\n",
-                    m(i).name, m(i).file);
-          else
-            printf ("'%s' is a %s from the file %s\n",
-                    m(i).name, m(i).type, m(i).file);
-          endif
-        endif
-      endfor
-    else
-      varargout = {m.file};
-    endif
   else
-    print_usage ();
+    varargout = {m.file};
   endif
 
 endfunction