changeset 32001:51b503f5ca3a

isstrprop.m: Add additional calling form with 'ForceCellOutput' for Matlab compatibility. * isstrprop.m: Document new 'ForceCellOutput' option as third input. Change function signature to accept 4 arguments. Update input validation to verify third and fourth inputs. Return cell array instead of logical array if requested. Add BIST tests for new behavior and new input validation.
author Rik <rik@octave.org>
date Wed, 12 Apr 2023 14:40:52 -0700
parents 20bf7bf8c95d
children 939e5d952675
files scripts/strings/isstrprop.m
diffstat 1 files changed, 34 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/strings/isstrprop.m	Wed Apr 12 17:57:48 2023 +0200
+++ b/scripts/strings/isstrprop.m	Wed Apr 12 14:40:52 2023 -0700
@@ -24,7 +24,8 @@
 ########################################################################
 
 ## -*- texinfo -*-
-## @deftypefn {} {@var{tf} =} isstrprop (@var{str}, @var{prop})
+## @deftypefn  {} {@var{tf} =} isstrprop (@var{str}, @var{prop})
+## @deftypefnx {} {@var{tf} =} isstrprop (@var{str}, @var{prop}, 'ForceCellOutput', @var{flag})
 ## Test character string properties.
 ##
 ## For example:
@@ -87,14 +88,27 @@
 ##
 ## @end table
 ##
+## If the option @qcode{'ForceCellOutput'} is given and @var{flag} is true then
+## a cell value is returned rather than a logical array.
+##
 ## @seealso{isalpha, isalnum, islower, isupper, isdigit, isxdigit,
 ## isspace, ispunct, iscntrl, isgraph, isprint, isascii}
 ## @end deftypefn
 
-function tf = isstrprop (str, prop)
+function tf = isstrprop (str, prop, opt, flag)
+
+  if (nargin != 2 && nargin != 4)
+    print_usage ();
+  endif
 
-  if (nargin != 2)
-    print_usage ();
+  force_cell_output = false;
+  if (nargin > 2)
+    if (! (isrow (opt) && strcmpi (opt, 'ForceCellOutput')))
+      error ("isstrprop: only accepted option is 'ForceCellOutput'");
+    elseif (! (isscalar (flag) && isreal (flag)))
+      error ("isstrprop: FLAG must be a real scalar");
+    endif
+    force_cell_output = flag; 
   endif
 
   switch (prop)
@@ -126,6 +140,10 @@
       error ("isstrprop: invalid string property");
   endswitch
 
+  if (force_cell_output)
+    tf = {tf};
+  endif
+
 endfunction
 
 
@@ -134,11 +152,21 @@
 %!assert (isstrprop ("Hello World", "wspace"), isspace ("Hello World"))
 %!assert (isstrprop ("Hello World", "graphic"), isgraph ("Hello World"))
 %!assert (isstrprop (char ("AbC", "123"), "upper"), logical ([1 0 1; 0 0 0]))
+%!assert (isstrprop (char ("AbC", "123"), "upper", 'ForceCellOutput', true),
+%!        {logical([1 0 1; 0 0 0])})
 %!assert (isstrprop ({"AbC", "123"}, "lower"),
 %!        {logical([0 1 0]), logical([0 0 0])})
 
 ## Test input validation
 %!error <Invalid call> isstrprop ()
-%!error isstrprop ("abc123")
-%!error isstrprop ("abc123", "alpha", "alpha")
+%!error <Invalid call> isstrprop ("abc")
+%!error <Invalid call> isstrprop ("abc", 'alpha', 'ForceCellOutput')
+%!error <only accepted option is 'ForceCellOutput'>
+%! isstrprop ('a', 'alpha', ['ForceCellOutput';'ForceCellOutput'], true)
+%!error <only accepted option is 'ForceCellOutput'>
+%! isstrprop ('a', 'alpha', 'Foobar', true)
+%!error <FLAG must be a real scalar>
+%! isstrprop ('a', 'alpha', 'ForceCellOutput', [true, true])
+%!error <FLAG must be a real scalar>
+%! isstrprop ('a', 'alpha', 'ForceCellOutput', {true})
 %!error <invalid string property> isstrprop ("abc123", "foo")