bugGNU Octave - Bugs: bug #56699, "ForceCellOutput" option...

 
 

bug #56699: "ForceCellOutput" option in strfind

Submitter:  Guillaume <gyom>
Submitted:  Wed 31 Jul 2019 09:20:05 AM UTC
   
 
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Matlab Compatibility
Status:  Fixed Assigned to:  None
Originator Name:  Guillaume Open/Closed:  * Closed
Release:  * dev Operating System:  * Any
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Mon 05 Aug 2019 03:52:55 PM UTC, comment #1: 

I verified the patch and checked it in on the development branch under your name.  See changeset https://hg.savannah.gnu.org/hgweb/octave/rev/86c5dd1283b6.

Marking as fixed and closing report.

Rik <rik5>
Group administrator
Wed 31 Jul 2019 09:20:05 AM UTC, original submission:  

strfind has a "ForceCellOutput" option to always return a cell array even if the first input is a char array.
https://www.mathworks.com/help/matlab/ref/strfind.html

Here is my attempt at implementing it:


diff -r b8715e8eb39e libinterp/corefcn/strfind.cc
--- a/libinterp/corefcn/strfind.cc        Mon Jul 29 23:41:07 2019 -0400
+++ b/libinterp/corefcn/strfind.cc        Wed Jul 31 10:18:09 2019 +0100
@@ -151,6 +151,7 @@
 @deftypefn  {} {@var{idx} =} strfind (@var{str}, @var{pattern})
 @deftypefnx {} {@var{idx} =} strfind (@var{cellstr}, @var{pattern})
 @deftypefnx {} {@var{idx} =} strfind (@dots{}, "overlaps", @var{val})
+@deftypefnx {} {@var{idx} =} strfind (@dots{}, "forcecelloutput", @var{val})
 Search for @var{pattern} in the string @var{str} and return the starting
 index of every such occurrence in the vector @var{idx}.

@@ -165,6 +166,9 @@
 If a cell array of strings @var{cellstr} is specified then @var{idx} is a
 cell array of vectors, as specified above.

+The optional argument @qcode{"forcecelloutput"} allows to force @var{idx}
+to always be a cell array of vectors. The default is false.
+
 Examples:

 @example
@@ -185,6 +189,14 @@
           [1,2] = [](1x0)
           [1,3] = [](1x0)
         @}
+
+strfind ("abababa", "aba", "forcecelloutput", true)
+     @result{}
+        @{
+          [1,1] =
+
+             1   3   5
+        @}
 @end group
 @end example
 @seealso{regexp, regexpi, find}
@@ -196,15 +208,19 @@
     print_usage ();

   bool overlaps = true;
+  bool forcecelloutput = false;
   if (nargin == 4)
     {
       if (! args(2).is_string () || ! args(3).is_scalar_type ())
         error ("strfind: invalid optional arguments");

       std::string opt = args(2).string_value ();
+      std::transform (opt.begin (), opt.end (), opt.begin (), tolower);

       if (opt == "overlaps")
         overlaps = args(3).bool_value ();
+      else if (opt == "forcecelloutput")
+        forcecelloutput = args(3).bool_value ();
       else
         error ("strfind: unknown option: %s", opt.c_str ());
     }
@@ -221,14 +237,18 @@
       qs_preprocess (needle, table);

       if (argstr.is_string ())
-        if (argpat.isempty ())
-          // Return a null matrix for null pattern for MW compatibility
-          retval = Matrix ();
-        else
-          retval = octave_value (qs_search (needle,
-                                            argstr.char_array_value (),
-                                            table, overlaps),
-                                 true, true);
+        {
+          if (argpat.isempty ())
+            // Return a null matrix for null pattern for MW compatibility
+            retval = Matrix ();
+          else
+            retval = octave_value (qs_search (needle,
+                                              argstr.char_array_value (),
+                                              table, overlaps),
+                                   true, true);
+          if (forcecelloutput)
+            retval = Cell (retval);
+        }
       else if (argstr.iscell ())
         {
           const Cell argsc = argstr.cell_value ();
@@ -266,7 +286,11 @@
 /*
 %!assert (strfind ("abababa", "aba"), [1, 3, 5])
 %!assert (strfind ("abababa", "aba", "overlaps", false), [1, 5])
+%!assert (strfind ("abababa", "aba", "forcecelloutput", false), [1, 3, 5])
+%!assert (strfind ("abababa", "aba", "forcecelloutput", true), {[1, 3, 5]})
 %!assert (strfind ({"abababa", "bla", "bla"}, "a"), {[1, 3, 5, 7], 3, 3})
+%!assert (strfind ({"abababa", "bla", "bla"}, "a", "forcecelloutput", false), {[1, 3, 5, 7], 3, 3})
+%!assert (strfind ({"abababa", "bla", "bla"}, "a", "forcecelloutput", true), {[1, 3, 5, 7], 3, 3})
 %!assert (strfind ("Linux _is_ user-friendly. It just isn't ignorant-friendly or idiot-friendly.", "friendly"), [17, 50, 68])
 %!assert (strfind ("abc", ""), [])
 %!assert (strfind ("abc", {"", "b", ""}), {[], 2, []})


Guillaume <gyom>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by gyom (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    Follow 2 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2019-08-05 rik5 StatusNone Fixed
        Open/ClosedOpen Closed

    Back to the top

    Powered by Savane 3.13-caa5.
    Corresponding source code