Mercurial > hg > octave-lyh
annotate scripts/strings/findstr.m @ 11523:fd0a3ac60b0e
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 14 Jan 2011 05:47:45 -0500 |
parents | 693e22af08ae |
children | c792872f8942 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 1996-2011 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 | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
24 ## can include overlapping positions (this is the default). For example: |
3426 | 25 ## |
3361 | 26 ## @example |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7411
diff
changeset
|
27 ## @group |
3361 | 28 ## findstr ("ababab", "a") |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7411
diff
changeset
|
29 ## @result{} [1, 3, 5] |
3361 | 30 ## findstr ("abababa", "aba", 0) |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7411
diff
changeset
|
31 ## @result{} [1, 5] |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7411
diff
changeset
|
32 ## @end group |
3361 | 33 ## @end example |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7411
diff
changeset
|
34 ## @seealso{strfind, strmatch, strcmp, strncmp, strcmpi, strncmpi, find} |
3361 | 35 ## @end deftypefn |
2272 | 36 |
3891 | 37 ## Note that this implementation swaps the strings if second one is longer |
38 ## than the first, so try to put the longer one first. | |
39 ## | |
5428 | 40 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at> |
2355 | 41 ## Adapted-By: jwe |
2314 | 42 |
2311 | 43 function v = findstr (s, t, overlap) |
2275 | 44 |
45 if (nargin < 2 || nargin > 3) | |
6046 | 46 print_usage (); |
2275 | 47 endif |
48 | |
5348 | 49 if (all (size (s) > 1) || all (size (t) > 1)) |
50 error ("findstr: arguments must have only one non-singleton dimension"); | |
3891 | 51 endif |
52 | |
2275 | 53 if (nargin == 2) |
54 overlap = 1; | |
2272 | 55 endif |
56 | |
3891 | 57 ## Make S be the longer string. |
58 if (length (s) < length (t)) | |
59 tmp = s; | |
60 s = t; | |
61 t = tmp; | |
62 endif | |
63 | |
64 l_s = length (s); | |
65 l_t = length (t); | |
66 | |
67 if (l_t == 0) | |
4321 | 68 ## zero length target: return empty set |
69 v = []; | |
3891 | 70 |
71 elseif (l_t == 1) | |
72 ## length one target: simple find | |
73 v = find (s == t); | |
74 | |
75 elseif (l_t == 2) | |
76 ## length two target: find first at i and second at i+1 | |
77 v = find (s(1:l_s-1) == t(1) & s(2:l_s) == t(2)); | |
78 | |
79 else | |
80 ## length three or more: match the first three by find then go through | |
81 ## the much smaller list to determine which of them are real matches | |
82 limit = l_s - l_t + 1; | |
83 v = find (s(1:limit) == t(1) | |
10549 | 84 & s(2:limit+1) == t(2) |
85 & s (3:limit+2) == t(3)); | |
3891 | 86 endif |
3759 | 87 |
3891 | 88 ## Need to search the index vector if our find was too short |
89 ## (target length > 3), or if we don't allow overlaps. Note though | |
90 ## that there cannot be any overlaps if the first character in the | |
91 ## target is different from the remaining characters in the target, | |
92 ## so a single character, two different characters, or first character | |
93 ## different from the second two don't need to be searched. | |
94 if (l_t >= 3 || (! overlap && l_t > 1 && any (t(1) == t(2:l_t)))) | |
95 ## force strings to be both row vectors or both column vectors | |
96 if (all (size (s) != size (t))) | |
97 t = t.'; | |
98 endif | |
99 | |
100 ## determine which ones to keep | |
101 keep = zeros (size (v)); | |
102 ind = 0:l_t-1; | |
103 if (overlap) | |
104 for idx = 1:length (v) | |
10549 | 105 keep(idx) = all (s(v(idx) + ind) == t); |
3891 | 106 endfor |
2272 | 107 else |
8506 | 108 ## First possible position for next non-overlapping match. |
109 next = 1; | |
3891 | 110 for idx = 1:length (v) |
10549 | 111 if (v(idx) >= next && s(v(idx) + ind) == t) |
112 keep(idx) = 1; | |
113 ## Skip to the next possible match position. | |
114 next = v(idx) + l_t; | |
115 else | |
116 keep(idx) = 0; | |
117 endif | |
3891 | 118 endfor |
2272 | 119 endif |
3891 | 120 if (! isempty (v)) |
121 v = v(find (keep)); | |
122 endif | |
123 endif | |
5400 | 124 |
125 if (isempty (v)) | |
126 v = []; | |
127 endif | |
128 | |
8506 | 129 ## Always return a column vector, because that's what the old one did. |
3891 | 130 if (rows (v) > 1) |
131 v = v.'; | |
2272 | 132 endif |
133 | |
134 endfunction | |
7411 | 135 |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7411
diff
changeset
|
136 %!assert ((findstr ("abababa", "a") == [1, 3, 5, 7] |
7411 | 137 %! && findstr ("abababa", "aba") == [1, 3, 5] |
138 %! && findstr ("abababa", "aba", 0) == [1, 5])); | |
139 | |
140 %!error findstr (); | |
141 | |
142 %!error findstr ("foo", "bar", 3, 4); | |
143 |