annotate scripts/strings/strmatch.m @ 20139:bcf0a288aa6c stable

maint: Merge default to stable in preparation for 4.0.0 release.
author John W. Eaton <jwe@octave.org>
date Tue, 28 Apr 2015 12:12:16 -0400
parents d63878346099 55c2d33e6773
children df437a52bcaf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19956
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
1 ## Copyright (C) 2000-2013 Paul Kienzle
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
2 ## Copyright (C) 2003 Alois Schloegl
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
3 ## Copyright (C) 2010 VZLU Prague
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
4 ##
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
5 ## This file is part of Octave.
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
6 ##
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
7 ## Octave is free software; you can redistribute it and/or modify it
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
8 ## under the terms of the GNU General Public License as published by
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
9 ## the Free Software Foundation; either version 3 of the License, or (at
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
10 ## your option) any later version.
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
11 ##
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
12 ## Octave is distributed in the hope that it will be useful, but
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
13 ## WITHOUT ANY WARRANTY; without even the implied warranty of
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
15 ## General Public License for more details.
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
16 ##
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
17 ## You should have received a copy of the GNU General Public License
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
18 ## along with Octave; see the file COPYING. If not, see
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
19 ## <http://www.gnu.org/licenses/>.
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
20
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
21 ## -*- texinfo -*-
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
22 ## @deftypefn {Function File} {} strmatch (@var{s}, @var{A})
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
23 ## @deftypefnx {Function File} {} strmatch (@var{s}, @var{A}, "exact")
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
24 ## Return indices of entries of @var{A} which begin with the string @var{s}.
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
25 ## The second argument @var{A} must be a string, character matrix, or a cell
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
26 ## array of strings. If the third argument @qcode{"exact"} is not given, then
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
27 ## @var{s} only needs to match @var{A} up to the length of @var{s}.
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
28 ## Trailing spaces and nulls in @var{s} and @var{A} are ignored when matching.
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
29 ##
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
30 ## For example:
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
31 ##
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
32 ## @example
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
33 ## @group
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
34 ## strmatch ("apple", "apple juice")
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
35 ## @result{} 1
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
36 ##
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
37 ## strmatch ("apple", ["apple "; "apple juice"; "an apple"])
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
38 ## @result{} [1; 2]
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
39 ##
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
40 ## strmatch ("apple", ["apple "; "apple juice"; "an apple"], "exact")
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
41 ## @result{} [1]
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
42 ## @end group
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
43 ## @end example
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
44 ##
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
45 ## @strong{Caution:} @code{strmatch} is scheduled for deprecation. Use
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
46 ## @code{strncmp} (normal case), or @code{strcmp} (@qcode{"exact"} case), or
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
47 ## @code{regexp} in all new code.
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
48 ## @seealso{strfind, findstr, strcmp, strncmp, strcmpi, strncmpi, find}
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
49 ## @end deftypefn
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
50
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
51 ## Author: Paul Kienzle, Alois Schloegl
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
52 ## Adapted-by: jwe
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
53
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
54 function idx = strmatch (s, A, exact)
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
55
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
56 if (nargin < 2 || nargin > 3)
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
57 print_usage ();
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
58 endif
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
59
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
60 if (! ischar (s) || (! isempty (s) && ! isvector (s)))
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
61 error ("strmatch: S must be a string");
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
62 elseif (! (ischar (A) || iscellstr (A)))
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
63 error ("strmatch: A must be a string or cell array of strings");
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
64 endif
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
65
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
66 ## Trim blanks and nulls from search string
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
67 s = regexprep (s, "[ \\0]+$", '');
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
68 len = length (s);
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
69
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
70 exact = nargin == 3 && ischar (exact) && strcmp (exact, "exact");
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
71
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
72 if (ischar (A))
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
73 [nr, nc] = size (A);
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
74 if (len > nc)
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
75 idx = [];
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
76 else
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
77 match = all (bsxfun (@eq, A(:,1:len), s), 2);
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
78 if (exact)
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
79 AA = A(:,len+1:nc);
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
80 match &= all (AA == " " | AA == "\0", 2);
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
81 endif
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
82 idx = find (match);
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
83 endif
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
84 else
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
85 if (len > 0)
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
86 idx = find (strncmp (s, A, len));
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
87 else
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
88 idx = find (strcmp (s, A));
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
89 endif
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
90 if (exact)
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
91 ## We can't just use strcmp, because we need to ignore spaces at end.
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
92 B = regexprep (A(idx), "[ \\0]+$", '');
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
93 idx = idx(strcmp (s, B));
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
94 endif
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
95 endif
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
96
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
97 endfunction
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
98
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
99
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
100 %!assert (strmatch ("a", {"aaa", "bab", "bbb"}), 1)
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
101 %!assert (strmatch ("apple", "apple juice"), 1)
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
102 %!assert (strmatch ("apple", ["apple pie"; "apple juice"; "an apple"]), [1; 2])
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
103 %!assert (strmatch ("apple", {"apple pie"; "apple juice"; "tomato"}), [1; 2])
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
104 %!assert (strmatch ("apple pie", "apple"), [])
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
105 %!assert (strmatch ("a ", "a"), 1)
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
106 %!assert (strmatch ("a", "a \0", "exact"), 1)
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
107 %!assert (strmatch ("a b", {"a b", "a c", "c d"}), 1)
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
108 %!assert (strmatch ("", {"", "foo", "bar", ""}), [1, 4])
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
109 %!assert (strmatch ('', { '', '% comment', 'var a = 5', ''}, 'exact'), [1,4])
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
110
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
111 %% Test input validation
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
112 %!error <Invalid call to strmatch> strmatch ()
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
113 %!error <Invalid call to strmatch> strmatch ("a")
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
114 %!error <Invalid call to strmatch> strmatch ("a", "aaa", "exact", 1)
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
115 %!error <S must be a string> strmatch (1, "aaa")
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
116 %!error <S must be a string> strmatch (char ("a", "bb"), "aaa")
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
117 %!error <A must be a string> strmatch ("a", 1)
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
118 %!error <A must be a string> strmatch ("a", {"hello", [1]})
55c2d33e6773 restore strmatch function; backout changeset edf5d63c82e1
John W. Eaton <jwe@octave.org>
parents:
diff changeset
119