[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Octave-bug-tracker] [bug #56699] "ForceCellOutput" option in strfind
From: |
Guillaume |
Subject: |
[Octave-bug-tracker] [bug #56699] "ForceCellOutput" option in strfind |
Date: |
Wed, 31 Jul 2019 05:20:08 -0400 (EDT) |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0 |
URL:
<https://savannah.gnu.org/bugs/?56699>
Summary: "ForceCellOutput" option in strfind
Project: GNU Octave
Submitted by: gyom
Submitted on: Wed 31 Jul 2019 09:20:05 AM UTC
Category: Octave Function
Severity: 3 - Normal
Priority: 5 - Normal
Item Group: Matlab Compatibility
Status: None
Assigned to: None
Originator Name: Guillaume
Originator Email:
Open/Closed: Open
Discussion Lock: Any
Release: dev
Operating System: Any
_______________________________________________________
Details:
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, []})
_______________________________________________________
Reply to this item at:
<https://savannah.gnu.org/bugs/?56699>
_______________________________________________
Message sent via Savannah
https://savannah.gnu.org/
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Octave-bug-tracker] [bug #56699] "ForceCellOutput" option in strfind,
Guillaume <=