Mercurial > forge
comparison FIXES/index.m @ 0:6b33357c7561 octave-forge
Initial revision
author | pkienzle |
---|---|
date | Wed, 10 Oct 2001 19:54:49 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:6b33357c7561 |
---|---|
1 ## Copyright (C) 1996 Kurt Hornik | |
2 ## | |
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 | |
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA | |
18 ## 02111-1307, USA. | |
19 | |
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, | |
24 ## | |
25 ## @example | |
26 ## index ("Teststring", "t") | |
27 ## @result{} 4 | |
28 ## @end example | |
29 ## | |
30 ## @strong{Note:} This function does not work for arrays of strings. | |
31 ## @end deftypefn | |
32 | |
33 ## Author: Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at> | |
34 ## Adapted-By: jwe | |
35 ## Paul Kienzle <pkienzle@kienzle.powernet.co.uk> | |
36 ## vectorize for speed | |
37 | |
38 function n = index (s, t) | |
39 | |
40 ## This is patterned after the AWK function of the same name. | |
41 | |
42 if (nargin != 2) | |
43 usage ("index (s, t)"); | |
44 endif | |
45 | |
46 if (!isstr (s) || !isstr (t) || all (size (s) > 1) || all (size (t) > 1) ) | |
47 error ("index: expecting string arguments"); | |
48 endif | |
49 | |
50 l_s = length (s); | |
51 l_t = length (t); | |
52 | |
53 if ( l_s == 0 || l_s < l_t ) | |
54 ## zero length source, or target longer than source | |
55 v = []; | |
56 | |
57 elseif ( l_t == 0 ) | |
58 ## zero length target: return first | |
59 v = 1; | |
60 | |
61 elseif ( l_t == 1 ) | |
62 ## length one target: simple find | |
63 v = find (s==t); | |
64 | |
65 elseif ( l_t == 2 ) | |
66 ## length two target: find first at i and second at i+1 | |
67 v = find (s (1 : l_s-1) == t (1) & s (2 : l_s) == t (2)); | |
68 | |
69 else | |
70 ## length three or more: match the first three by find then go through | |
71 ## the much smaller list to determine which of them are real matches | |
72 limit = l_s - l_t + 1; | |
73 v = find (s (1 : limit) == t(1) & s (2 : limit+1) == t (2) | |
74 & s (3 : limit+2) == t(3) ); | |
75 endif | |
76 | |
77 if (l_t > 3) | |
78 | |
79 ## force strings to be both row vectors or both column vectors | |
80 if (all (size (s) != size (t))) | |
81 t = t.'; | |
82 endif | |
83 | |
84 ## search index vector for a match | |
85 ind = 0 : l_t - 1; | |
86 n = 0; # return 0 if loop terminates without finding any match | |
87 for idx = 1:length(v) | |
88 if (s (v(idx) + ind) == t) | |
89 n = v(idx); | |
90 break; | |
91 endif | |
92 endfor | |
93 | |
94 elseif (length(v) > 0) | |
95 n = v(1); | |
96 | |
97 else | |
98 n = 0; | |
99 | |
100 endif | |
101 | |
102 endfunction |