Mercurial > hg > octave-lyh
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 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 2000-2011 Paul Kienzle |
5178 | 2 ## Copyright (C) 2003 Alois Schloegl |
10055 | 3 ## Copyright (C) 2010 VZLU Prague |
5178 | 4 ## |
5181 | 5 ## This file is part of Octave. |
5178 | 6 ## |
5181 | 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 | |
7016 | 9 ## the Free Software Foundation; either version 3 of the License, or (at |
10 ## your option) any later version. | |
5181 | 11 ## |
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. | |
5178 | 16 ## |
17 ## You should have received a copy of the GNU General Public License | |
7016 | 18 ## along with Octave; see the file COPYING. If not, see |
19 ## <http://www.gnu.org/licenses/>. | |
5178 | 20 |
5182 | 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 | 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 | 27 ## Trailing whitespace is ignored. |
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 | 44 ## @end deftypefn |
5181 | 45 |
46 ## Author: Paul Kienzle, Alois Schloegl | |
47 ## Adapted-by: jwe | |
48 | |
49 function idx = strmatch (s, A, exact) | |
50 | |
5178 | 51 if (nargin < 2 || nargin > 3) |
6046 | 52 print_usage (); |
5178 | 53 endif |
54 | |
10055 | 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 | 57 endif |
58 | |
59 ## Truncate trailing whitespace. | |
60 s = strtrimr (s); | |
61 | |
62 len = length (s); | |
63 | |
64 exact = nargin == 3 && ischar (exact) && strcmp (exact, "exact"); | |
65 | |
5181 | 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 | 72 if (exact) |
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 | 75 idx = idx (strcmp (s, B)); |
5182 | 76 endif |
10055 | 77 elseif (ischar (A)) |
78 [nr, nc] = size (A); | |
79 if (len > nc) | |
80 idx = []; | |
81 else | |
82 match = all (bsxfun (@eq, A(:,1:len), s), 2); | |
83 if (exact) | |
84 AA = A(:,len+1:nc); | |
85 match &= all (AA == "\0" | AA == " ", 2); | |
86 endif | |
87 idx = find (match); | |
5181 | 88 endif |
10055 | 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 | 91 endif |
5178 | 92 |
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 | 95 ## Removes nuls and blanks from the end of the array |
96 function s = strtrimr (s) | |
10424 | 97 blnks = s == "\0" | s == " "; |
98 i = find (blnks, 1, "last"); | |
99 if (i && all (blnks(i:end))) | |
100 s = s(1:i-1); | |
10055 | 101 endif |
102 endfunction | |
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 | 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]) |