2313
|
1 ## Copyright (C) 1996 Kurt Hornik |
2325
|
2 ## |
2313
|
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 |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
2313
|
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. |
|
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/>. |
2272
|
18 |
3361
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {} findstr (@var{s}, @var{t}, @var{overlap}) |
|
21 ## Return the vector of all positions in the longer of the two strings |
|
22 ## @var{s} and @var{t} where an occurrence of the shorter of the two starts. |
|
23 ## If the optional argument @var{overlap} is nonzero, the returned vector |
|
24 ## can include overlapping positions (this is the default). For example, |
3426
|
25 ## |
3361
|
26 ## @example |
|
27 ## findstr ("ababab", "a") |
|
28 ## @result{} [ 1, 3, 5 ] |
|
29 ## findstr ("abababa", "aba", 0) |
|
30 ## @result{} [ 1, 5 ] |
|
31 ## @end example |
|
32 ## @end deftypefn |
2272
|
33 |
3891
|
34 ## Note that this implementation swaps the strings if second one is longer |
|
35 ## than the first, so try to put the longer one first. |
|
36 ## |
5428
|
37 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at> |
2355
|
38 ## Adapted-By: jwe |
2314
|
39 |
2311
|
40 function v = findstr (s, t, overlap) |
2275
|
41 |
|
42 if (nargin < 2 || nargin > 3) |
6046
|
43 print_usage (); |
2275
|
44 endif |
|
45 |
5348
|
46 if (all (size (s) > 1) || all (size (t) > 1)) |
|
47 error ("findstr: arguments must have only one non-singleton dimension"); |
3891
|
48 endif |
|
49 |
2275
|
50 if (nargin == 2) |
|
51 overlap = 1; |
2272
|
52 endif |
|
53 |
3891
|
54 ## Make S be the longer string. |
|
55 if (length (s) < length (t)) |
|
56 tmp = s; |
|
57 s = t; |
|
58 t = tmp; |
|
59 endif |
|
60 |
|
61 l_s = length (s); |
|
62 l_t = length (t); |
|
63 |
|
64 if (l_t == 0) |
4321
|
65 ## zero length target: return empty set |
|
66 v = []; |
3891
|
67 |
|
68 elseif (l_t == 1) |
|
69 ## length one target: simple find |
|
70 v = find (s == t); |
|
71 |
|
72 elseif (l_t == 2) |
|
73 ## length two target: find first at i and second at i+1 |
|
74 v = find (s(1:l_s-1) == t(1) & s(2:l_s) == t(2)); |
|
75 |
|
76 else |
|
77 ## length three or more: match the first three by find then go through |
|
78 ## the much smaller list to determine which of them are real matches |
|
79 limit = l_s - l_t + 1; |
|
80 v = find (s(1:limit) == t(1) |
|
81 & s(2:limit+1) == t(2) |
|
82 & s (3:limit+2) == t(3)); |
|
83 endif |
3759
|
84 |
3891
|
85 ## Need to search the index vector if our find was too short |
|
86 ## (target length > 3), or if we don't allow overlaps. Note though |
|
87 ## that there cannot be any overlaps if the first character in the |
|
88 ## target is different from the remaining characters in the target, |
|
89 ## so a single character, two different characters, or first character |
|
90 ## different from the second two don't need to be searched. |
|
91 if (l_t >= 3 || (! overlap && l_t > 1 && any (t(1) == t(2:l_t)))) |
|
92 ## force strings to be both row vectors or both column vectors |
|
93 if (all (size (s) != size (t))) |
|
94 t = t.'; |
|
95 endif |
|
96 |
|
97 ## determine which ones to keep |
|
98 keep = zeros (size (v)); |
|
99 ind = 0:l_t-1; |
|
100 if (overlap) |
|
101 for idx = 1:length (v) |
|
102 keep(idx) = all (s(v(idx) + ind) == t); |
|
103 endfor |
2272
|
104 else |
3891
|
105 next = 1; # first possible position for next non-overlapping match |
|
106 for idx = 1:length (v) |
|
107 if (v(idx) >= next && s(v(idx) + ind) == t) |
|
108 keep(idx) = 1; |
|
109 next = v(idx) + l_t; # skip to the next possible match position |
|
110 else |
|
111 keep(idx) = 0; |
|
112 endif |
|
113 endfor |
2272
|
114 endif |
3891
|
115 if (! isempty (v)) |
|
116 v = v(find (keep)); |
|
117 endif |
|
118 endif |
5400
|
119 |
|
120 if (isempty (v)) |
|
121 v = []; |
|
122 endif |
|
123 |
3891
|
124 ## Always return a column vector, because that's what the old one did |
|
125 if (rows (v) > 1) |
|
126 v = v.'; |
2272
|
127 endif |
|
128 |
|
129 endfunction |