Mercurial > hg > octave-lyh
comparison scripts/strings/index.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 | cefd568ea073 |
children | 72c96de7a403 |
comparison
equal
deleted
inserted
replaced
13288:497bb1cf7b15 | 13289:1a6537dbce7b |
---|---|
18 | 18 |
19 ## -*- texinfo -*- | 19 ## -*- texinfo -*- |
20 ## @deftypefn {Function File} {} index (@var{s}, @var{t}) | 20 ## @deftypefn {Function File} {} index (@var{s}, @var{t}) |
21 ## @deftypefnx {Function File} {} index (@var{s}, @var{t}, @var{direction}) | 21 ## @deftypefnx {Function File} {} index (@var{s}, @var{t}, @var{direction}) |
22 ## Return the position of the first occurrence of the string @var{t} in the | 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: | 23 ## string @var{s}, or 0 if no occurrence is found. @var{s} may also be a |
24 ## string array or cell array of strings. | |
25 ## | |
26 ## For example: | |
24 ## | 27 ## |
25 ## @example | 28 ## @example |
26 ## @group | 29 ## @group |
27 ## index ("Teststring", "t") | 30 ## index ("Teststring", "t") |
28 ## @result{} 4 | 31 ## @result{} 4 |
29 ## @end group | 32 ## @end group |
30 ## @end example | 33 ## @end example |
31 ## | 34 ## |
32 ## If @var{direction} is @samp{"first"}, return the first element found. | 35 ## If @var{direction} is @samp{"first"}, return the first element found. |
33 ## If @var{direction} is @samp{"last"}, return the last element found. | 36 ## If @var{direction} is @samp{"last"}, return the last element found. |
34 ## The @code{rindex} function is equivalent to @code{index} with | |
35 ## @var{direction} set to @samp{"last"}. | |
36 ## | 37 ## |
37 ## @strong{Caution:} This function does not work for arrays of | |
38 ## character strings. | |
39 ## @seealso{find, rindex} | 38 ## @seealso{find, rindex} |
40 ## @end deftypefn | 39 ## @end deftypefn |
41 | 40 |
42 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at> | 41 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at> |
43 ## Adapted-By: jwe | 42 ## Adapted-By: jwe |
43 ## This is patterned after the AWK function of the same name. | |
44 | 44 |
45 function n = index (s, t, direction) | 45 function n = index (s, t, direction = "first") |
46 | |
47 ## This is patterned after the AWK function of the same name. | |
48 | 46 |
49 if (nargin < 2 || nargin > 3) | 47 if (nargin < 2 || nargin > 3) |
50 print_usage (); | 48 print_usage (); |
51 elseif (nargin < 3) | |
52 direction = "first"; | |
53 endif | 49 endif |
54 direction = lower (direction); | 50 |
51 if (ischar (s)) | |
52 if (! isrow (s)) | |
53 s = cellstr (s); # Handle string arrays by conversion to cellstr | |
54 endif | |
55 elseif (! iscellstr (s)) | |
56 error ("index: S must be a string, string array, or cellstr"); | |
57 endif | |
55 | 58 |
56 f = strfind (s, t); | 59 f = strfind (s, t); |
57 if (iscell (f)) | 60 if (isempty (f)) |
61 f = 0; | |
62 elseif (iscell (f)) | |
58 f(cellfun ("isempty", f)) = {0}; | 63 f(cellfun ("isempty", f)) = {0}; |
59 elseif (isempty (f)) | |
60 f = 0; | |
61 endif | 64 endif |
62 | 65 |
63 if (strcmp (direction, "last")) | 66 direction = tolower (direction); |
67 | |
68 if (strcmp (direction, "first")) | |
64 if (iscell (f)) | 69 if (iscell (f)) |
65 n = cellfun ("min", f); | 70 n = cellfun ("min", f); |
66 else | 71 else |
67 n = f(end); | 72 n = f(1); |
68 endif | 73 endif |
69 elseif (strcmp (direction, "first")) | 74 elseif (strcmp (direction, "last")) |
70 if (iscell (f)) | 75 if (iscell (f)) |
71 n = cellfun ("max", f); | 76 n = cellfun ("max", f); |
72 else | 77 else |
73 n = f(1); | 78 n = f(end); |
74 endif | 79 endif |
75 else | 80 else |
76 error ("index: DIRECTION must be either \"first\" or \"last\""); | 81 error ('index: DIRECTION must be either "first" or "last"'); |
77 endif | 82 endif |
83 | |
78 endfunction | 84 endfunction |
79 | 85 |
80 ## Test the function out | 86 |
81 %!assert(index("astringbstringcstring", "s"), 2) | 87 %!assert (index ("foobarbaz", "b") == 4 && index ("foobarbaz", "z") == 9); |
82 %!assert(index("astringbstringcstring", "st"), 2) | 88 |
83 %!assert(index("astringbstringcstring", "str"), 2) | 89 %!assert (index("astringbstringcstring", "s"), 2) |
84 %!assert(index("astringbstringcstring", "string"), 2) | 90 %!assert (index("astringbstringcstring", "st"), 2) |
85 %!assert(index("abc---", "abc+++"), 0) | 91 %!assert (index("astringbstringcstring", "str"), 2) |
92 %!assert (index("astringbstringcstring", "string"), 2) | |
93 %!assert (index("abc---", "abc+++"), 0) | |
86 | 94 |
87 ## test everything out in reverse | 95 ## test everything out in reverse |
88 %!assert(index("astringbstringcstring", "s", "last"), 16) | 96 %!assert (index("astringbstringcstring", "s", "last"), 16) |
89 %!assert(index("astringbstringcstring", "st", "last"), 16) | 97 %!assert (index("astringbstringcstring", "st", "last"), 16) |
90 %!assert(index("astringbstringcstring", "str", "last"), 16) | 98 %!assert (index("astringbstringcstring", "str", "last"), 16) |
91 %!assert(index("astringbstringcstring", "string", "last"), 16) | 99 %!assert (index("astringbstringcstring", "string", "last"), 16) |
92 %!assert(index("abc---", "abc+++", "last"), 0) | 100 %!assert (index("abc---", "abc+++", "last"), 0) |
93 | 101 |
102 %!test | |
103 %! str = char ("Hello", "World", "Goodbye", "World"); | |
104 %! assert (index (str, "o"), [5; 2; 2; 2]); | |
105 %! assert (index (str, "o", "last"), [5; 2; 3; 2]); | |
106 %! str = cellstr (str); | |
107 %! assert (index (str, "o"), [5; 2; 2; 2]); | |
108 %! assert (index (str, "o", "last"), [5; 2; 3; 2]); | |
94 | 109 |
95 %!assert(index ("foobarbaz", "b") == 4 && index ("foobarbaz", "z") == 9); | 110 %% Test input validation |
111 %!error index () | |
112 %!error index ("a") | |
113 %!error index ("a", "b", "first", "d") | |
114 %!error index (1, "bar") | |
115 %!error index ("foo", "bar", 3) | |
96 | 116 |
97 %!error index (); | |
98 | |
99 %!error index ("foo", "bar", 3); | |
100 |