annotate scripts/strings/strfind.m @ 7017:a1dbe9d80eee

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