1 ## Copyright (C) 2000-2012 Paul Kienzle
2 ## Copyright (C) 2003 Alois Schloegl
3 ## Copyright (C) 2010 VZLU Prague
5 ## This file is part of Octave.
7 ## Octave is free software; you can redistribute it and/or modify it
8 ## under the terms of the GNU General Public License as published by
9 ## the Free Software Foundation; either version 3 of the License, or (at
10 ## your option) any later version.
12 ## Octave is distributed in the hope that it will be useful, but
13 ## WITHOUT ANY WARRANTY; without even the implied warranty of
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ## General Public License for more details.
17 ## You should have received a copy of the GNU General Public License
18 ## along with Octave; see the file COPYING. If not, see
19 ## <http://www.gnu.org/licenses/>.
22 ## @deftypefn {Function File} {} strmatch (@var{s}, @var{A})
23 ## @deftypefnx {Function File} {} strmatch (@var{s}, @var{A}, "exact")
24 ## Return indices of entries of @var{A} which begin with the string @var{s}.
25 ## The second argument @var{A} must be a string, character matrix, or a cell
26 ## array of strings. If the third argument @code{"exact"} is not given, then
27 ## @var{s} only needs to match @var{A} up to the length of @var{s}.
28 ## Trailing spaces and nulls in @var{s} and @var{A} are ignored when matching.
35 ## strmatch ("apple", "apple juice")
38 ## strmatch ("apple", ["apple "; "apple juice"; "an apple"])
41 ## strmatch ("apple", ["apple "; "apple juice"; "an apple"], "exact")
46 ## @strong{Caution:} @code{strmatch} is scheduled for deprecation. Use
47 ## @code{strcmpi} or @code{strncmpi} in all new code.
48 ## @seealso{strfind, findstr, strcmp, strncmp, strcmpi, strncmpi, find}
51 ## Author: Paul Kienzle, Alois Schloegl
54 function idx = strmatch (s, A, exact)
56 if (nargin < 2 || nargin > 3)
60 if (! ischar (s) || (! isempty (s) && ! isvector (s)))
61 error ("strmatch: S must be a string");
62 elseif (! (ischar (A) || iscellstr (A)))
63 error ("strmatch: A must be a string or cell array of strings");
66 ## Trim blanks and nulls from search string
67 s = regexprep (s, "[ \\0]+$", '');
70 exact = nargin == 3 && ischar (exact) && strcmp (exact, "exact");
77 match = all (bsxfun (@eq, A(:,1:len), s), 2);
80 match &= all (AA == " " | AA == "\0", 2);
86 idx = find (strncmp (s, A, len));
88 idx = find (strcmp (s, A));
91 ## We can't just use strcmp, because we need to ignore spaces at end.
92 B = regexprep (A(idx), "[ \\0]+$", '');
93 idx = idx(strcmp (s, B));
100 %!assert (strmatch("a", {"aaa", "bab", "bbb"}), 1);
101 %!assert (strmatch ("apple", "apple juice"), 1);
102 %!assert (strmatch ("apple", ["apple pie"; "apple juice"; "an apple"]), [1; 2]);
103 %!assert (strmatch ("apple", {"apple pie"; "apple juice"; "tomato"}), [1; 2]);
104 %!assert (strmatch ("apple pie", "apple"), []);
105 %!assert (strmatch ("a ", "a"), 1);
106 %!assert (strmatch ("a", "a \0", "exact"), 1);
107 %!assert (strmatch ("a b", {"a b", "a c", "c d"}), 1);
108 %!assert (strmatch ("", {"", "foo", "bar", ""}), [1, 4]);
109 %!assert (strmatch ('', { '', '% comment', 'var a = 5', ''}, 'exact'), [1,4]);
111 %% Test input validation
112 %!error <Invalid call to strmatch> strmatch();
113 %!error <Invalid call to strmatch> strmatch("a");
114 %!error <Invalid call to strmatch> strmatch("a", "aaa", "exact", 1);
115 %!error <S must be a string> strmatch(1, "aaa");
116 %!error <S must be a string> strmatch(char ("a", "bb"), "aaa");
117 %!error <A must be a string> strmatch("a", 1);
118 %!error <A must be a string> strmatch("a", {"hello", [1]});