changeset 20543:df7d34a1c7e6

str2func: Allow anonymous function string inputs (bug #45682). * ov-fcn-handle.cc (Ffunc2str): Use eval_string to create a function handle when string begins with anonymous function marker '@'. Update docstring. Add BIST tests.
author Rik <rik@octave.org>
date Wed, 23 Sep 2015 02:33:35 -0700
parents 2aa4fb60ae77
children cd4a8b4631da
files libinterp/octave-value/ov-fcn-handle.cc
diffstat 1 files changed, 24 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/octave-value/ov-fcn-handle.cc	Tue Sep 22 21:45:24 2015 +0200
+++ b/libinterp/octave-value/ov-fcn-handle.cc	Wed Sep 23 02:33:35 2015 -0700
@@ -1854,10 +1854,6 @@
 \n\
 If the optional @qcode{\"global\"} argument is passed, locally visible\n\
 functions are ignored in the lookup.\n\
-\n\
-Note: @code{str2func} does not currently accept strings which define\n\
-anonymous functions (those which begin with @samp{@@}).\n\
-Use @w{@code{eval (@var{str})}} as a replacement.\n\
 @seealso{func2str, inline}\n\
 @end deftypefn")
 {
@@ -1871,10 +1867,15 @@
           std::string nm = args(0).string_value ();
           if (nm[0] == '@')
             {
-              error ("str2func: Can't process anonymous functions.");
-              return retval;
+              int parse_status;
+              octave_value anon_fcn_handle =
+                eval_string (nm, true, parse_status);
+
+              if (parse_status == 0)
+                retval = anon_fcn_handle; 
             }
-          retval = make_fcn_handle (nm, nargin != 2);
+          else
+            retval = make_fcn_handle (nm, nargin != 2);
         }
       else
         error ("str2func: FCN_NAME must be a string");
@@ -1886,6 +1887,22 @@
 }
 
 /*
+%!test
+%! f = str2func ("<");
+%! assert (class (f), "function_handle");
+%! assert (func2str (f), "lt");
+%! assert (f (1, 2), true);
+%! assert (f (2, 1), false);
+
+%!test
+%! f = str2func ("@(x) sin (x)");
+%! assert (func2str (f), "@(x) sin (x)");
+%! assert (f (0:3), sin (0:3));
+
+%!error <FCN_NAME must be a string> str2func ({"sin"})
+*/
+
+/*
 %!function y = __testrecursionfunc (f, x, n)
 %!  if (nargin < 3)
 %!    n = 0;