Mercurial > hg > octave-nkf
annotate scripts/strings/findstr.m @ 20706:1339ad3c9faa stable
Fix typo in str2func docstring.
* ov-fcn-handle.cc (Ffunc2str): Fix typo where 'a' should be '@'.
author | Rik <rik@octave.org> |
---|---|
date | Tue, 22 Sep 2015 02:06:17 -0700 |
parents | df437a52bcaf |
children |
rev | line source |
---|---|
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19254
diff
changeset
|
1 ## Copyright (C) 1996-2015 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 -*- |
13177
17b702fae303
findstr.m: Use more modern code practices in function.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
20 ## @deftypefn {Function File} {} findstr (@var{s}, @var{t}) |
17b702fae303
findstr.m: Use more modern code practices in function.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
21 ## @deftypefnx {Function File} {} findstr (@var{s}, @var{t}, @var{overlap}) |
20374
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20166
diff
changeset
|
22 ## Return the vector of all positions in the longer of the two strings @var{s} |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20166
diff
changeset
|
23 ## and @var{t} where an occurrence of the shorter of the two starts. |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20166
diff
changeset
|
24 ## |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20166
diff
changeset
|
25 ## If the optional argument @var{overlap} is true (default), the returned |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20166
diff
changeset
|
26 ## vector can include overlapping positions. For example: |
3426 | 27 ## |
3361 | 28 ## @example |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7411
diff
changeset
|
29 ## @group |
3361 | 30 ## findstr ("ababab", "a") |
13177
17b702fae303
findstr.m: Use more modern code practices in function.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
31 ## @result{} [1, 3, 5]; |
3361 | 32 ## 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
|
33 ## @result{} [1, 5] |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7411
diff
changeset
|
34 ## @end group |
3361 | 35 ## @end example |
13177
17b702fae303
findstr.m: Use more modern code practices in function.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
36 ## |
17b702fae303
findstr.m: Use more modern code practices in function.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
37 ## @strong{Caution:} @code{findstr} is scheduled for deprecation. Use |
17b702fae303
findstr.m: Use more modern code practices in function.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
38 ## @code{strfind} in all new code. |
20166
e78c0514523d
restore strmatch function; backout changeset f9959972949a
John W. Eaton <jwe@octave.org>
parents:
20038
diff
changeset
|
39 ## @seealso{strfind, strmatch, strcmp, strncmp, strcmpi, strncmpi, find} |
3361 | 40 ## @end deftypefn |
2272 | 41 |
3891 | 42 ## Note that this implementation swaps the strings if second one is longer |
43 ## than the first, so try to put the longer one first. | |
44 ## | |
5428 | 45 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at> |
2355 | 46 ## Adapted-By: jwe |
2314 | 47 |
13177
17b702fae303
findstr.m: Use more modern code practices in function.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
48 function v = findstr (s, t, overlap = true) |
2275 | 49 |
50 if (nargin < 2 || nargin > 3) | |
6046 | 51 print_usage (); |
2275 | 52 endif |
53 | |
5348 | 54 if (all (size (s) > 1) || all (size (t) > 1)) |
55 error ("findstr: arguments must have only one non-singleton dimension"); | |
3891 | 56 endif |
57 | |
58 ## Make S be the longer string. | |
59 if (length (s) < length (t)) | |
13177
17b702fae303
findstr.m: Use more modern code practices in function.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
60 [s, t] = deal (t, s); |
3891 | 61 endif |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
62 |
3891 | 63 l_s = length (s); |
64 l_t = length (t); | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
65 |
3891 | 66 if (l_t == 0) |
4321 | 67 ## zero length target: return empty set |
68 v = []; | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
69 |
3891 | 70 elseif (l_t == 1) |
71 ## length one target: simple find | |
72 v = find (s == t); | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
73 |
3891 | 74 elseif (l_t == 2) |
75 ## length two target: find first at i and second at i+1 | |
76 v = find (s(1:l_s-1) == t(1) & s(2:l_s) == t(2)); | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
77 |
3891 | 78 else |
79 ## length three or more: match the first three by find then go through | |
80 ## the much smaller list to determine which of them are real matches | |
81 limit = l_s - l_t + 1; | |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
82 v = find ( s(1:limit) == t(1) |
10549 | 83 & s(2:limit+1) == t(2) |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
84 & s(3:limit+2) == t(3)); |
3891 | 85 endif |
3759 | 86 |
3891 | 87 ## Need to search the index vector if our find was too short |
88 ## (target length > 3), or if we don't allow overlaps. Note though | |
89 ## that there cannot be any overlaps if the first character in the | |
90 ## target is different from the remaining characters in the target, | |
91 ## so a single character, two different characters, or first character | |
92 ## different from the second two don't need to be searched. | |
93 if (l_t >= 3 || (! overlap && l_t > 1 && any (t(1) == t(2:l_t)))) | |
94 ## force strings to be both row vectors or both column vectors | |
95 if (all (size (s) != size (t))) | |
96 t = t.'; | |
97 endif | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
98 |
3891 | 99 ## determine which ones to keep |
100 keep = zeros (size (v)); | |
101 ind = 0:l_t-1; | |
102 if (overlap) | |
103 for idx = 1:length (v) | |
10549 | 104 keep(idx) = all (s(v(idx) + ind) == t); |
3891 | 105 endfor |
2272 | 106 else |
8506 | 107 ## First possible position for next non-overlapping match. |
108 next = 1; | |
3891 | 109 for idx = 1:length (v) |
10549 | 110 if (v(idx) >= next && s(v(idx) + ind) == t) |
111 keep(idx) = 1; | |
112 ## Skip to the next possible match position. | |
113 next = v(idx) + l_t; | |
114 else | |
115 keep(idx) = 0; | |
116 endif | |
3891 | 117 endfor |
2272 | 118 endif |
3891 | 119 if (! isempty (v)) |
120 v = v(find (keep)); | |
121 endif | |
122 endif | |
5400 | 123 |
124 if (isempty (v)) | |
125 v = []; | |
126 endif | |
127 | |
13177
17b702fae303
findstr.m: Use more modern code practices in function.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
128 ## Always return a row vector, because that's what the old one did. |
17b702fae303
findstr.m: Use more modern code practices in function.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
129 if (iscolumn (v)) |
3891 | 130 v = v.'; |
2272 | 131 endif |
132 | |
133 endfunction | |
7411 | 134 |
13177
17b702fae303
findstr.m: Use more modern code practices in function.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
135 |
17b702fae303
findstr.m: Use more modern code practices in function.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
136 %!assert (findstr ("abababa", "a"), [1, 3, 5, 7]) |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
137 %!assert (findstr ("abababa", "aba"), [1, 3, 5]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
138 %!assert (findstr ("aba", "abababa", 0), [1, 5]) |
7411 | 139 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
140 ## Test input validation |
13177
17b702fae303
findstr.m: Use more modern code practices in function.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
141 %!error findstr () |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
142 %!error findstr ("foo", "bar", 3, 4) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
143 %!error <must have only one non-singleton dimension> findstr (["AB" ; "CD"], "C") |
7411 | 144 |