changeset 25760:2ccad4396afc

findstr.m: Make m-file a legacy function. * NEWS: Announce change. * scripts/legacy/module.mk, scripts/strings/module.mk: Move findstr.m from strings/ dir to legacy/ dir. * findstr.m: Add Octave:legacy-function warning at top of function. Change docstring to note that this function is obsolete. * strfind.cc (Fstrfind): Remove legacy functions findstr, strmatch from @seealso list. * textread.m, html_compare_plot_demos.m, prefer.tst: Replace instances of findstr with strfind in existing code.
author Rik <rik@octave.org>
date Thu, 09 Aug 2018 13:26:23 -0700
parents ecdced9fe445
children 7c0dabd59d18
files NEWS libinterp/corefcn/strfind.cc scripts/io/textread.m scripts/legacy/findstr.m scripts/legacy/module.mk scripts/strings/findstr.m scripts/strings/module.mk scripts/testfun/private/html_compare_plot_demos.m test/prefer.tst
diffstat 9 files changed, 168 insertions(+), 152 deletions(-) [+]
line wrap: on
line diff
--- a/NEWS	Thu Aug 09 11:28:11 2018 -0700
+++ b/NEWS	Thu Aug 09 13:26:23 2018 -0700
@@ -76,6 +76,7 @@
 
       Function             | Replacement
       ---------------------|------------------
+      findstr              | strfind
       flipdim              | flip
       isequalwithequalnans | isequaln
       isstr                | ischar
--- a/libinterp/corefcn/strfind.cc	Thu Aug 09 11:28:11 2018 -0700
+++ b/libinterp/corefcn/strfind.cc	Thu Aug 09 13:26:23 2018 -0700
@@ -186,7 +186,7 @@
         @}
 @end group
 @end example
