Mercurial > hg > octave-nkf
annotate scripts/strings/index.m @ 9051:1bf0ce0930be
Grammar check TexInfo in all .m files
Cleanup documentation sources to follow a few consistent rules.
Spellcheck was NOT done. (but will be in another changeset)
author | Rik <rdrider0-list@yahoo.com> |
---|---|
date | Fri, 27 Mar 2009 22:31:03 -0700 |
parents | eb63fbe60fab |
children | a87afd063e7d |
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 | |
6749 | 57 if (! (ischar (s) && ischar (t))) |
6139 | 58 error ("index: expecting character string arguments"); |
59 elseif (! strcmp (direction, {"first", "last"})) | |
60 error ("index: direction must be either \"first\" or \"last\""); | |
2271 | 61 endif |
2325 | 62 |
3911 | 63 l_s = length (s); |
64 l_t = length (t); | |
6139 | 65 |
66 n = 0; | |
67 if (l_s == 0 || l_s < l_t) | |
3911 | 68 ## zero length source, or target longer than source |
6139 | 69 ## return 0 |
3911 | 70 v = []; |
6139 | 71 |
72 elseif (l_t == 0) | |
3911 | 73 ## zero length target: return first |
74 v = 1; | |
6139 | 75 |
76 elseif (l_t == 1) | |
3911 | 77 ## length one target: simple find |
8507 | 78 v = find (s == t, 1, direction); |
6139 | 79 |
80 elseif (l_t == 2) | |
3911 | 81 ## length two target: find first at i and second at i+1 |
6139 | 82 v = find (s (1:l_s-1) == t(1) & s(2:l_s) == t(2), 1, direction); |
83 | |
3911 | 84 else |
85 ## length three or more: match the first three by find then go through | |
86 ## the much smaller list to determine which of them are real matches | |
87 limit = l_s - l_t + 1; | |
6139 | 88 v = find (s (1:limit) == t(1) |
89 & s (2:limit+1) == t(2) | |
90 & s (3:limit+2) == t(3)); | |
91 if (strcmp (direction, "last")) | |
92 v = v(length(v):-1:1); | |
3911 | 93 endif |
6139 | 94 |
95 if (l_t > 3) | |
96 | |
97 ## force strings to be both row vectors or both column vectors | |
98 if (all (size (s) != size (t))) | |
99 t = t.'; | |
3911 | 100 endif |
101 | |
6139 | 102 ## search index vector for a match |
103 ind = 0:l_t-1; | |
104 ## return 0 if loop terminates without finding any match | |
105 for idx = 1:length(v) | |
106 if (s (v(idx) + ind) == t) | |
107 n = v(idx); | |
108 break; | |
109 endif | |
110 endfor | |
6901 | 111 v = []; |
6139 | 112 endif |
3911 | 113 |
114 endif | |
115 | |
6139 | 116 if (n == 0 && ! isempty (v)) |
117 ## return the first found if n is not already set and v is not empty | |
118 n = v(1); | |
119 endif | |
120 | |
2271 | 121 endfunction |
6139 | 122 |
123 ## Test the function out | |
124 %!assert(index("astringbstringcstring", "s"), 2) | |
125 %!assert(index("astringbstringcstring", "st"), 2) | |
126 %!assert(index("astringbstringcstring", "str"), 2) | |
127 %!assert(index("astringbstringcstring", "string"), 2) | |
6901 | 128 %!assert(index("abc---", "abc+++"), 0) |
6139 | 129 |
130 ## test everything out in reverse | |
131 %!assert(index("astringbstringcstring", "s", "last"), 16) | |
132 %!assert(index("astringbstringcstring", "st", "last"), 16) | |
133 %!assert(index("astringbstringcstring", "str", "last"), 16) | |
134 %!assert(index("astringbstringcstring", "string", "last"), 16) | |
6901 | 135 %!assert(index("abc---", "abc+++", "last"), 0) |
7411 | 136 |
137 | |
138 %!assert(index ("foobarbaz", "b") == 4 && index ("foobarbaz", "z") == 9); | |
139 | |
140 %!error index (); | |
141 | |
142 %!error index ("foo", "bar", 3); | |
143 |