2313
|
1 ## Copyright (C) 1996 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 |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
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 |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
2271
|
19 |
3361
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} index (@var{s}, @var{t}) |
|
22 ## Return the position of the first occurrence of the string @var{t} in the |
|
23 ## string @var{s}, or 0 if no occurrence is found. For example, |
3426
|
24 ## |
3361
|
25 ## @example |
|
26 ## index ("Teststring", "t") |
|
27 ## @result{} 4 |
|
28 ## @end example |
3426
|
29 ## |
4946
|
30 ## @strong{Caution:} This function does not work for arrays of strings. |
3361
|
31 ## @end deftypefn |
2271
|
32 |
5428
|
33 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at> |
2355
|
34 ## Adapted-By: jwe |
2314
|
35 |
2311
|
36 function n = index (s, t) |
2271
|
37 |
2303
|
38 ## This is patterned after the AWK function of the same name. |
2271
|
39 |
|
40 if (nargin != 2) |
|
41 usage ("index (s, t)"); |
|
42 endif |
3911
|
43 |
5443
|
44 if (!ischar (s) || !ischar (t) || all (size (s) > 1) || all (size (t) > 1) ) |
2271
|
45 error ("index: expecting string arguments"); |
|
46 endif |
2325
|
47 |
3911
|
48 l_s = length (s); |
|
49 l_t = length (t); |
|
50 |
|
51 if ( l_s == 0 || l_s < l_t ) |
|
52 ## zero length source, or target longer than source |
|
53 v = []; |
|
54 |
|
55 elseif ( l_t == 0 ) |
|
56 ## zero length target: return first |
|
57 v = 1; |
|
58 |
|
59 elseif ( l_t == 1 ) |
|
60 ## length one target: simple find |
|
61 v = find (s==t); |
|
62 |
|
63 elseif ( l_t == 2 ) |
|
64 ## length two target: find first at i and second at i+1 |
|
65 v = find (s (1 : l_s-1) == t (1) & s (2 : l_s) == t (2)); |
|
66 |
|
67 else |
|
68 ## length three or more: match the first three by find then go through |
|
69 ## the much smaller list to determine which of them are real matches |
|
70 limit = l_s - l_t + 1; |
|
71 v = find (s (1 : limit) == t(1) & s (2 : limit+1) == t (2) |
|
72 & s (3 : limit+2) == t(3) ); |
|
73 endif |
|
74 |
|
75 if (l_t > 3) |
|
76 |
|
77 ## force strings to be both row vectors or both column vectors |
|
78 if (all (size (s) != size (t))) |
|
79 t = t.'; |
|
80 endif |
|
81 |
|
82 ## search index vector for a match |
|
83 ind = 0 : l_t - 1; |
|
84 n = 0; # return 0 if loop terminates without finding any match |
|
85 for idx = 1:length(v) |
|
86 if (s (v(idx) + ind) == t) |
|
87 n = v(idx); |
|
88 break; |
|
89 endif |
|
90 endfor |
|
91 |
|
92 elseif (length(v) > 0) |
|
93 n = v(1); |
|
94 |
|
95 else |
|
96 n = 0; |
|
97 |
|
98 endif |
|
99 |
2271
|
100 endfunction |