Mercurial > hg > octave-lyh
annotate scripts/strings/strmatch.m @ 11191:01ddaedd6ad5
Reverse changeset b1f4bdc276b6. Use all lower case for "uniformoutput" option.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Thu, 04 Nov 2010 12:18:08 -0700 |
parents | 0b05b204775b |
children | d7fbb08e28cf |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2000, 2005, 2006, 2007, 2009 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 -*- |
22 ## @deftypefn {Function File} {} strmatch (@var{s}, @var{a}, "exact") | |
6701 | 23 ## Return indices of entries of @var{a} that match the string @var{s}. |
5182 | 24 ## The second argument @var{a} may be a string matrix or a cell array of |
25 ## strings. If the third argument @code{"exact"} is not given, then | |
10055 | 26 ## @var{s} only needs to match @var{a} up to the length of @var{s}. |
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)) |
56 error ("strmatch: first argument must be a string"); | |
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)) |
10055 | 67 idx = find (strncmp (s, A, len)); |
68 if (exact) | |
69 ## 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
|
70 B = cellfun (@strtrimr, A(idx), "uniformoutput", false); |
10055 | 71 idx = idx (strcmp (s, B)); |
5182 | 72 endif |
10055 | 73 elseif (ischar (A)) |
74 [nr, nc] = size (A); | |
75 if (len > nc) | |
76 idx = []; | |
77 else | |
78 match = all (bsxfun (@eq, A(:,1:len), s), 2); | |
79 if (exact) | |
80 AA = A(:,len+1:nc); | |
81 match &= all (AA == "\0" | AA == " ", 2); | |
82 endif | |
83 idx = find (match); | |
5181 | 84 endif |
10055 | 85 else |
86 error ("strmatch: second argument must be a string or cell array of strings"); | |
5181 | 87 endif |
5178 | 88 |
89 endfunction | |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
90 |
10055 | 91 ## Removes nuls and blanks from the end of the array |
92 function s = strtrimr (s) | |
10424 | 93 blnks = s == "\0" | s == " "; |
94 i = find (blnks, 1, "last"); | |
95 if (i && all (blnks(i:end))) | |
96 s = s(1:i-1); | |
10055 | 97 endif |
98 endfunction | |
99 | |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
100 %!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
|
101 %!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
|
102 %!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
|
103 %!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
|
104 %!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
|
105 %! [1; 2]); |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
106 %!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
|
107 %! [1; 2]); |
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 pie", "apple"), []); |
10424 | 109 %!assert (strmatch ("a b", {"a b", "a c", "c d"})); |
110 |