Mercurial > hg > octave-nkf
annotate scripts/strings/strfind.m @ 8920:eb63fbe60fab
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sat, 07 Mar 2009 10:41:27 -0500 |
parents | 502e58a0d44f |
children | 58604c45ca74 |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2004, 2006, 2007, 2009 by Alois Schloegl |
5678 | 2 ## |
7016 | 3 ## This file is part of Octave. |
5678 | 4 ## |
7016 | 5 ## Octave is free software; you can redistribute it and/or modify it |
6 ## under the terms of the GNU General Public License as published by | |
7 ## the Free Software Foundation; either version 3 of the License, or (at | |
8 ## your option) any later version. | |
9 ## | |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
5678 | 14 ## |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
5678 | 18 |
19 ## -*- texinfo -*- | |
20 ## @deftypefn {Function File} {@var{idx} =} strfind (@var{str}, @var{pattern}) | |
21 ## @deftypefnx {Function File} {@var{idx} =} strfind (@var{cellstr}, @var{pattern}) | |
22 ## Search for @var{pattern} in the string @var{str} and return the | |
23 ## starting index of every such occurrence in the vector @var{idx}. | |
24 ## If there is no such occurrence, or if @var{pattern} is longer | |
25 ## than @var{str}, then @var{idx} is the empty array @code{[]}. | |
26 ## | |
27 ## If the cell array of strings @var{cellstr} is specified instead of the | |
28 ## string @var{str}, then @var{idx} is a cell array of vectors, as specified | |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
29 ## above. Examples: |
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 ## strfind ("abababa", "aba") |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
34 ## @result{} [1, 3, 5] |
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 ## strfind (@{"abababa", "bebebe", "ab"@}, "aba") |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
37 ## @result{} ans = |
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 ## [1,1] = |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
40 ## |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
41 ## 1 3 5 |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
42 ## |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
43 ## [1,2] = [](1x0) |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
44 ## [1,3] = [](1x0) |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
45 ## @} |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
46 ## @end group |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
47 ## @end example |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
48 ## @seealso{findstr, strmatch, strcmp, strncmp, strcmpi, strncmpi, find} |
5678 | 49 ## @end deftypefn |
50 | |
51 ## Author: alois schloegl <a.schloegl@ieee.org> | |
52 ## Created: 1 November 2004 | |
53 ## Adapted-By: William Poetra Yoga Hadisoeseno <williampoetra@gmail.com> | |
54 | |
55 function idx = strfind (text, pattern) | |
56 | |
57 if (nargin != 2) | |
6046 | 58 print_usage (); |
5678 | 59 elseif (! ischar (pattern)) |
60 error ("strfind: pattern must be a string value"); | |
61 endif | |
62 | |
63 lp = length (pattern); | |
64 | |
65 if (ischar (text)) | |
66 idx = __strfind_string__ (text, pattern, lp); | |
67 elseif (iscellstr (text)) | |
68 idx = cell (size (text)); | |
69 for i = 1:(numel (text)) | |
70 idx{i} = __strfind_string__ (text{i}, pattern, lp); | |
71 endfor | |
72 else | |
73 error ("strfind: text must be a string or cell array of strings"); | |
74 endif | |
75 | |
76 ### endfunction | |
77 | |
78 function idx = __strfind_string__ (text, pattern, lp) | |
79 | |
80 idx = 1:(length (text) - lp + 1); | |
81 k = 0; | |
82 while (k < lp && ! isempty (idx)) | |
83 idx = idx(text(idx + k) == pattern(++k)); | |
84 endwhile | |
85 | |
86 ### endfunction | |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
87 |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
88 %!error <Invalid call to strfind> strfind (); |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
89 %!error <Invalid call to strfind> strfind ("foo", "bar", 1); |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
90 %!error <pattern must be a string value> strfind ("foo", 100); |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
91 %!error <text must be a string or cell array of string> strfind (100, "foo"); |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
92 |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
93 %!assert (strfind ("abababa", "aba"), [1, 3, 5]); |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
94 %!assert (strfind ({"abababa", "bla", "bla"}, "a"), {[1, 3, 5, 7], 3, 3}); |