annotate scripts/strings/strmatch.m @ 11523:fd0a3ac60b0e

update copyright notices
author John W. Eaton <jwe@octave.org>
date Fri, 14 Jan 2011 05:47:45 -0500
parents 1740012184f9
children c792872f8942
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11523
fd0a3ac60b0e update copyright notices
John W. Eaton <jwe@octave.org>
parents: 11472
diff changeset
1 ## Copyright (C) 2000-2011 Paul Kienzle
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
2 ## Copyright (C) 2003 Alois Schloegl
10055
38600f8cba83 optimize strmatch
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
3 ## Copyright (C) 2010 VZLU Prague
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
4 ##
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
5 ## This file is part of Octave.
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
6 ##
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
7 ## Octave is free software; you can redistribute it and/or modify it
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
8 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6701
diff changeset
9 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6701
diff changeset
10 ## your option) any later version.
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
11 ##
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
12 ## Octave is distributed in the hope that it will be useful, but
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
13 ## WITHOUT ANY WARRANTY; without even the implied warranty of
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
15 ## General Public License for more details.
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
16 ##
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
17 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6701
diff changeset
18 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6701
diff changeset
19 ## <http://www.gnu.org/licenses/>.
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
20
5182
5b361aa47dff [project @ 2005-03-03 06:21:47 by jwe]
jwe
parents: 5181
diff changeset
21 ## -*- texinfo -*-
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 11354
diff changeset
22 ## @deftypefn {Function File} {} strmatch (@var{s}, @var{A}, "exact")
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 11354
diff changeset
23 ## Return indices of entries of @var{A} that match the string @var{s}.
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 11354
diff changeset
24 ## The second argument @var{A} may be a string matrix or a cell array of
5182
5b361aa47dff [project @ 2005-03-03 06:21:47 by jwe]
jwe
parents: 5181
diff changeset
25 ## strings. If the third argument @code{"exact"} is not given, then
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 11354
diff changeset
26 ## @var{s} only needs to match @var{A} up to the length of @var{s}.
10055
38600f8cba83 optimize strmatch
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
27 ## Trailing whitespace is ignored.
38600f8cba83 optimize strmatch
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
28 ## Results are returned as a column vector.
8442
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
29 ## For example:
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
30 ##
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
31 ## @example
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
32 ## @group
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
33 ## strmatch ("apple", "apple juice")
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
34 ## @result{} 1
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
35 ##
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
36 ## strmatch ("apple", ["apple pie"; "apple juice"; "an apple"])
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
37 ## @result{} [1; 2]
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
38 ##
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
39 ## strmatch ("apple", @{"apple pie"; "apple juice"; "tomato"@})
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
40 ## @result{} [1; 2]
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
41 ## @end group
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
42 ## @end example
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
43 ## @seealso{strfind, findstr, strcmp, strncmp, strcmpi, strncmpi, find}
5182
5b361aa47dff [project @ 2005-03-03 06:21:47 by jwe]
jwe
parents: 5181
diff changeset
44 ## @end deftypefn
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
45
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
46 ## Author: Paul Kienzle, Alois Schloegl
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
47 ## Adapted-by: jwe
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
48
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
49 function idx = strmatch (s, A, exact)
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
50
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
51 if (nargin < 2 || nargin > 3)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5307
diff changeset
52 print_usage ();
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
53 endif
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
54
10055
38600f8cba83 optimize strmatch
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
55 if (! ischar (s))
11472
1740012184f9 Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents: 11469
diff changeset
56 error ("strmatch: S must be a string");
10055
38600f8cba83 optimize strmatch
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
57 endif
38600f8cba83 optimize strmatch
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
58
38600f8cba83 optimize strmatch
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
59 ## Truncate trailing whitespace.
38600f8cba83 optimize strmatch
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
60 s = strtrimr (s);
38600f8cba83 optimize strmatch
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
61
38600f8cba83 optimize strmatch
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
62 len = length (s);
38600f8cba83 optimize strmatch
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
63
38600f8cba83 optimize strmatch
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
64 exact = nargin == 3 && ischar (exact) && strcmp (exact, "exact");
38600f8cba83 optimize strmatch
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
65
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
66 if (iscell (A))
11323
d7fbb08e28cf strmatch.m: avoid passing length of 0 to strncmp
John W. Eaton <jwe@octave.org>
parents: 11191
diff changeset
67 if (len > 0)
d7fbb08e28cf strmatch.m: avoid passing length of 0 to strncmp
John W. Eaton <jwe@octave.org>
parents: 11191
diff changeset
68 idx = find (strncmp (s, A, len));
d7fbb08e28cf strmatch.m: avoid passing length of 0 to strncmp
John W. Eaton <jwe@octave.org>
parents: 11191
diff changeset
69 else
d7fbb08e28cf strmatch.m: avoid passing length of 0 to strncmp
John W. Eaton <jwe@octave.org>
parents: 11191
diff changeset
70 idx = find (strcmp (s, A));
d7fbb08e28cf strmatch.m: avoid passing length of 0 to strncmp
John W. Eaton <jwe@octave.org>
parents: 11191
diff changeset
71 endif
10055
38600f8cba83 optimize strmatch
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
72 if (exact)
38600f8cba83 optimize strmatch
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
73 ## We can't just use strcmp, because we need to ignore whitespace.
11191
01ddaedd6ad5 Reverse changeset b1f4bdc276b6. Use all lower case for "uniformoutput" option.
Rik <octave@nomad.inbox5.com>
parents: 10424
diff changeset
74 B = cellfun (@strtrimr, A(idx), "uniformoutput", false);
10055
38600f8cba83 optimize strmatch
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
75 idx = idx (strcmp (s, B));
5182
5b361aa47dff [project @ 2005-03-03 06:21:47 by jwe]
jwe
parents: 5181
diff changeset
76 endif
10055
38600f8cba83 optimize strmatch
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
77 elseif (ischar (A))
38600f8cba83 optimize strmatch
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
78 [nr, nc] = size (A);
38600f8cba83 optimize strmatch
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
79 if (len > nc)
38600f8cba83 optimize strmatch
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
80 idx = [];
38600f8cba83 optimize strmatch
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
81 else
38600f8cba83 optimize strmatch
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
82 match = all (bsxfun (@eq, A(:,1:len), s), 2);
38600f8cba83 optimize strmatch
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
83 if (exact)
38600f8cba83 optimize strmatch
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
84 AA = A(:,len+1:nc);
38600f8cba83 optimize strmatch
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
85 match &= all (AA == "\0" | AA == " ", 2);
38600f8cba83 optimize strmatch
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
86 endif
38600f8cba83 optimize strmatch
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
87 idx = find (match);
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
88 endif
10055
38600f8cba83 optimize strmatch
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
89 else
11472
1740012184f9 Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents: 11469
diff changeset
90 error ("strmatch: A must be a string or cell array of strings");
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
91 endif
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
92
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
93 endfunction
8442
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
94
10055
38600f8cba83 optimize strmatch
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
95 ## Removes nuls and blanks from the end of the array
38600f8cba83 optimize strmatch
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
96 function s = strtrimr (s)
10424
0b05b204775b fix strmatch
Jaroslav Hajek <highegg@gmail.com>
parents: 10055
diff changeset
97 blnks = s == "\0" | s == " ";
0b05b204775b fix strmatch
Jaroslav Hajek <highegg@gmail.com>
parents: 10055
diff changeset
98 i = find (blnks, 1, "last");
0b05b204775b fix strmatch
Jaroslav Hajek <highegg@gmail.com>
parents: 10055
diff changeset
99 if (i && all (blnks(i:end)))
0b05b204775b fix strmatch
Jaroslav Hajek <highegg@gmail.com>
parents: 10055
diff changeset
100 s = s(1:i-1);
10055
38600f8cba83 optimize strmatch
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
101 endif
38600f8cba83 optimize strmatch
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
102 endfunction
38600f8cba83 optimize strmatch
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
103
8442
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
104 %!error <Invalid call to strmatch> strmatch();
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
105 %!error <Invalid call to strmatch> strmatch("a", "aaa", "exact", 1);
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
106 %!assert (strmatch("a", {"aaa", "bab", "bbb"}), 1);
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
107 %!assert (strmatch ("apple", "apple juice"), 1);
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
108 %!assert (strmatch ("apple", ["apple pie"; "apple juice"; "an apple"]),
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
109 %! [1; 2]);
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
110 %!assert (strmatch ("apple", {"apple pie"; "apple juice"; "tomato"}),
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
111 %! [1; 2]);
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
112 %!assert (strmatch ("apple pie", "apple"), []);
10424
0b05b204775b fix strmatch
Jaroslav Hajek <highegg@gmail.com>
parents: 10055
diff changeset
113 %!assert (strmatch ("a b", {"a b", "a c", "c d"}));
11323
d7fbb08e28cf strmatch.m: avoid passing length of 0 to strncmp
John W. Eaton <jwe@octave.org>
parents: 11191
diff changeset
114 %!assert (strmatch ("", {"", "foo", "bar", ""}), [1, 4])
11354
7bb759d617e2 strmatch.m: Add test with null search pattern.
Rik <octave@nomad.inbox5.com>
parents: 11323
diff changeset
115 %!assert (strmatch ('', { '', '% comment line', 'var a = 5', ''}, 'exact'), [1,4])