Mercurial > hg > octave-nkf
comparison scripts/strings/rindex.m @ 13289:1a6537dbce7b
Expand index,rindex functions to accept char array inputs
* index.m, rindex.m: Allow char array inputs. Update documentation and tests.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Fri, 07 Oct 2011 14:18:18 -0700 |
parents | fd0a3ac60b0e |
children | 72c96de7a403 |
comparison
equal
deleted
inserted
replaced
13288:497bb1cf7b15 | 13289:1a6537dbce7b |
---|---|
18 | 18 |
19 ## -*- texinfo -*- | 19 ## -*- texinfo -*- |
20 ## @deftypefn {Function File} {} rindex (@var{s}, @var{t}) | 20 ## @deftypefn {Function File} {} rindex (@var{s}, @var{t}) |
21 ## Return the position of the last occurrence of the character string | 21 ## Return the position of the last occurrence of the character string |
22 ## @var{t} in the character string @var{s}, or 0 if no occurrence is | 22 ## @var{t} in the character string @var{s}, or 0 if no occurrence is |
23 ## found. For example: | 23 ## found. @var{s} may also be a string array or cell array of strings. |
24 ## | |
25 ## For example: | |
24 ## | 26 ## |
25 ## @example | 27 ## @example |
26 ## @group | 28 ## @group |
27 ## rindex ("Teststring", "t") | 29 ## rindex ("Teststring", "t") |
28 ## @result{} 6 | 30 ## @result{} 6 |
29 ## @end group | 31 ## @end group |
30 ## @end example | 32 ## @end example |
31 ## | 33 ## |
32 ## @strong{Caution:} This function does not work for arrays of | 34 ## The @code{rindex} function is equivalent to @code{index} with |
33 ## character strings. | 35 ## @var{direction} set to @samp{"last"}. |
36 ## | |
34 ## @seealso{find, index} | 37 ## @seealso{find, index} |
35 ## @end deftypefn | 38 ## @end deftypefn |
36 | 39 |
37 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at> | 40 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at> |
38 ## Adapted-By: jwe | 41 ## Adapted-By: jwe |
42 ## This is patterned after the AWK function of the same name. | |
39 | 43 |
40 function n = rindex (s, t) | 44 function n = rindex (s, t) |
41 | |
42 ## This is patterned after the AWK function of the same name. | |
43 | 45 |
44 if (nargin != 2) | 46 if (nargin != 2) |
45 print_usage (); | 47 print_usage (); |
46 endif | 48 endif |
47 | 49 |
48 n = index (s, t, "last"); | 50 n = index (s, t, "last"); |
49 | 51 |
50 endfunction | 52 endfunction |
51 | 53 |
54 | |
52 %!assert(rindex ("foobarbaz", "b") == 7 && rindex ("foobarbaz", "o") == 3); | 55 %!assert(rindex ("foobarbaz", "b") == 7 && rindex ("foobarbaz", "o") == 3); |
53 | 56 |
54 %!error rindex (); | 57 %!test |
58 %! str = char ("Hello", "World", "Goodbye", "World"); | |
59 %! assert (rindex (str, "o"), [5; 2; 3; 2]); | |
60 %! str = cellstr (str); | |
61 %! assert (rindex (str, "o"), [5; 2; 3; 2]); | |
55 | 62 |
56 %!error rindex ("foo", "bar", 3); | 63 %% Test input validation |
64 %!error rindex () | |
65 %!error rindex ("foo") | |
66 %!error rindex ("foo", "bar", "last") | |
57 | 67 |