Mercurial > hg > octave-lyh
comparison scripts/strings/findstr.m @ 2275:38fea6d34daf
[project @ 1996-05-24 04:06:52 by jwe]
author | jwe |
---|---|
date | Fri, 24 May 1996 04:08:52 +0000 |
parents | e97fba45f0a3 |
children | 6dedd4e0a82f |
comparison
equal
deleted
inserted
replaced
2274:9750746d7da5 | 2275:38fea6d34daf |
---|---|
14 # | 14 # |
15 # You should have received a copy of the GNU General Public License | 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 | 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. | 17 # Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
18 | 18 |
19 function v = findstr (s, t) | 19 function v = findstr (s, t, overlap) |
20 | 20 |
21 # usage: findstr (s, t) | 21 # usage: findstr (s, t [, overlap]) |
22 # | 22 # |
23 # Returns the vector of all positions in the longer of the two strings | 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. | 24 # S and T where an occurence of the shorter of the two starts. |
25 | 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 | |
26 # Original version by Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at>. | 34 # Original version by Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at>. |
27 | 35 |
28 if (nargin != 2) | 36 if (nargin < 2 || nargin > 3) |
29 usage ("findstr (s, t)"); | 37 usage ("findstr (s, t [, overlap])"); |
38 endif | |
39 | |
40 if (nargin == 2) | |
41 overlap = 1; | |
30 endif | 42 endif |
31 | 43 |
32 if (isstr (s) && isstr (t)) | 44 if (isstr (s) && isstr (t)) |
33 | 45 |
34 # Make S be the longer string. | 46 # Make S be the longer string. |
40 endif | 52 endif |
41 | 53 |
42 s = toascii (s); | 54 s = toascii (s); |
43 t = toascii (t); | 55 t = toascii (t); |
44 | 56 |
45 ind = 1 : length (t); | 57 l_t = length (t); |
46 limit = length (s) - length (t) + 1; | 58 |
59 ind = 1 : l_t; | |
60 limit = length (s) - l_t + 1; | |
47 v = zeros (1, limit); | 61 v = zeros (1, limit); |
48 i = 0; | 62 i = 0; |
49 | 63 |
50 for k = 1 : limit | 64 k = 1; |
65 while (k <= limit) | |
51 if (s (ind + k - 1) == t) | 66 if (s (ind + k - 1) == t) |
52 v (++i) = k; | 67 v (++i) = k; |
68 if (! overlap) | |
69 k = k + l_t - 1; | |
70 endif | |
53 endif | 71 endif |
54 endfor | 72 k++; |
73 endwhile | |
55 | 74 |
56 if (i > 0) | 75 if (i > 0) |
57 v = v (1:i); | 76 v = v (1:i); |
58 else | 77 else |
59 v = []; | 78 v = []; |
60 endif | 79 endif |
61 | 80 |
62 else | 81 else |
63 error ("findstr: both arguments must be strings"); | 82 error ("findstr: expecting first two arguments to be strings"); |
64 endif | 83 endif |
65 | 84 |
66 endfunction | 85 endfunction |