Mercurial > octave-antonio
comparison scripts/strings/rindex.m @ 3911:8389e78e67d4
[project @ 2002-04-28 02:15:38 by jwe]
author | jwe |
---|---|
date | Sun, 28 Apr 2002 02:15:39 +0000 |
parents | f8dde1807dee |
children | 48a39e2b2ab7 |
comparison
equal
deleted
inserted
replaced
3910:79a90a0f0eff | 3911:8389e78e67d4 |
---|---|
39 | 39 |
40 if (nargin != 2) | 40 if (nargin != 2) |
41 usage ("rindex (s, t)"); | 41 usage ("rindex (s, t)"); |
42 endif | 42 endif |
43 | 43 |
44 n = 0; | 44 if (!isstr (s) || !isstr (t) || all (size (s) > 1) || all (size (t) > 1) ) |
45 | |
46 if (isstr (s) && isstr (t)) | |
47 | |
48 l_s = length (s); | |
49 l_t = length (t); | |
50 | |
51 if (l_t <= l_s) | |
52 tmp = l_s - l_t + 1; | |
53 for idx = tmp : -1 : 1 | |
54 if (strcmp (substr (s, idx, l_t), t)) | |
55 n = idx; | |
56 return; | |
57 endif | |
58 endfor | |
59 endif | |
60 | |
61 else | |
62 error ("rindex: expecting string arguments"); | 45 error ("rindex: expecting string arguments"); |
63 endif | 46 endif |
64 | 47 |
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 last | |
57 v = l_s; | |
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 = length(v):-1:1 | |
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(length(v)); | |
94 | |
95 else | |
96 n = 0; | |
97 | |
98 endif | |
99 | |
65 endfunction | 100 endfunction |