2272
|
1 # Copyright (C) 1996 John W. Eaton |
|
2 # |
|
3 # This file is part of Octave. |
|
4 # |
|
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 the |
|
7 # Free Software Foundation; either version 2, or (at your option) any |
|
8 # later version. |
|
9 # |
|
10 # Octave is distributed in the hope that it will be useful, but WITHOUT |
|
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 # for more details. |
|
14 # |
|
15 # You should have received a copy of the GNU General Public License |
|
16 # along with Octave; see the file COPYING. If not, write to the Free |
|
17 # Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
18 |
2275
|
19 function v = findstr (s, t, overlap) |
2272
|
20 |
2275
|
21 # usage: findstr (s, t [, overlap]) |
2272
|
22 # |
|
23 # Returns the vector of all positions in the longer of the two strings |
|
24 # S and T where an occurence of the shorter of the two starts. |
2275
|
25 # |
|
26 # If the optional argument OVERLAP is nonzero, the returned vector |
|
27 # can include overlapping positions (this is the default). |
|
28 # |
|
29 # For example, |
|
30 # |
|
31 # findstr ("abababa", "aba") => [1, 3, 5] |
|
32 # findstr ("abababa", "aba", 0) => [1, 5] |
|
33 |
2272
|
34 # Original version by Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at>. |
|
35 |
2275
|
36 if (nargin < 2 || nargin > 3) |
|
37 usage ("findstr (s, t [, overlap])"); |
|
38 endif |
|
39 |
|
40 if (nargin == 2) |
|
41 overlap = 1; |
2272
|
42 endif |
|
43 |
|
44 if (isstr (s) && isstr (t)) |
|
45 |
|
46 # Make S be the longer string. |
|
47 |
|
48 if (length (s) < length (t)) |
|
49 tmp = s; |
|
50 s = t; |
|
51 t = tmp; |
|
52 endif |
|
53 |
|
54 s = toascii (s); |
|
55 t = toascii (t); |
|
56 |
2275
|
57 l_t = length (t); |
|
58 |
|
59 ind = 1 : l_t; |
|
60 limit = length (s) - l_t + 1; |
2272
|
61 v = zeros (1, limit); |
|
62 i = 0; |
|
63 |
2275
|
64 k = 1; |
|
65 while (k <= limit) |
2272
|
66 if (s (ind + k - 1) == t) |
|
67 v (++i) = k; |
2275
|
68 if (! overlap) |
|
69 k = k + l_t - 1; |
|
70 endif |
2272
|
71 endif |
2275
|
72 k++; |
|
73 endwhile |
2272
|
74 |
|
75 if (i > 0) |
|
76 v = v (1:i); |
|
77 else |
|
78 v = []; |
|
79 endif |
|
80 |
|
81 else |
2275
|
82 error ("findstr: expecting first two arguments to be strings"); |
2272
|
83 endif |
|
84 |
|
85 endfunction |