Mercurial > hg > octave-nkf
annotate scripts/strings/index.m @ 15063:36cbcc37fdb8
Refactor configure.ac to make it more understandable.
Use common syntax for messages in config.h
Correct typos, refer to libraries in all caps, use two spaces after period.
Follow Autoconf guidelines and place general tests before specific tests.
* configure.ac, m4/acinclude.m4: Use common syntax for messages in config.h
Correct typos, refer to libraries in all caps, use two spaces after period.
Follow Autoconf guidelines and place general tests before specific tests.
author | Rik <rik@octave.org> |
---|---|
date | Tue, 31 Jul 2012 10:28:51 -0700 |
parents | cbcaf5602469 |
children | bc924baa2c4e |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
13289
diff
changeset
|
1 ## Copyright (C) 1996-2012 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/>. | |
2271 | 18 |
3361 | 19 ## -*- texinfo -*- |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10409
diff
changeset
|
20 ## @deftypefn {Function File} {} index (@var{s}, @var{t}) |
6139 | 21 ## @deftypefnx {Function File} {} index (@var{s}, @var{t}, @var{direction}) |
3361 | 22 ## Return the position of the first occurrence of the string @var{t} in the |
13289
1a6537dbce7b
Expand index,rindex functions to accept char array inputs
Rik <octave@nomad.inbox5.com>
parents:
12931
diff
changeset
|
23 ## string @var{s}, or 0 if no occurrence is found. @var{s} may also be a |
1a6537dbce7b
Expand index,rindex functions to accept char array inputs
Rik <octave@nomad.inbox5.com>
parents:
12931
diff
changeset
|
24 ## string array or cell array of strings. |
1a6537dbce7b
Expand index,rindex functions to accept char array inputs
Rik <octave@nomad.inbox5.com>
parents:
12931
diff
changeset
|
25 ## |
1a6537dbce7b
Expand index,rindex functions to accept char array inputs
Rik <octave@nomad.inbox5.com>
parents:
12931
diff
changeset
|
26 ## For example: |
3426 | 27 ## |
3361 | 28 ## @example |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
29 ## @group |
3361 | 30 ## index ("Teststring", "t") |
14402
cbcaf5602469
doc: Make spacing nicer for Strings chapter of manual.
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
31 ## @result{} 4 |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
32 ## @end group |
3361 | 33 ## @end example |
3426 | 34 ## |
6139 | 35 ## If @var{direction} is @samp{"first"}, return the first element found. |
36 ## If @var{direction} is @samp{"last"}, return the last element found. | |
37 ## | |
38 ## @seealso{find, rindex} | |
3361 | 39 ## @end deftypefn |
2271 | 40 |
5428 | 41 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at> |
2355 | 42 ## Adapted-By: jwe |
13289
1a6537dbce7b
Expand index,rindex functions to accept char array inputs
Rik <octave@nomad.inbox5.com>
parents:
12931
diff
changeset
|
43 ## This is patterned after the AWK function of the same name. |
2314 | 44 |
13289
1a6537dbce7b
Expand index,rindex functions to accept char array inputs
Rik <octave@nomad.inbox5.com>
parents:
12931
diff
changeset
|
45 function n = index (s, t, direction = "first") |
2271 | 46 |
6139 | 47 if (nargin < 2 || nargin > 3) |
6046 | 48 print_usage (); |
2271 | 49 endif |
13289
1a6537dbce7b
Expand index,rindex functions to accept char array inputs
Rik <octave@nomad.inbox5.com>
parents:
12931
diff
changeset
|
50 |
1a6537dbce7b
Expand index,rindex functions to accept char array inputs
Rik <octave@nomad.inbox5.com>
parents:
12931
diff
changeset
|
51 if (ischar (s)) |
1a6537dbce7b
Expand index,rindex functions to accept char array inputs
Rik <octave@nomad.inbox5.com>
parents:
12931
diff
changeset
|
52 if (! isrow (s)) |
1a6537dbce7b
Expand index,rindex functions to accept char array inputs
Rik <octave@nomad.inbox5.com>
parents:
12931
diff
changeset
|
53 s = cellstr (s); # Handle string arrays by conversion to cellstr |
1a6537dbce7b
Expand index,rindex functions to accept char array inputs
Rik <octave@nomad.inbox5.com>
parents:
12931
diff
changeset
|
54 endif |
1a6537dbce7b
Expand index,rindex functions to accept char array inputs
Rik <octave@nomad.inbox5.com>
parents:
12931
diff
changeset
|
55 elseif (! iscellstr (s)) |
1a6537dbce7b
Expand index,rindex functions to accept char array inputs
Rik <octave@nomad.inbox5.com>
parents:
12931
diff
changeset
|
56 error ("index: S must be a string, string array, or cellstr"); |
1a6537dbce7b
Expand index,rindex functions to accept char array inputs
Rik <octave@nomad.inbox5.com>
parents:
12931
diff
changeset
|
57 endif |
6139 | 58 |
10409
a87afd063e7d
optimize index (call strfind)
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
59 f = strfind (s, t); |
13289
1a6537dbce7b
Expand index,rindex functions to accept char array inputs
Rik <octave@nomad.inbox5.com>
parents:
12931
diff
changeset
|
60 if (isempty (f)) |
1a6537dbce7b
Expand index,rindex functions to accept char array inputs
Rik <octave@nomad.inbox5.com>
parents:
12931
diff
changeset
|
61 f = 0; |
1a6537dbce7b
Expand index,rindex functions to accept char array inputs
Rik <octave@nomad.inbox5.com>
parents:
12931
diff
changeset
|
62 elseif (iscell (f)) |
10409
a87afd063e7d
optimize index (call strfind)
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
63 f(cellfun ("isempty", f)) = {0}; |
2271 | 64 endif |
2325 | 65 |
13289
1a6537dbce7b
Expand index,rindex functions to accept char array inputs
Rik <octave@nomad.inbox5.com>
parents:
12931
diff
changeset
|
66 direction = tolower (direction); |
1a6537dbce7b
Expand index,rindex functions to accept char array inputs
Rik <octave@nomad.inbox5.com>
parents:
12931
diff
changeset
|
67 |
1a6537dbce7b
Expand index,rindex functions to accept char array inputs
Rik <octave@nomad.inbox5.com>
parents:
12931
diff
changeset
|
68 if (strcmp (direction, "first")) |
10409
a87afd063e7d
optimize index (call strfind)
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
69 if (iscell (f)) |
12931
cefd568ea073
Replace function handles with function names in cellfun calls for 15% speedup.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
70 n = cellfun ("min", f); |
10409
a87afd063e7d
optimize index (call strfind)
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
71 else |
13289
1a6537dbce7b
Expand index,rindex functions to accept char array inputs
Rik <octave@nomad.inbox5.com>
parents:
12931
diff
changeset
|
72 n = f(1); |
10409
a87afd063e7d
optimize index (call strfind)
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
73 endif |
13289
1a6537dbce7b
Expand index,rindex functions to accept char array inputs
Rik <octave@nomad.inbox5.com>
parents:
12931
diff
changeset
|
74 elseif (strcmp (direction, "last")) |
10409
a87afd063e7d
optimize index (call strfind)
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
75 if (iscell (f)) |
12931
cefd568ea073
Replace function handles with function names in cellfun calls for 15% speedup.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
76 n = cellfun ("max", f); |
10409
a87afd063e7d
optimize index (call strfind)
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
77 else |
13289
1a6537dbce7b
Expand index,rindex functions to accept char array inputs
Rik <octave@nomad.inbox5.com>
parents:
12931
diff
changeset
|
78 n = f(end); |
10409
a87afd063e7d
optimize index (call strfind)
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
79 endif |
3911 | 80 else |
13289
1a6537dbce7b
Expand index,rindex functions to accept char array inputs
Rik <octave@nomad.inbox5.com>
parents:
12931
diff
changeset
|
81 error ('index: DIRECTION must be either "first" or "last"'); |
3911 | 82 endif |
13289
1a6537dbce7b
Expand index,rindex functions to accept char array inputs
Rik <octave@nomad.inbox5.com>
parents:
12931
diff
changeset
|
83 |
2271 | 84 endfunction |
6139 | 85 |
13289
1a6537dbce7b
Expand index,rindex functions to accept char array inputs
Rik <octave@nomad.inbox5.com>
parents:
12931
diff
changeset
|
86 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
87 %!assert (index ("foobarbaz", "b"), 4) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
88 %!assert (index ("foobarbaz", "z"), 9) |
13289
1a6537dbce7b
Expand index,rindex functions to accept char array inputs
Rik <octave@nomad.inbox5.com>
parents:
12931
diff
changeset
|
89 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
90 %!assert (index ("astringbstringcstring", "s"), 2) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
91 %!assert (index ("astringbstringcstring", "st"), 2) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
92 %!assert (index ("astringbstringcstring", "str"), 2) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
93 %!assert (index ("astringbstringcstring", "string"), 2) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
94 %!assert (index ("abc---", "abc+++"), 0) |
6139 | 95 |
96 ## test everything out in reverse | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
97 %!assert (index ("astringbstringcstring", "s", "last"), 16) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
98 %!assert (index ("astringbstringcstring", "st", "last"), 16) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
99 %!assert (index ("astringbstringcstring", "str", "last"), 16) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
100 %!assert (index ("astringbstringcstring", "string", "last"), 16) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
101 %!assert (index ("abc---", "abc+++", "last"), 0) |
7411 | 102 |
13289
1a6537dbce7b
Expand index,rindex functions to accept char array inputs
Rik <octave@nomad.inbox5.com>
parents:
12931
diff
changeset
|
103 %!test |
1a6537dbce7b
Expand index,rindex functions to accept char array inputs
Rik <octave@nomad.inbox5.com>
parents:
12931
diff
changeset
|
104 %! str = char ("Hello", "World", "Goodbye", "World"); |
1a6537dbce7b
Expand index,rindex functions to accept char array inputs
Rik <octave@nomad.inbox5.com>
parents:
12931
diff
changeset
|
105 %! assert (index (str, "o"), [5; 2; 2; 2]); |
1a6537dbce7b
Expand index,rindex functions to accept char array inputs
Rik <octave@nomad.inbox5.com>
parents:
12931
diff
changeset
|
106 %! assert (index (str, "o", "last"), [5; 2; 3; 2]); |
1a6537dbce7b
Expand index,rindex functions to accept char array inputs
Rik <octave@nomad.inbox5.com>
parents:
12931
diff
changeset
|
107 %! str = cellstr (str); |
1a6537dbce7b
Expand index,rindex functions to accept char array inputs
Rik <octave@nomad.inbox5.com>
parents:
12931
diff
changeset
|
108 %! assert (index (str, "o"), [5; 2; 2; 2]); |
1a6537dbce7b
Expand index,rindex functions to accept char array inputs
Rik <octave@nomad.inbox5.com>
parents:
12931
diff
changeset
|
109 %! assert (index (str, "o", "last"), [5; 2; 3; 2]); |
7411 | 110 |
13289
1a6537dbce7b
Expand index,rindex functions to accept char array inputs
Rik <octave@nomad.inbox5.com>
parents:
12931
diff
changeset
|
111 %% Test input validation |
1a6537dbce7b
Expand index,rindex functions to accept char array inputs
Rik <octave@nomad.inbox5.com>
parents:
12931
diff
changeset
|
112 %!error index () |
1a6537dbce7b
Expand index,rindex functions to accept char array inputs
Rik <octave@nomad.inbox5.com>
parents:
12931
diff
changeset
|
113 %!error index ("a") |
1a6537dbce7b
Expand index,rindex functions to accept char array inputs
Rik <octave@nomad.inbox5.com>
parents:
12931
diff
changeset
|
114 %!error index ("a", "b", "first", "d") |
1a6537dbce7b
Expand index,rindex functions to accept char array inputs
Rik <octave@nomad.inbox5.com>
parents:
12931
diff
changeset
|
115 %!error index (1, "bar") |
1a6537dbce7b
Expand index,rindex functions to accept char array inputs
Rik <octave@nomad.inbox5.com>
parents:
12931
diff
changeset
|
116 %!error index ("foo", "bar", 3) |
7411 | 117 |