]> Creatis software - CreaPhase.git/blob - octave_packages/m/strings/index.m
update packages
[CreaPhase.git] / octave_packages / m / strings / index.m
1 ## Copyright (C) 1996-2012 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 3 of the License, or (at
8 ## your option) 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, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn  {Function File} {} index (@var{s}, @var{t})
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
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:
27 ##
28 ## @example
29 ## @group
30 ## index ("Teststring", "t")
31 ##    @result{} 4
32 ## @end group
33 ## @end example
34 ##
35 ## If @var{direction} is @samp{"first"}, return the first element found.
36 ## If @var{direction} is @samp{"last"}, return the last element found.
37 ##
38 ## @seealso{find, rindex}
39 ## @end deftypefn
40
41 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at>
42 ## Adapted-By: jwe
43 ## This is patterned after the AWK function of the same name.
44
45 function n = index (s, t, direction = "first")
46
47   if (nargin < 2 || nargin > 3)
48     print_usage ();
49   endif
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
58
59   f = strfind (s, t);
60   if (isempty (f))
61     f = 0;
62   elseif (iscell (f))
63     f(cellfun ("isempty", f)) = {0};
64   endif
65
66   direction = tolower (direction);
67
68   if (strcmp (direction, "first"))
69     if (iscell (f))
70       n = cellfun ("min", f);
71     else
72       n = f(1);
73     endif
74   elseif (strcmp (direction, "last"))
75     if (iscell (f))
76       n = cellfun ("max", f);
77     else
78       n = f(end);
79     endif
80   else
81     error ('index: DIRECTION must be either "first" or "last"');
82   endif
83
84 endfunction
85
86
87 %!assert (index ("foobarbaz", "b") == 4 && index ("foobarbaz", "z") == 9);
88
89 %!assert (index("astringbstringcstring", "s"), 2)
90 %!assert (index("astringbstringcstring", "st"), 2)
91 %!assert (index("astringbstringcstring", "str"), 2)
92 %!assert (index("astringbstringcstring", "string"), 2)
93 %!assert (index("abc---", "abc+++"), 0)
94
95 ## test everything out in reverse
96 %!assert (index("astringbstringcstring", "s", "last"), 16)
97 %!assert (index("astringbstringcstring", "st", "last"), 16)
98 %!assert (index("astringbstringcstring", "str", "last"), 16)
99 %!assert (index("astringbstringcstring", "string", "last"), 16)
100 %!assert (index("abc---", "abc+++", "last"), 0)
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]);
109
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)
116