annotate scripts/strings/strfind.m @ 6046:34f96dd5441b

[project @ 2006-10-10 16:10:25 by jwe]
author jwe
date Tue, 10 Oct 2006 16:10:31 +0000
parents 067948dc39bf
children 93c65f2a5668
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5678
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
1 ## Copyright (C) 2004 by Alois Schloegl
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
2 ##
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
3 ## This program is free software; you can redistribute it and/or
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
4 ## modify it under the terms of the GNU General Public License
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
5 ## as published by the Free Software Foundation; either version 2
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
6 ## of the License, or (at your option) any later version.
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
7 ##
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
8 ## This program is distributed in the hope that it will be useful,
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
11 ## GNU General Public License for more details.
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
12 ##
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
13 ## You should have received a copy of the GNU General Public License
5720
067948dc39bf [project @ 2006-03-28 11:25:29 by jwe]
jwe
parents: 5678
diff changeset
14 ## along with Octave; see the file COPYING. If not, write to the Free
067948dc39bf [project @ 2006-03-28 11:25:29 by jwe]
jwe
parents: 5678
diff changeset
15 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
067948dc39bf [project @ 2006-03-28 11:25:29 by jwe]
jwe
parents: 5678
diff changeset
16 ## 02110-1301, USA.
5678
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
17
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
18 ## -*- texinfo -*-
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
19 ## @deftypefn {Function File} {@var{idx} =} strfind (@var{str}, @var{pattern})
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
20 ## @deftypefnx {Function File} {@var{idx} =} strfind (@var{cellstr}, @var{pattern})
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
21 ## Search for @var{pattern} in the string @var{str} and return the
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
22 ## starting index of every such occurrence in the vector @var{idx}.
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
23 ## If there is no such occurrence, or if @var{pattern} is longer
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
24 ## than @var{str}, then @var{idx} is the empty array @code{[]}.
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
25 ##
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
26 ## If the cell array of strings @var{cellstr} is specified instead of the
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
27 ## string @var{str}, then @var{idx} is a cell array of vectors, as specified
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
28 ## above.
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
29 ## @seealso{findstr, strmatch, strcmp, strncmp, strcmpi, strncmpi}
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
30 ## @end deftypefn
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
31
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
32 ## Author: alois schloegl <a.schloegl@ieee.org>
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
33 ## Created: 1 November 2004
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
34 ## Adapted-By: William Poetra Yoga Hadisoeseno <williampoetra@gmail.com>
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
35
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
36 function idx = strfind (text, pattern)
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
37
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
38 if (nargin != 2)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5720
diff changeset
39 print_usage ();
5678
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
40 elseif (! ischar (pattern))
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
41 error ("strfind: pattern must be a string value");
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
42 endif
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
43
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
44 lp = length (pattern);
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
45
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
46 if (ischar (text))
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
47 idx = __strfind_string__ (text, pattern, lp);
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
48 elseif (iscellstr (text))
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
49 idx = cell (size (text));
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
50 for i = 1:(numel (text))
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
51 idx{i} = __strfind_string__ (text{i}, pattern, lp);
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
52 endfor
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
53 else
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
54 error ("strfind: text must be a string or cell array of strings");
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
55 endif
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
56
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
57 ### endfunction
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
58
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
59 function idx = __strfind_string__ (text, pattern, lp)
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
60
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
61 idx = 1:(length (text) - lp + 1);
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
62 k = 0;
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
63 while (k < lp && ! isempty (idx))
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
64 idx = idx(text(idx + k) == pattern(++k));
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
65 endwhile
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
66
52323f13c86b [project @ 2006-03-16 06:45:08 by jwe]
jwe
parents:
diff changeset
67 ### endfunction