-@seealso{findstr, strmatch, regexp, regexpi, find}
+@seealso{regexp, regexpi, find}
 @end deftypefn */)
 {
   int nargin = args.length ();
@@ -372,7 +372,7 @@
 @end group
 @end example
 
-@seealso{regexprep, strfind, findstr}
+@seealso{regexprep, strfind}
 @end deftypefn */)
 {
   int nargin = args.length ();
--- a/scripts/io/textread.m	Thu Aug 09 11:28:11 2018 -0700
+++ b/scripts/io/textread.m	Thu Aug 09 13:26:23 2018 -0700
@@ -203,7 +203,7 @@
   ## FIXME: The below isn't ML-compatible: counts lines, not format string uses
   if (isfinite (nlines) && (nlines > 0))
     l_eol_char = length (eol_char);
-    eoi = findstr (str, eol_char);
+    eoi = strfind (str, eol_char);
     n_eoi = length (eoi);
     nblks = 0;
     ## Avoid slow repeated str concatenation, first seek requested end of data
@@ -216,7 +216,7 @@
         else
           str = nstr;
         endif
-        eoi = findstr (str, eol_char);
+        eoi = strfind (str, eol_char);
         n_eoi += numel (eoi);
         ++nblks;
       endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/legacy/findstr.m	Thu Aug 09 13:26:23 2018 -0700
@@ -0,0 +1,158 @@
+## Copyright (C) 1996-2018 Kurt Hornik
+##
+## This file is part of Octave.
+##
+## Octave is free software: you can redistribute it and/or modify it
+## under the terms of the GNU General Public License as published by
+## the Free Software Foundation, either version 3 of the License, or
+## (at your option) any later version.
+##
+## Octave is distributed in the hope that it will be useful, but
+## WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+## GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with Octave; see the file COPYING.  If not, see
+## <https://www.gnu.org/licenses/>.
+
+## -*- texinfo -*-
+## @deftypefn  {} {} findstr (@var{s}, @var{t})
+## @deftypefnx {} {} findstr (@var{s}, @var{t}, @var{overlap})
+##
+## This function is obsolete.  Use @code{strfind} instead.
+##
+## Return the vector of all positions in the longer of the two strings @var{s}
+## and @var{t} where an occurrence of the shorter of the two starts.
+##
+## If the optional argument @var{overlap} is true (default), the returned
+## vector can include overlapping positions.  For example:
+##
+## @example
+## @group
+## findstr ("ababab", "a")
+##      @result{} [1, 3, 5];
+## findstr ("abababa", "aba", 0)
+##      @result{} [1, 5]
+## @end group
+## @end example
+##
+## @strong{Caution:} @code{findstr} is obsolete.  Use @code{strfind} in all new
+## code.
+## @seealso{strfind, strmatch, strcmp, strncmp, strcmpi, strncmpi, find}
+## @end deftypefn
+
+## Note that this implementation swaps the strings if second one is longer
+## than the first, so try to put the longer one first.
+
+## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at>
+## Adapted-By: jwe
+
+function v = findstr (s, t, overlap = true)
+
+  persistent warned = false;
+  if (! warned)
+    warned = true;
+    warning ("Octave:legacy-function",
+             "findstr is obsolete; use strfind instead\n");
+  endif
+
+  if (nargin < 2 || nargin > 3)
+    print_usage ();
+  endif
+
+  if (all (size (s) > 1) || all (size (t) > 1))
+    error ("findstr: arguments must have only one non-singleton dimension");
+  endif
+
+  ## Make S be the longer string.
+  if (length (s) < length (t))
+    [s, t] = deal (t, s);
+  endif
+
+  l_s = length (s);
+  l_t = length (t);
+
+  if (l_t == 0)
+    ## zero length target: return empty set
+    v = [];
+
+  elseif (l_t == 1)
+    ## length one target: simple find
+    v = find (s == t);
+
+  elseif (l_t == 2)
+    ## length two target: find first at i and second at i+1
+    v = find (s(1:l_s-1) == t(1) & s(2:l_s) == t(2));
+
+  else
+    ## length three or more: match the first three by find then go through
+    ## the much smaller list to determine which of them are real matches
+    limit = l_s - l_t + 1;
+    v = find (  s(1:limit)   == t(1)
+              & s(2:limit+1) == t(2)
+              & s(3:limit+2) == t(3));
+  endif
+
+  ## Need to search the index vector if our find was too short
+  ## (target length > 3), or if we don't allow overlaps.  Note though
+  ## that there cannot be any overlaps if the first character in the
+  ## target is different from the remaining characters in the target,
+  ## so a single character, two different characters, or first character
+  ## different from the second two don't need to be searched.
+  if (l_t >= 3 || (! overlap && l_t > 1 && any (t(1) == t(2:l_t))))
+    ## force strings to be both row vectors or both column vectors
+    if (all (size (s) != size (t)))
+      t = t.';
+    endif
+
+    ## determine which ones to keep
+    keep = zeros (size (v));
+    ind = 0:l_t-1;
+    if (overlap)
+      for idx = 1:length (v)
+        keep(idx) = all (s(v(idx) + ind) == t);
+      endfor
+    else
+      ## First possible position for next non-overlapping match.
+      next = 1;
+      for idx = 1:length (v)
+        if (v(idx) >= next && s(v(idx) + ind) == t)
+          keep(idx) = 1;
+          ## Skip to the next possible match position.
+          next = v(idx) + l_t;
+        else
+          keep(idx) = 0;
+        endif
+      endfor
+    endif
+    if (! isempty (v))
+      v = v(find (keep));
+    endif
+  endif
+
+  if (isempty (v))
+    v = [];
+  endif
+
+  ## Always return a row vector, because that's what the old one did.
+  if (iscolumn (v))
+    v = v.';
+  endif
+
+endfunction
+
+
+## First test is necessary to provoke 1-time legacy warning
+%!test
+%! warning ("off", "Octave:legacy-function", "local");
+%! findstr ("", "");
+
+%!assert (findstr ("abababa", "a"), [1, 3, 5, 7])
+%!assert (findstr ("abababa", "aba"), [1, 3, 5])
+%!assert (findstr ("aba", "abababa", 0), [1, 5])
+
+## Test input validation
+%!error findstr ()
+%!error findstr ("foo", "bar", 3, 4)
+%!error <must have only one non-singleton dimension> findstr (["AB" ; "CD"], "C")
--- a/scripts/legacy/module.mk	Thu Aug 09 11:28:11 2018 -0700
+++ b/scripts/legacy/module.mk	Thu Aug 09 13:26:23 2018 -0700
@@ -1,6 +1,7 @@
 FCN_FILE_DIRS += scripts/legacy
 
 %canon_reldir%_FCN_FILES = \
+  %reldir%/findstr.m \
   %reldir%/flipdim.m \
   %reldir%/isequalwithequalnans.m \
   %reldir%/isstr.m \
--- a/scripts/strings/findstr.m	Thu Aug 09 11:28:11 2018 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,143 +0,0 @@
-## Copyright (C) 1996-2018 Kurt Hornik
-##
-## This file is part of Octave.
-##
-## Octave is free software: you can redistribute it and/or modify it
-## under the terms of the GNU General Public License as published by
-## the Free Software Foundation, either version 3 of the License, or
-## (at your option) any later version.
-##
-## Octave is distributed in the hope that it will be useful, but
-## WITHOUT ANY WARRANTY; without even the implied warranty of
-## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-## GNU General Public License for more details.
-##
-## You should have received a copy of the GNU General Public License
-## along with Octave; see the file COPYING.  If not, see
-## <https://www.gnu.org/licenses/>.
-
-## -*- texinfo -*-
-## @deftypefn  {} {} findstr (@var{s}, @var{t})
-## @deftypefnx {} {} findstr (@var{s}, @var{t}, @var{overlap})
-## Return the vector of all positions in the longer of the two strings @var{s}
-## and @var{t} where an occurrence of the shorter of the two starts.
-##
-## If the optional argument @var{overlap} is true (default), the returned
-## vector can include overlapping positions.  For example:
-##
-## @example
-## @group
-## findstr ("ababab", "a")
-##      @result{} [1, 3, 5];
-## findstr ("abababa", "aba", 0)
-##      @result{} [1, 5]
-## @end group
-## @end example
-##
-## @strong{Caution:} @code{findstr} is scheduled for deprecation.  Use
-## @code{strfind} in all new code.
-## @seealso{strfind, strmatch, strcmp, strncmp, strcmpi, strncmpi, find}
-## @end deftypefn
-
-## Note that this implementation swaps the strings if second one is longer
-## than the first, so try to put the longer one first.
-##
-## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at>
-## Adapted-By: jwe
-
-function v = findstr (s, t, overlap = true)
-
-  if (nargin < 2 || nargin > 3)
-    print_usage ();
-  endif
-
-  if (all (size (s) > 1) || all (size (t) > 1))
-    error ("findstr: arguments must have only one non-singleton dimension");
-  endif
-
-  ## Make S be the longer string.
-  if (length (s) < length (t))
-    [s, t] = deal (t, s);
-  endif
-
-  l_s = length (s);
-  l_t = length (t);
-
-  if (l_t == 0)
-    ## zero length target: return empty set
-    v = [];
-
-  elseif (l_t == 1)
-    ## length one target: simple find
-    v = find (s == t);
-
-  elseif (l_t == 2)
-    ## length two target: find first at i and second at i+1
-    v = find (s(1:l_s-1) == t(1) & s(2:l_s) == t(2));
-
-  else
-    ## length three or more: match the first three by find then go through
-    ## the much smaller list to determine which of them are real matches
-    limit = l_s - l_t + 1;
-    v = find (  s(1:limit)   == t(1)
-              & s(2:limit+1) == t(2)
-              & s(3:limit+2) == t(3));
-  endif
-
-  ## Need to search the index vector if our find was too short
-  ## (target length > 3), or if we don't allow overlaps.  Note though
-  ## that there cannot be any overlaps if the first character in the
-  ## target is different from the remaining characters in the target,
-  ## so a single character, two different characters, or first character
-  ## different from the second two don't need to be searched.
-  if (l_t >= 3 || (! overlap && l_t > 1 && any (t(1) == t(2:l_t))))
-    ## force strings to be both row vectors or both column vectors
-    if (all (size (s) != size (t)))
-      t = t.';
-    endif
-
-    ## determine which ones to keep
-    keep = zeros (size (v));
-    ind = 0:l_t-1;
-    if (overlap)
-      for idx = 1:length (v)
-        keep(idx) = all (s(v(idx) + ind) == t);
-      endfor
-    else
-      ## First possible position for next non-overlapping match.
-      next = 1;
-      for idx = 1:length (v)
-        if (v(idx) >= next && s(v(idx) + ind) == t)
-          keep(idx) = 1;
-          ## Skip to the next possible match position.
-          next = v(idx) + l_t;
-        else
-          keep(idx) = 0;
-        endif
-      endfor
-    endif
-    if (! isempty (v))
-      v = v(find (keep));
-    endif
-  endif
-
-  if (isempty (v))
-    v = [];
-  endif
-
-  ## Always return a row vector, because that's what the old one did.
-  if (iscolumn (v))
-    v = v.';
-  endif
-
-endfunction
-
-
-%!assert (findstr ("abababa", "a"), [1, 3, 5, 7])
-%!assert (findstr ("abababa", "aba"), [1, 3, 5])
-%!assert (findstr ("aba", "abababa", 0), [1, 5])
-
-## Test input validation
-%!error findstr ()
-%!error findstr ("foo", "bar", 3, 4)
-%!error <must have only one non-singleton dimension> findstr (["AB" ; "CD"], "C")
--- a/scripts/strings/module.mk	Thu Aug 09 11:28:11 2018 -0700
+++ b/scripts/strings/module.mk	Thu Aug 09 13:26:23 2018 -0700
@@ -10,7 +10,6 @@
   %reldir%/dec2bin.m \
   %reldir%/dec2hex.m \
   %reldir%/erase.m \
-  %reldir%/findstr.m \
   %reldir%/hex2dec.m \
   %reldir%/index.m \
   %reldir%/isletter.m \
--- a/scripts/testfun/private/html_compare_plot_demos.m	Thu Aug 09 11:28:11 2018 -0700
+++ b/scripts/testfun/private/html_compare_plot_demos.m	Thu Aug 09 13:26:23 2018 -0700
@@ -80,7 +80,7 @@
   fclose (fid);
 
   anchor = "<!-- ##ADD TABLE HERE## -->";
-  n = findstr (template, anchor);
+  n = strfind (template, anchor);
   header = strtrim (template(1:n-1));
   trailer = strtrim (template(n+numel(anchor):end));
 
--- a/test/prefer.tst	Thu Aug 09 11:28:11 2018 -0700
+++ b/test/prefer.tst	Thu Aug 09 13:26:23 2018 -0700
@@ -95,8 +95,8 @@
 %! print_empty_dimensions (0);
 %! a = cell (1, 1);
 %! b = type ("-q", "a");
-%! assert (!isempty (findstr (b{1}, "[]")));
-%! assert (isempty (findstr (b{1} ,"[](0x0)")));
+%! assert (! isempty (strfind (b{1}, "[]")));
+%! assert (isempty (strfind (b{1} ,"[](0x0)")));
 %! print_empty_dimensions (ped);
 
 %!test
@@ -104,7 +104,7 @@
 %! print_empty_dimensions (1);
 %! a = cell (1, 1);
 %! b = type ("-q", "a");
-%! assert (!isempty (findstr (b{1}, "[](0x0)")));
+%! assert (! isempty (strfind (b{1}, "[](0x0)")));
 %! print_empty_dimensions (ped);
 
 %!assert (all (size (inv ([])) == [0, 0]))