Mercurial > hg > octave-nkf
annotate scripts/strings/index.m @ 10409:a87afd063e7d
optimize index (call strfind)
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Thu, 11 Mar 2010 10:27:30 +0100 |
parents | 1bf0ce0930be |
children | be55736a0783 |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 1996, 1999, 2000, 2002, 2004, 2005, 2006, 2007, 2008, 2009 |
7017 | 2 ## Kurt Hornik |
2325 | 3 ## |
2313 | 4 ## This file is part of Octave. |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
2313 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
2271 | 19 |
3361 | 20 ## -*- texinfo -*- |
21 ## @deftypefn {Function File} {} index (@var{s}, @var{t}) | |
6139 | 22 ## @deftypefnx {Function File} {} index (@var{s}, @var{t}, @var{direction}) |
3361 | 23 ## Return the position of the first occurrence of the string @var{t} in the |
24 ## string @var{s}, or 0 if no occurrence is found. For example, | |
3426 | 25 ## |
3361 | 26 ## @example |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
27 ## @group |
3361 | 28 ## index ("Teststring", "t") |
29 ## @result{} 4 | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
30 ## @end group |
3361 | 31 ## @end example |
3426 | 32 ## |
6139 | 33 ## If @var{direction} is @samp{"first"}, return the first element found. |
34 ## If @var{direction} is @samp{"last"}, return the last element found. | |
35 ## The @code{rindex} function is equivalent to @code{index} with | |
36 ## @var{direction} set to @samp{"last"}. | |
37 ## | |
38 ## @strong{Caution:} This function does not work for arrays of | |
39 ## character strings. | |
40 ## @seealso{find, rindex} | |
3361 | 41 ## @end deftypefn |
2271 | 42 |
5428 | 43 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at> |
2355 | 44 ## Adapted-By: jwe |
2314 | 45 |
6139 | 46 function n = index (s, t, direction) |
2271 | 47 |
2303 | 48 ## This is patterned after the AWK function of the same name. |
2271 | 49 |
6139 | 50 if (nargin < 2 || nargin > 3) |
6046 | 51 print_usage (); |
6139 | 52 elseif (nargin < 3) |
53 direction = "first"; | |
2271 | 54 endif |
6139 | 55 direction = lower (direction); |
56 | |
10409
a87afd063e7d
optimize index (call strfind)
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
57 f = strfind (s, t); |
a87afd063e7d
optimize index (call strfind)
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
58 if (iscell (f)) |
a87afd063e7d
optimize index (call strfind)
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
59 f(cellfun ("isempty", f)) = {0}; |
a87afd063e7d
optimize index (call strfind)
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
60 elseif (isempty (f)) |
a87afd063e7d
optimize index (call strfind)
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
61 f = 0; |
2271 | 62 endif |
2325 | 63 |
10409
a87afd063e7d
optimize index (call strfind)
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
64 if (strcmp (direction, "last")) |
a87afd063e7d
optimize index (call strfind)
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
65 if (iscell (f)) |
a87afd063e7d
optimize index (call strfind)
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
66 n = cellfun (@min, f); |
a87afd063e7d
optimize index (call strfind)
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
67 else |
a87afd063e7d
optimize index (call strfind)
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
68 n = f(end); |
a87afd063e7d
optimize index (call strfind)
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
69 endif |
a87afd063e7d
optimize index (call strfind)
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
70 elseif (strcmp (direction, "first")) |
a87afd063e7d
optimize index (call strfind)
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
71 if (iscell (f)) |
a87afd063e7d
optimize index (call strfind)
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
72 n = cellfun (@max, f); |
a87afd063e7d
optimize index (call strfind)
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
73 else |
a87afd063e7d
optimize index (call strfind)
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
74 n = f(1); |
a87afd063e7d
optimize index (call strfind)
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
75 endif |
3911 | 76 else |
10409
a87afd063e7d
optimize index (call strfind)
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
77 error ("index: direction must be either \"first\" or \"last\""); |
3911 | 78 endif |
2271 | 79 endfunction |
6139 | 80 |
81 ## Test the function out | |
82 %!assert(index("astringbstringcstring", "s"), 2) | |
83 %!assert(index("astringbstringcstring", "st"), 2) | |
84 %!assert(index("astringbstringcstring", "str"), 2) | |
85 %!assert(index("astringbstringcstring", "string"), 2) | |
6901 | 86 %!assert(index("abc---", "abc+++"), 0) |
6139 | 87 |
88 ## test everything out in reverse | |
89 %!assert(index("astringbstringcstring", "s", "last"), 16) | |
90 %!assert(index("astringbstringcstring", "st", "last"), 16) | |
91 %!assert(index("astringbstringcstring", "str", "last"), 16) | |
92 %!assert(index("astringbstringcstring", "string", "last"), 16) | |
6901 | 93 %!assert(index("abc---", "abc+++", "last"), 0) |
7411 | 94 |
95 | |
96 %!assert(index ("foobarbaz", "b") == 4 && index ("foobarbaz", "z") == 9); | |
97 | |
98 %!error index (); | |
99 | |
100 %!error index ("foo", "bar", 3); | |
101 |